1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
|
import re
import sys
from subprocess import PIPE, Popen, check_output
import pytest
from virtualenv.__main__ import run_with_catch
from virtualenv.util.error import ProcessCallFailed
def test_main():
process = Popen([sys.executable, "-m", "virtualenv", "--help"], universal_newlines=True, stdout=PIPE)
out, _ = process.communicate()
assert not process.returncode
assert out
@pytest.fixture()
def raise_on_session_done(mocker):
def _func(exception):
from virtualenv.run import session_via_cli
prev_session = session_via_cli
def _session_via_cli(args, options=None, setup_logging=True, env=None):
prev_session(args, options, setup_logging, env)
raise exception
mocker.patch("virtualenv.run.session_via_cli", side_effect=_session_via_cli)
return _func
def test_fail_no_traceback(raise_on_session_done, tmp_path, capsys):
raise_on_session_done(ProcessCallFailed(code=2, out="out\n", err="err\n", cmd=["something"]))
with pytest.raises(SystemExit) as context:
run_with_catch([str(tmp_path)])
assert context.value.code == 2
out, err = capsys.readouterr()
assert out == f"subprocess call failed for [{'something'!r}] with code 2\nout\nSystemExit: 2\n"
assert err == "err\n"
def test_fail_with_traceback(raise_on_session_done, tmp_path, capsys):
raise_on_session_done(TypeError("something bad"))
with pytest.raises(TypeError, match="something bad"):
run_with_catch([str(tmp_path), "--with-traceback"])
out, err = capsys.readouterr()
assert out == ""
assert err == ""
@pytest.mark.usefixtures("session_app_data")
def test_session_report_full(tmp_path, capsys):
run_with_catch([str(tmp_path)])
out, err = capsys.readouterr()
assert err == ""
lines = out.splitlines()
regexes = [
r"created virtual environment .* in \d+ms",
r" creator .*",
r" seeder .*",
r" added seed packages: .*pip==.*, setuptools==.*, wheel==.*",
r" activators .*",
]
_match_regexes(lines, regexes)
def _match_regexes(lines, regexes):
for line, regex in zip(lines, regexes):
comp_regex = re.compile(rf"^{regex}$")
assert comp_regex.match(line), line
@pytest.mark.usefixtures("session_app_data")
def test_session_report_minimal(tmp_path, capsys):
run_with_catch([str(tmp_path), "--activators", "", "--without-pip"])
out, err = capsys.readouterr()
assert err == ""
lines = out.splitlines()
regexes = [
r"created virtual environment .* in \d+ms",
r" creator .*",
]
_match_regexes(lines, regexes)
@pytest.mark.usefixtures("session_app_data")
def test_session_report_subprocess(tmp_path):
# when called via a subprocess the logging framework should flush and POSIX line normalization happen
out = check_output(
[sys.executable, "-m", "virtualenv", str(tmp_path), "--activators", "powershell", "--without-pip"],
universal_newlines=True,
)
lines = out.split("\n")
regexes = [
r"created virtual environment .* in \d+ms",
r" creator .*",
r" activators .*",
]
_match_regexes(lines, regexes)
|