Skip to content

Commit 8a84867

Browse files
committed
add positive tests
1 parent 71e79cd commit 8a84867

File tree

2 files changed

+56
-6
lines changed

2 files changed

+56
-6
lines changed

src/auditwheel/tools.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -198,9 +198,11 @@ def __init__(
198198
)
199199
raise argparse.ArgumentError(self, msg % args) from None
200200
default = self.env_default
201-
if default:
202-
required = False
203-
if self.env_default and choices is not None and self.env_default not in choices:
201+
if (
202+
self.env_default is not None
203+
and choices is not None
204+
and self.env_default not in choices
205+
):
204206
self.option_strings = kwargs["option_strings"]
205207
args = {
206208
"value": self.env_default,
@@ -213,7 +215,12 @@ def __init__(
213215
)
214216
raise argparse.ArgumentError(self, msg % args)
215217

216-
super().__init__(default=default, required=required, choices=choices, **kwargs)
218+
if default is not None:
219+
required = False
220+
221+
super().__init__(
222+
default=default, required=required, choices=choices, type=type, **kwargs
223+
)
217224

218225
def __call__(
219226
self,

tests/unit/test_tools.py

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
("manylinux2010", "linux", "linux"),
2121
],
2222
)
23-
def test_environment_action(
23+
def test_plat_environment_action(
2424
monkeypatch: pytest.MonkeyPatch,
2525
environ: str | None,
2626
passed: str | None,
@@ -45,6 +45,49 @@ def test_environment_action(
4545
assert expected == args.PLAT
4646

4747

48+
_all_zip_level: list[int] = list(
49+
range(zlib.Z_NO_COMPRESSION, zlib.Z_BEST_COMPRESSION + 1)
50+
)
51+
52+
53+
@pytest.mark.parametrize(
54+
("environ", "passed", "expected"),
55+
[
56+
(None, None, -1),
57+
(0, None, 0),
58+
(0, 1, 1),
59+
(6, 1, 1),
60+
],
61+
)
62+
def test_zip_environment_action(
63+
monkeypatch: pytest.MonkeyPatch,
64+
environ: int | None,
65+
passed: int | None,
66+
expected: int,
67+
) -> None:
68+
choices = _all_zip_level
69+
argv = []
70+
if passed is not None:
71+
argv = ["--zip-level", str(passed)]
72+
if environ is not None:
73+
monkeypatch.setenv("AUDITWHEEL_ZIP_LEVEL", str(environ))
74+
p = argparse.ArgumentParser()
75+
p.add_argument(
76+
"-z",
77+
"--zip-level",
78+
action=EnvironmentDefault,
79+
metavar="zip",
80+
env="AUDITWHEEL_ZIP_LEVEL",
81+
dest="zip",
82+
type=int,
83+
help="Compress level to be used to create zip file.",
84+
choices=choices,
85+
default=zlib.Z_DEFAULT_COMPRESSION,
86+
)
87+
args = p.parse_args(argv)
88+
assert expected == args.zip
89+
90+
4891
def test_environment_action_invalid_plat_env(monkeypatch: pytest.MonkeyPatch) -> None:
4992
choices = ["linux", "manylinux1", "manylinux2010"]
5093
monkeypatch.setenv("AUDITWHEEL_PLAT", "foo")
@@ -61,7 +104,7 @@ def test_environment_action_invalid_plat_env(monkeypatch: pytest.MonkeyPatch) ->
61104

62105

63106
def test_environment_action_invalid_zip_env(monkeypatch: pytest.MonkeyPatch) -> None:
64-
choices = list(range(zlib.Z_NO_COMPRESSION, zlib.Z_BEST_COMPRESSION + 1))
107+
choices = _all_zip_level
65108
monkeypatch.setenv("AUDITWHEEL_ZIP_LEVEL", "foo")
66109
p = argparse.ArgumentParser()
67110
with pytest.raises(argparse.ArgumentError):

0 commit comments

Comments
 (0)