1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
|
"""Test for the --progressive mode."""
from __future__ import annotations
import json
import subprocess
import sys
from pathlib import Path
from ansiblelint.constants import GIT_CMD
from ansiblelint.file_utils import cwd
FAULTY_PLAYBOOK = """---
- name: faulty
hosts: localhost
tasks:
- name: hello
debug:
msg: world
"""
CORRECT_PLAYBOOK = """---
- name: Correct
hosts: localhost
tasks:
- name: Hello
ansible.builtin.debug:
msg: world
"""
def git_init() -> None:
"""Init temporary git repository."""
subprocess.run([*GIT_CMD, "init", "--initial-branch=main"], check=True)
subprocess.run([*GIT_CMD, "config", "user.email", "test@example.com"], check=True)
subprocess.run([*GIT_CMD, "config", "user.name", "test"], check=True)
def git_commit(filename: Path, content: str) -> None:
"""Create and commit a file."""
filename.write_text(content)
subprocess.run([*GIT_CMD, "add", filename], check=True)
subprocess.run(
[
*GIT_CMD,
"commit",
"--no-gpg-sign",
"-a",
"-m",
f"Commit {filename}",
],
check=True,
)
def run_lint(cmd: list[str]) -> subprocess.CompletedProcess[str]:
"""Run ansible-lint."""
# pylint: disable=subprocess-run-check
return subprocess.run(
cmd,
capture_output=True,
text=True,
)
def test_validate_progressive_mode_json_output(tmp_path: Path) -> None:
"""Test that covers the following scenarios for progressive mode.
1. JSON output is valid in quiet and verbose modes
2. New files are correctly handled whether lintable paths are passed or not
3. Regression is not reported when the last commit doesn't add any new violations
"""
cmd = [
sys.executable,
"-m",
"ansiblelint",
"--progressive",
"-f",
"json",
]
with cwd(tmp_path):
git_init()
git_commit(tmp_path / "README.md", "pytest")
git_commit(tmp_path / "playbook-faulty.yml", FAULTY_PLAYBOOK)
cmd.append("-q")
res = run_lint(cmd)
assert res.returncode == 2
json.loads(res.stdout)
git_commit(tmp_path / "playbook-correct.yml", CORRECT_PLAYBOOK)
cmd.extend(["-vv", "playbook-correct.yml"])
res = run_lint(cmd)
assert res.returncode == 0
json.loads(res.stdout)
|