File: test_metadata.py

package info (click to toggle)
python-dist-meta 0.9.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,964 kB
  • sloc: python: 2,944; makefile: 9
file content (307 lines) | stat: -rw-r--r-- 10,319 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
# stdlib
from typing import List

# 3rd party
import pytest
from coincidence import AdvancedDataRegressionFixture, AdvancedFileRegressionFixture
from domdf_python_tools.paths import PathPlus

# this package
from dist_meta import metadata
from dist_meta.metadata import MissingFieldError
from dist_meta.metadata_mapping import MetadataMapping


@pytest.fixture()
def example_metadata() -> str:
	return (PathPlus(__file__).parent / "example_metadata").read_text()


# Symbols ensure the reference file is the same for both cases
@pytest.mark.parametrize("newline", [
		pytest.param('\n', id='#'),
		pytest.param("\r\n", id='/'),
		])
def test_loads(
		example_metadata: str,
		advanced_data_regression: AdvancedDataRegressionFixture,
		advanced_file_regression: AdvancedFileRegressionFixture,
		newline: str,
		):
	fields = metadata.loads(example_metadata.replace('\n', newline))

	advanced_data_regression.check(fields.keys())
	assert fields["Metadata-Version"] == "2.1"
	assert fields["Name"] == "cawdrey"
	assert fields["Version"] == "0.4.2"
	assert fields["Summary"] == "Several useful custom dictionaries for Python 📖 🐍"
	assert fields["Home-page"] == "https://github.com/domdfcoding/cawdrey"
	assert fields["Author"] == "Dominic Davis-Foster"
	assert fields["Author-email"] == "dominic@davis-foster.co.uk"
	assert fields["License"] == "GNU Lesser General Public License v3 or later (LGPLv3+)"
	assert fields.get_all("Project-URL") == [
			"Documentation, https://cawdrey.readthedocs.io/en/latest",
			"Issue Tracker, https://github.com/domdfcoding/cawdrey/issues",
			"Source Code, https://github.com/domdfcoding/cawdrey",
			]
	assert fields[
			"Keywords"
			] == "frozenordereddict,orderedfrozendict,frozen,immutable,frozendict,dict,dictionary,map,Mapping,MappingProxyType,Counter"
	assert fields.get_all("Platform") == ["Windows", "macOS", "Linux"]

	assert fields["Requires-Python"] == ">=3.6.1"
	assert fields["Description-Content-Type"] == "text/x-rst"
	assert fields.get_all("Requires-Dist") == ["domdf-python-tools (>=1.1.0)", "typing-extensions (>=3.7.4.3)"]

	assert fields["Provides-Extra"] == "all"

	advanced_file_regression.check(fields["Description"])


def test_load(
		example_metadata: str,
		tmp_pathplus: PathPlus,
		advanced_data_regression: AdvancedDataRegressionFixture,
		advanced_file_regression: AdvancedFileRegressionFixture,
		):
	(tmp_pathplus / "METADATA").write_text(example_metadata)
	fields = metadata.load(tmp_pathplus / "METADATA")

	advanced_data_regression.check(fields.keys())
	assert fields["Metadata-Version"] == "2.1"
	assert fields["Name"] == "cawdrey"
	assert fields["Version"] == "0.4.2"
	assert fields["Summary"] == "Several useful custom dictionaries for Python 📖 🐍"
	assert fields["Home-page"] == "https://github.com/domdfcoding/cawdrey"
	assert fields["Author"] == "Dominic Davis-Foster"
	assert fields["Author-email"] == "dominic@davis-foster.co.uk"
	assert fields["License"] == "GNU Lesser General Public License v3 or later (LGPLv3+)"
	assert fields.get_all("Project-URL") == [
			"Documentation, https://cawdrey.readthedocs.io/en/latest",
			"Issue Tracker, https://github.com/domdfcoding/cawdrey/issues",
			"Source Code, https://github.com/domdfcoding/cawdrey",
			]
	assert fields[
			"Keywords"
			] == "frozenordereddict,orderedfrozendict,frozen,immutable,frozendict,dict,dictionary,map,Mapping,MappingProxyType,Counter"
	assert fields.get_all("Platform") == ["Windows", "macOS", "Linux"]

	assert fields["Requires-Python"] == ">=3.6.1"
	assert fields["Description-Content-Type"] == "text/x-rst"
	assert fields.get_all("Requires-Dist") == ["domdf-python-tools (>=1.1.0)", "typing-extensions (>=3.7.4.3)"]

	assert fields["Provides-Extra"] == "all"

	advanced_file_regression.check(fields["Description"])


