Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent execution with broken PATH variable #281

Merged
merged 1 commit into from
Dec 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/ansible_dev_environment/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ def init_output(self) -> None:

def args_sanity(self) -> None:
"""Perform some sanity checking on the args."""
# Ensure PATH is not broken (~ should not present as many tools do not expand it)
if "~" in os.environ.get("PATH", ""):
err = "~ character was found inside PATH, correct your environment configuration to avoid it. See https://stackoverflow.com/a/44704799/99834"
self.output.critical(err)
# Missing args
if (
hasattr(self.args, "requirement")
Expand Down
21 changes: 20 additions & 1 deletion tests/unit/test_installer.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import yaml

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


def test_args_sanity(monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]) -> None:
"""Test the args_sanity method.

Args:
monkeypatch: The monkeypatch fixture.
capsys: The capsys fixture.
"""
# Adds invalid entry in PATH to detect that we can detect it and exit
monkeypatch.setenv("PATH", "~/bin", prepend=os.pathsep)
monkeypatch.setattr("sys.argv", ["ade", "check"])

with pytest.raises(SystemExit) as exc:
main(dry=True)
assert exc.value.code == 1

captured = capsys.readouterr()
assert "~ character was found inside PATH" in captured.err


@pytest.mark.parametrize("first", (True, False), ids=["editable", "not_editable"])
@pytest.mark.parametrize("second", (True, False), ids=["editable", "not_editable"])
def test_reinstall_local_collection( # pylint: disable=too-many-positional-arguments
Expand Down
Loading