Skip to content

Commit 71e79cd

Browse files
committed
Add test
1 parent 85721f9 commit 71e79cd

File tree

2 files changed

+49
-2
lines changed

2 files changed

+49
-2
lines changed

src/auditwheel/tools.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,20 @@ def __init__(
183183
self.env = env
184184
if self.env_default:
185185
if type:
186-
self.env_default = type(self.env_default)
186+
try:
187+
self.env_default = type(self.env_default)
188+
except Exception:
189+
self.option_strings = kwargs["option_strings"]
190+
args = {
191+
"value": self.env_default,
192+
"type": type,
193+
"env": self.env,
194+
}
195+
msg = (
196+
"invalid type: %(value)r from environment variable "
197+
"%(env)r cannot be converted to %(type)r"
198+
)
199+
raise argparse.ArgumentError(self, msg % args) from None
187200
default = self.env_default
188201
if default:
189202
required = False

tests/unit/test_tools.py

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import argparse
44
import lzma
55
import zipfile
6+
import zlib
67
from pathlib import Path
78

89
import pytest
@@ -44,7 +45,7 @@ def test_environment_action(
4445
assert expected == args.PLAT
4546

4647

47-
def test_environment_action_invalid_env(monkeypatch: pytest.MonkeyPatch) -> None:
48+
def test_environment_action_invalid_plat_env(monkeypatch: pytest.MonkeyPatch) -> None:
4849
choices = ["linux", "manylinux1", "manylinux2010"]
4950
monkeypatch.setenv("AUDITWHEEL_PLAT", "foo")
5051
p = argparse.ArgumentParser()
@@ -59,6 +60,39 @@ def test_environment_action_invalid_env(monkeypatch: pytest.MonkeyPatch) -> None
5960
)
6061

6162

63+
def test_environment_action_invalid_zip_env(monkeypatch: pytest.MonkeyPatch) -> None:
64+
choices = list(range(zlib.Z_NO_COMPRESSION, zlib.Z_BEST_COMPRESSION + 1))
65+
monkeypatch.setenv("AUDITWHEEL_ZIP_LEVEL", "foo")
66+
p = argparse.ArgumentParser()
67+
with pytest.raises(argparse.ArgumentError):
68+
p.add_argument(
69+
"-z",
70+
"--zip-level",
71+
action=EnvironmentDefault,
72+
metavar="zip",
73+
env="AUDITWHEEL_ZIP_LEVEL",
74+
dest="zip",
75+
type=int,
76+
help="Compress level to be used to create zip file.",
77+
choices=choices,
78+
default=zlib.Z_DEFAULT_COMPRESSION,
79+
)
80+
monkeypatch.setenv("AUDITWHEEL_ZIP_LEVEL", "10")
81+
with pytest.raises(argparse.ArgumentError):
82+
p.add_argument(
83+
"-z",
84+
"--zip-level",
85+
action=EnvironmentDefault,
86+
metavar="zip",
87+
env="AUDITWHEEL_ZIP_LEVEL",
88+
dest="zip",
89+
type=int,
90+
help="Compress level to be used to create zip file.",
91+
choices=choices,
92+
default=zlib.Z_DEFAULT_COMPRESSION,
93+
)
94+
95+
6296
def _write_test_permissions_zip(path: Path) -> None:
6397
source_zip_xz = Path(__file__).parent / "test-permissions.zip.xz"
6498
with lzma.open(source_zip_xz) as f:

0 commit comments

Comments
 (0)