Skip to content

Commit

Permalink
Fixed error when sarif file option is provided (#3587)
Browse files Browse the repository at this point in the history
  • Loading branch information
audgirka authored Jun 22, 2023
1 parent a3f724d commit 1a50eb7
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tox.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ jobs:
WSLENV: FORCE_COLOR:PYTEST_REQPASS:TOXENV:GITHUB_STEP_SUMMARY
# Number of expected test passes, safety measure for accidental skip of
# tests. Update value if you add/remove tests.
PYTEST_REQPASS: 804
PYTEST_REQPASS: 805
steps:
- name: Activate WSL1
if: "contains(matrix.shell, 'wsl')"
Expand Down
6 changes: 5 additions & 1 deletion src/ansiblelint/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,11 @@ def render_matches(self, matches: list[MatchError]) -> None:
if self.options.sarif_file:
sarif = formatters.SarifFormatter(self.options.cwd, True)
json = sarif.format_result(matches)
with self.options.sarif_file.open("w", encoding="utf-8") as sarif_file:
with Path.open(
self.options.sarif_file,
"w",
encoding="utf-8",
) as sarif_file:
sarif_file.write(json)

def count_results(self, matches: list[MatchError]) -> SummarizedResults:
Expand Down
21 changes: 21 additions & 0 deletions test/test_formatter_sarif.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,3 +169,24 @@ def test_sarif_file(file: str, return_code: int) -> None:
assert result.returncode == return_code
assert os.path.exists(output_file.name) # noqa: PTH110
assert os.path.getsize(output_file.name) > 0


@pytest.mark.parametrize(
("file", "return_code"),
(pytest.param("examples/playbooks/valid.yml", 0),),
)
def test_sarif_file_creates_it_if_none_exists(file: str, return_code: int) -> None:
"""Test ability to create sarif file if none exists and dump output to it (--sarif-file)."""
sarif_file_name = "test_output.sarif"
cmd = [
sys.executable,
"-m",
"ansiblelint",
"--sarif-file",
sarif_file_name,
]
result = subprocess.run([*cmd, file], check=False, capture_output=True)
assert result.returncode == return_code
assert os.path.exists(sarif_file_name) # noqa: PTH110
assert os.path.getsize(sarif_file_name) > 0
os.remove(sarif_file_name)

0 comments on commit 1a50eb7

Please sign in to comment.