Skip to content

Commit

Permalink
Ensure that JSON output does not contain newlines (#3315)
Browse files Browse the repository at this point in the history
  • Loading branch information
ssbarnea authored Apr 21, 2023
1 parent 53144e9 commit e157979
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 5 deletions.
7 changes: 4 additions & 3 deletions src/ansiblelint/formatters/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,8 @@ def format_result(self, matches: list[MatchError]) -> str:
# Append issue to result list
result.append(issue)

return json.dumps(result, sort_keys=False, indent=2)
# Keep it single line due to https://github.com/ansible/ansible-navigator/issues/1490
return json.dumps(result, sort_keys=False)

@staticmethod
def _remap_severity(match: MatchError) -> str:
Expand Down Expand Up @@ -237,8 +238,8 @@ def format_result(self, matches: list[MatchError]) -> str:
"version": self.SARIF_SCHEMA_VERSION,
"runs": runs,
}

return json.dumps(report, sort_keys=False, indent=2)
# Keep it single line due to https://github.com/ansible/ansible-navigator/issues/1490
return json.dumps(report, sort_keys=False)

def _extract_results(
self,
Expand Down
5 changes: 4 additions & 1 deletion test/test_formatter_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,10 @@ def test_format_list(self) -> None:
def test_result_is_json(self) -> None:
"""Test if returned string value is a JSON."""
assert isinstance(self.formatter, CodeclimateJSONFormatter)
json.loads(self.formatter.format_result(self.matches))
output = self.formatter.format_result(self.matches)
json.loads(output)
# https://github.com/ansible/ansible-navigator/issues/1490
assert "\n" not in output

def test_single_match(self) -> None:
"""Test negative case. Only lists are allowed. Otherwise a RuntimeError will be raised."""
Expand Down
5 changes: 4 additions & 1 deletion test/test_formatter_sarif.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,10 @@ def test_format_list(self) -> None:
def test_result_is_json(self) -> None:
"""Test if returned string value is a JSON."""
assert isinstance(self.formatter, SarifFormatter)
json.loads(self.formatter.format_result(self.matches))
output = self.formatter.format_result(self.matches)
json.loads(output)
# https://github.com/ansible/ansible-navigator/issues/1490
assert "\n" not in output

def test_single_match(self) -> None:
"""Test negative case. Only lists are allowed. Otherwise, a RuntimeError will be raised."""
Expand Down

0 comments on commit e157979

Please sign in to comment.