File: test_errors.py

package info (click to toggle)
python-hatch-requirements-txt 0.4.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 360 kB
  • sloc: python: 671; makefile: 2
file content (340 lines) | stat: -rw-r--r-- 13,171 bytes parent folder | download
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
# stdlib
from typing import Callable

# 3rd party
import pytest
from domdf_python_tools.compat import importlib_metadata
from domdf_python_tools.paths import PathPlus, in_directory
from hatchling.build import build_sdist, build_wheel
from packaging.requirements import InvalidRequirement
from packaging.version import Version

_hatchling_version = Version(importlib_metadata.version("hatchling"))
hatchling_version = (_hatchling_version.major, _hatchling_version.minor)

pyproject_toml = """
[project]
name = "demo"
version = "0.0.1"
authors = [
  { name="Example Author", email="author@example.com" },
]
description = "A small example package"
readme = "README.md"
requires-python = ">=3.6"
classifiers = [
    "Programming Language :: Python :: 3",
    "License :: OSI Approved :: MIT License",
    "Operating System :: OS Independent",
]
dynamic = ["dependencies"]

[project.urls]
Homepage = "https://github.com/pypa/sampleproject"
"Bug Tracker" = "https://github.com/pypa/sampleproject/issues"

[tool.hatch.metadata.hooks.requirements_txt]
files = ["requirements.txt"]
"""


@pytest.mark.parametrize("build_func", [build_wheel, build_sdist])
def test_missing_requirements_txt(tmp_pathplus: PathPlus, build_func: Callable):

	dist_dir = tmp_pathplus / "dist"
	dist_dir.maybe_make()

	(tmp_pathplus / "pyproject.toml").write_clean(pyproject_toml)
	(tmp_pathplus / "README.md").touch()
	(tmp_pathplus / "LICENSE").touch()
	(tmp_pathplus / "demo").maybe_make()
	(tmp_pathplus / "demo" / "__init__.py").touch()

	with in_directory(tmp_pathplus), pytest.raises(FileNotFoundError, match=r"^requirements\.txt$"):
		wheel_file = build_func(dist_dir)


@pytest.mark.parametrize("build_func", [build_wheel, build_sdist])
def test_missing_invalid_requirements(tmp_pathplus: PathPlus, build_func: Callable):

	dist_dir = tmp_pathplus / "dist"
	dist_dir.maybe_make()

	(tmp_pathplus / "pyproject.toml").write_clean(pyproject_toml)
	(tmp_pathplus / "requirements.txt").write_lines(["Fo???o", "bar", "baz>1"])
	(tmp_pathplus / "README.md").touch()
	(tmp_pathplus / "LICENSE").touch()
	(tmp_pathplus / "demo").maybe_make()
	(tmp_pathplus / "demo" / "__init__.py").touch()

	with in_directory(tmp_pathplus), pytest.raises(InvalidRequirement):
		wheel_file = build_func(dist_dir)


@pytest.mark.xfail(hatchling_version >= (1, 22), reason="Metadata hooks no longer called if dynamic not set")
@pytest.mark.parametrize("build_func", [build_wheel, build_sdist])
def test_not_dynamic_but_files_defined(tmp_pathplus: PathPlus, build_func: Callable):

	dist_dir = tmp_pathplus / "dist"
	dist_dir.maybe_make()

	(tmp_pathplus / "pyproject.toml").write_clean(pyproject_toml.replace('dynamic = ["dependencies"]', ''))
	(tmp_pathplus / "requirements.txt").write_lines(["Foo", "bar", "# fizz", "baz>1"])
	(tmp_pathplus / "README.md").touch()
	(tmp_pathplus / "LICENSE").touch()
	(tmp_pathplus / "demo").maybe_make()
	(tmp_pathplus / "demo" / "__init__.py").touch()

	with in_directory(tmp_pathplus), pytest.raises(ValueError, match=(
		r"^Cannot specify 'files' in \[tool.hatch.metadata.hooks.requirements_txt\] "
		r"when 'dependencies' is not listed in 'project.dynamic'.$"
	)):
		wheel_file = build_func(dist_dir)


