-
Notifications
You must be signed in to change notification settings - Fork 262
/
Copy pathtest_testing.py
225 lines (183 loc) · 7.33 KB
/
test_testing.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
import inspect
import subprocess
import textwrap
from pathlib import Path
import pytest
from . import test_projects, utils
project_with_a_test = test_projects.new_c_project(
setup_cfg_add=textwrap.dedent(
r"""
[options.extras_require]
test = pytest
"""
)
)
project_with_a_test.files["test/spam_test.py"] = r'''
import os
import platform
import sys
import struct
from unittest import TestCase
import spam
def path_contains(parent, child):
""" returns True if `child` is inside `parent`.
Works around path-comparison bugs caused by short-paths on Windows e.g.
vssadm~1 instead of vssadministrator
"""
parent = os.path.abspath(parent)
child = os.path.abspath(child)
while child != os.path.dirname(child):
child = os.path.dirname(child)
if os.stat(parent) == os.stat(child):
# parent and child refer to the same directory on the filesystem
return True
return False
class TestSpam(TestCase):
def test_filter(self):
self.assertEqual(0, spam.filter("spam"))
self.assertNotEqual(0, spam.filter("ham"))
def test_virtualenv(self):
# sys.prefix is different from sys.base_prefix when running a virtualenv
# See https://docs.python.org/3/library/venv.html, which virtualenv seems
# to honor in recent releases
if sys.prefix == sys.base_prefix:
self.fail("Not running in a virtualenv")
self.assertTrue(path_contains(sys.prefix, sys.executable))
self.assertTrue(path_contains(sys.prefix, spam.__file__))
self.assertIn("VIRTUAL_ENV", os.environ)
def test_uname(self):
if platform.system() == "Windows":
return
# if we're running in 32-bit Python, check that the machine is i686.
# See #336 for more info.
bits = struct.calcsize("P") * 8
if bits == 32:
self.assertIn(platform.machine(), ["i686", "armv7l","armv8l", "wasm32"])
'''
def test(tmp_path):
project_dir = tmp_path / "project"
project_with_a_test.generate(project_dir)
# build and test the wheels
actual_wheels = utils.cibuildwheel_run(
project_dir,
add_env={
"CIBW_TEST_REQUIRES": "pytest",
# the 'false ||' bit is to ensure this command runs in a shell on
# mac/linux.
"CIBW_TEST_COMMAND": f"false || {utils.invoke_pytest()} {{project}}/test",
"CIBW_TEST_COMMAND_WINDOWS": "COLOR 00 || pytest {project}/test",
},
)
# also check that we got the right wheels
expected_wheels = utils.expected_wheels("spam", "0.1.0")
assert set(actual_wheels) == set(expected_wheels)
def test_extras_require(tmp_path):
project_dir = tmp_path / "project"
project_with_a_test.generate(project_dir)
# build and test the wheels
actual_wheels = utils.cibuildwheel_run(
project_dir,
add_env={
"CIBW_TEST_EXTRAS": "test",
# the 'false ||' bit is to ensure this command runs in a shell on
# mac/linux.
"CIBW_TEST_COMMAND": f"false || {utils.invoke_pytest()} {{project}}/test",
"CIBW_TEST_COMMAND_WINDOWS": "COLOR 00 || pytest {project}/test",
},
single_python=True,
)
# also check that we got the right wheels
expected_wheels = utils.expected_wheels("spam", "0.1.0", single_python=True)
assert set(actual_wheels) == set(expected_wheels)
def test_dependency_groups(tmp_path):
group_project = project_with_a_test.copy()
group_project.files["pyproject.toml"] = inspect.cleandoc("""
[build-system]
requires = ["setuptools"]
build-backend = "setuptools.build_meta"
[dependency-groups]
dev = ["pytest"]
""")
project_dir = tmp_path / "project"
group_project.generate(project_dir)
# build and test the wheels
actual_wheels = utils.cibuildwheel_run(
project_dir,
add_env={
"CIBW_TEST_GROUPS": "dev",
# the 'false ||' bit is to ensure this command runs in a shell on
# mac/linux.
"CIBW_TEST_COMMAND": f"false || {utils.invoke_pytest()} {{project}}/test",
"CIBW_TEST_COMMAND_WINDOWS": "COLOR 00 || pytest {project}/test",
},
single_python=True,
)
# also check that we got the right wheels
expected_wheels = utils.expected_wheels("spam", "0.1.0", single_python=True)
assert set(actual_wheels) == set(expected_wheels)
project_with_a_failing_test = test_projects.new_c_project()
project_with_a_failing_test.files["test/spam_test.py"] = r"""
from unittest import TestCase
class TestSpam(TestCase):
def test_something(self):
self.fail('this test is supposed to fail')
"""
def test_failing_test(tmp_path):
"""Ensure a failing test causes cibuildwheel to error out and exit"""
project_dir = tmp_path / "project"
output_dir = tmp_path / "output"
project_with_a_failing_test.generate(project_dir)
with pytest.raises(subprocess.CalledProcessError):
utils.cibuildwheel_run(
project_dir,
output_dir=output_dir,
add_env={
"CIBW_TEST_REQUIRES": "pytest",
"CIBW_TEST_COMMAND": f"{utils.invoke_pytest()} {{project}}/test",
# manylinux1 has a version of bash that's been shown to have
# problems with this, so let's check that.
"CIBW_MANYLINUX_I686_IMAGE": "manylinux1",
"CIBW_MANYLINUX_X86_64_IMAGE": "manylinux1",
# CPython 3.8 when running on macOS arm64 is unusual. The build
# always runs in x86_64, so the arm64 tests are not run. See
# #1169 for reasons why. That means the build succeeds, which
# we don't want. So we skip that build.
"CIBW_SKIP": "cp38-macosx_arm64",
},
)
assert len(list(output_dir.iterdir())) == 0
@pytest.mark.parametrize("test_runner", ["pytest", "unittest"])
def test_bare_pytest_invocation(tmp_path: Path, test_runner: str) -> None:
"""Check that if a user runs a bare test suite, it runs in the project folder"""
project_dir = tmp_path / "project"
project_with_a_test.generate(project_dir)
actual_wheels = utils.cibuildwheel_run(
project_dir,
add_env={
"CIBW_TEST_REQUIRES": "pytest" if test_runner == "pytest" else "",
"CIBW_TEST_COMMAND": (
"python -m pytest"
if test_runner == "pytest"
else "python -m unittest discover test spam_test.py"
),
},
)
# check that we got the right wheels
expected_wheels = utils.expected_wheels("spam", "0.1.0")
assert set(actual_wheels) == set(expected_wheels)
def test_test_sources(tmp_path):
project_dir = tmp_path / "project"
project_with_a_test.generate(project_dir)
# build and test the wheels in the test cwd, after copying in the test sources.
actual_wheels = utils.cibuildwheel_run(
project_dir,
add_env={
"CIBW_TEST_REQUIRES": "pytest",
"CIBW_TEST_COMMAND": "pytest",
"CIBW_TEST_COMMAND_WINDOWS": "pytest",
"CIBW_TEST_SOURCES": "test",
},
)
# also check that we got the right wheels
expected_wheels = utils.expected_wheels("spam", "0.1.0")
assert set(actual_wheels) == set(expected_wheels)