WIP - initial punt at audit command

This commit is contained in:
Joe Rickerby
2026-02-04 09:26:35 +00:00
parent e8646fb903
commit 619e1dc989
7 changed files with 299 additions and 0 deletions
+134
View File
@@ -0,0 +1,134 @@
from pathlib import Path
import pytest
from cibuildwheel.audit import is_abi3_wheel, run_audit
class TestIsAbi3Wheel:
def test_abi3_wheel(self) -> None:
assert is_abi3_wheel(Path("example-1.0.0-cp38-abi3-manylinux_2_17_x86_64.whl"))
def test_abi3_wheel_macos(self) -> None:
assert is_abi3_wheel(Path("example-1.0.0-cp39-abi3-macosx_10_9_x86_64.whl"))
def test_abi3_wheel_windows(self) -> None:
assert is_abi3_wheel(Path("example-1.0.0-cp310-abi3-win_amd64.whl"))
def test_non_abi3_wheel(self) -> None:
assert not is_abi3_wheel(Path("example-1.0.0-cp310-cp310-manylinux_2_17_x86_64.whl"))
def test_pure_python_wheel(self) -> None:
assert not is_abi3_wheel(Path("example-1.0.0-py3-none-any.whl"))
class TestRunAudit:
def test_empty_command_does_nothing(self, tmp_path: Path) -> None:
# Create a wheel file
wheel_path = tmp_path / "example-1.0.0-cp38-abi3-manylinux_2_17_x86_64.whl"
wheel_path.touch()
# Should not raise and should be a no-op
run_audit(
audit_command="",
output_dir=tmp_path,
wheels_before=set(),
)
def test_audit_runs_on_new_wheels(self, tmp_path: Path) -> None:
# Create a wheel file (simulating a build)
wheel_path = tmp_path / "example-1.0.0-cp310-cp310-manylinux_2_17_x86_64.whl"
wheel_path.touch()
# Create a marker file to verify command ran
marker = tmp_path / "audit_ran.txt"
run_audit(
audit_command=f"touch {marker}",
output_dir=tmp_path,
wheels_before=set(),
)
assert marker.exists()
def test_audit_skips_old_wheels(self, tmp_path: Path) -> None:
# Create a wheel file
wheel_path = tmp_path / "example-1.0.0-cp310-cp310-manylinux_2_17_x86_64.whl"
wheel_path.touch()
# Create a marker file to verify command ran
marker = tmp_path / "audit_ran.txt"
# Pre-existing wheel should be skipped
run_audit(
audit_command=f"touch {marker}",
output_dir=tmp_path,
wheels_before={wheel_path.name},
)
assert not marker.exists()
def test_abi3_only_mode_skips_non_abi3(self, tmp_path: Path) -> None:
# Create a non-abi3 wheel
wheel_path = tmp_path / "example-1.0.0-cp310-cp310-manylinux_2_17_x86_64.whl"
wheel_path.touch()
# Create a marker file to verify command ran
marker = tmp_path / "audit_ran.txt"
run_audit(
audit_command=f"echo {{abi3_wheel}} && touch {marker}",
output_dir=tmp_path,
wheels_before=set(),
)
# Should not run because no abi3 wheels
assert not marker.exists()
def test_abi3_only_mode_runs_on_abi3(self, tmp_path: Path) -> None:
# Create an abi3 wheel
wheel_path = tmp_path / "example-1.0.0-cp38-abi3-manylinux_2_17_x86_64.whl"
wheel_path.touch()
# Create a marker file to verify command ran
marker = tmp_path / "audit_ran.txt"
run_audit(
audit_command=f"echo {{abi3_wheel}} && touch {marker}",
output_dir=tmp_path,
wheels_before=set(),
)
assert marker.exists()
def test_wheel_placeholder_expanded(self, tmp_path: Path) -> None:
# Create a wheel file
wheel_path = tmp_path / "example-1.0.0-cp310-cp310-manylinux_2_17_x86_64.whl"
wheel_path.touch()
# Write wheel path to a file to verify expansion
output_file = tmp_path / "wheel_path.txt"
run_audit(
audit_command=f"echo {{wheel}} > {output_file}",
output_dir=tmp_path,
wheels_before=set(),
)
assert output_file.exists()
content = output_file.read_text().strip()
assert content == str(wheel_path)
def test_audit_fails_on_error(self, tmp_path: Path) -> None:
# Create a wheel file
wheel_path = tmp_path / "example-1.0.0-cp310-cp310-manylinux_2_17_x86_64.whl"
wheel_path.touch()
with pytest.raises(SystemExit) as exc_info:
run_audit(
audit_command="exit 1",
output_dir=tmp_path,
wheels_before=set(),
)
assert exc_info.value.code == 1
+43
View File
@@ -541,3 +541,46 @@ before-all = ["override2"]
options_reader.get("config-settings", option_format=ShlexTableFormat())
== "key1=value1 key2=override2 empty='' key3=value3"
)
def test_audit_option(tmp_path, platform):
pyproject_toml: Path = tmp_path / "pyproject.toml"
pyproject_toml.write_text(
"""
[tool.cibuildwheel]
audit = "abi3audit {abi3_wheel}"
"""
)
options_reader = OptionsReader(pyproject_toml, platform=platform, env={})
assert options_reader.get("audit", option_format=ListFormat(" && ")) == "abi3audit {abi3_wheel}"
def test_audit_option_list(tmp_path, platform):
pyproject_toml: Path = tmp_path / "pyproject.toml"
pyproject_toml.write_text(
"""
[tool.cibuildwheel]
audit = ["first command", "second command"]
"""
)
options_reader = OptionsReader(pyproject_toml, platform=platform, env={})
assert (
options_reader.get("audit", option_format=ListFormat(" && "))
== "first command && second command"
)
def test_audit_option_env(tmp_path, platform):
pyproject_toml: Path = tmp_path / "pyproject.toml"
pyproject_toml.write_text(
"""
[tool.cibuildwheel]
"""
)
options_reader = OptionsReader(
pyproject_toml, platform=platform, env={"CIBW_AUDIT": "my-audit-tool {wheel}"}
)
assert options_reader.get("audit", option_format=ListFormat(" && ")) == "my-audit-tool {wheel}"