Skip to content
Closed
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
22 changes: 15 additions & 7 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,27 @@


@pytest.fixture
def py(monkeypatch):
def py(monkeypatch, tmp_path):
"""Provide a convenience function for calling the Python launcher.

The function has a 'path' attribute for a pathlib.Path object pointing
at where the Python launcher is located.

The function has a 'python_executable' attribute for a pathlib.Path
object pointing to a temporary symlink to the virtual environments'
python executable.

The critical environment variables which can influence the execution of
the Python launcher are set to a known good state. This includes setting
PATH to a single directory of where the Python interpreter executing this
file is located.
PATH to a single directory in which we create a symlink named with major
and minor versions in the name.
"""
python_executable = pathlib.Path(sys.executable)
symlink_name = f"python{sys.version_info.major}.{sys.version_info.minor}"
python_executable = tmp_path / symlink_name
os.symlink(sys.executable, python_executable)

monkeypatch.delenv("PYLAUNCH_DEBUG", raising=False)
monkeypatch.setenv("PATH", os.fspath(python_executable.parent))
monkeypatch.setenv("PATH", os.fspath(tmp_path))
monkeypatch.delenv("VIRTUAL_ENV", raising=False)
py_path = pathlib.Path(__file__).parent.parent / "target" / "debug" / "py"

Expand All @@ -41,6 +48,7 @@ def call_py(*args, debug=False):
return subprocess.run(call, capture_output=True, text=True, env=env)

call_py.path = py_path
call_py.python_executable = python_executable
yield call_py


Expand All @@ -49,14 +57,14 @@ def test_help(py, flag):
call = py(flag)
assert not call.returncode
assert os.fspath(py.path) in call.stdout
assert sys.executable in call.stdout
assert os.fspath(py.python_executable) in call.stdout
assert not call.stderr


def test_list(py):
call = py("--list")
assert not call.returncode
assert sys.executable in call.stdout
assert os.fspath(py.python_executable) in call.stdout
assert ".".join(map(str, sys.version_info[:2])) in call.stdout
assert not call.stderr

Expand Down