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

Fix execution outside git repositories #1675

Merged
merged 1 commit into from Jul 17, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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)