|
1 | 1 | import json |
2 | 2 | import logging |
| 3 | +import os |
3 | 4 | import pytest |
4 | 5 | import string |
5 | 6 | import time_machine |
@@ -2190,3 +2191,74 @@ def none_ready(batch): |
2190 | 2191 | assert_plan_success(result) |
2191 | 2192 |
|
2192 | 2193 | 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