summaryrefslogtreecommitdiffstats
path: root/test/rules/test_literal_compare.py
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-28 16:04:56 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-28 16:04:56 +0000
commitd964cec5e6aa807b75c7a4e7cdc5f11e54b2eda2 (patch)
tree794bc3738a00b5e599f06d1f2f6d79048d87ff8e /test/rules/test_literal_compare.py
parentInitial commit. (diff)
downloadansible-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/rules/test_literal_compare.py')
-rw-r--r--test/rules/test_literal_compare.py93
1 files changed, 93 insertions, 0 deletions
diff --git a/test/rules/test_literal_compare.py b/test/rules/test_literal_compare.py
new file mode 100644
index 0000000..6953cb1
--- /dev/null
+++ b/test/rules/test_literal_compare.py
@@ -0,0 +1,93 @@
+"""Tests for literal-compare rule."""
+import pytest
+
+from ansiblelint.rules import RulesCollection
+from ansiblelint.rules.literal_compare import ComparisonToLiteralBoolRule
+from ansiblelint.testing import RunFromText
+
+PASS_WHEN = """
+- name: Example task
+ debug:
+ msg: test
+ when: my_var
+
+- name: Another example task
+ debug:
+ msg: test
+ when:
+ - 1 + 1 == 2
+ - true
+"""
+
+PASS_WHEN_NOT_FALSE = """
+- name: Example task
+ debug:
+ msg: test
+ when: not my_var
+"""
+
+PASS_WHEN_NOT_NULL = """
+- name: Example task
+ debug:
+ msg: test
+ when: my_var not None
+"""
+
+FAIL_LITERAL_TRUE = """
+- name: Example task
+ debug:
+ msg: test
+ when: my_var == True
+"""
+
+FAIL_LITERAL_FALSE = """
+- name: Example task
+ debug:
+ msg: test
+ when: my_var == false
+
+- name: Another example task
+ debug:
+ msg: test
+ when:
+ - my_var == false
+"""
+
+
+@pytest.mark.parametrize(
+ ("input_str", "found_errors"),
+ (
+ pytest.param(
+ PASS_WHEN,
+ 0,
+ id="pass_when",
+ ),
+ pytest.param(
+ PASS_WHEN_NOT_FALSE,
+ 0,
+ id="when_not_false",
+ ),
+ pytest.param(
+ PASS_WHEN_NOT_NULL,
+ 0,
+ id="when_not_null",
+ ),
+ pytest.param(
+ FAIL_LITERAL_TRUE,
+ 1,
+ id="literal_true",
+ ),
+ pytest.param(
+ FAIL_LITERAL_FALSE,
+ 2,
+ id="literal_false",
+ ),
+ ),
+)
+def test_literal_compare(input_str: str, found_errors: int) -> None:
+ """Test literal-compare."""
+ collection = RulesCollection()
+ collection.register(ComparisonToLiteralBoolRule())
+ runner = RunFromText(collection)
+ results = runner.run_role_tasks_main(input_str)
+ assert len(results) == found_errors