diff --git a/src/ansiblelint/file_utils.py b/src/ansiblelint/file_utils.py index e52ca13b10..4f371868aa 100644 --- a/src/ansiblelint/file_utils.py +++ b/src/ansiblelint/file_utils.py @@ -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: @@ -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] diff --git a/test/TestUtils.py b/test/TestUtils.py index e519b1c7da..5f49dff32a 100644 --- a/test/TestUtils.py +++ b/test/TestUtils.py @@ -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'), @@ -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) diff --git a/test/conftest.py b/test/conftest.py new file mode 100644 index 0000000000..cc6258c237 --- /dev/null +++ b/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)