Debian Codegraph @

tests/test_validate.py

raw
1 import errno
2 import pytest
3 import responses
4
5 from flit import validate as fv
6
7 def test_validate_entrypoints():
8 assert fv.validate_entrypoints(
9 {'console_scripts': {'flit': 'flit:main'}}) == []
10 assert fv.validate_entrypoints(
11 {'some.group': {'flit': 'flit.buildapi'}}) == []
12
13 res = fv.validate_entrypoints({'some.group': {'flit': 'a:b:c'}})
14 assert len(res) == 1
15
16 def test_validate_name():
17 def check(name):
18 return fv.validate_name({'name': name})
19
20 assert check('foo.bar_baz') == []
21 assert check('5minus6') == []
22
23 assert len(check('_foo')) == 1 # Must start with alphanumeric
24 assert len(check('foo.')) == 1 # Must end with alphanumeric
25 assert len(check('Bücher')) == 1 # ASCII only
26
27 def test_validate_requires_python():
28 assert fv.validate_requires_python({}) == [] # Not required
29
30 def check(spec):
31 return fv.validate_requires_python({'requires_python': spec})
32
33 assert check('>=3') == []
34 assert check('>=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*') == []
35
36 assert len(check('3')) == 1
37 assert len(check('@12')) == 1
38 assert len(check('>=2.7; !=3.0.*')) == 1 # Comma separated, not semicolon
39
40 def test_validate_requires_dist():
41 assert fv.validate_requires_dist({}) == [] # Not required
42
43 def check(spec):
44 return fv.validate_requires_dist({'requires_dist': [spec]})
45
46 assert check('requests') == []
47 assert check('requests[extra-foo]') == []
48 assert check('requests (>=2.14)') == [] # parentheses allowed but not recommended
49 assert check('requests >=2.14') == []
50 assert check('pexpect; sys_platform == "win32"') == []
51 # Altogether now
52 assert check('requests[extra-foo] >=2.14; python_version < "3.0"') == []
53
54 # URL specifier
55 assert check('requests @ https://example.com/requests.tar.gz') == []
56 assert check(
57 'requests @ https://example.com/requests.tar.gz ; python_version < "3.8"'
58 ) == []
59
60 # Problems
61 assert len(check('Bücher')) == 1
62 assert len(check('requests 2.14')) == 1
63 assert len(check('pexpect; sys.platform == "win32"')) == 1 # '.' -> '_'
64 assert len(check('requests >=2.14 @ https://example.com/requests.tar.gz')) == 1
65 # Several problems in one requirement
66 assert len(check('pexpect[_foo] =3; sys.platform == "win32"')) == 3
67
68 def test_validate_environment_marker():
69 vem = fv.validate_environment_marker
70
71 assert vem('python_version >= "3" and os_name == \'posix\'') == []
72
73 res = vem('python_version >= "3') # Unclosed string
74 assert len(res) == 1
75 assert res[0].startswith("Invalid string")
76
77 res = vem('python_verson >= "3"') # Misspelled name
78 assert len(res) == 1
79 assert res[0].startswith("Invalid variable")
80
81 res = vem("os_name is 'posix'") # No 'is' comparisons
82 assert len(res) == 1
83 assert res[0].startswith("Invalid expression")
84
85 res = vem("'2' < python_version < '4'") # No chained comparisons
86 assert len(res) == 1
87 assert res[0].startswith("Invalid expression")
88
89 assert len(vem('os.name == "linux\'')) == 2
90
91 def test_validate_url():
92 vurl = fv.validate_url
93 assert vurl("https://github.com/pypa/flit") == []
94
95 assert len(vurl("github.com/pypa/flit")) == 1
96 assert len(vurl("https://")) == 1
97
98
99 def test_validate_project_urls():
100 vpu = fv.validate_project_urls
101
102 def check(prurl):
103 return vpu({'project_urls': [prurl]})
104 assert vpu({}) == [] # Not required
105 assert check('Documentation, https://flit.readthedocs.io/') == []
106
107 # Missing https://
108 assert len(check('Documentation, flit.readthedocs.io')) == 1
109 # Double comma
110 assert len(check('A, B, flit.readthedocs.io')) == 1
111 # No name
112 assert len(check(', https://flit.readthedocs.io/')) == 1
113 # Name longer than 32 chars
114 assert len(check('Supercalifragilisticexpialidocious, https://flit.readthedocs.io/')) == 1
115
116
117 def test_read_classifiers_cached(monkeypatch, tmp_path):
118
119 def mock_get_cache_dir():
120 tmp_file = tmp_path / "classifiers.lst"
121 with tmp_file.open("w") as fh:
122 fh.write("A\nB\nC")
123 return tmp_path
124
125 monkeypatch.setattr(fv, "get_cache_dir", mock_get_cache_dir)
126
127 classifiers = fv._read_classifiers_cached()
128
129 assert classifiers == {'A', 'B', 'C'}
130
131
132 @responses.activate
133 def test_download_and_cache_classifiers(monkeypatch, tmp_path):
134 responses.add(
135 responses.GET,
136 'https://pypi.org/pypi?%3Aaction=list_classifiers',
137 body="A\nB\nC")
138
139 def mock_get_cache_dir():
140 return tmp_path
141
142 monkeypatch.setattr(fv, "get_cache_dir", mock_get_cache_dir)
143
144 classifiers = fv._download_and_cache_classifiers()
145
146 assert classifiers == {"A", "B", "C"}
147
148
149 def test_validate_classifiers_private(monkeypatch):
150 """
151 Test that `Private :: Do Not Upload` considered a valid classifier.
152 This is a special case because it is not listed in a trove classifier
153 but it is a way to make sure that a private package is not get uploaded
154 on PyPI by accident.
155
156 Implementation on PyPI side:
157 https://github.com/pypa/warehouse/pull/5440
158 Issue about officially documenting the trick:
159 https://github.com/pypa/packaging.python.org/issues/643
160 """
161 monkeypatch.setattr(fv, "_read_classifiers_cached", set)
162
163 actual = fv.validate_classifiers({'invalid'})
164 assert actual == ["Unrecognised classifier: 'invalid'"]
165
166 assert fv.validate_classifiers({'Private :: Do Not Upload'}) == []
167
168
169 @responses.activate
170 @pytest.mark.parametrize("error", [PermissionError, OSError(errno.EROFS, "")])
171 def test_download_and_cache_classifiers_with_unacessible_dir(monkeypatch, error):
172 responses.add(
173 responses.GET,
174 'https://pypi.org/pypi?%3Aaction=list_classifiers',
175 body="A\nB\nC")
176
177 class MockCacheDir:
178 def mkdir(self, parents):
179 raise error
180 def __truediv__(self, other):
181 raise error
182
183 monkeypatch.setattr(fv, "get_cache_dir", MockCacheDir)
184
185 classifiers = fv._download_and_cache_classifiers()
186
187 assert classifiers == {"A", "B", "C"}
188
189
190 def test_verify_classifiers_valid_classifiers():
191 classifiers = {"A"}
192 valid_classifiers = {"A", "B"}
193
194 problems = fv._verify_classifiers(classifiers, valid_classifiers)
195
196 assert problems == []
197
198 def test_verify_classifiers_invalid_classifiers():
199 classifiers = {"A", "B"}
200 valid_classifiers = {"A"}
201
202 problems = fv._verify_classifiers(classifiers, valid_classifiers)
203
204 assert problems == ["Unrecognised classifier: 'B'"]
205
206 def test_validate_readme_rst():
207 metadata = {
208 'description_content_type': 'text/x-rst',
209 'description': "Invalid ``rst'",
210 }
211 problems = fv.validate_readme_rst(metadata)
212
213 assert len(problems) == 2 # 1 message that rst is invalid + 1 with details
214 assert "valid rst" in problems[0]
215
216 # Markdown should be ignored
217 metadata = {
218 'description_content_type': 'text/markdown',
219 'description': "Invalid `rst'",
220 }
221 problems = fv.validate_readme_rst(metadata)
222
223 assert problems == []
224
225 RST_WITH_CODE = """
226 Code snippet:
227
228 .. code-block:: python
229
230 a = [i ** 2 for i in range(5)]
231 """
232
233 def test_validate_readme_rst_code():
234 # Syntax highlighting shouldn't require pygments
235 metadata = {
236 'description_content_type': 'text/x-rst',
237 'description': RST_WITH_CODE,
238 }
239 problems = fv.validate_readme_rst(metadata)
240 for p in problems:
241 print(p)
242
243 assert problems == []