diff --git a/CHANGELOG.md b/CHANGELOG.md index e43b251..e1468d8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file. ## [Unreleased] +### Formatting Changes and Bug Fixes + +- sqlfmt no longer includes an extra trailing newline when writing formatted code to stdout. + ([#729](https://github.com/tconbeer/sqlfmt/issues/729), [#730](https://github.com/tconbeer/sqlfmt/pull/730)) + ## [0.28.2] - 2025-10-24 ### Formatting Changes and Bug Fixes diff --git a/src/sqlfmt/report.py b/src/sqlfmt/report.py index daca864..aed52c5 100644 --- a/src/sqlfmt/report.py +++ b/src/sqlfmt/report.py @@ -31,11 +31,11 @@ def unstyle_output(msg: str) -> str: return s -def display_output(msg: str, err: bool = True) -> None: +def display_output(msg: str, err: bool = True, nl: bool = True) -> None: """ - A thin wrapper around click.echo; defaults to printing to stderr. + A thin wrapper around click.echo; defaults to stderr with trailing newline. """ - click.echo(msg, err=err) + click.echo(msg, err=err, nl=nl) @dataclass @@ -61,9 +61,10 @@ def __post_init__(self) -> None: def maybe_print_to_stdout(self) -> None: """ If sqlfmt received a query via stdin, print the formatted string to stdout + without an additional trailing newline """ if self.source_path == STDIN_PATH: - display_output(self.formatted_string, err=False) + display_output(self.formatted_string, err=False, nl=False) @property def has_changed(self) -> bool: diff --git a/tests/functional_tests/test_end_to_end.py b/tests/functional_tests/test_end_to_end.py index 1846b77..fdcf7ff 100755 --- a/tests/functional_tests/test_end_to_end.py +++ b/tests/functional_tests/test_end_to_end.py @@ -120,8 +120,8 @@ def test_end_to_end_errors( @pytest.mark.parametrize( "options,stream_input,expected_stdout,expected_exit", [ - ("-", "select 1\n", "select 1\n\n", 0), - ("-", "select 1\n", "select 1\n\n", 0), + ("-", "select 1\n", "select 1\n", 0), # removes excess space + ("-", "select 1\n\n", "select 1\n", 0), # one trailing newline ("- --check", "select 1\n", "", 1), ("- --diff", "select 1\n", "", 1), ("- --check", "select 1\n", "", 0), diff --git a/tests/unit_tests/test_cli.py b/tests/unit_tests/test_cli.py index 0ff03a2..295c9bb 100755 --- a/tests/unit_tests/test_cli.py +++ b/tests/unit_tests/test_cli.py @@ -72,7 +72,7 @@ def test_stdin(sqlfmt_runner: CliRunner) -> None: stream_input = "select 1" results = sqlfmt_runner.invoke(sqlfmt_main, args="-", input=stream_input) assert results.exit_code == 0 - assert results.stdout == "select 1\n\n" + assert results.stdout == "select 1\n" def test_preformatted_check(sqlfmt_runner: CliRunner, preformatted_dir: Path) -> None: