Skip to content

Commit

Permalink
Fix project directory detection with config inside .config (#2540)
Browse files Browse the repository at this point in the history
Related: #2499
  • Loading branch information
ssbarnea committed Oct 3, 2022
1 parent 551da55 commit e3fdb43
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tox.yml
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ jobs:
WSLENV: FORCE_COLOR:PYTEST_REQPASS:TOXENV:TOX_PARALLEL_NO_SPINNER
# Number of expected test passes, safety measure for accidental skip of
# tests. Update value if you add/remove tests.
PYTEST_REQPASS: 704
PYTEST_REQPASS: 705

steps:
- name: Activate WSL1
Expand Down
8 changes: 5 additions & 3 deletions src/ansiblelint/file_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -427,9 +427,11 @@ def guess_project_dir(config_file: str | None) -> str:
target = pathlib.Path(config_file)
if target.exists():
# for config inside .config, we return the parent dir as project dir
if target.name == ".config":
target = target.parent
path = str(target.parent.absolute())
cfg_path = target.parent
if cfg_path.parts[-1] == ".config":
path = str(cfg_path.parent.absolute())
else:
path = str(cfg_path.absolute())

if path is None:
try:
Expand Down
12 changes: 11 additions & 1 deletion test/test_file_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,13 +218,23 @@ def mockreturn(options: Namespace) -> dict[str, Any]:
assert lintable_detected.kind == result[lintable_expected.name]


def test_guess_project_dir(tmp_path: Path) -> None:
def test_guess_project_dir_tmp_path(tmp_path: Path) -> None:
"""Verify guess_project_dir()."""
with cwd(str(tmp_path)):
result = guess_project_dir(None)
assert result == str(tmp_path)


def test_guess_project_dir_dotconfig() -> None:
"""Verify guess_project_dir()."""
with cwd("examples"):
assert os.path.exists(
".config/ansible-lint.yml"
), "Test requires config file inside .config folder."
result = guess_project_dir(".config/ansible-lint.yml")
assert result == str(os.getcwd())


BASIC_PLAYBOOK = """
- name: "playbook"
tasks:
Expand Down

0 comments on commit e3fdb43

Please sign in to comment.