Skip to content

Commit 9b42f89

Browse files
authored
Refactor no-relative-paths tests (#3270)
1 parent 0ecae66 commit 9b42f89

File tree

5 files changed

+70
-61
lines changed

5 files changed

+70
-61
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
---
2+
- name: Fixture for no-relative-paths
3+
hosts: localhost
4+
tasks:
5+
- name: Template example # <-- 1st
6+
ansible.builtin.template:
7+
src: ../templates/foo.j2
8+
dest: /etc/file.conf
9+
mode: "0644"
10+
- name: Copy example # <-- 2nd
11+
ansible.builtin.copy:
12+
src: ../files/foo.conf
13+
dest: /etc/foo.conf
14+
mode: "0644"
15+
# Removed from test suite as module is no longer part of core
16+
# - name: Some win_template example
17+
# win_template:
18+
# src: ../win_templates/file.conf.j2
19+
# dest: file.conf
20+
# - name: Some win_copy example
21+
# win_copy:
22+
# src: ../files/foo.conf
23+
# dest: renamed-foo.conf
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
- name: Fixture for no-relative-paths
3+
hosts: localhost
4+
tasks:
5+
- name: Content example with no src
6+
ansible.builtin.copy:
7+
content: "# This file was moved to /etc/other.conf"
8+
dest: /etc/mine.conf
9+
mode: "0644"
10+
- name: Copy example
11+
ansible.builtin.copy:
12+
src: /home/example/files/foo.conf
13+
dest: /etc/foo.conf
14+
mode: "0644"
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
---
22
- name: Bad git 1 # noqa: latest[git]
3-
action: ansible.builtin.git repo=foo clone=no
3+
action: ansible.builtin.git repo=. clone=no
44
- name: Bad git 2 <-- 1st
5-
action: ansible.builtin.git repo=foo clone=no
5+
action: ansible.builtin.git repo=. clone=no
66
- name: Block with rescue and always section
77
block:
88
- name: Bad git 3 # noqa: latest[git]
9-
action: ansible.builtin.git repo=foo clone=no
9+
action: ansible.builtin.git repo=. clone=no
1010
- name: Bad git 4 <-- 2nd
11-
action: ansible.builtin.git repo=foo clone=no
11+
action: ansible.builtin.git repo=. clone=no
1212
rescue:
1313
- name: Bad git 5 # noqa: latest[git]
14-
action: ansible.builtin.git repo=foo clone=no
14+
action: ansible.builtin.git repo=. clone=no
1515
- name: Bad git 6 <-- 3rd
16-
action: ansible.builtin.git repo=foo clone=no
16+
action: ansible.builtin.git repo=. clone=no
1717
always:
1818
- name: Bad git 7 # noqa: latest[git]
19-
action: ansible.builtin.git repo=foo clone=no
19+
action: ansible.builtin.git repo=. clone=no
2020
- name: Bad git 8 <-- 4th
21-
action: ansible.builtin.git repo=foo clone=no
21+
action: ansible.builtin.git repo=. clone=no

src/ansiblelint/rules/no_relative_paths.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
from __future__ import annotations
66

7+
import sys
78
from typing import TYPE_CHECKING, Any
89

910
from ansiblelint.rules import AnsibleLintRule
@@ -43,3 +44,27 @@ def matchtask(
4344
return True
4445

4546
return False
47+
48+
49+
# testing code to be loaded only with pytest or when executed the rule file
50+
if "pytest" in sys.modules:
51+
import pytest
52+
53+
from ansiblelint.rules import RulesCollection # pylint: disable=ungrouped-imports
54+
from ansiblelint.runner import Runner # pylint: disable=ungrouped-imports
55+
56+
@pytest.mark.parametrize(
57+
("test_file", "failures"),
58+
(
59+
pytest.param("examples/playbooks/no_relative_paths_fail.yml", 2, id="fail"),
60+
pytest.param("examples/playbooks/no_relative_paths_pass.yml", 0, id="pass"),
61+
),
62+
)
63+
def test_no_relative_paths(
64+
default_rules_collection: RulesCollection, test_file: str, failures: int
65+
) -> None:
66+
"""Test rule matches."""
67+
results = Runner(test_file, rules=default_rules_collection).run()
68+
assert len(results) == failures
69+
for result in results:
70+
assert result.tag == "no-relative-paths"

test/rules/test_no_relative_paths.py

Lines changed: 0 additions & 53 deletions
This file was deleted.

0 commit comments

Comments
 (0)