Skip to content

Commit d7cf1ee

Browse files
committed
Fix: respect env. var for leading comma & normalize format settings
1 parent 4fa9992 commit d7cf1ee

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed

sqlmesh/cli/options.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ def format_options(func: t.Callable) -> t.Callable:
6363
"--normalize",
6464
is_flag=True,
6565
help="Whether or not to normalize identifiers to lowercase.",
66+
default=None,
6667
)(func)
6768
func = click.option(
6869
"--pad",
@@ -82,6 +83,7 @@ def format_options(func: t.Callable) -> t.Callable:
8283
func = click.option(
8384
"--leading-comma",
8485
is_flag=True,
86+
default=None,
8587
help="Determines whether or not the comma is leading or trailing in select expressions. Default is trailing.",
8688
)(func)
8789
func = click.option(

tests/cli/test_cli.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import json
22
import logging
3+
import os
34
import pytest
45
import string
56
import time_machine
@@ -2190,3 +2191,74 @@ def none_ready(batch):
21902191
assert_plan_success(result)
21912192

21922193
assert "Checking signals" not in result.output
2194+
2195+
2196+
@pytest.mark.isolated
2197+
@time_machine.travel(FREEZE_TIME)
2198+
def test_format_leading_comma_default(runner: CliRunner, tmp_path: Path):
2199+
"""Test that format command respects leading_comma environment variable."""
2200+
create_example_project(tmp_path, template=ProjectTemplate.EMPTY)
2201+
2202+
# Create a SQL file with trailing comma format
2203+
test_sql = tmp_path / "models" / "test_format.sql"
2204+
test_sql.write_text("""MODEL (
2205+
name sqlmesh_example.test_format,
2206+
kind FULL
2207+
);
2208+
2209+
SELECT
2210+
col1,
2211+
col2,
2212+
col3
2213+
FROM table1""")
2214+
2215+
# Test 1: Default behavior (no env var set) - should not change the file
2216+
result = runner.invoke(cli, ["--paths", str(tmp_path), "format", "--check"])
2217+
assert result.exit_code == 0
2218+
2219+
# Test 2: Set env var to true - should require reformatting to leading comma
2220+
os.environ["SQLMESH__FORMAT__LEADING_COMMA"] = "true"
2221+
try:
2222+
result = runner.invoke(cli, ["--paths", str(tmp_path), "format", "--check"])
2223+
# Should exit with 1 because formatting is needed
2224+
assert result.exit_code == 1
2225+
2226+
# Actually format the file
2227+
result = runner.invoke(cli, ["--paths", str(tmp_path), "format"])
2228+
assert result.exit_code == 0
2229+
2230+
# Check that the file now has leading commas
2231+
formatted_content = test_sql.read_text()
2232+
assert ", col2" in formatted_content
2233+
assert ", col3" in formatted_content
2234+
2235+
# Now check should pass
2236+
result = runner.invoke(cli, ["--paths", str(tmp_path), "format", "--check"])
2237+
assert result.exit_code == 0
2238+
finally:
2239+
# Clean up env var
2240+
del os.environ["SQLMESH__FORMAT__LEADING_COMMA"]
2241+
2242+
# Test 3: Explicit command line flag overrides env var
2243+
os.environ["SQLMESH__FORMAT__LEADING_COMMA"] = "false"
2244+
try:
2245+
# Write file with leading commas
2246+
test_sql.write_text("""MODEL (
2247+
name sqlmesh_example.test_format,
2248+
kind FULL
2249+
);
2250+
2251+
SELECT
2252+
col1
2253+
, col2
2254+
, col3
2255+
FROM table1""")
2256+
2257+
# Check with --leading-comma flag (should pass)
2258+
result = runner.invoke(
2259+
cli,
2260+
["--paths", str(tmp_path), "format", "--check", "--leading-comma"],
2261+
)
2262+
assert result.exit_code == 0
2263+
finally:
2264+
del os.environ["SQLMESH__FORMAT__LEADING_COMMA"]

0 commit comments

Comments
 (0)