Skip to content

Commit

Permalink
Add newline at the end of JSON output (#1095)
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-turbaszek authored May 22, 2024
1 parent 7391771 commit d99449c
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
1 change: 1 addition & 0 deletions RELEASE-NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
## Fixes and improvements
* Improved support for quoted identifiers.
* Fixed creating patches with `snow app version create` when there are 2 or more existing patches on a version
* Using `--format=json` adds trailing new line to avoid `%` being added by some terminals to signal no new line at the end of output.

# v2.3.1
## Backward incompatibility
Expand Down
4 changes: 3 additions & 1 deletion src/snowflake/cli/app/printing.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,9 @@ def print_structured(result: CommandResult):
if isinstance(result, MultipleResults):
_stream_json(result)
else:
return json.dump(result, sys.stdout, cls=CustomJSONEncoder, indent=4)
json.dump(result, sys.stdout, cls=CustomJSONEncoder, indent=4)
# Adds empty line at the end
print()


def _stream_json(result):
Expand Down
13 changes: 10 additions & 3 deletions tests/output/test_printing.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
from datetime import datetime
from textwrap import dedent
from typing import NamedTuple
Expand Down Expand Up @@ -288,18 +289,24 @@ def test_print_with_no_data_table(capsys):

def test_print_with_no_data_in_query_json(capsys, _empty_cursor):
print_result(QueryResult(_empty_cursor()), output_format=OutputFormat.JSON)
assert get_output(capsys) == "[]"
json_str = get_output(capsys)
json.loads(json_str)
assert json_str == "[]\n"


def test_print_with_no_data_in_single_value_query_json(capsys, _empty_cursor):
print_result(SingleQueryResult(_empty_cursor()), output_format=OutputFormat.JSON)
assert get_output(capsys) == "null"
json_str = get_output(capsys)
json.loads(json_str)
assert json_str == "null\n"


def test_print_with_no_response_json(capsys):
print_result(None, output_format=OutputFormat.JSON)

assert get_output(capsys) == "null"
json_str = get_output(capsys)
json.loads(json_str)
assert json_str == "null\n"


@pytest.fixture
Expand Down

0 comments on commit d99449c

Please sign in to comment.