Skip to content

Commit 6373e76

Browse files
committed
Implement error handling for 'format' mode in Style50 and ensure SQL formatter returns newline consistently. Add tests for new behavior.
1 parent 96a864e commit 6373e76

3 files changed

Lines changed: 18 additions & 3 deletions

File tree

‎style50/_api.py‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ def format_file(self, path):
6262

6363
def run(self, *args, **kwargs):
6464
"""Wraps Style50.check and renders the results using the renderer determined by self.output"""
65+
if self.output == "format":
66+
raise Error("format mode does not support run(); use format_file() instead")
67+
6568
results = self.check(*args, **kwargs)
6669

6770
if self.output == "html":

‎style50/languages.py‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,8 @@ class Html(StyleCheck):
112112
magic_names = ["HTML document"]
113113

114114
def style(self, code):
115-
# djhtml reads from stdin when path is '-'
115+
# djhtml returns exit 1 when it reformats (same convention as diff/black),
116+
# so exit=None is required to avoid treating successful reformats as errors.
116117
return self.run(["djhtml", "-"], input=code, exit=None)
117118

118119

@@ -132,5 +133,7 @@ class Sql(StyleCheck):
132133
magic_names = []
133134

134135
def style(self, code):
135-
# Keep this lightweight and dependency-minimal while producing stable formatting.
136-
return sqlparse.format(code, reindent=True, keyword_case="upper", indent_width=4) + ("\n" if not code.endswith("\n") else "")
136+
formatted = sqlparse.format(code, reindent=True, keyword_case="upper", indent_width=4)
137+
if not formatted.endswith("\n"):
138+
formatted += "\n"
139+
return formatted

‎tests/test_languages.py‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ def test_sql_formatter_runs():
3535
formatted = Sql(original).styled
3636
assert isinstance(formatted, str)
3737
assert "FROM" in formatted
38+
assert formatted.endswith("\n")
3839

3940

4041
def test_html_formatter_invokes_djhtml(monkeypatch):
@@ -93,3 +94,11 @@ def fake_run(command, input=None, exit=0, shell=False):
9394

9495
assert seen["cmd"] == C.clangFormat
9596

97+
98+
def test_format_mode_run_raises_error():
99+
from style50._api import Style50, Error
100+
101+
s = Style50("format")
102+
with pytest.raises(Error, match="format mode does not support run"):
103+
s.run(["nonexistent_file.py"])
104+

0 commit comments

Comments
 (0)