Skip to content

Commit

Permalink
Pickup correct path component w/ANSIBLE_ROLES_PATH (ansible#3912)
Browse files Browse the repository at this point in the history
  • Loading branch information
cavcrosby committed May 19, 2024
1 parent 492d840 commit 5e3c07f
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tox.yml
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ jobs:
env:
# Number of expected test passes, safety measure for accidental skip of
# tests. Update value if you add/remove tests.
PYTEST_REQPASS: 870
PYTEST_REQPASS: 872
steps:
- uses: actions/checkout@v4
with:
Expand Down
3 changes: 3 additions & 0 deletions examples/roles/role_detection/base/bar/defaults/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
base_var_1: foo
base_var_2: foo
3 changes: 3 additions & 0 deletions examples/roles/role_detection/foo/defaults/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
foo_var_1: bar
foo_var_2: bar
8 changes: 7 additions & 1 deletion src/ansiblelint/file_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import wcmatch.wcmatch
from yaml.error import YAMLError

from ansiblelint.app import get_app
from ansiblelint.config import ANSIBLE_OWNED_KINDS, BASE_KINDS, Options, options
from ansiblelint.constants import CONFIG_FILENAMES, FileType, States

Expand Down Expand Up @@ -226,7 +227,12 @@ def __init__(
parts = self.path.parent.parts
if "roles" in parts:
role = self.path
while role.parent.name != "roles" and role.name:
roles_path = get_app(cached=True).runtime.config.default_roles_path
while (
str(role.parent.absolute()) not in roles_path
and role.parent.name != "roles"
and role.name
):
role = role.parent
if role.exists():
self.role = role.name
Expand Down
38 changes: 38 additions & 0 deletions test/test_cli_role_paths.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,3 +196,41 @@ def test_run_playbook_github(result: bool, env: dict[str, str]) -> None:
"Package installs should not use latest"
)
assert (expected in result_gh.stderr) is result


def test_run_role_identified(local_test_dir: Path) -> None:
"""Test that role name is identified correctly."""
cwd = local_test_dir

env = os.environ.copy()
env["ANSIBLE_ROLES_PATH"] = os.path.realpath(
(cwd / "../examples/roles/role_detection").resolve(),
)
result = run_ansible_lint(
Path("roles/role_detection/foo/defaults/main.yml"),
cwd=cwd,
env=env,
)
assert result.returncode == RC.SUCCESS


def test_run_role_identified_prefix_missing(local_test_dir: Path) -> None:
"""Test that role name is identified correctly, with prefix violations."""
cwd = local_test_dir

env = os.environ.copy()
env["ANSIBLE_ROLES_PATH"] = os.path.realpath(
(cwd / "../examples/roles/role_detection/base").resolve(),
)
result = run_ansible_lint(
Path("roles/role_detection/base/bar/defaults/main.yml"),
cwd=cwd,
env=env,
)
assert result.returncode == RC.VIOLATIONS_FOUND
assert (
"Variables names from within roles should use bar_ as a prefix" in result.stdout
)
assert (
"Variables names from within roles should use bar_ as a prefix" in result.stdout
)

0 comments on commit 5e3c07f

Please sign in to comment.