Skip to content

Commit 471f3ca

Browse files
authored
Fix config path detection logic (#3312)
1 parent e5d67f5 commit 471f3ca

File tree

5 files changed

+18
-8
lines changed

5 files changed

+18
-8
lines changed

examples/roles/test-role/molecule/default/include-import-role.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
- name: test
2+
- name: Fixture for test_run_playbook test
33
gather_facts: false
44
hosts: all
55
roles:

src/ansiblelint/cli.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -581,7 +581,7 @@ def get_config(arguments: list[str]) -> Namespace:
581581
log_entries.append(
582582
(
583583
logging.INFO,
584-
f"Identified `{project_dir}` as project root as project root containing {method}.",
584+
f"Identified [filename]{project_dir}[/] as project root due [bold]{method}[/].",
585585
),
586586
)
587587

src/ansiblelint/file_utils.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -519,9 +519,14 @@ def find_project_root(
519519
return directory, ".hg directory"
520520

521521
for cfg_file in cfg_files:
522-
if not os.path.isabs(cfg_file):
523-
if (directory / cfg_file).is_file():
524-
return directory, str(cfg_file)
522+
# note that if cfg_file is already absolute, 'directory' is ignored
523+
resolved_cfg_path = directory / cfg_file
524+
if resolved_cfg_path.is_file():
525+
if os.path.isabs(cfg_file):
526+
directory = Path(cfg_file).parent
527+
if directory.name == ".config":
528+
directory = directory.parent
529+
return directory, f"config file {resolved_cfg_path}"
525530

526531
if not directory:
527532
return Path.cwd(), "current working directory"

test/test_cli_role_paths.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import pytest
88

9+
from ansiblelint.constants import RC
910
from ansiblelint.testing import run_ansible_lint
1011
from ansiblelint.text import strip_ansi_escape
1112

@@ -90,8 +91,12 @@ def test_run_playbook(local_test_dir: str) -> None:
9091
env = os.environ.copy()
9192
env["ANSIBLE_ROLES_PATH"] = role_path
9293

93-
result = run_ansible_lint(lintable, cwd=cwd, env=env)
94-
assert "Use shell only when shell functionality is required" in result.stdout
94+
result = run_ansible_lint("-f", "pep8", lintable, cwd=cwd, env=env)
95+
# All 4 failures are expected to be found inside the included role and not
96+
# from the playbook given as argument.
97+
assert result.returncode == RC.VIOLATIONS_FOUND
98+
assert "tasks/main.yml:2: command-instead-of-shell" in result.stdout
99+
assert "tasks/world.yml:2: name[missing]" in result.stdout
95100

96101

97102
@pytest.mark.parametrize(

test/test_file_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ def test_find_project_root_dotconfig() -> None:
324324
), "Test requires config file inside .config folder."
325325
path, method = find_project_root([])
326326
assert str(path) == str(os.getcwd())
327-
assert method == ".config/ansible-lint.yml"
327+
assert ".config/ansible-lint.yml" in method
328328

329329

330330
BASIC_PLAYBOOK = """

0 commit comments

Comments
 (0)