-
Notifications
You must be signed in to change notification settings - Fork 256
/
Copy pathtest_builder.py
303 lines (235 loc) · 8.87 KB
/
test_builder.py
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
from __future__ import annotations
import sys
from email.parser import Parser
from pathlib import Path
from typing import TYPE_CHECKING
import pytest
from poetry.core.factory import Factory
from poetry.core.masonry.builders.builder import Builder
if TYPE_CHECKING:
from pytest_mock import MockerFixture
def test_building_not_possible_in_non_package_mode() -> None:
with pytest.raises(RuntimeError) as err:
Builder(
Factory().create_poetry(
Path(__file__).parent.parent.parent / "fixtures" / "non_package_mode"
)
)
assert str(err.value) == "Building a package is not possible in non-package mode."
def test_builder_find_excluded_files(mocker: MockerFixture) -> None:
p = mocker.patch("poetry.core.vcs.git.Git.get_ignored_files")
p.return_value = []
builder = Builder(
Factory().create_poetry(Path(__file__).parent / "fixtures" / "complete")
)
assert builder.find_excluded_files() == {"my_package/sub_pkg1/extra_file.xml"}
@pytest.mark.xfail(
sys.platform == "win32",
reason="Windows is case insensitive for the most part",
)
def test_builder_find_case_sensitive_excluded_files(mocker: MockerFixture) -> None:
p = mocker.patch("poetry.core.vcs.git.Git.get_ignored_files")
p.return_value = []
builder = Builder(
Factory().create_poetry(
Path(__file__).parent / "fixtures" / "case_sensitive_exclusions"
)
)
assert builder.find_excluded_files() == {
"my_package/FooBar/Bar.py",
"my_package/FooBar/lowercasebar.py",
"my_package/Foo/SecondBar.py",
"my_package/Foo/Bar.py",
"my_package/Foo/lowercasebar.py",
"my_package/bar/foo.py",
"my_package/bar/CapitalFoo.py",
}
@pytest.mark.xfail(
sys.platform == "win32",
reason="Windows is case insensitive for the most part",
)
def test_builder_find_invalid_case_sensitive_excluded_files(
mocker: MockerFixture,
) -> None:
p = mocker.patch("poetry.core.vcs.git.Git.get_ignored_files")
p.return_value = []
builder = Builder(
Factory().create_poetry(
Path(__file__).parent / "fixtures" / "invalid_case_sensitive_exclusions"
)
)
assert {"my_package/Bar/foo/bar/Foo.py"} == builder.find_excluded_files()
def test_get_metadata_content() -> None:
builder = Builder(
Factory().create_poetry(Path(__file__).parent / "fixtures" / "complete")
)
metadata = builder.get_metadata_content()
p = Parser()
parsed = p.parsestr(metadata)
assert parsed["Metadata-Version"] == "2.3"
assert parsed["Name"] == "my-package"
assert parsed["Version"] == "1.2.3"
assert parsed["Summary"] == "Some description."
assert parsed["Author"] == "Sébastien Eustace"
assert parsed["Author-email"] == "sebastien@eustace.io"
assert parsed["Keywords"] == "packaging,dependency,poetry"
assert parsed["Requires-Python"] == ">=3.6,<4.0"
assert parsed["License"] == "MIT"
assert parsed["Home-page"] == "https://python-poetry.org/"
classifiers = parsed.get_all("Classifier")
assert classifiers == [
"License :: OSI Approved :: MIT License",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.6",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"Topic :: Software Development :: Build Tools",
"Topic :: Software Development :: Libraries :: Python Modules",
]
extras = parsed.get_all("Provides-Extra")
assert extras == ["time"]
requires = parsed.get_all("Requires-Dist")
assert requires == [
"cachy[msgpack] (>=0.2.0,<0.3.0)",
"cleo (>=0.6,<0.7)",
(
'pendulum (>=1.4,<2.0) ; (python_version ~= "2.7" and sys_platform =='
' "win32" or python_version in "3.4 3.5") and (extra == "time")'
),
]
urls = parsed.get_all("Project-URL")
assert urls == [
"Documentation, https://python-poetry.org/docs",
"Issue Tracker, https://github.com/python-poetry/poetry/issues",
"Repository, https://github.com/python-poetry/poetry",
]
def test_metadata_pretty_name() -> None:
builder = Builder(
Factory().create_poetry(Path(__file__).parent / "fixtures" / "Pretty.Name")
)
metadata = Parser().parsestr(builder.get_metadata_content())
assert metadata["Name"] == "Pretty.Name"
def test_metadata_homepage_default() -> None:
builder = Builder(
Factory().create_poetry(Path(__file__).parent / "fixtures" / "simple_version")
)
metadata = Parser().parsestr(builder.get_metadata_content())
assert metadata["Home-page"] is None
def test_metadata_with_vcs_dependencies() -> None:
builder = Builder(
Factory().create_poetry(
Path(__file__).parent / "fixtures" / "with_vcs_dependency"
)
)
metadata = Parser().parsestr(builder.get_metadata_content())
requires_dist = metadata["Requires-Dist"]
assert requires_dist == "cleo @ git+https://github.com/sdispater/cleo.git@master"
def test_metadata_with_url_dependencies() -> None:
builder = Builder(
Factory().create_poetry(
Path(__file__).parent / "fixtures" / "with_url_dependency"
)
)
metadata = Parser().parsestr(builder.get_metadata_content())
requires_dist = metadata["Requires-Dist"]
assert (
requires_dist == "demo @"
" https://python-poetry.org/distributions/demo-0.1.0-py2.py3-none-any.whl"
)
def test_missing_script_files_throws_error() -> None:
builder = Builder(
Factory().create_poetry(
Path(__file__).parent / "fixtures" / "script_reference_file_missing"
)
)
with pytest.raises(RuntimeError) as err:
builder.convert_script_files()
assert "is not found." in str(err.value)
def test_invalid_script_files_definition() -> None:
with pytest.raises(RuntimeError) as err:
Builder(
Factory().create_poetry(
Path(__file__).parent
/ "fixtures"
/ "script_reference_file_invalid_definition"
)
)
assert "configuration is invalid" in str(err.value)
assert "scripts.invalid_definition" in str(err.value)
@pytest.mark.parametrize(
"fixture, result",
[
(
"script_callable_legacy_string",
{"console_scripts": ["script-legacy = my_package:main"]},
),
(
"script_reference_console",
{
"console_scripts": [
"extra-script = my_package.extra:main[time]",
"script = my_package.extra:main",
]
},
),
(
"script_reference_file",
{},
),
],
)
@pytest.mark.filterwarnings("ignore:.* script .* extra:DeprecationWarning")
def test_builder_convert_entry_points(
fixture: str, result: dict[str, list[str]]
) -> None:
entry_points = Builder(
Factory().create_poetry(Path(__file__).parent / "fixtures" / fixture)
).convert_entry_points()
assert entry_points == result
@pytest.mark.parametrize(
"fixture, result",
[
(
"script_callable_legacy_table",
[],
),
(
"script_callable_legacy_string",
[],
),
(
"script_reference_console",
[],
),
(
"script_reference_file",
[Path("bin") / "script.sh"],
),
],
)
def test_builder_convert_script_files(fixture: str, result: list[Path]) -> None:
project_root = Path(__file__).parent / "fixtures" / fixture
script_files = Builder(Factory().create_poetry(project_root)).convert_script_files()
assert [p.relative_to(project_root) for p in script_files] == result
def test_metadata_with_readme_files() -> None:
test_path = Path(__file__).parent.parent.parent / "fixtures" / "with_readme_files"
builder = Builder(Factory().create_poetry(test_path))
metadata = Parser().parsestr(builder.get_metadata_content())
readme1 = test_path / "README-1.rst"
readme2 = test_path / "README-2.rst"
description = "\n".join(
[readme1.read_text(encoding="utf-8"), readme2.read_text(encoding="utf-8"), ""]
)
assert metadata.get_payload() == description
def test_metadata_with_wildcard_dependency_constraint() -> None:
test_path = (
Path(__file__).parent / "fixtures" / "with_wildcard_dependency_constraint"
)
builder = Builder(Factory().create_poetry(test_path))
metadata = Parser().parsestr(builder.get_metadata_content())
requires = metadata.get_all("Requires-Dist")
assert requires == ["google-api-python-client (>=1.8,!=2.0.*)"]