diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 16:04:56 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 16:04:56 +0000 |
commit | d964cec5e6aa807b75c7a4e7cdc5f11e54b2eda2 (patch) | |
tree | 794bc3738a00b5e599f06d1f2f6d79048d87ff8e /test/test_progressive.py | |
parent | Initial commit. (diff) | |
download | ansible-lint-d964cec5e6aa807b75c7a4e7cdc5f11e54b2eda2.tar.xz ansible-lint-d964cec5e6aa807b75c7a4e7cdc5f11e54b2eda2.zip |
Adding upstream version 6.13.1.upstream/6.13.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'test/test_progressive.py')
-rw-r--r-- | test/test_progressive.py | 93 |
1 files changed, 93 insertions, 0 deletions
diff --git a/test/test_progressive.py b/test/test_progressive.py new file mode 100644 index 0000000..805291b --- /dev/null +++ b/test/test_progressive.py @@ -0,0 +1,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) |