Skip to content

Commit 1a50eb7

Browse files
authored
Fixed error when sarif file option is provided (#3587)
1 parent a3f724d commit 1a50eb7

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

.github/workflows/tox.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ jobs:
6161
WSLENV: FORCE_COLOR:PYTEST_REQPASS:TOXENV:GITHUB_STEP_SUMMARY
6262
# Number of expected test passes, safety measure for accidental skip of
6363
# tests. Update value if you add/remove tests.
64-
PYTEST_REQPASS: 804
64+
PYTEST_REQPASS: 805
6565
steps:
6666
- name: Activate WSL1
6767
if: "contains(matrix.shell, 'wsl')"

src/ansiblelint/app.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,11 @@ def render_matches(self, matches: list[MatchError]) -> None:
103103
if self.options.sarif_file:
104104
sarif = formatters.SarifFormatter(self.options.cwd, True)
105105
json = sarif.format_result(matches)
106-
with self.options.sarif_file.open("w", encoding="utf-8") as sarif_file:
106+
with Path.open(
107+
self.options.sarif_file,
108+
"w",
109+
encoding="utf-8",
110+
) as sarif_file:
107111
sarif_file.write(json)
108112

109113
def count_results(self, matches: list[MatchError]) -> SummarizedResults:

test/test_formatter_sarif.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,3 +169,24 @@ def test_sarif_file(file: str, return_code: int) -> None:
169169
assert result.returncode == return_code
170170
assert os.path.exists(output_file.name) # noqa: PTH110
171171
assert os.path.getsize(output_file.name) > 0
172+
173+
174+
@pytest.mark.parametrize(
175+
("file", "return_code"),
176+
(pytest.param("examples/playbooks/valid.yml", 0),),
177+
)
178+
def test_sarif_file_creates_it_if_none_exists(file: str, return_code: int) -> None:
179+
"""Test ability to create sarif file if none exists and dump output to it (--sarif-file)."""
180+
sarif_file_name = "test_output.sarif"
181+
cmd = [
182+
sys.executable,
183+
"-m",
184+
"ansiblelint",
185+
"--sarif-file",
186+
sarif_file_name,
187+
]
188+
result = subprocess.run([*cmd, file], check=False, capture_output=True)
189+
assert result.returncode == return_code
190+
assert os.path.exists(sarif_file_name) # noqa: PTH110
191+
assert os.path.getsize(sarif_file_name) > 0
192+
os.remove(sarif_file_name)

0 commit comments

Comments
 (0)