Skip to content

Commit

Permalink
Fix path expansion when outside user home (ansible#2515)
Browse files Browse the repository at this point in the history
  • Loading branch information
ssbarnea committed Oct 4, 2022
1 parent 07e3a51 commit 8301c96
Show file tree
Hide file tree
Showing 4 changed files with 29 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: 701
PYTEST_REQPASS: 702

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 @@ -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."""
Expand All @@ -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 = ""
Expand Down Expand Up @@ -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."""
Expand Down
2 changes: 1 addition & 1 deletion src/ansiblelint/rules/syntax_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 22 additions & 0 deletions test/test_file_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
normpath,
normpath_path,
)
from ansiblelint.rules import RulesCollection
from ansiblelint.runner import Runner

from .conftest import cwd

Expand Down Expand Up @@ -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"

0 comments on commit 8301c96

Please sign in to comment.