Debian Codegraph @

tests/unit/test_main.py

raw
1 from __future__ import annotations
2
3 import subprocess
4 import sys
5 from importlib import metadata
6
7 import pytest
8
9 from auditwheel.architecture import Architecture
10 from auditwheel.libc import Libc, LibcVersion
11 from auditwheel.main import main
12
13
14 def test_help(monkeypatch, capsys):
15 # GIVEN
16 monkeypatch.setattr(sys, "argv", ["auditwheel"])
17 # handle running tests using 'python -m pytest' rather than just 'pytest' on Python 3.14+
18 monkeypatch.delattr(sys.modules.get("__main__"), "__spec__", raising=False)
19
20 # WHEN
21 retval = main()
22
23 # THEN
24 assert retval is None
25 captured = capsys.readouterr()
26 assert "usage: auditwheel [-h] [-V] [-v] command ..." in captured.out
27
28
29 @pytest.mark.parametrize("function", ["show", "repair"])
30 def test_unexisting_wheel(monkeypatch, capsys, tmp_path, function):
31 monkeypatch.setattr(sys, "platform", "linux")
32 monkeypatch.setattr(Architecture, "detect", lambda: Architecture.x86_64)
33 wheel = str(tmp_path / "not-a-file.whl")
34 monkeypatch.setattr(sys, "argv", ["auditwheel", function, wheel])
35
36 with pytest.raises(SystemExit):
37 main()
38
39 captured = capsys.readouterr()
40 assert "No such file" in captured.err
41
42
43 @pytest.mark.parametrize(
44 ("libc", "filename", "plat", "message"),
45 [
46 (
47 Libc.GLIBC,
48 "foo-1.0-py3-none-manylinux1_aarch64.whl",
49 "manylinux_2_28_x86_64",
50 "can't repair wheel foo-1.0-py3-none-manylinux1_aarch64.whl with aarch64 architecture to a wheel targeting x86_64", # noqa: E501
51 ),
52 (
53 Libc.GLIBC,
54 "foo-1.0-py3-none-musllinux_1_1_x86_64.whl",
55 "manylinux_2_28_x86_64",
56 "can't repair wheel foo-1.0-py3-none-musllinux_1_1_x86_64.whl with MUSL libc to a wheel targeting GLIBC", # noqa: E501
57 ),
58 (
59 Libc.MUSL,
60 "foo-1.0-py3-none-manylinux1_x86_64.whl",
61 "musllinux_1_1_x86_64",
62 "can't repair wheel foo-1.0-py3-none-manylinux1_x86_64.whl with GLIBC libc to a wheel targeting MUSL", # noqa: E501
63 ),
64 ],
65 )
66 def test_repair_wheel_mismatch(
67 monkeypatch,
68 capsys,
69 tmp_path,
70 libc,
71 filename,
72 plat,
73 message,
74 ):
75 monkeypatch.setattr(sys, "platform", "linux")
76 monkeypatch.setattr(Architecture, "detect", lambda: Architecture.x86_64)
77 monkeypatch.setattr(Libc, "detect", lambda: libc)
78 monkeypatch.setattr(Libc, "get_current_version", lambda _: LibcVersion(1, 1))
79 wheel = tmp_path / filename
80 wheel.write_text("")
81 monkeypatch.setattr(
82 sys,
83 "argv",
84 ["auditwheel", "repair", "--plat", plat, str(wheel)],
85 )
86
87 with pytest.raises(SystemExit):
88 main()
89
90 captured = capsys.readouterr()
91 assert message in captured.err
92
93
94 def test_main_module() -> None:
95 version = metadata.version("auditwheel")
96 result = subprocess.run(
97 [sys.executable, "-m", "auditwheel", "-V"],
98 check=True,
99 capture_output=True,
100 text=True,
101 )
102 assert result.stdout.startswith(f"auditwheel {version}")