Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed error when sarif file option is provided #3587

Merged
merged 2 commits into from
Jun 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)