diff --git a/.github/workflows/tox.yml b/.github/workflows/tox.yml index 77d546253e..448dba4e9f 100644 --- a/.github/workflows/tox.yml +++ b/.github/workflows/tox.yml @@ -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: diff --git a/examples/roles/role_detection/base/bar/defaults/main.yml b/examples/roles/role_detection/base/bar/defaults/main.yml new file mode 100644 index 0000000000..faa4ea15ff --- /dev/null +++ b/examples/roles/role_detection/base/bar/defaults/main.yml @@ -0,0 +1,3 @@ +--- +base_var_1: foo +base_var_2: foo diff --git a/examples/roles/role_detection/foo/defaults/main.yml b/examples/roles/role_detection/foo/defaults/main.yml new file mode 100644 index 0000000000..1a5b54b727 --- /dev/null +++ b/examples/roles/role_detection/foo/defaults/main.yml @@ -0,0 +1,3 @@ +--- +foo_var_1: bar +foo_var_2: bar diff --git a/src/ansiblelint/file_utils.py b/src/ansiblelint/file_utils.py index 83bd04f1f1..04ce3cdaac 100644 --- a/src/ansiblelint/file_utils.py +++ b/src/ansiblelint/file_utils.py @@ -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 @@ -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 diff --git a/test/test_cli_role_paths.py b/test/test_cli_role_paths.py index d01a87cd44..131c3b550d 100644 --- a/test/test_cli_role_paths.py +++ b/test/test_cli_role_paths.py @@ -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 + )