Skip to content
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
5 changes: 3 additions & 2 deletions src/virtualenv/config/cli/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,8 @@ def _fix_default(self, action: Action) -> None:
if outcome is not None:
break
if outcome is not None:
action.default, action.default_source = outcome
action.default, default_source = outcome
vars(action)["default_source"] = default_source
else:
outcome = action.default, "default"
self.options.set_src(action.dest, *outcome)
Expand All @@ -122,7 +123,7 @@ def enable_help(self) -> None:
self._fix_defaults()
self.add_argument("-h", "--help", action="help", default=SUPPRESS, help="show this help message and exit")

def parse_known_args(
def parse_known_args( # ty: ignore[invalid-method-override]
self, args: Sequence[str] | None = None, namespace: VirtualEnvOptions | None = None
) -> tuple[VirtualEnvOptions, list[str]]:
if namespace is None:
Expand Down
31 changes: 22 additions & 9 deletions tests/unit/activation/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,13 @@ def get_version(self, raise_on_fail):
try:
process = Popen(
self._version_cmd,
stdin=subprocess.DEVNULL,
universal_newlines=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
encoding="utf-8",
)
out, err = process.communicate()
out, err = process.communicate(timeout=30)
except Exception as exception:
self._version = exception
if raise_on_fail:
Expand Down Expand Up @@ -75,12 +76,15 @@ def __call__(self, monkeypatch, tmp_path):
invoke, env = [*self._invoke_script, str(test_script)], self.env(tmp_path)

try:
process = Popen(invoke, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, env=env)
raw_, _ = process.communicate(timeout=120)
except subprocess.TimeoutExpired:
process = Popen(invoke, stdin=subprocess.DEVNULL, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, env=env)
raw_, _ = process.communicate(timeout=60)
except subprocess.TimeoutExpired as exc:
process.kill()
process.communicate()
pytest.fail(f"Activation script timed out: {invoke}")
remaining, _ = process.communicate(timeout=5)
partial = (exc.stdout or b"") + (remaining or b"")
pytest.fail(
f"Activation script timed out:\nPartial output:\n{partial.decode(errors='replace')}\nCommand: {invoke}"
)
except subprocess.CalledProcessError as exception:
output = exception.output + exception.stderr
assert not exception.returncode, output # noqa: PT017
Expand All @@ -103,7 +107,7 @@ def env(self, tmp_path): # noqa: ARG002
env["PYTHONIOENCODING"] = "utf-8"
env["PATH"] = os.pathsep.join([dirname(sys.executable), *env.get("PATH", "").split(os.pathsep)])
# clear up some environment variables so they don't affect the tests
for key in [k for k in env if k.startswith(("_OLD", "VIRTUALENV_"))]:
for key in [k for k in env if k.startswith(("_OLD", "VIRTUALENV_", "COVERAGE_"))]:
del env[key]
return env

Expand All @@ -116,7 +120,7 @@ def _generate_test_script(self, activate_script, tmp_path):
return test_script

def _get_test_lines(self, activate_script):
return [
steps = [
self.print_python_exe(),
self.print_os_env_var("VIRTUAL_ENV"),
self.print_os_env_var("VIRTUAL_ENV_PROMPT"),
Expand All @@ -133,9 +137,17 @@ def _get_test_lines(self, activate_script):
self.print_os_env_var("VIRTUAL_ENV_PROMPT"),
"", # just finish with an empty new line
]
result = []
for index, step in enumerate(steps):
result.extend((self.print_marker(index), step))
return result

def print_marker(self, step) -> str:
return f'echo "__STEP_{step}__"'

def assert_output(self, out, raw, tmp_path) -> None:
"""Compare _get_test_lines() with the expected values."""
out = [line for line in out if not line.strip('"').startswith("__STEP_")]
assert out[0], raw
assert out[1] == "None", raw
assert out[2] == "None", raw
Expand Down Expand Up @@ -213,11 +225,12 @@ def __call__(self, monkeypatch, tmp_path):
env, activate_script = super().__call__(monkeypatch, tmp_path)
process = Popen(
self.non_source_activate(activate_script),
stdin=subprocess.DEVNULL,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
env=env,
)
_out, err_ = process.communicate()
_out, err_ = process.communicate(timeout=60)
err = err_.decode("utf-8")
assert process.returncode
assert self.non_source_fail_message in err
Expand Down
3 changes: 2 additions & 1 deletion tests/unit/activation/test_powershell.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import shutil
import sys
from argparse import Namespace

Expand Down Expand Up @@ -101,7 +102,7 @@ def test_powershell(activation_tester_class, activation_tester, monkeypatch) ->

class PowerShell(activation_tester_class):
def __init__(self, session) -> None:
cmd = "powershell.exe" if sys.platform == "win32" else "pwsh"
cmd = "pwsh" if shutil.which("pwsh") else "powershell.exe" if sys.platform == "win32" else "pwsh"
super().__init__(PowerShellActivator, session, cmd, "activate.ps1", "ps1")
self._version_cmd = [cmd, "-c", "$PSVersionTable"]
self._invoke_script = [cmd, "-NonInteractive", "-NoProfile", "-ExecutionPolicy", "ByPass", "-File"]
Expand Down
Loading