def test_load_no_version(tmp_pathplus: PathPlus):
	(tmp_pathplus / "METADATA").write_lines([
			"Generator: bdist_wheel (0.36.2)",
			"Name: cawdrey",
			"Version: 0.4.2",
			"Home-page: https://github.com/domdfcoding/cawdrey",
			])

	with pytest.raises(MissingFieldError, match=f"No 'Metadata-Version' field was provided."):
		metadata.load(tmp_pathplus / "METADATA")


def test_dumps(advanced_file_regression: AdvancedFileRegressionFixture):
	fields = MetadataMapping()
	fields["Metadata-Version"] = "2.1"
	fields["Name"] = "cawdrey"
	fields["Version"] = "0.4.2"
	fields["Home-page"] = "https://github.com/domdfcoding/cawdrey"
	fields["Platform"] = "Windows"
	fields["Platform"] = "macOS"
	fields["Platform"] = "Linux"

	advanced_file_regression.check(metadata.dumps(fields), extension='')


def test_dumps_license_expression_file(advanced_file_regression: AdvancedFileRegressionFixture):
	fields = MetadataMapping()
	fields["Metadata-Version"] = "2.1"
	fields["Name"] = "cawdrey"
	fields["Version"] = "0.4.2"
	fields["Home-page"] = "https://github.com/domdfcoding/cawdrey"
	fields["Platform"] = "Windows"
	fields["Platform"] = "macOS"
	fields["Platform"] = "Linux"
	fields["License-Expression"] = "MIT OR Apache-2.0"
	fields["License-File"] = "LICENSE"
	fields["License-File"] = "COPYING"

	advanced_file_regression.check(metadata.dumps(fields), extension='')


def test_dumps_description(advanced_file_regression: AdvancedFileRegressionFixture):
	fields = MetadataMapping()
	fields["Metadata-Version"] = "2.1"
	fields["Name"] = "cawdrey"
	fields["Version"] = "0.4.2"
	fields["Home-page"] = "https://github.com/domdfcoding/cawdrey"
	fields["Platform"] = "Windows"
	fields["Platform"] = "macOS"
	fields["Platform"] = "Linux"
	fields["Description"] = "This is the body\n\nIt can have multiple lines\n\t\tand indents"

	advanced_file_regression.check(metadata.dumps(fields), extension='')


def test_dump(
		tmp_pathplus: PathPlus,
		advanced_file_regression: AdvancedFileRegressionFixture,
		):
	fields = MetadataMapping()
	fields["Metadata-Version"] = "2.1"
	fields["Name"] = "cawdrey"
	fields["Version"] = "0.4.2"
	fields["Home-page"] = "https://github.com/domdfcoding/cawdrey"
	fields["Platform"] = "Windows"
	fields["Platform"] = "macOS"
	fields["Platform"] = "Linux"

	metadata.dump(fields, tmp_pathplus / "METADATA")
	advanced_file_regression.check_file(tmp_pathplus / "METADATA")


def test_dumps_version_too_low():
	fields = MetadataMapping()
	fields["Metadata-Version"] = "1.1"
	fields["Name"] = "cawdrey"
	fields["Version"] = "0.4.2"
	fields["Home-page"] = "https://github.com/domdfcoding/cawdrey"
	fields["Platform"] = "Windows"
	fields["Platform"] = "macOS"
	fields["Platform"] = "Linux"

	with pytest.raises(ValueError, match="'dump_metadata' only supports metadata version 2.1 and above."):
		metadata.dumps(fields)


