Skip to content

Commit

Permalink
add positive tests
Browse files Browse the repository at this point in the history
  • Loading branch information
oraluben committed Mar 6, 2025
1 parent 71e79cd commit 8a84867
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 6 deletions.
15 changes: 11 additions & 4 deletions src/auditwheel/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,9 +198,11 @@ def __init__(
)
raise argparse.ArgumentError(self, msg % args) from None
default = self.env_default
if default:
required = False
if self.env_default and choices is not None and self.env_default not in choices:
if (
self.env_default is not None
and choices is not None
and self.env_default not in choices
):
self.option_strings = kwargs["option_strings"]
args = {
"value": self.env_default,
Expand All @@ -213,7 +215,12 @@ def __init__(
)
raise argparse.ArgumentError(self, msg % args)

super().__init__(default=default, required=required, choices=choices, **kwargs)
if default is not None:
required = False

super().__init__(
default=default, required=required, choices=choices, type=type, **kwargs
)

def __call__(
self,
Expand Down
47 changes: 45 additions & 2 deletions tests/unit/test_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
("manylinux2010", "linux", "linux"),
],
)
def test_environment_action(
def test_plat_environment_action(
monkeypatch: pytest.MonkeyPatch,
environ: str | None,
passed: str | None,
Expand All @@ -45,6 +45,49 @@ def test_environment_action(
assert expected == args.PLAT


_all_zip_level: list[int] = list(
range(zlib.Z_NO_COMPRESSION, zlib.Z_BEST_COMPRESSION + 1)
)


@pytest.mark.parametrize(
("environ", "passed", "expected"),
[
(None, None, -1),
(0, None, 0),
(0, 1, 1),
(6, 1, 1),
],
)
def test_zip_environment_action(
monkeypatch: pytest.MonkeyPatch,
environ: int | None,
passed: int | None,
expected: int,
) -> None:
choices = _all_zip_level
argv = []
if passed is not None:
argv = ["--zip-level", str(passed)]
if environ is not None:
monkeypatch.setenv("AUDITWHEEL_ZIP_LEVEL", str(environ))
p = argparse.ArgumentParser()
p.add_argument(
"-z",
"--zip-level",
action=EnvironmentDefault,
metavar="zip",
env="AUDITWHEEL_ZIP_LEVEL",
dest="zip",
type=int,
help="Compress level to be used to create zip file.",
choices=choices,
default=zlib.Z_DEFAULT_COMPRESSION,
)
args = p.parse_args(argv)
assert expected == args.zip


def test_environment_action_invalid_plat_env(monkeypatch: pytest.MonkeyPatch) -> None:
choices = ["linux", "manylinux1", "manylinux2010"]
monkeypatch.setenv("AUDITWHEEL_PLAT", "foo")
Expand All @@ -61,7 +104,7 @@ def test_environment_action_invalid_plat_env(monkeypatch: pytest.MonkeyPatch) ->


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

0 comments on commit 8a84867

Please sign in to comment.