@pytest.mark.parametrize("build_func", [build_wheel, build_sdist])
def test_not_in_dynamic_but_files_defined(tmp_pathplus: PathPlus, build_func: Callable):

	dist_dir = tmp_pathplus / "dist"
	dist_dir.maybe_make()

	(tmp_pathplus / "pyproject.toml").write_clean(
			pyproject_toml.replace('dynamic = ["dependencies"]', 'dynamic = ["classifiers"]')
			)
	(tmp_pathplus / "requirements.txt").write_lines(["Foo", "bar", "# fizz", "baz>1"])
	(tmp_pathplus / "README.md").touch()
	(tmp_pathplus / "LICENSE").touch()
	(tmp_pathplus / "demo").maybe_make()
	(tmp_pathplus / "demo" / "__init__.py").touch()

	with in_directory(tmp_pathplus), pytest.raises(ValueError, match=(
		r"^Cannot specify 'files' in \[tool.hatch.metadata.hooks.requirements_txt\] "
		r"when 'dependencies' is not listed in 'project.dynamic'.$"
	)):
		wheel_file = build_func(dist_dir)


@pytest.mark.xfail(hatchling_version >= (1, 22), reason="Metadata hooks no longer called if dynamic not set")
@pytest.mark.parametrize("build_func", [build_wheel, build_sdist])
def test_not_dynamic_but_filename_defined(tmp_pathplus: PathPlus, build_func: Callable):

	dist_dir = tmp_pathplus / "dist"
	dist_dir.maybe_make()

	new_pyproject_toml = pyproject_toml.replace('dynamic = ["dependencies"]', '').replace(
			'files = ["requirements.txt"]', 'filename = "requirements.txt"'
			)
	(tmp_pathplus / "pyproject.toml").write_clean(new_pyproject_toml)
	(tmp_pathplus / "requirements.txt").write_lines(["Foo", "bar", "# fizz", "baz>1"])
	(tmp_pathplus / "README.md").touch()
	(tmp_pathplus / "LICENSE").touch()
	(tmp_pathplus / "demo").maybe_make()
	(tmp_pathplus / "demo" / "__init__.py").touch()

	with in_directory(tmp_pathplus), pytest.raises(ValueError, match=(
		r"^Cannot specify 'filename' in \[tool.hatch.metadata.hooks.requirements_txt\] "
		r"when 'dependencies' is not listed in 'project.dynamic'.$"
	)):
		build_func(dist_dir)


@pytest.mark.xfail(hatchling_version >= (1, 22), reason="Metadata hooks no longer called if dynamic not set")
@pytest.mark.parametrize("build_func", [build_wheel, build_sdist])
def test_not_in_dynamic_but_filename_defined(tmp_pathplus: PathPlus, build_func: Callable):

	dist_dir = tmp_pathplus / "dist"
	dist_dir.maybe_make()

	new_pyproject_toml = pyproject_toml.replace('dynamic = ["dependencies"]', 'dynamic = ["classifiers"]').replace(
			'files = ["requirements.txt"]', 'filename = "requirements.txt"'
			)
	(tmp_pathplus / "pyproject.toml").write_clean(new_pyproject_toml)
	(tmp_pathplus / "requirements.txt").write_lines(["Foo", "bar", "# fizz", "baz>1"])
	(tmp_pathplus / "README.md").touch()
	(tmp_pathplus / "LICENSE").touch()
	(tmp_pathplus / "demo").maybe_make()
	(tmp_pathplus / "demo" / "__init__.py").touch()

	with in_directory(tmp_pathplus), pytest.raises(ValueError, match=(
		r"^Cannot specify 'filename' in \[tool.hatch.metadata.hooks.requirements_txt\] "
		r"when 'dependencies' is not listed in 'project.dynamic'.$"
	)):
		build_func(dist_dir)


