summaryrefslogtreecommitdiffstats
path: root/test/rules
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-06-26 06:24:57 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-06-26 06:24:57 +0000
commit1faea9a6c75f33109e8f66b57b432fdad57b3f46 (patch)
tree4184ce38ac0cf9d5a46bbbae03c87be82927f12b /test/rules
parentAdding upstream version 6.17.2. (diff)
downloadansible-lint-upstream.tar.xz
ansible-lint-upstream.zip
Adding upstream version 24.6.1.upstream/24.6.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'test/rules')
-rw-r--r--test/rules/fixtures/ematcher.py1
-rw-r--r--test/rules/fixtures/raw_task.py1
-rw-r--r--test/rules/fixtures/unset_variable_matcher.py1
-rw-r--r--test/rules/test_args.py19
-rw-r--r--test/rules/test_deprecated_module.py1
-rw-r--r--test/rules/test_inline_env_var.py5
-rw-r--r--test/rules/test_no_changed_when.py1
-rw-r--r--test/rules/test_package_latest.py3
-rw-r--r--test/rules/test_role_names.py1
-rw-r--r--test/rules/test_syntax_check.py68
10 files changed, 83 insertions, 18 deletions
diff --git a/test/rules/fixtures/ematcher.py b/test/rules/fixtures/ematcher.py
index 1b04b6b..b034064 100644
--- a/test/rules/fixtures/ematcher.py
+++ b/test/rules/fixtures/ematcher.py
@@ -1,4 +1,5 @@
"""Custom rule used as fixture."""
+
from ansiblelint.rules import AnsibleLintRule
diff --git a/test/rules/fixtures/raw_task.py b/test/rules/fixtures/raw_task.py
index 0d5b023..6dfd7d9 100644
--- a/test/rules/fixtures/raw_task.py
+++ b/test/rules/fixtures/raw_task.py
@@ -1,4 +1,5 @@
"""Test Rule that needs_raw_task."""
+
from __future__ import annotations
from typing import TYPE_CHECKING
diff --git a/test/rules/fixtures/unset_variable_matcher.py b/test/rules/fixtures/unset_variable_matcher.py
index 8486009..ea8b0c0 100644
--- a/test/rules/fixtures/unset_variable_matcher.py
+++ b/test/rules/fixtures/unset_variable_matcher.py
@@ -1,4 +1,5 @@
"""Custom linting rule used as test fixture."""
+
from ansiblelint.rules import AnsibleLintRule
diff --git a/test/rules/test_args.py b/test/rules/test_args.py
new file mode 100644
index 0000000..30d83f1
--- /dev/null
+++ b/test/rules/test_args.py
@@ -0,0 +1,19 @@
+"""Tests for args rule."""
+
+from ansiblelint.file_utils import Lintable
+from ansiblelint.rules import RulesCollection
+from ansiblelint.runner import Runner
+
+
+def test_args_module_relative_import(default_rules_collection: RulesCollection) -> None:
+ """Validate args check of a module with a relative import."""
+ lintable = Lintable(
+ "examples/playbooks/module_relative_import.yml",
+ kind="playbook",
+ )
+ result = Runner(lintable, rules=default_rules_collection).run()
+ assert len(result) == 1, result
+ assert result[0].lineno == 5
+ assert result[0].filename == "examples/playbooks/module_relative_import.yml"
+ assert result[0].tag == "args[module]"
+ assert result[0].message == "missing required arguments: name"
diff --git a/test/rules/test_deprecated_module.py b/test/rules/test_deprecated_module.py
index a57d8db..6346b80 100644
--- a/test/rules/test_deprecated_module.py
+++ b/test/rules/test_deprecated_module.py
@@ -1,4 +1,5 @@
"""Tests for deprecated-module rule."""
+
from pathlib import Path
from ansiblelint.rules import RulesCollection
diff --git a/test/rules/test_inline_env_var.py b/test/rules/test_inline_env_var.py
index 98f337e..aa833ec 100644
--- a/test/rules/test_inline_env_var.py
+++ b/test/rules/test_inline_env_var.py
@@ -1,4 +1,5 @@
"""Tests for inline-env-var rule."""
+
from ansiblelint.rules import RulesCollection
from ansiblelint.rules.inline_env_var import EnvVarsInCommandRule
from ansiblelint.testing import RunFromText
@@ -13,7 +14,7 @@ SUCCESS_PLAY_TASKS = """
HELLO: hello
- name: Use some key-value pairs
- command: chdir=/tmp creates=/tmp/bobbins warn=no touch bobbins
+ command: chdir=/tmp creates=/tmp/bobbins touch bobbins
- name: Commands can have flags
command: abc --xyz=def blah
@@ -68,7 +69,7 @@ FAIL_PLAY_TASKS = """
command: HELLO=hello echo $HELLO
- name: Typo some stuff
- command: cerates=/tmp/blah warn=no touch /tmp/blah
+ command: crates=/tmp/blah touch /tmp/blah
"""
diff --git a/test/rules/test_no_changed_when.py b/test/rules/test_no_changed_when.py
index c89d8f4..3316e12 100644
--- a/test/rules/test_no_changed_when.py
+++ b/test/rules/test_no_changed_when.py
@@ -1,4 +1,5 @@
"""Tests for no-change-when rule."""
+
from ansiblelint.rules import RulesCollection
from ansiblelint.rules.no_changed_when import CommandHasChangesCheckRule
from ansiblelint.runner import Runner
diff --git a/test/rules/test_package_latest.py b/test/rules/test_package_latest.py
index 5631f02..972fced 100644
--- a/test/rules/test_package_latest.py
+++ b/test/rules/test_package_latest.py
@@ -1,4 +1,5 @@
"""Tests for package-latest rule."""
+
from ansiblelint.rules import RulesCollection
from ansiblelint.rules.package_latest import PackageIsNotLatestRule
from ansiblelint.runner import Runner
@@ -20,4 +21,4 @@ def test_package_not_latest_negative() -> None:
failure = "examples/playbooks/package-check-failure.yml"
bad_runner = Runner(failure, rules=collection)
errs = bad_runner.run()
- assert len(errs) == 4
+ assert len(errs) == 5
diff --git a/test/rules/test_role_names.py b/test/rules/test_role_names.py
index 491cf14..e13e56a 100644
--- a/test/rules/test_role_names.py
+++ b/test/rules/test_role_names.py
@@ -1,4 +1,5 @@
"""Test the RoleNames rule."""
+
from __future__ import annotations
from typing import TYPE_CHECKING, Any
diff --git a/test/rules/test_syntax_check.py b/test/rules/test_syntax_check.py
index 2fe36a3..6ec111d 100644
--- a/test/rules/test_syntax_check.py
+++ b/test/rules/test_syntax_check.py
@@ -1,32 +1,71 @@
"""Tests for syntax-check rule."""
+
from typing import Any
+import pytest
+
from ansiblelint.file_utils import Lintable
from ansiblelint.rules import RulesCollection
from ansiblelint.runner import Runner
+@pytest.mark.parametrize(
+ ("filename", "expected_results"),
+ (
+ pytest.param(
+ "examples/playbooks/conflicting_action.yml",
+ [
+ (
+ "syntax-check[specific]",
+ 4,
+ 7,
+ "conflicting action statements: ansible.builtin.debug, ansible.builtin.command",
+ ),
+ ],
+ id="0",
+ ),
+ pytest.param(
+ "examples/playbooks/conflicting_action2.yml",
+ [
+ (
+ "parser-error",
+ 1,
+ None,
+ "conflicting action statements: block, include_role",
+ ),
+ (
+ "syntax-check[specific]",
+ 5,
+ 7,
+ "'include_role' is not a valid attribute for a Block",
+ ),
+ ],
+ id="1",
+ ),
+ ),
+)
def test_get_ansible_syntax_check_matches(
default_rules_collection: RulesCollection,
+ filename: str,
+ expected_results: list[tuple[str, int, int, str]],
) -> None:
"""Validate parsing of ansible output."""
lintable = Lintable(
- "examples/playbooks/conflicting_action.yml",
+ filename,
kind="playbook",
)
- result = Runner(lintable, rules=default_rules_collection).run()
+ result = sorted(Runner(lintable, rules=default_rules_collection).run())
- assert result[0].lineno == 4
- assert result[0].column == 7
- assert (
- result[0].message
- == "conflicting action statements: ansible.builtin.debug, ansible.builtin.command"
- )
- # We internally convert absolute paths returned by ansible into paths
- # relative to current directory.
- assert result[0].filename.endswith("/conflicting_action.yml")
- assert len(result) == 1
+ assert len(result) == len(expected_results)
+ for index, expected in enumerate(expected_results):
+ assert result[index].tag == expected[0]
+ assert result[index].lineno == expected[1]
+ assert result[index].column == expected[2]
+ assert str(expected[3]) in result[index].message
+ # We internally convert absolute paths returned by ansible into paths
+ # relative to current directory.
+ # assert result[index].filename.endswith("/conflicting_action.yml")
def test_empty_playbook(default_rules_collection: RulesCollection) -> None:
@@ -58,11 +97,10 @@ def test_extra_vars_passed_to_command(
assert not result
-def test_syntax_check_role() -> None:
+def test_syntax_check_role(default_rules_collection: RulesCollection) -> None:
"""Validate syntax check of a broken role."""
lintable = Lintable("examples/playbooks/roles/invalid_due_syntax", kind="role")
- rules = RulesCollection()
- result = Runner(lintable, rules=rules).run()
+ result = Runner(lintable, rules=default_rules_collection).run()
assert len(result) == 1, result
assert result[0].lineno == 2
assert result[0].filename == "examples/roles/invalid_due_syntax/tasks/main.yml"