chore: optimise unit tests (#2293)

* optimise unit tests - Cache loading the config TOML

Unit test time: 13.1s -> 10.5s

* optimise unit tests - use the same pyproject validator across the tests

Unit test time: 10.5s -> 5.7s

* optimise unit tests - selector optimisation

Unit test time: 5.7s -> 5.1s

* optimise unit tests - build options computation cache

Unit test time: 5.1s -> 3.2s

* Use functools.cache rather than functools.lru_cache

* Don't overwrite build_options when applying functools.cache
This commit is contained in:
Joe Rickerby
2025-03-10 21:29:07 +01:00
committed by GitHub
parent bddd438a88
commit c136aefe20
4 changed files with 38 additions and 31 deletions
+21 -20
View File
@@ -10,15 +10,22 @@ from cibuildwheel.util import resources
DIR = Path(__file__).parent.resolve()
def test_validate_default_schema():
@pytest.fixture(scope="session")
def validator() -> validate_pyproject.api.Validator:
"""
Reuse the validator for all tests, to keep unit tests fast.
"""
return validate_pyproject.api.Validator()
def test_validate_default_schema(validator: validate_pyproject.api.Validator) -> None:
with resources.DEFAULTS.open("rb") as f:
example = tomllib.load(f)
validator = validate_pyproject.api.Validator()
assert validator(example) is not None
def test_validate_container_engine():
def test_validate_container_engine(validator: validate_pyproject.api.Validator) -> None:
"""
This test checks container engine can be overridden - it used to be a
global option but is now a build option.
@@ -38,12 +45,13 @@ def test_validate_container_engine():
"""
)
validator = validate_pyproject.api.Validator()
assert validator(example) is not None
@pytest.mark.parametrize("platform", ["macos", "windows"])
def test_validate_bad_container_engine(platform: str) -> None:
def test_validate_bad_container_engine(
validator: validate_pyproject.api.Validator, platform: str
) -> None:
"""
container-engine is not a valid option for macos or windows
"""
@@ -54,12 +62,11 @@ def test_validate_bad_container_engine(platform: str) -> None:
"""
)
validator = validate_pyproject.api.Validator()
with pytest.raises(validate_pyproject.error_reporting.ValidationError):
validator(example)
def test_overrides_select():
def test_overrides_select(validator: validate_pyproject.api.Validator) -> None:
example = tomllib.loads(
"""
[[tool.cibuildwheel.overrides]]
@@ -68,11 +75,10 @@ def test_overrides_select():
"""
)
validator = validate_pyproject.api.Validator()
assert validator(example) is not None
def test_overrides_no_select():
def test_overrides_no_select(validator: validate_pyproject.api.Validator) -> None:
example = tomllib.loads(
"""
[[tool.cibuildwheel.overrides]]
@@ -80,12 +86,11 @@ def test_overrides_no_select():
"""
)
validator = validate_pyproject.api.Validator()
with pytest.raises(validate_pyproject.error_reporting.ValidationError):
validator(example)
def test_overrides_only_select():
def test_overrides_only_select(validator: validate_pyproject.api.Validator) -> None:
example = tomllib.loads(
"""
[[tool.cibuildwheel.overrides]]
@@ -93,12 +98,11 @@ def test_overrides_only_select():
"""
)
validator = validate_pyproject.api.Validator()
with pytest.raises(validate_pyproject.error_reporting.ValidationError):
validator(example)
def test_overrides_valid_inherit():
def test_overrides_valid_inherit(validator: validate_pyproject.api.Validator) -> None:
example = tomllib.loads(
"""
[[tool.cibuildwheel.overrides]]
@@ -108,11 +112,10 @@ def test_overrides_valid_inherit():
"""
)
validator = validate_pyproject.api.Validator()
assert validator(example) is not None
def test_overrides_invalid_inherit():
def test_overrides_invalid_inherit(validator: validate_pyproject.api.Validator) -> None:
example = tomllib.loads(
"""
[[tool.cibuildwheel.overrides]]
@@ -122,12 +125,11 @@ def test_overrides_invalid_inherit():
"""
)
validator = validate_pyproject.api.Validator()
with pytest.raises(validate_pyproject.error_reporting.ValidationError):
validator(example)
def test_overrides_invalid_inherit_value():
def test_overrides_invalid_inherit_value(validator: validate_pyproject.api.Validator) -> None:
example = tomllib.loads(
"""
[[tool.cibuildwheel.overrides]]
@@ -137,12 +139,11 @@ def test_overrides_invalid_inherit_value():
"""
)
validator = validate_pyproject.api.Validator()
with pytest.raises(validate_pyproject.error_reporting.ValidationError):
validator(example)
def test_docs_examples():
def test_docs_examples(validator: validate_pyproject.api.Validator) -> None:
"""
Parse out all the configuration examples, build valid TOML out of them, and
make sure they pass.
@@ -182,5 +183,5 @@ def test_docs_examples():
print(example_txt)
print()
example = tomllib.loads(example_txt)
validator = validate_pyproject.api.Validator()
assert validator(example) is not None