Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pickup correct path component w/ANSIBLE_ROLES_PATH #4176

Merged
merged 1 commit into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/tox.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ jobs:
env:
# Number of expected test passes, safety measure for accidental skip of
# tests. Update value if you add/remove tests.
PYTEST_REQPASS: 877
PYTEST_REQPASS: 879
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
)
Loading