-
Notifications
You must be signed in to change notification settings - Fork 284
Expand file tree
/
Copy pathtest_utils.py
More file actions
155 lines (139 loc) · 4.61 KB
/
test_utils.py
File metadata and controls
155 lines (139 loc) · 4.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# This file is dual licensed under the terms of the Apache License, Version
# 2.0, and the BSD License. See the LICENSE file in the root of this repository
# for complete details.
import pytest
from packaging.tags import Tag
from packaging.utils import (
InvalidSdistFilename,
InvalidWheelFilename,
canonicalize_name,
canonicalize_version,
parse_sdist_filename,
parse_wheel_filename,
)
from packaging.version import Version
@pytest.mark.parametrize(
("name", "expected"),
[
("foo", "foo"),
("Foo", "foo"),
("fOo", "foo"),
("foo.bar", "foo-bar"),
("Foo.Bar", "foo-bar"),
("Foo.....Bar", "foo-bar"),
("foo_bar", "foo-bar"),
("foo___bar", "foo-bar"),
("foo-bar", "foo-bar"),
("foo----bar", "foo-bar"),
],
)
def test_canonicalize_name(name, expected):
assert canonicalize_name(name) == expected
@pytest.mark.parametrize(
("version", "expected"),
[
(Version("1.4.0"), "1.4"),
("1.4.0", "1.4"),
("1.40.0", "1.40"),
("1.4.0.0.00.000.0000", "1.4"),
("1.0", "1"),
("1.0+abc", "1+abc"),
("1.0.dev0", "1.dev0"),
("1.0.post0", "1.post0"),
("1.0a0", "1a0"),
("1.0rc0", "1rc0"),
("100!0.0", "100!0"),
# improper version strings are unchanged
("lolwat", "lolwat"),
("1.0.1-test7", "1.0.1-test7"),
],
)
def test_canonicalize_version(version, expected):
assert canonicalize_version(version) == expected
@pytest.mark.parametrize(("version"), ["1.4.0", "1.0"])
def test_canonicalize_version_no_strip_trailing_zero(version):
assert canonicalize_version(version, strip_trailing_zero=False) == version
@pytest.mark.parametrize(
("filename", "name", "version", "build", "tags"),
[
(
"foo-1.0-py3-none-any.whl",
"foo",
Version("1.0"),
(),
{Tag("py3", "none", "any")},
),
(
"some_PACKAGE-1.0-py3-none-any.whl",
"some-package",
Version("1.0"),
(),
{Tag("py3", "none", "any")},
),
(
"foo-1.0-1000-py3-none-any.whl",
"foo",
Version("1.0"),
(1000, ""),
{Tag("py3", "none", "any")},
),
(
"foo-1.0-1000abc-py3-none-any.whl",
"foo",
Version("1.0"),
(1000, "abc"),
{Tag("py3", "none", "any")},
),
(
"foo-2-py2.py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl",
"foo",
Version("2"),
(),
{
Tag("py2", "none", "manylinux_2_17_x86_64"),
Tag("py2", "none", "manylinux2014_x86_64"),
Tag("py3", "none", "manylinux_2_17_x86_64"),
Tag("py3", "none", "manylinux2014_x86_64"),
},
),
],
)
def test_parse_wheel_filename(filename, name, version, build, tags):
# See https://peps.python.org/pep-0427/#file-name-convention for the spec.
assert parse_wheel_filename(filename) == (name, version, build, tags)
@pytest.mark.parametrize(
("filename"),
[
("foo-1.0.whl"), # Missing tags
("foo-1.0-py3-none-any.wheel"), # Incorrect file extension (`.wheel`)
("foo__bar-1.0-py3-none-any.whl"), # Invalid name (`__`)
("foo#bar-1.0-py3-none-any.whl"), # Invalid name (`#`)
# Build number doesn't start with a digit (`abc`)
("foo-1.0-abc-py3-none-any.whl"),
("foo-1.0-200-py3-none-any-junk.whl"), # Too many dashes (`-junk`)
],
)
def test_parse_wheel_invalid_filename(filename):
# See https://peps.python.org/pep-0427/#file-name-convention for the spec.
with pytest.raises(InvalidWheelFilename):
parse_wheel_filename(filename)
@pytest.mark.parametrize(
("filename", "name", "version"),
[("foo-1.0.tar.gz", "foo", Version("1.0")), ("foo-1.0.zip", "foo", Version("1.0"))],
)
def test_parse_sdist_filename(filename, name, version):
# See https://peps.python.org/pep-0625/#specification for the spec.
assert parse_sdist_filename(filename) == (name, version)
@pytest.mark.parametrize(
("filename"),
[
"foo-1.0.xz", # no .tar.gz ending
"foo1.0.tar.gz", # no dash for package name / version separator
"foo", # missing file extension
"foo-1.9", # missing file extension
],
)
def test_parse_sdist_invalid_filename(filename):
# See https://peps.python.org/pep-0625/#specification for the spec.
with pytest.raises(InvalidSdistFilename):
parse_sdist_filename(filename)