Skip to content

Commit 388d015

Browse files
authored
Fix strict mode (#2601)
Fixes: #2592
1 parent 1bb0f2d commit 388d015

File tree

6 files changed

+51
-10
lines changed

6 files changed

+51
-10
lines changed

.github/workflows/tox.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ jobs:
166166
WSLENV: FORCE_COLOR:PYTEST_REQPASS:TOXENV:TOX_PARALLEL_NO_SPINNER
167167
# Number of expected test passes, safety measure for accidental skip of
168168
# tests. Update value if you add/remove tests.
169-
PYTEST_REQPASS: 705
169+
PYTEST_REQPASS: 707
170170

171171
steps:
172172
- name: Activate WSL1

examples/playbooks/strict-mode.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
- name: Fixture for test_strict
3+
hosts: localhost
4+
tasks:
5+
- ansible.builtin.debug: # <-- name should be first key (warning)
6+
msg: "Hello World"
7+
name: Display debug information

src/ansiblelint/__main__.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,8 +214,9 @@ def main(argv: list[str] | None = None) -> int: # noqa: C901
214214
if options.write_list:
215215
_do_transform(result, options)
216216

217-
mark_as_success = False
217+
mark_as_success = True
218218
if result.matches and options.progressive:
219+
mark_as_success = False
219220
_logger.info(
220221
"Matches found, running again on previous revision in order to detect regressions"
221222
)
@@ -247,6 +248,9 @@ def main(argv: list[str] | None = None) -> int: # noqa: C901
247248
ignored,
248249
)
249250

251+
if options.strict and result.matches:
252+
mark_as_success = False
253+
250254
app.render_matches(result.matches)
251255

252256
_perform_mockings_cleanup()

src/ansiblelint/app.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -193,17 +193,23 @@ def report_outcome(self, result: LintResult, mark_as_success: bool = False) -> i
193193
"because 'yaml' is in 'skip_list'."
194194
)
195195

196+
if summary.failures:
197+
mark_as_success = False
198+
196199
if not self.options.quiet:
197200
console_stderr.print(render_yaml(msg))
198-
self.report_summary(summary, changed_files_count, files_count)
201+
self.report_summary(
202+
summary, changed_files_count, files_count, is_success=mark_as_success
203+
)
199204

200-
if not self.options.strict and (mark_as_success or not summary.failures):
201-
return SUCCESS_RC
202-
return VIOLATIONS_FOUND_RC
205+
return SUCCESS_RC if mark_as_success else VIOLATIONS_FOUND_RC
203206

204207
@staticmethod
205208
def report_summary( # pylint: disable=too-many-branches,too-many-locals
206-
summary: SummarizedResults, changed_files_count: int, files_count: int
209+
summary: SummarizedResults,
210+
changed_files_count: int,
211+
files_count: int,
212+
is_success: bool,
207213
) -> None:
208214
"""Report match and file counts."""
209215
# sort the stats by profiles
@@ -269,10 +275,10 @@ def report_summary( # pylint: disable=too-many-branches,too-many-locals
269275
console_stderr.print(table)
270276
console_stderr.print()
271277

272-
if summary.failures:
273-
msg = "[red][bold]Failed[/][/] after "
274-
else:
278+
if is_success:
275279
msg = "[green]Passed[/] with "
280+
else:
281+
msg = "[red][bold]Failed[/][/] after "
276282

277283
if summary.passed_profile:
278284
msg += f"[bold]{summary.passed_profile}[/] profile"

src/ansiblelint/rules/only_builtins.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ def test_only_builtin_fail() -> None:
5252
"""Test rule matches."""
5353
result = run_ansible_lint(
5454
"--config-file=/dev/null",
55+
"--strict",
5556
"--warn-list=",
5657
"--enable-list",
5758
"only-builtins",

test/test_strict.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
"""Test strict mode."""
2+
import pytest
3+
4+
from ansiblelint.testing import run_ansible_lint
5+
6+
7+
@pytest.mark.parametrize(
8+
("strict", "returncode", "message"),
9+
(
10+
pytest.param(True, 2, "Failed", id="on"),
11+
pytest.param(False, 0, "Passed", id="off"),
12+
),
13+
)
14+
def test_strict(strict: bool, returncode: int, message: str) -> None:
15+
"""Test running from inside meta folder."""
16+
args = ["examples/playbooks/strict-mode.yml"]
17+
if strict:
18+
args.insert(0, "--strict")
19+
result = run_ansible_lint(*args)
20+
assert result.returncode == returncode
21+
assert "key-order[task]" in result.stdout
22+
summary_line = result.stderr.splitlines()[-1]
23+
assert message in summary_line

0 commit comments

Comments
 (0)