forked from tox-dev/tox
/
test_provision_int.py
167 lines (149 loc) · 5.1 KB
/
test_provision_int.py
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import signal
import subprocess
import sys
import time
import pytest
if sys.version_info[:2] >= (3, 4):
from pathlib import Path
else:
from pathlib2 import Path
from tox.constants import INFO
from tox.util.main import MAIN_FILE
@pytest.mark.skipif(
"sys.platform == 'win32' and sys.version_info < (3,)",
reason="does not run on windows with py2",
)
def test_provision_missing(initproj, cmd):
initproj(
"pkg123-0.7",
filedefs={
"tox.ini": """\
[tox]
skipsdist=True
minversion = 3.7.0
requires =
setuptools == 40.6.3
[testenv]
commands=python -c "import sys; print(sys.executable); raise SystemExit(1)"
""",
},
)
result = cmd("-e", "py")
result.assert_fail()
assert "tox.exception.InvocationError" not in result.output()
assert not result.err
assert ".tox create: " in result.out
assert ".tox installdeps: " in result.out
assert "py create: " in result.out
at = next(at for at, l in enumerate(result.outlines) if l.startswith("py run-test: ")) + 1
meta_python = Path(result.outlines[at])
assert meta_python.exists()
@pytest.mark.skipif("sys.platform == 'win32'", reason="pyenv does not exists on Windows")
def test_provision_from_pyvenv(initproj, cmd, monkeypatch):
initproj(
"pkg123-0.7",
filedefs={
"tox.ini": """\
[tox]
skipsdist=True
minversion = 3.7.0
requires =
setuptools == 40.6.3
[testenv]
commands=python -c "import sys; print(sys.executable); raise SystemExit(1)"
""",
},
)
monkeypatch.setenv(str("__PYVENV_LAUNCHER__"), sys.executable)
result = cmd("-e", "py", "-vv")
result.assert_fail()
assert ".tox/.tox/bin/python -m virtualenv" in result.out
@pytest.mark.skipif(INFO.IS_PYPY, reason="TODO: process numbers work differently on pypy")
@pytest.mark.skipif(
"sys.platform == 'win32'",
reason="triggering SIGINT reliably on Windows is hard",
)
@pytest.mark.parametrize("signal_type", [signal.SIGINT, signal.SIGTERM])
def test_provision_interrupt_child(initproj, monkeypatch, capfd, signal_type):
monkeypatch.delenv(str("PYTHONPATH"), raising=False)
monkeypatch.setenv(str("TOX_REPORTER_TIMESTAMP"), str("1"))
initproj(
"pkg123-0.7",
filedefs={
"tox.ini": """
[tox]
skipsdist=True
minversion = 3.7.0
requires = setuptools == 40.6.3
tox == 3.7.0
[testenv:b]
commands=python -c "import time; open('a', 'w').write('content'); \
time.sleep(10)"
basepython = python
""",
},
)
cmd = [sys.executable, MAIN_FILE, "-v", "-v", "-e", "b"]
process = subprocess.Popen(
cmd,
creationflags=(
subprocess.CREATE_NEW_PROCESS_GROUP
if sys.platform == "win32"
else 0
# needed for Windows signal send ability (CTRL+C)
),
)
try:
import psutil
current_process = psutil.Process(process.pid)
except ImportError:
current_process = None
signal_file = Path() / "a"
while not signal_file.exists() and process.poll() is None:
time.sleep(0.1)
if process.poll() is not None:
out, err = process.communicate()
assert False, out
all_process = []
if current_process is not None:
all_process.append(current_process)
all_process.extend(current_process.children(recursive=False))
# 1 process for the host tox, 1 for the provisioned
assert len(all_process) >= 2, all_process
process.send_signal(signal.CTRL_C_EVENT if sys.platform == "win32" else signal_type)
process.communicate()
out, err = capfd.readouterr()
assert ".tox KeyboardInterrupt: from" in out, out
for process in all_process:
assert not process.is_running(), "{}{}".format(
out,
"\n".join(repr(i) for i in all_process),
)
@pytest.mark.skipif("sys.platform == 'win32'", reason="pyenv does not exists on Windows")
def test_provision_race(initproj, cmd, monkeypatch):
initproj(
"pkg123-0.7",
filedefs={
"tox.ini": """\
[tox]
skipsdist=True
minversion = 3.7.0
requires =
setuptools == 40.6.3
[testenv]
commands=python -c "import sys; print(sys.executable); raise SystemExit(1)"
[testenv:x2]
""",
},
)
procs = [
subprocess.Popen(
[sys.executable, "-m", "tox", "-e", "py", "-vv"],
stdout=subprocess.PIPE,
)
for _ in range(2)
]
for proc in procs:
stdout, stderr = proc.communicate()
assert proc.returncode != 0
assert b".tox/.tox/bin/python -m virtualenv" in stdout