Skip to content

Commit d78e7ef

Browse files
committed
tests: add tests for CLI
Signed-off-by: Henry Schreiner <henryfs@princeton.edu>
1 parent 7d3694f commit d78e7ef

File tree

3 files changed

+68
-7
lines changed

3 files changed

+68
-7
lines changed

src/packaging/tags.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,7 @@ def __str__(self) -> str:
114114
return f"{self._interpreter}-{self._abi}-{self._platform}"
115115

116116
def __repr__(self) -> str:
117-
return (
118-
f"{self.__class__.__name__}"
119-
f"({self._interpreter!r}, {self._abi!r}, {self._platform!r})"
120-
)
117+
return f"<{self} @ {id(self)}>"
121118

122119
def __setstate__(self, state: tuple[None, dict[str, Any]]) -> None:
123120
# The cached _hash is wrong when unpickling.

src/packaging/version.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -931,9 +931,9 @@ def _cmpkey(
931931
return epoch, _release, _pre, _post, _dev, _local
932932

933933

934-
if __name__ == "__main__":
935-
import argparse
936-
import operator
934+
def main() -> None:
935+
import argparse # noqa: PLC0415
936+
import operator # noqa: PLC0415
937937

938938
operations = {
939939
"lt": operator.lt,
@@ -971,3 +971,7 @@ def _cmpkey(
971971
if args.command == "compare":
972972
result = operations[args.operator](args.version1, args.version2)
973973
raise SystemExit(not result)
974+
975+
976+
if __name__ == "__main__":
977+
main()

tests/test_version_cli.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# This file is dual licensed under the terms of the Apache License, Version
2+
# 2.0, and the BSD License. See the LICENSE file in the root of this repository
3+
# for complete details.
4+
5+
from __future__ import annotations
6+
7+
import sys
8+
9+
import pytest
10+
11+
from packaging.version import main
12+
13+
14+
@pytest.mark.parametrize(
15+
("args", "retcode"),
16+
[
17+
("1.2 eq 1.2", 0),
18+
("1.2 eq 1.2.0", 0),
19+
("1.2 eq 1.2dev1", 1),
20+
("1.2 == 1.2", 0),
21+
("1.2 == 1.2.0", 0),
22+
("1.2 == 1.2dev1", 1),
23+
("1.2 ne 1.2.0", 1),
24+
("1.2 ne 1.2dev1", 0),
25+
("1.2 != 1.2.0", 1),
26+
("1.2 != 1.2dev1", 0),
27+
("1.2 lt 1.2.0", 1),
28+
("1.2 lt 1.2dev1", 1),
29+
("1.2 lt 1.3", 0),
30+
("1.2 < 1.2.0", 1),
31+
("1.2 < 1.2dev1", 1),
32+
("1.2 < 1.3", 0),
33+
("1.2 gt 1.2.0", 1),
34+
("1.2 gt 1.2dev1", 0),
35+
("1.2 gt 1.1", 0),
36+
("1.2 > 1.2.0", 1),
37+
("1.2 > 1.2dev1", 0),
38+
("1.2 > 1.1", 0),
39+
("1.2 le 1.2", 0),
40+
("1.2 le 1.3", 0),
41+
("1.2 le 1.1", 1),
42+
("1.2 <= 1.2", 0),
43+
("1.2 <= 1.3", 0),
44+
("1.2 <= 1.1", 1),
45+
("1.2 ge 1.2", 0),
46+
("1.2 ge 1.1", 0),
47+
("1.2 ge 1.3", 1),
48+
("1.2 >= 1.2", 0),
49+
("1.2 >= 1.1", 0),
50+
("1.2 >= 1.3", 1),
51+
("1.2 foo 1.2", 2),
52+
("1.2 == unreal", 2),
53+
],
54+
)
55+
def test_compare(monkeypatch: pytest.MonkeyPatch, args: str, retcode: int) -> None:
56+
monkeypatch.setattr(sys, "argv", ["prog", "compare", *args.split()])
57+
with pytest.raises(SystemExit) as excinfo:
58+
main()
59+
60+
assert excinfo.value.code == retcode

0 commit comments

Comments
 (0)