Files
cibuildwheel/test/test_testing.py
T

222 lines
7.1 KiB
Python
Raw Normal View History

2024-11-04 15:25:01 -05:00
import inspect
2020-05-02 22:50:52 +01:00
import subprocess
import textwrap
from pathlib import Path
2020-05-02 22:50:52 +01:00
import pytest
2021-01-06 13:50:58 -05:00
from . import test_projects, utils
2020-05-02 16:54:19 +01:00
project_with_a_test = test_projects.new_c_project(
2021-04-30 17:56:34 -04:00
setup_cfg_add=textwrap.dedent(
2021-05-03 11:45:43 -04:00
r"""
[options.extras_require]
test = pytest
2021-05-03 11:45:43 -04:00
"""
2021-04-30 17:56:34 -04:00
)
)
2020-05-02 16:54:19 +01:00
2023-10-27 01:21:20 -04:00
project_with_a_test.files["test/spam_test.py"] = r'''
import os
import platform
import sys
import struct
2020-05-02 16:54:19 +01:00
from unittest import TestCase
2020-05-02 16:54:19 +01:00
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
2020-05-02 16:54:19 +01:00
class TestSpam(TestCase):
def test_filter(self):
self.assertEqual(0, spam.filter("spam"))
self.assertNotEqual(0, spam.filter("ham"))
def test_virtualenv(self):
2020-06-14 00:37:34 +02:00
# 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
2021-02-14 20:44:20 +01:00
if sys.prefix == sys.base_prefix:
2020-06-14 00:37:34 +02:00
self.fail("Not running in a virtualenv")
2020-06-14 00:37:34 +02:00
self.assertTrue(path_contains(sys.prefix, sys.executable))
self.assertTrue(path_contains(sys.prefix, spam.__file__))
2024-05-29 02:20:02 -04:00
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"])
2020-05-02 16:54:19 +01:00
'''
2020-05-03 14:08:57 +01:00
def test(tmp_path):
2021-05-03 11:45:43 -04:00
project_dir = tmp_path / "project"
2020-05-02 16:54:19 +01:00
project_with_a_test.generate(project_dir)
# build and test the wheels
2021-04-30 17:56:34 -04:00
actual_wheels = utils.cibuildwheel_run(
project_dir,
add_env={
"CIBW_TEST_REQUIRES": "pytest",
2021-04-30 17:56:34 -04:00
# the 'false ||' bit is to ensure this command runs in a shell on
# mac/linux.
2024-05-28 05:31:36 -07:00
"CIBW_TEST_COMMAND": f"false || {utils.invoke_pytest()} {{project}}/test",
"CIBW_TEST_COMMAND_WINDOWS": "COLOR 00 || pytest {project}/test",
2021-04-30 17:56:34 -04:00
},
)
2020-05-02 16:54:19 +01:00
# also check that we got the right wheels
2021-05-03 11:45:43 -04:00
expected_wheels = utils.expected_wheels("spam", "0.1.0")
2020-05-02 16:54:19 +01:00
assert set(actual_wheels) == set(expected_wheels)
2020-05-03 14:08:57 +01:00
def test_extras_require(tmp_path):
2021-05-03 11:45:43 -04:00
project_dir = tmp_path / "project"
2020-05-02 16:54:19 +01:00
project_with_a_test.generate(project_dir)
# build and test the wheels
2021-04-30 17:56:34 -04:00
actual_wheels = utils.cibuildwheel_run(
project_dir,
add_env={
2021-05-03 11:45:43 -04:00
"CIBW_TEST_EXTRAS": "test",
2021-04-30 17:56:34 -04:00
# the 'false ||' bit is to ensure this command runs in a shell on
# mac/linux.
2024-05-28 05:31:36 -07:00
"CIBW_TEST_COMMAND": f"false || {utils.invoke_pytest()} {{project}}/test",
2024-11-04 15:25:01 -05:00
"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",
2021-04-30 17:56:34 -04:00
},
2024-05-26 12:41:20 +02:00
single_python=True,
2021-04-30 17:56:34 -04:00
)
2020-05-02 16:54:19 +01:00
# also check that we got the right wheels
2024-05-26 12:41:20 +02:00
expected_wheels = utils.expected_wheels("spam", "0.1.0", single_python=True)
2020-05-02 16:54:19 +01:00
assert set(actual_wheels) == set(expected_wheels)
project_with_a_failing_test = test_projects.new_c_project()
2023-10-27 01:21:20 -04:00
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')
2021-05-03 11:45:43 -04:00
"""
2020-05-02 22:42:35 +01:00
def test_failing_test(tmp_path):
2020-05-02 16:54:19 +01:00
"""Ensure a failing test causes cibuildwheel to error out and exit"""
2021-05-03 11:45:43 -04:00
project_dir = tmp_path / "project"
output_dir = tmp_path / "output"
project_with_a_failing_test.generate(project_dir)
2020-05-02 16:54:19 +01:00
with pytest.raises(subprocess.CalledProcessError):
2021-04-30 17:56:34 -04:00
utils.cibuildwheel_run(
project_dir,
output_dir=output_dir,
add_env={
"CIBW_TEST_REQUIRES": "pytest",
"CIBW_TEST_COMMAND": f"{utils.invoke_pytest()} {{project}}/test",
# 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",
2021-04-30 17:56:34 -04:00
},
)
2020-05-02 22:42:35 +01:00
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)