File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -1338,7 +1338,8 @@ def format(
13381338 ): # introduced to satisfy type checker as still want to pull filter out as many targets as possible before loop
13391339 continue
13401340
1341- with open (target ._path , "r+" , encoding = "utf-8" ) as file :
1341+ mode = "r" if check else "r+"
1342+ with open (target ._path , mode , encoding = "utf-8" ) as file :
13421343 before = file .read ()
13431344
13441345 after = self ._format (
Original file line number Diff line number Diff line change 1+ import os
12import pathlib
3+ import stat
24
35from pytest_mock .plugin import MockerFixture
46from sqlmesh .core .config import Config
@@ -146,6 +148,25 @@ def test_ignore_formating_files(tmp_path: pathlib.Path):
146148 )
147149
148150
151+ def test_format_check_read_only_files (tmp_path : pathlib .Path , mocker : MockerFixture ):
152+ models_dir = pathlib .Path ("models" )
153+
154+ model_text = "MODEL(name this.model, dialect 'duckdb'); SELECT 1 AS col"
155+ model = create_temp_file (
156+ tmp_path ,
157+ pathlib .Path (models_dir , "model.sql" ),
158+ model_text ,
159+ )
160+ os .chmod (model , stat .S_IRUSR | stat .S_IRGRP | stat .S_IROTH )
161+
162+ context = Context (paths = tmp_path , config = Config ())
163+ context .console = mocker .Mock ()
164+ context .load ()
165+
166+ assert not context .format (check = True )
167+ assert model .read_text (encoding = "utf-8" ) == model_text
168+
169+
149170def test_format_without_state_load (tmp_path : pathlib .Path , mocker : MockerFixture ):
150171 mock = mocker .patch (
151172 "sqlmesh.core.state_sync.db.facade.EngineAdapterStateSync.get_versions" ,
You can’t perform that action at this time.
0 commit comments