Skip to content

Commit d051070

Browse files
committed
Feat: improve format --check output
1 parent 119e4f8 commit d051070

2 files changed

Lines changed: 24 additions & 2 deletions

File tree

sqlmesh/core/context.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1062,7 +1062,9 @@ def format(
10621062
**kwargs: t.Any,
10631063
) -> bool:
10641064
"""Format all SQL models and audits."""
1065+
unformatted_file_paths = []
10651066
format_targets = {**self._models, **self._audits}
1067+
10661068
for target in format_targets.values():
10671069
if target._path is None or target._path.suffix != ".sql":
10681070
continue
@@ -1104,7 +1106,17 @@ def format(
11041106
file.write(after)
11051107
file.truncate()
11061108
elif before != after:
1107-
return False
1109+
unformatted_file_paths.append(target._path)
1110+
1111+
if unformatted_file_paths:
1112+
for path in unformatted_file_paths:
1113+
self.console.log_status_update(f"Would reformat {path}")
1114+
1115+
self.console.log_status_update(
1116+
f"\n{len(unformatted_file_paths)} file(s) would be reformatted."
1117+
)
1118+
return False
1119+
11081120
return True
11091121

11101122
@python_api_analytics

tests/core/test_format.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
import pathlib
22

3+
from pytest_mock.plugin import MockerFixture
34
from sqlmesh.core.config import Config
45
from sqlmesh.core.context import Context
56
from sqlmesh.core.dialect import parse
67
from sqlmesh.core.audit import ModelAudit
78
from sqlmesh.core.model import SqlModel, load_sql_based_model
89
from tests.utils.test_filesystem import create_temp_file
10+
from unittest.mock import call
911

1012

11-
def test_format_files(tmp_path: pathlib.Path):
13+
def test_format_files(tmp_path: pathlib.Path, mocker: MockerFixture):
1214
models_dir = pathlib.Path("models")
1315
audits_dir = pathlib.Path("audits")
1416

@@ -35,6 +37,7 @@ def test_format_files(tmp_path: pathlib.Path):
3537

3638
config = Config()
3739
context = Context(paths=tmp_path, config=config)
40+
context.console = mocker.Mock()
3841
context.load()
3942

4043
assert isinstance(context.get_model("this.model"), SqlModel)
@@ -54,6 +57,13 @@ def test_format_files(tmp_path: pathlib.Path):
5457
)
5558

5659
assert not context.format(check=True)
60+
assert context.console.log_status_update.mock_calls == [ # type: ignore
61+
call(f"Would reformat {tmp_path}/models/model_3.sql"),
62+
call(f"Would reformat {tmp_path}/models/model_2.sql"),
63+
call(f"Would reformat {tmp_path}/models/model_1.sql"),
64+
call(f"Would reformat {tmp_path}/audits/audit_1.sql"),
65+
call("\n4 file(s) would be reformatted."),
66+
]
5767

5868
# Transpile project to BigQuery
5969
context.format(transpile="bigquery")

0 commit comments

Comments
 (0)