From 8301c96925ab0780b96b4dd41c4ba8667b021881 Mon Sep 17 00:00:00 2001 From: Sorin Sbarnea Date: Thu, 29 Sep 2022 16:44:12 +0100 Subject: [PATCH] Fix path expansion when outside user home (#2515) Fixes: #2513 --- .github/workflows/tox.yml | 2 +- src/ansiblelint/file_utils.py | 8 +++++--- src/ansiblelint/rules/syntax_check.py | 2 +- test/test_file_utils.py | 22 ++++++++++++++++++++++ 4 files changed, 29 insertions(+), 5 deletions(-) diff --git a/.github/workflows/tox.yml b/.github/workflows/tox.yml index a959313085..8b1e2c2499 100644 --- a/.github/workflows/tox.yml +++ b/.github/workflows/tox.yml @@ -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: 701 + PYTEST_REQPASS: 702 steps: - name: Activate WSL1 diff --git a/src/ansiblelint/file_utils.py b/src/ansiblelint/file_utils.py index 39b288fa42..96fd931589 100644 --- a/src/ansiblelint/file_utils.py +++ b/src/ansiblelint/file_utils.py @@ -237,7 +237,7 @@ def __init__( # determine base file kind (yaml, xml, ini, ...) self.base_kind = kind_from_path(self.path, base=True) - self.abspath = self.path.absolute() + self.abspath = self.path.expanduser().absolute() def __getitem__(self, key: Any) -> Any: """Provide compatibility subscriptable support.""" @@ -257,7 +257,7 @@ def get(self, key: Any, default: Any = None) -> Any: def _populate_content_cache_from_disk(self) -> None: # Can raise UnicodeDecodeError try: - self._content = self.path.resolve().read_text(encoding="utf-8") + self._content = self.path.expanduser().resolve().read_text(encoding="utf-8") except FileNotFoundError as ex: if vars(options).get("progressive"): self._content = "" @@ -319,7 +319,9 @@ def write(self, force: bool = False) -> None: if not force and not self.updated: # No changes to write. return - self.path.resolve().write_text(self._content or "", encoding="utf-8") + self.path.expanduser().resolve().write_text( + self._content or "", encoding="utf-8" + ) def __hash__(self) -> int: """Return a hash value of the lintables.""" diff --git a/src/ansiblelint/rules/syntax_check.py b/src/ansiblelint/rules/syntax_check.py index af5670a3b4..1f8649da08 100644 --- a/src/ansiblelint/rules/syntax_check.py +++ b/src/ansiblelint/rules/syntax_check.py @@ -54,7 +54,7 @@ def _get_ansible_syntax_check_matches(lintable: Lintable) -> list[MatchError]: "ansible-playbook", "--syntax-check", *args, - str(lintable.path), + str(lintable.path.expanduser()), ] # To reduce noisy warnings like diff --git a/test/test_file_utils.py b/test/test_file_utils.py index 3e66958e15..4886033e65 100644 --- a/test/test_file_utils.py +++ b/test/test_file_utils.py @@ -23,6 +23,8 @@ normpath, normpath_path, ) +from ansiblelint.rules import RulesCollection +from ansiblelint.runner import Runner from .conftest import cwd @@ -428,3 +430,23 @@ def test_lintable_content_deleter( def test_normpath_path(path: str, result: str) -> None: """Tests behavior of normpath.""" assert normpath_path(path) == Path(result) + + +def test_bug_2513( + tmp_path: Path, + default_rules_collection: RulesCollection, +) -> None: + """Regression test for bug 2513. + + Test that when CWD is outside ~, and argument is like ~/playbook.yml + we will still be able to process the files. + See: https://github.com/ansible/ansible-lint/issues/2513 + """ + filename = "~/.cache/ansible-lint/playbook.yml" + os.makedirs(os.path.dirname(os.path.expanduser(filename)), exist_ok=True) + lintable = Lintable(filename, content="---\n- hosts: all\n") + lintable.write(force=True) + with cwd(str(tmp_path)): + results = Runner(filename, rules=default_rules_collection).run() + assert len(results) == 1 + assert results[0].rule.id == "name"