def test_dump_no_meta_version(tmp_pathplus: PathPlus):
	fields = MetadataMapping()
	fields["Name"] = "cawdrey"
	fields["Version"] = "0.4.2"
	fields["Home-page"] = "https://github.com/domdfcoding/cawdrey"
	fields["Platform"] = "Windows"
	fields["Platform"] = "macOS"
	fields["Platform"] = "Linux"

	with pytest.raises(MissingFieldError, match=f"No 'Metadata-Version' field was provided."):
		metadata.dump(fields, tmp_pathplus / "METADATA")

	assert not (tmp_pathplus / "METADATA").exists()


def test_dumps_no_name():
	fields = MetadataMapping()
	fields["Metadata-Version"] = "2.1"
	fields["Version"] = "0.4.2"
	fields["Home-page"] = "https://github.com/domdfcoding/cawdrey"
	fields["Platform"] = "Windows"
	fields["Platform"] = "macOS"
	fields["Platform"] = "Linux"

	with pytest.raises(MissingFieldError, match="No 'Name' field was provided."):
		metadata.dumps(fields)


def test_dumps_no_version():
	fields = MetadataMapping()
	fields["Metadata-Version"] = "2.1"
	fields["Name"] = "cawdrey"
	fields["Home-page"] = "https://github.com/domdfcoding/cawdrey"
	fields["Platform"] = "Windows"
	fields["Platform"] = "macOS"
	fields["Platform"] = "Linux"

	with pytest.raises(MissingFieldError, match="No 'Version' field was provided."):
		metadata.dumps(fields)


def test_loads_description_as_key_pipe():
	fields = metadata.loads(
			'\n'.join([
					"Metadata-Version: 2.1",
					"Name: BeagleVote",
					"version: 1.0a2",
					"Description: This project provides powerful math functions",
					"        |For example, you can use `sum()` to sum numbers:",
					"        |",
					"        |Example::",
					"        |",
					"        |    >>> sum(1, 2)",
					"        |    3",
					"        |",
					])
			)

	assert fields["Metadata-Version"] == "2.1"
	assert fields["Name"] == "BeagleVote"
	assert fields["Version"] == "1.0a2"
	assert fields["Description"] == '\n'.join([
			"This project provides powerful math functions",
			"For example, you can use `sum()` to sum numbers:",
			'',
			"Example::",
			'',
			"    >>> sum(1, 2)",
			"    3",
			'',
			])


def test_loads_description_as_key_spaces():
	# Not to spec, but how setuptools and distutils do it
	fields = metadata.loads(
			'\n'.join([
					"Metadata-Version: 2.1",
					"Name: BeagleVote",
					"version: 1.0a2",
					"Description: This project provides powerful math functions",
					"        For example, you can use `sum()` to sum numbers:",
					"        ",
					"        Example::",
					"        ",
					"            >>> sum(1, 2)",
					"            3",
					"        ",
					])
			)

	assert fields["Metadata-Version"] == "2.1"
	assert fields["Name"] == "BeagleVote"
	assert fields["Version"] == "1.0a2"
	assert fields["Description"] == '\n'.join([
			"This project provides powerful math functions",
			"For example, you can use `sum()` to sum numbers:",
			'',
			"Example::",
			'',
			"    >>> sum(1, 2)",
			"    3",
			'',
			])


@pytest.mark.parametrize(
		"lines, wsp, expected",
		[
				(["hello", "world"], '\t', ["hello", "world"]),
				(["\thello", "\tworld"], '\t', ["hello", "world"]),
				(["hello", "\tworld"], '\t', ["hello", "world"]),
				(["  hello", "  world"], '\t', ["  hello", "  world"]),
				(["|hello", "|world"], '\t', ["|hello", "|world"]),
				(["  hello", "  world"], ' ', ["hello", "world"]),
				(["hello", "  world"], ' ', ["hello", "world"]),
				(["  hello", "world"], ' ', ["  hello", "world"]),
				(["|hello", "|world"], '|', ["hello", "world"]),
				(["hello", "|world"], '|', ["hello", "world"]),
				(["|hello", "world"], '|', ["|hello", "world"]),
				]
		)
def test_clean_desc(lines: List[str], wsp: str, expected: List[str]):
	assert (metadata._clean_desc(lines, wsp) == expected)