Skip to content

Commit

Permalink
Improve path injection logic to resolve symlinked directories (#4488)
Browse files Browse the repository at this point in the history
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
ssbarnea and pre-commit-ci[bot] authored Jan 20, 2025
1 parent 8b45ff5 commit e918e02
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 13 deletions.
6 changes: 3 additions & 3 deletions src/ansiblelint/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -446,16 +446,16 @@ def path_inject(own_location: str = "") -> None:
str(userbase_bin_path) not in paths
and (userbase_bin_path / "bin" / "ansible").exists()
):
inject_paths.append(str(userbase_bin_path))
inject_paths.append(userbase_bin_path.resolve().as_posix())

py_path = Path(sys.executable).parent
py_path = Path(sys.executable).parent.resolve()
pipx_path = os.environ.get("PIPX_HOME", "pipx")
if (
str(py_path) not in paths
and (py_path / "ansible").exists()
and pipx_path not in str(py_path)
):
inject_paths.append(str(py_path))
inject_paths.append(py_path.as_posix())

# last option, if nothing else is found, just look next to ourselves...
if own_location:
Expand Down
23 changes: 13 additions & 10 deletions test/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,27 @@


@pytest.mark.parametrize(
("expected_warning"),
("in_path"),
(False, True),
ids=("normal", "isolated"),
ids=("in", "missing"),
)
def test_call_from_outside_venv(expected_warning: bool) -> None:
def test_call_from_outside_venv(in_path: bool) -> None:
"""Asserts ability to be called w/ or w/o venv activation."""
git_location = shutil.which("git")
if not git_location:
pytest.fail("git not found")
git_path = Path(git_location).parent
py_path = Path(sys.executable).parent.resolve()
py_path = Path(sys.executable).parent.resolve().as_posix()

if expected_warning:
env = os.environ.copy()
env["VIRTUAL_ENV"] = ""
env["NO_COLOR"] = "1"
if in_path:
# VIRTUAL_ENV obliterated here to emulate call from outside a virtual environment
env = {"HOME": str(Path.home()), "PATH": str(git_path), "VIRTUAL_ENV": ""}
env["HOME"] = Path.home().as_posix()
env["PATH"] = git_path.as_posix()
else:
env = os.environ.copy()
if py_path.as_posix() not in env["PATH"]:
if py_path not in env["PATH"]:
env["PATH"] = f"{py_path}:{env['PATH']}"

for v in ("COVERAGE_FILE", "COVERAGE_PROCESS_START"):
Expand All @@ -45,15 +48,15 @@ def test_call_from_outside_venv(expected_warning: bool) -> None:
# environment variables from the current process, so we emulate being
# called from outside the venv.
proc = subprocess.run(
[str(py_path / "ansible-lint"), "--version"],
[f"{py_path}/ansible-lint", "--version"],
check=False,
capture_output=True,
text=True,
env=env,
)
assert proc.returncode == 0, proc
warning_found = "PATH altered to include" in proc.stderr
assert warning_found is expected_warning
assert warning_found is in_path


@pytest.mark.parametrize(
Expand Down

0 comments on commit e918e02

Please sign in to comment.