Skip to content

Commit

Permalink
Fix sarif output to use 'match.details' in result object, instead of …
Browse files Browse the repository at this point in the history
…'match.message' (#3163)

Co-authored-by: Ajinkya Udgirkar <[email protected]>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Sorin Sbarnea <[email protected]>
  • Loading branch information
4 people committed Mar 24, 2023
1 parent 0fd5900 commit e53dcdd
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
4 changes: 3 additions & 1 deletion src/ansiblelint/formatters/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,9 @@ def _to_sarif_result(self, match: MatchError) -> dict[str, Any]:
result: dict[str, Any] = {
"ruleId": match.tag,
"message": {
"text": str(match.message),
"text": str(match.details)
if str(match.details)
else str(match.message),
},
"locations": [
{
Expand Down
9 changes: 5 additions & 4 deletions test/test_formatter_sarif.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def setup_class(self) -> None:
message="message",
linenumber=1,
column=10,
details="hello",
details="details",
filename=Lintable("filename.yml", content=""),
rule=self.rule,
tag="yaml[test]",
Expand All @@ -47,7 +47,7 @@ def setup_class(self) -> None:
MatchError(
message="message",
linenumber=2,
details="hello",
details="",
filename=Lintable("filename.yml", content=""),
rule=self.rule,
tag="yaml[test]",
Expand All @@ -66,7 +66,7 @@ def test_result_is_json(self) -> None:
json.loads(self.formatter.format_result(self.matches))

def test_single_match(self) -> None:
"""Test negative case. Only lists are allowed. Otherwise a RuntimeError will be raised."""
"""Test negative case. Only lists are allowed. Otherwise, a RuntimeError will be raised."""
assert isinstance(self.formatter, SarifFormatter)
with pytest.raises(RuntimeError):
self.formatter.format_result(self.matches[0]) # type: ignore
Expand Down Expand Up @@ -99,7 +99,6 @@ def test_validate_sarif_schema(self) -> None:
assert len(results) == 2
for i, result in enumerate(results):
assert result["ruleId"] == self.matches[i].tag
assert result["message"]["text"] == self.matches[0].message
assert (
result["locations"][0]["physicalLocation"]["artifactLocation"]["uri"]
== self.matches[i].filename
Expand All @@ -125,6 +124,8 @@ def test_validate_sarif_schema(self) -> None:
not in result["locations"][0]["physicalLocation"]["region"]
)
assert sarif["runs"][0]["originalUriBaseIds"][SarifFormatter.BASE_URI_ID]["uri"]
assert results[0]["message"]["text"] == self.matches[0].details
assert results[1]["message"]["text"] == self.matches[1].message


def test_sarif_parsable_ignored() -> None:
Expand Down

0 comments on commit e53dcdd

Please sign in to comment.