Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions sqlmesh/cli/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ def format_options(func: t.Callable) -> t.Callable:
"--normalize",
is_flag=True,
help="Whether or not to normalize identifiers to lowercase.",
default=None,
)(func)
func = click.option(
"--pad",
Expand All @@ -82,6 +83,7 @@ def format_options(func: t.Callable) -> t.Callable:
func = click.option(
"--leading-comma",
is_flag=True,
default=None,
help="Determines whether or not the comma is leading or trailing in select expressions. Default is trailing.",
)(func)
func = click.option(
Expand Down
72 changes: 72 additions & 0 deletions tests/cli/test_cli.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json
import logging
import os
import pytest
import string
import time_machine
Expand Down Expand Up @@ -2190,3 +2191,74 @@ def none_ready(batch):
assert_plan_success(result)

assert "Checking signals" not in result.output


@pytest.mark.isolated
@time_machine.travel(FREEZE_TIME)
def test_format_leading_comma_default(runner: CliRunner, tmp_path: Path):
"""Test that format command respects leading_comma environment variable."""
create_example_project(tmp_path, template=ProjectTemplate.EMPTY)

# Create a SQL file with trailing comma format
test_sql = tmp_path / "models" / "test_format.sql"
test_sql.write_text("""MODEL (
name sqlmesh_example.test_format,
kind FULL
);

SELECT
col1,
col2,
col3
FROM table1""")

# Test 1: Default behavior (no env var set) - should not change the file
result = runner.invoke(cli, ["--paths", str(tmp_path), "format", "--check"])
assert result.exit_code == 0

# Test 2: Set env var to true - should require reformatting to leading comma
os.environ["SQLMESH__FORMAT__LEADING_COMMA"] = "true"
try:
result = runner.invoke(cli, ["--paths", str(tmp_path), "format", "--check"])
# Should exit with 1 because formatting is needed
assert result.exit_code == 1

# Actually format the file
result = runner.invoke(cli, ["--paths", str(tmp_path), "format"])
assert result.exit_code == 0

# Check that the file now has leading commas
formatted_content = test_sql.read_text()
assert ", col2" in formatted_content
assert ", col3" in formatted_content

# Now check should pass
result = runner.invoke(cli, ["--paths", str(tmp_path), "format", "--check"])
assert result.exit_code == 0
finally:
# Clean up env var
del os.environ["SQLMESH__FORMAT__LEADING_COMMA"]

# Test 3: Explicit command line flag overrides env var
os.environ["SQLMESH__FORMAT__LEADING_COMMA"] = "false"
try:
# Write file with leading commas
test_sql.write_text("""MODEL (
name sqlmesh_example.test_format,
kind FULL
);

SELECT
col1
, col2
, col3
FROM table1""")

# Check with --leading-comma flag (should pass)
result = runner.invoke(
cli,
["--paths", str(tmp_path), "format", "--check", "--leading-comma"],
)
assert result.exit_code == 0
finally:
del os.environ["SQLMESH__FORMAT__LEADING_COMMA"]