@pytest.mark.parametrize("build_func", [build_wheel, build_sdist])
def test_optional_not_dynamic(tmp_pathplus: PathPlus, build_func: Callable):

	dist_dir = tmp_pathplus / "dist"
	dist_dir.maybe_make()

	optional_dependencies_section = """
[tool.hatch.metadata.hooks.requirements_txt.optional-dependencies]
dev = ["requirements-dev.txt"]
	"""
	(tmp_pathplus / "pyproject.toml").write_clean(pyproject_toml + optional_dependencies_section)
	(tmp_pathplus / "requirements.txt").write_lines(["Foo", "bar", "# fizz", "baz>1"])
	(tmp_pathplus / "README.md").touch()
	(tmp_pathplus / "LICENSE").touch()
	(tmp_pathplus / "demo").maybe_make()
	(tmp_pathplus / "demo" / "__init__.py").touch()

	with in_directory(tmp_pathplus), pytest.raises(ValueError, match=(
		r"^Cannot specify 'optional-dependencies' in \[tool.hatch.metadata.hooks.requirements_txt\] "
		r"when 'optional-dependencies' is not listed in 'project.dynamic'.$"
	)):
		build_func(dist_dir)


@pytest.mark.parametrize("build_func", [build_wheel, build_sdist])
def test_already_given(tmp_pathplus: PathPlus, build_func: Callable):

	dist_dir = tmp_pathplus / "dist"
	dist_dir.maybe_make()

	(tmp_pathplus / "pyproject.toml").write_clean(
			pyproject_toml.replace('dynamic = ["dependencies"]', 'dynamic = ["dependencies"]\ndependencies = []')
			)
	(tmp_pathplus / "requirements.txt").write_lines(["Foo", "# fizz", "bar", "baz>1"])
	(tmp_pathplus / "README.md").touch()
	(tmp_pathplus / "LICENSE").touch()
	(tmp_pathplus / "demo").maybe_make()
	(tmp_pathplus / "demo" / "__init__.py").touch()

	with in_directory(tmp_pathplus), pytest.raises(ValueError, match=r"^'dependencies' is dynamic but already listed in \[project\].$"):
		build_func(dist_dir)


@pytest.mark.parametrize("build_func", [build_wheel, build_sdist])
def test_optional_already_given(tmp_pathplus: PathPlus, build_func: Callable):

	dist_dir = tmp_pathplus / "dist"
	dist_dir.maybe_make()

	optional_dependencies_sections = """
[tool.hatch.metadata.hooks.requirements_txt.optional-dependencies]
dev = ["requirements-dev.txt"]

[project.optional-dependencies]
test = ["pytest"]
	"""
	(tmp_pathplus / "pyproject.toml").write_clean(
			pyproject_toml.
			replace('dynamic = ["dependencies"]', 'dynamic = ["dependencies", "optional-dependencies"]')
			+ optional_dependencies_sections
			)
	(tmp_pathplus / "requirements.txt").write_lines(["Foo", "# fizz", "bar", "baz>1"])
	(tmp_pathplus / "requirements-dev.txt").write_lines(["pre-commit"])
	(tmp_pathplus / "README.md").touch()
	(tmp_pathplus / "LICENSE").touch()
	(tmp_pathplus / "demo").maybe_make()
	(tmp_pathplus / "demo" / "__init__.py").touch()

	with in_directory(tmp_pathplus), pytest.raises(ValueError, match=r"^'optional-dependencies' is dynamic but already listed in \[project\].$"):
		build_func(dist_dir)


@pytest.mark.parametrize("build_func", [build_wheel, build_sdist])
def test_filename_and_files(tmp_pathplus: PathPlus, build_func: Callable):

	dist_dir = tmp_pathplus / "dist"
	dist_dir.maybe_make()

	(tmp_pathplus / "pyproject.toml").write_clean(pyproject_toml + 'filename = "requirements.txt"\n')
	(tmp_pathplus / "requirements.txt").write_lines(["Foo", "# fizz", "bar", "baz>1"])
	(tmp_pathplus / "README.md").touch()
	(tmp_pathplus / "LICENSE").touch()
	(tmp_pathplus / "demo").maybe_make()
	(tmp_pathplus / "demo" / "__init__.py").touch()

	with in_directory(tmp_pathplus), pytest.raises(ValueError, match=(
		"^Cannot specify both 'filename' and 'files' in "
		"\\[tool.hatch.metadata.hooks.requirements_txt\\].$"
	)):
		build_func(dist_dir)


