summaryrefslogtreecommitdiffstats
path: root/test/test_utils.py
diff options
context:
space:
mode:
Diffstat (limited to 'test/test_utils.py')
-rw-r--r--test/test_utils.py107
1 files changed, 70 insertions, 37 deletions
diff --git a/test/test_utils.py b/test/test_utils.py
index 1b9a2dc..6f728dc 100644
--- a/test/test_utils.py
+++ b/test/test_utils.py
@@ -51,37 +51,44 @@ runtime = Runtime(require_module=True)
@pytest.mark.parametrize(
- ("string", "expected_cmd", "expected_args", "expected_kwargs"),
+ ("string", "expected_args", "expected_kwargs"),
(
- pytest.param("", "", [], {}, id="blank"),
- pytest.param("vars:", "vars", [], {}, id="single_word"),
- pytest.param("hello: a=1", "hello", [], {"a": "1"}, id="string_module_and_arg"),
- pytest.param("action: hello a=1", "hello", [], {"a": "1"}, id="strips_action"),
+ pytest.param("", [], {}, id="a"),
+ pytest.param("a=1", [], {"a": "1"}, id="b"),
+ pytest.param("hello a=1", ["hello"], {"a": "1"}, id="c"),
pytest.param(
- "action: whatever bobbins x=y z=x c=3",
- "whatever",
- ["bobbins", "x=y", "z=x", "c=3"],
- {},
+ "whatever bobbins x=y z=x c=3",
+ ["whatever", "bobbins"],
+ {"x": "y", "z": "x", "c": "3"},
id="more_than_one_arg",
),
pytest.param(
- "action: command chdir=wxy creates=zyx tar xzf zyx.tgz",
- "command",
- ["tar", "xzf", "zyx.tgz"],
+ "command chdir=wxy creates=zyx tar xzf zyx.tgz",
+ ["command", "tar", "xzf", "zyx.tgz"],
{"chdir": "wxy", "creates": "zyx"},
id="command_with_args",
),
+ pytest.param(
+ "{{ varset }}.yml",
+ ["{{ varset }}.yml"],
+ {},
+ id="x",
+ ),
+ pytest.param(
+ "foo bar.yml",
+ ["foo bar.yml"],
+ {},
+ id="path-with-spaces",
+ ),
),
)
def test_tokenize(
string: str,
- expected_cmd: str,
expected_args: Sequence[str],
expected_kwargs: dict[str, Any],
) -> None:
"""Test that tokenize works for different input types."""
- (cmd, args, kwargs) = utils.tokenize(string)
- assert cmd == expected_cmd
+ (args, kwargs) = utils.tokenize(string)
assert args == expected_args
assert kwargs == expected_kwargs
@@ -113,37 +120,42 @@ def test_normalize(
alternate_forms: tuple[dict[str, Any]],
) -> None:
"""Test that tasks specified differently are normalized same way."""
- normal_form = utils.normalize_task(reference_form, "tasks.yml")
+ task = utils.Task(reference_form, filename="tasks.yml")
+ normal_form = task._normalize_task() # noqa: SLF001
for form in alternate_forms:
- assert normal_form == utils.normalize_task(form, "tasks.yml")
+ task2 = utils.Task(form, filename="tasks.yml")
+ assert normal_form == task2._normalize_task() # noqa: SLF001
def test_normalize_complex_command() -> None:
"""Test that tasks specified differently are normalized same way."""
- task1 = {
- "name": "hello",
- "action": {"module": "pip", "name": "df", "editable": "false"},
- }
- task2 = {"name": "hello", "pip": {"name": "df", "editable": "false"}}
- task3 = {"name": "hello", "pip": "name=df editable=false"}
- task4 = {"name": "hello", "action": "pip name=df editable=false"}
- assert utils.normalize_task(task1, "tasks.yml") == utils.normalize_task(
- task2,
- "tasks.yml",
+ task1 = utils.Task(
+ {
+ "name": "hello",
+ "action": {"module": "pip", "name": "df", "editable": "false"},
+ },
+ filename="tasks.yml",
)
- assert utils.normalize_task(task2, "tasks.yml") == utils.normalize_task(
- task3,
- "tasks.yml",
+ task2 = utils.Task(
+ {"name": "hello", "pip": {"name": "df", "editable": "false"}},
+ filename="tasks.yml",
)
- assert utils.normalize_task(task3, "tasks.yml") == utils.normalize_task(
- task4,
- "tasks.yml",
+ task3 = utils.Task(
+ {"name": "hello", "pip": "name=df editable=false"},
+ filename="tasks.yml",
)
+ task4 = utils.Task(
+ {"name": "hello", "action": "pip name=df editable=false"},
+ filename="tasks.yml",
+ )
+ assert task1._normalize_task() == task2._normalize_task() # noqa: SLF001
+ assert task2._normalize_task() == task3._normalize_task() # noqa: SLF001
+ assert task3._normalize_task() == task4._normalize_task() # noqa: SLF001
@pytest.mark.parametrize(
- ("task", "expected_form"),
+ ("task_raw", "expected_form"),
(
pytest.param(
{
@@ -191,8 +203,12 @@ def test_normalize_complex_command() -> None:
),
),
)
-def test_normalize_task_v2(task: dict[str, Any], expected_form: dict[str, Any]) -> None:
+def test_normalize_task_v2(
+ task_raw: dict[str, Any],
+ expected_form: dict[str, Any],
+) -> None:
"""Check that it normalizes task and returns the expected form."""
+ task = utils.Task(task_raw)
assert utils.normalize_task_v2(task) == expected_form
@@ -262,8 +278,8 @@ def test_template(template: str, output: str) -> None:
def test_task_to_str_unicode() -> None:
"""Ensure that extracting messages from tasks preserves Unicode."""
- task = {"fail": {"msg": "unicode é ô à"}}
- result = utils.task_to_str(utils.normalize_task(task, "filename.yml"))
+ task = utils.Task({"fail": {"msg": "unicode é ô à"}}, filename="filename.yml")
+ result = utils.task_to_str(task._normalize_task()) # noqa: SLF001
assert result == "fail msg=unicode é ô à"
@@ -447,3 +463,20 @@ def test_task_in_list(file: str, names: list[str], positions: list[str]) -> None
for index, task in enumerate(tasks):
assert task.name == names[index]
assert task.position == positions[index]
+
+
+def test_find_children_in_module(default_rules_collection: RulesCollection) -> None:
+ """Verify correct function of find_children() in tasks."""
+ lintable = Lintable("plugins/modules/fake_module.py")
+ children = Runner(
+ rules=default_rules_collection,
+ ).find_children(lintable)
+ assert len(children) == 1
+ child = children[0]
+
+ # Parent is a python file
+ assert lintable.base_kind == "text/python"
+
+ # Child correctly looks like a YAML file
+ assert child.base_kind == "text/yaml"
+ assert child.content.startswith("---")