Skip to content

Commit

Permalink
Fix execution outside git repositories (#1675)
Browse files Browse the repository at this point in the history
Fixes #1674
  • Loading branch information
ssbarnea committed Jul 17, 2021
1 parent ec17f46 commit 790aeac
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/ansiblelint/file_utils.py
Expand Up @@ -252,7 +252,7 @@ def discover_lintables(options: Namespace) -> Dict[str, Any]:
return OrderedDict.fromkeys(sorted(out))


def guess_project_dir(config_file: str) -> str:
def guess_project_dir(config_file: Optional[str]) -> str:
"""Return detected project dir or current working directory."""
path = None
if config_file is not None:
Expand All @@ -267,7 +267,7 @@ def guess_project_dir(config_file: str) -> str:
stderr=subprocess.PIPE,
stdout=subprocess.PIPE,
universal_newlines=True,
check=False,
check=True,
)

path = result.stdout.splitlines()[0]
Expand Down
10 changes: 10 additions & 0 deletions test/TestUtils.py
Expand Up @@ -43,10 +43,13 @@
Lintable,
expand_path_vars,
expand_paths_vars,
guess_project_dir,
normpath,
)
from ansiblelint.testing import run_ansible_lint

from .conftest import cwd


@pytest.mark.parametrize(
('string', 'expected_cmd', 'expected_args', 'expected_kwargs'),
Expand Down Expand Up @@ -510,3 +513,10 @@ def test_nested_items() -> None:
("list-item", "orange", "fruits"),
]
assert list(utils.nested_items(data)) == items


def test_guess_project_dir(tmp_path: Path) -> None:
"""Verify guess_project_dir()."""
with cwd(str(tmp_path)):
result = guess_project_dir(None)
assert result == str(tmp_path)
14 changes: 14 additions & 0 deletions test/conftest.py
@@ -0,0 +1,14 @@
import os
from contextlib import contextmanager
from typing import Iterator


@contextmanager
def cwd(path: str) -> Iterator[None]:
"""Context manager for chdir."""
oldpwd = os.getcwd()
os.chdir(path)
try:
yield
finally:
os.chdir(oldpwd)

0 comments on commit 790aeac

Please sign in to comment.