@pytest.mark.parametrize("build_func", [build_wheel, build_sdist])
def test_filename_parameter_not_str(tmp_pathplus: PathPlus, build_func: Callable):

	dist_dir = tmp_pathplus / "dist"
	dist_dir.maybe_make()

	(tmp_pathplus / "pyproject.toml").write_clean(
			pyproject_toml.replace('files = ["requirements.txt"]', 'filename = ["requirements.txt"]')
			)
	(tmp_pathplus / "requirements.txt").write_lines(["Foo", "# fizz", "bar", "baz>1"])
	(tmp_pathplus / "README.md").touch()
	(tmp_pathplus / "LICENSE").touch()
	(tmp_pathplus / "demo").maybe_make()
	(tmp_pathplus / "demo" / "__init__.py").touch()

	type_error_msg = r"^Requirements file \['requirements.txt'\] must be a string, but got <class 'list'>.$"
	deprecation_warning_msg = r"The 'filename' option in \[tool.hatch.metadata.hooks.requirements_txt\] is deprecated. Please instead use the list 'files'"
	with in_directory(tmp_pathplus):
		with pytest.raises(TypeError, match=type_error_msg), pytest.warns(DeprecationWarning, match=deprecation_warning_msg):
			build_func(dist_dir)


@pytest.mark.parametrize("build_func", [build_wheel, build_sdist])
def test_files_parameter_not_list(tmp_pathplus: PathPlus, build_func: Callable):

	dist_dir = tmp_pathplus / "dist"
	dist_dir.maybe_make()

	(tmp_pathplus / "pyproject.toml").write_clean(
			pyproject_toml.replace('files = ["requirements.txt"]', 'files = "requirements.txt"')
			)
	(tmp_pathplus / "requirements.txt").write_lines(["Foo", "# fizz", "bar", "baz>1"])
	(tmp_pathplus / "README.md").touch()
	(tmp_pathplus / "LICENSE").touch()
	(tmp_pathplus / "demo").maybe_make()
	(tmp_pathplus / "demo" / "__init__.py").touch()

	with in_directory(tmp_pathplus), pytest.raises(TypeError, match=(
		"^Requirements files must be a list, but got <class 'str'>: requirements.txt.$"
	)):
		build_func(dist_dir)


@pytest.mark.parametrize("build_func", [build_wheel, build_sdist])
def test_filename_deprecation(tmp_pathplus: PathPlus, build_func: Callable):

	dist_dir = tmp_pathplus / "dist"
	dist_dir.maybe_make()

	(tmp_pathplus / "pyproject.toml").write_clean(
			pyproject_toml.replace('files = ["requirements.txt"]', 'filename = "requirements.txt"')
			)
	(tmp_pathplus / "requirements.txt").write_lines(["Foo", "# fizz", "bar", "baz>1"])
	(tmp_pathplus / "README.md").touch()
	(tmp_pathplus / "LICENSE").touch()
	(tmp_pathplus / "demo").maybe_make()
	(tmp_pathplus / "demo" / "__init__.py").touch()

	with in_directory(tmp_pathplus), pytest.warns(DeprecationWarning, match=(
		r"^The 'filename' option in \[tool.hatch.metadata.hooks.requirements_txt\] "
		r"is deprecated. Please instead use the list 'files'.$"
	)):
		build_func(dist_dir)


@pytest.mark.parametrize("build_func", [build_wheel, build_sdist])
def test_no_files_or_filename_deprecation(tmp_pathplus: PathPlus, build_func: Callable):

	dist_dir = tmp_pathplus / "dist"
	dist_dir.maybe_make()

	(tmp_pathplus / "pyproject.toml").write_clean(pyproject_toml.replace('files = ["requirements.txt"]', ''))
	(tmp_pathplus / "requirements.txt").write_lines(["Foo", "# fizz", "bar", "baz>1"])
	(tmp_pathplus / "README.md").touch()
	(tmp_pathplus / "LICENSE").touch()
	(tmp_pathplus / "demo").maybe_make()
	(tmp_pathplus / "demo" / "__init__.py").touch()

	with in_directory(tmp_pathplus), pytest.warns(DeprecationWarning, match=(
		r"Please explicitly specify 'files' in "
		r"\[tool.hatch.metadata.hooks.requirements_txt\]. Defaulting to "
		r"\['requirements.txt'\] is deprecated."
	)):
		build_func(dist_dir)