Skip to content

Commit cc1d977

Browse files
henryiiimayeut
andauthored
feat: support test-groups (#2063)
* feat: support test-groups Signed-off-by: Henry Schreiner <[email protected]> * refactor: address review comments Signed-off-by: Henry Schreiner <[email protected]> * tests: add a integration test Signed-off-by: Henry Schreiner <[email protected]> * Apply suggestions from code review Co-authored-by: Matthieu Darbois <[email protected]> * fix: better error messages based on feedback Signed-off-by: Henry Schreiner <[email protected]> --------- Signed-off-by: Henry Schreiner <[email protected]> Co-authored-by: Matthieu Darbois <[email protected]>
1 parent d2c7614 commit cc1d977

11 files changed

+200
-24
lines changed

.pre-commit-config.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ repos:
2929
args: ["--python-version=3.8"]
3030
additional_dependencies: &mypy-dependencies
3131
- bracex
32+
- dependency-groups>=1.2
3233
- nox
3334
- orjson
3435
- packaging

bin/generate_schema.py

+3
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,9 @@
164164
test-extras:
165165
description: Install your wheel for testing using `extras_require`
166166
type: string_array
167+
test-groups:
168+
description: Install extra groups when testing
169+
type: string_array
167170
test-requires:
168171
description: Install Python dependencies before running the tests
169172
type: string_array

cibuildwheel/options.py

+19-4
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
from .environment import EnvironmentParseError, ParsedEnvironment, parse_environment
2323
from .logger import log
2424
from .oci_container import OCIContainerEngineConfig
25-
from .projectfiles import get_requires_python_str
25+
from .projectfiles import get_requires_python_str, resolve_dependency_groups
2626
from .typing import PLATFORMS, PlatformName
2727
from .util import (
2828
MANYLINUX_ARCHS,
@@ -92,6 +92,7 @@ class BuildOptions:
9292
before_test: str | None
9393
test_requires: list[str]
9494
test_extras: str
95+
test_groups: list[str]
9596
build_verbosity: int
9697
build_frontend: BuildFrontendConfig | None
9798
config_settings: str
@@ -550,6 +551,8 @@ def get(
550551

551552

552553
class Options:
554+
pyproject_toml: dict[str, Any] | None
555+
553556
def __init__(
554557
self,
555558
platform: PlatformName,
@@ -568,6 +571,13 @@ def __init__(
568571
disallow=DISALLOWED_OPTIONS,
569572
)
570573

574+
self.package_dir = Path(command_line_arguments.package_dir)
575+
try:
576+
with self.package_dir.joinpath("pyproject.toml").open("rb") as f:
577+
self.pyproject_toml = tomllib.load(f)
578+
except FileNotFoundError:
579+
self.pyproject_toml = None
580+
571581
@property
572582
def config_file_path(self) -> Path | None:
573583
args = self.command_line_arguments
@@ -584,8 +594,7 @@ def config_file_path(self) -> Path | None:
584594

585595
@functools.cached_property
586596
def package_requires_python_str(self) -> str | None:
587-
args = self.command_line_arguments
588-
return get_requires_python_str(Path(args.package_dir))
597+
return get_requires_python_str(self.package_dir, self.pyproject_toml)
589598

590599
@property
591600
def globals(self) -> GlobalOptions:
@@ -672,6 +681,11 @@ def build_options(self, identifier: str | None) -> BuildOptions:
672681
"test-requires", option_format=ListFormat(sep=" ")
673682
).split()
674683
test_extras = self.reader.get("test-extras", option_format=ListFormat(sep=","))
684+
test_groups_str = self.reader.get("test-groups", option_format=ListFormat(sep=" "))
685+
test_groups = [x for x in test_groups_str.split() if x]
686+
test_requirements_from_groups = resolve_dependency_groups(
687+
self.pyproject_toml, *test_groups
688+
)
675689
build_verbosity_str = self.reader.get("build-verbosity")
676690

677691
build_frontend_str = self.reader.get(
@@ -771,8 +785,9 @@ def build_options(self, identifier: str | None) -> BuildOptions:
771785
return BuildOptions(
772786
globals=self.globals,
773787
test_command=test_command,
774-
test_requires=test_requires,
788+
test_requires=[*test_requires, *test_requirements_from_groups],
775789
test_extras=test_extras,
790+
test_groups=test_groups,
776791
before_test=before_test,
777792
before_build=before_build,
778793
before_all=before_all,

cibuildwheel/projectfiles.py

+28-7
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
import configparser
55
import contextlib
66
from pathlib import Path
7+
from typing import Any
78

8-
from ._compat import tomllib
9+
import dependency_groups
910

1011

1112
def get_parent(node: ast.AST | None, depth: int = 1) -> ast.AST | None:
@@ -84,15 +85,12 @@ def setup_py_python_requires(content: str) -> str | None:
8485
return None
8586

8687

87-
def get_requires_python_str(package_dir: Path) -> str | None:
88+
def get_requires_python_str(package_dir: Path, pyproject_toml: dict[str, Any] | None) -> str | None:
8889
"""Return the python requires string from the most canonical source available, or None"""
8990

9091
# Read in from pyproject.toml:project.requires-python
91-
with contextlib.suppress(FileNotFoundError):
92-
with (package_dir / "pyproject.toml").open("rb") as f1:
93-
info = tomllib.load(f1)
94-
with contextlib.suppress(KeyError, IndexError, TypeError):
95-
return str(info["project"]["requires-python"])
92+
with contextlib.suppress(KeyError, IndexError, TypeError):
93+
return str((pyproject_toml or {})["project"]["requires-python"])
9694

9795
# Read in from setup.cfg:options.python_requires
9896
config = configparser.ConfigParser()
@@ -106,3 +104,26 @@ def get_requires_python_str(package_dir: Path) -> str | None:
106104
return setup_py_python_requires(f2.read())
107105

108106
return None
107+
108+
109+
def resolve_dependency_groups(
110+
pyproject_toml: dict[str, Any] | None, *groups: str
111+
) -> tuple[str, ...]:
112+
"""
113+
Get the packages in dependency-groups for a package.
114+
"""
115+
116+
if not groups:
117+
return ()
118+
119+
if pyproject_toml is None:
120+
msg = f"Didn't find a pyproject.toml, so can't read [dependency-groups] {groups!r} from it!"
121+
raise FileNotFoundError(msg)
122+
123+
try:
124+
dependency_groups_toml = pyproject_toml["dependency-groups"]
125+
except KeyError:
126+
msg = f"Didn't find [dependency-groups] in pyproject.toml, which is needed to resolve {groups!r}."
127+
raise KeyError(msg) from None
128+
129+
return dependency_groups.resolve(dependency_groups_toml, *groups)

cibuildwheel/resources/cibuildwheel.schema.json

+30
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,21 @@
397397
],
398398
"title": "CIBW_TEST_EXTRAS"
399399
},
400+
"test-groups": {
401+
"description": "Install extra groups when testing",
402+
"oneOf": [
403+
{
404+
"type": "string"
405+
},
406+
{
407+
"type": "array",
408+
"items": {
409+
"type": "string"
410+
}
411+
}
412+
],
413+
"title": "CIBW_TEST_GROUPS"
414+
},
400415
"test-requires": {
401416
"description": "Install Python dependencies before running the tests",
402417
"oneOf": [
@@ -571,6 +586,9 @@
571586
"test-extras": {
572587
"$ref": "#/properties/test-extras"
573588
},
589+
"test-groups": {
590+
"$ref": "#/properties/test-groups"
591+
},
574592
"test-requires": {
575593
"$ref": "#/properties/test-requires"
576594
}
@@ -675,6 +693,9 @@
675693
"test-extras": {
676694
"$ref": "#/properties/test-extras"
677695
},
696+
"test-groups": {
697+
"$ref": "#/properties/test-groups"
698+
},
678699
"test-requires": {
679700
"$ref": "#/properties/test-requires"
680701
}
@@ -720,6 +741,9 @@
720741
"test-extras": {
721742
"$ref": "#/properties/test-extras"
722743
},
744+
"test-groups": {
745+
"$ref": "#/properties/test-groups"
746+
},
723747
"test-requires": {
724748
"$ref": "#/properties/test-requires"
725749
}
@@ -778,6 +802,9 @@
778802
"test-extras": {
779803
"$ref": "#/properties/test-extras"
780804
},
805+
"test-groups": {
806+
"$ref": "#/properties/test-groups"
807+
},
781808
"test-requires": {
782809
"$ref": "#/properties/test-requires"
783810
}
@@ -823,6 +850,9 @@
823850
"test-extras": {
824851
"$ref": "#/properties/test-extras"
825852
},
853+
"test-groups": {
854+
"$ref": "#/properties/test-groups"
855+
},
826856
"test-requires": {
827857
"$ref": "#/properties/test-requires"
828858
}

cibuildwheel/resources/defaults.toml

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ test-command = ""
2020
before-test = ""
2121
test-requires = []
2222
test-extras = []
23+
test-groups = []
2324

2425
container-engine = "docker"
2526

docs/options.md

+34
Original file line numberDiff line numberDiff line change
@@ -1604,6 +1604,40 @@ Platform-specific environment variables are also available:<br/>
16041604

16051605
In configuration files, you can use an inline array, and the items will be joined with a comma.
16061606

1607+
1608+
### `CIBW_TEST_GROUPS` {: #test-groups}
1609+
> Specify test dependencies from your project's `dependency-groups`
1610+
1611+
List of
1612+
[dependency-groups](https://peps.python.org/pep-0735)
1613+
that should be included when installing the wheel prior to running the
1614+
tests. This can be used to avoid having to redefine test dependencies in
1615+
`CIBW_TEST_REQUIRES` if they are already defined in `pyproject.toml`.
1616+
1617+
Platform-specific environment variables are also available:<br/>
1618+
`CIBW_TEST_GROUPS_MACOS` | `CIBW_TEST_GROUPS_WINDOWS` | `CIBW_TEST_GROUPS_LINUX` | `CIBW_TEST_GROUPS_PYODIDE`
1619+
1620+
#### Examples
1621+
1622+
!!! tab examples "Environment variables"
1623+
1624+
```yaml
1625+
# Will cause the wheel to be installed with these groups of dependencies
1626+
CIBW_TEST_GROUPS: "test qt"
1627+
```
1628+
1629+
Separate multiple items with a space.
1630+
1631+
!!! tab examples "pyproject.toml"
1632+
1633+
```toml
1634+
[tool.cibuildwheel]
1635+
# Will cause the wheel to be installed with these groups of dependencies
1636+
test-groups = ["test", "qt"]
1637+
```
1638+
1639+
In configuration files, you can use an inline array, and the items will be joined with a space.
1640+
16071641
### `CIBW_TEST_SKIP` {: #test-skip}
16081642
> Skip running tests on some builds
16091643

pyproject.toml

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ dependencies = [
4343
"bashlex!=0.13",
4444
"bracex",
4545
"certifi",
46+
"dependency-groups>=1.2",
4647
"filelock",
4748
"packaging>=20.9",
4849
"platformdirs",

test/test_testing.py

+33
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import annotations
22

3+
import inspect
34
import os
45
import subprocess
56
import textwrap
@@ -114,6 +115,38 @@ def test_extras_require(tmp_path):
114115
assert set(actual_wheels) == set(expected_wheels)
115116

116117

118+
def test_dependency_groups(tmp_path):
119+
group_project = project_with_a_test.copy()
120+
group_project.files["pyproject.toml"] = inspect.cleandoc("""
121+
[build-system]
122+
requires = ["setuptools"]
123+
build-backend = "setuptools.build_meta"
124+
125+
[dependency-groups]
126+
dev = ["pytest"]
127+
""")
128+
129+
project_dir = tmp_path / "project"
130+
group_project.generate(project_dir)
131+
132+
# build and test the wheels
133+
actual_wheels = utils.cibuildwheel_run(
134+
project_dir,
135+
add_env={
136+
"CIBW_TEST_GROUPS": "dev",
137+
# the 'false ||' bit is to ensure this command runs in a shell on
138+
# mac/linux.
139+
"CIBW_TEST_COMMAND": f"false || {utils.invoke_pytest()} {{project}}/test",
140+
"CIBW_TEST_COMMAND_WINDOWS": "COLOR 00 || pytest {project}/test",
141+
},
142+
single_python=True,
143+
)
144+
145+
# also check that we got the right wheels
146+
expected_wheels = utils.expected_wheels("spam", "0.1.0", single_python=True)
147+
assert set(actual_wheels) == set(expected_wheels)
148+
149+
117150
project_with_a_failing_test = test_projects.new_c_project()
118151
project_with_a_failing_test.files["test/spam_test.py"] = r"""
119152
from unittest import TestCase

unit_test/options_toml_test.py

+8
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
test-command = "pyproject"
2323
test-requires = "something"
2424
test-extras = ["one", "two"]
25+
test-groups = ["three", "four"]
2526
2627
manylinux-x86_64-image = "manylinux1"
2728
@@ -60,6 +61,7 @@ def test_simple_settings(tmp_path, platform, fname):
6061
== 'THING="OTHER" FOO="BAR"'
6162
)
6263
assert options_reader.get("test-extras", option_format=ListFormat(",")) == "one,two"
64+
assert options_reader.get("test-groups", option_format=ListFormat(" ")) == "three four"
6365

6466
assert options_reader.get("manylinux-x86_64-image") == "manylinux1"
6567
assert options_reader.get("manylinux-i686-image") == "manylinux2014"
@@ -85,7 +87,9 @@ def test_envvar_override(tmp_path, platform):
8587
"CIBW_MANYLINUX_X86_64_IMAGE": "manylinux_2_24",
8688
"CIBW_TEST_COMMAND": "mytest",
8789
"CIBW_TEST_REQUIRES": "docs",
90+
"CIBW_TEST_GROUPS": "mgroup two",
8891
"CIBW_TEST_REQUIRES_LINUX": "scod",
92+
"CIBW_TEST_GROUPS_LINUX": "lgroup",
8993
},
9094
)
9195

@@ -99,6 +103,10 @@ def test_envvar_override(tmp_path, platform):
99103
options_reader.get("test-requires", option_format=ListFormat(" "))
100104
== {"windows": "docs", "macos": "docs", "linux": "scod"}[platform]
101105
)
106+
assert (
107+
options_reader.get("test-groups", option_format=ListFormat(" "))
108+
== {"windows": "mgroup two", "macos": "mgroup two", "linux": "lgroup"}[platform]
109+
)
102110
assert options_reader.get("test-command") == "mytest"
103111

104112

0 commit comments

Comments
 (0)