Skip to content

Commit efaed95

Browse files
authored
Prevent execution with broken PATH variable (#281)
1 parent 1a346e3 commit efaed95

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

src/ansible_dev_environment/cli.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ def init_output(self) -> None:
5959

6060
def args_sanity(self) -> None:
6161
"""Perform some sanity checking on the args."""
62+
# Ensure PATH is not broken (~ should not present as many tools do not expand it)
63+
if "~" in os.environ.get("PATH", ""):
64+
err = "~ character was found inside PATH, correct your environment configuration to avoid it. See https://stackoverflow.com/a/44704799/99834"
65+
self.output.critical(err)
6266
# Missing args
6367
if (
6468
hasattr(self.args, "requirement")

tests/unit/test_installer.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import yaml
1616

1717
from ansible_dev_environment.arg_parser import parse
18-
from ansible_dev_environment.cli import Cli
18+
from ansible_dev_environment.cli import Cli, main
1919
from ansible_dev_environment.config import Config
2020
from ansible_dev_environment.subcommands.installer import Installer
2121
from ansible_dev_environment.utils import subprocess_run
@@ -737,6 +737,25 @@ def test_collection_pre_install(
737737
assert sorted({c.name for c in subdirs}) == ["posix", "utils"]
738738

739739

740+
def test_args_sanity(monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]) -> None:
741+
"""Test the args_sanity method.
742+
743+
Args:
744+
monkeypatch: The monkeypatch fixture.
745+
capsys: The capsys fixture.
746+
"""
747+
# Adds invalid entry in PATH to detect that we can detect it and exit
748+
monkeypatch.setenv("PATH", "~/bin", prepend=os.pathsep)
749+
monkeypatch.setattr("sys.argv", ["ade", "check"])
750+
751+
with pytest.raises(SystemExit) as exc:
752+
main(dry=True)
753+
assert exc.value.code == 1
754+
755+
captured = capsys.readouterr()
756+
assert "~ character was found inside PATH" in captured.err
757+
758+
740759
@pytest.mark.parametrize("first", (True, False), ids=["editable", "not_editable"])
741760
@pytest.mark.parametrize("second", (True, False), ids=["editable", "not_editable"])
742761
def test_reinstall_local_collection( # pylint: disable=too-many-positional-arguments

0 commit comments

Comments
 (0)