-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Expand file tree
/
Copy pathtest_check.py
More file actions
342 lines (272 loc) · 11.1 KB
/
test_check.py
File metadata and controls
342 lines (272 loc) · 11.1 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
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
341
342
from __future__ import annotations
import os
from typing import TYPE_CHECKING
import pytest
from poetry.factory import Factory
from poetry.packages import Locker
from poetry.toml import TOMLFile
if TYPE_CHECKING:
from collections.abc import Iterator
from cleo.testers.command_tester import CommandTester
from pytest_mock import MockerFixture
from poetry.poetry import Poetry
from tests.types import CommandTesterFactory
from tests.types import FixtureDirGetter
from tests.types import SetProjectContext
@pytest.fixture
def poetry_simple_project(set_project_context: SetProjectContext) -> Iterator[Poetry]:
with set_project_context("simple_project", in_place=False) as cwd:
yield Factory().create_poetry(cwd)
@pytest.fixture
def poetry_with_outdated_lockfile(
set_project_context: SetProjectContext,
) -> Iterator[Poetry]:
with set_project_context("outdated_lock", in_place=False) as cwd:
yield Factory().create_poetry(cwd)
@pytest.fixture
def poetry_with_up_to_date_lockfile(
set_project_context: SetProjectContext,
) -> Iterator[Poetry]:
with set_project_context("up_to_date_lock", in_place=False) as cwd:
yield Factory().create_poetry(cwd)
@pytest.fixture
def poetry_with_pypi_reference(
set_project_context: SetProjectContext,
) -> Iterator[Poetry]:
with set_project_context("pypi_reference", in_place=False) as cwd:
yield Factory().create_poetry(cwd)
@pytest.fixture
def poetry_with_invalid_pyproject(
set_project_context: SetProjectContext,
) -> Iterator[Poetry]:
with set_project_context("invalid_pyproject", in_place=False) as cwd:
yield Factory().create_poetry(cwd)
@pytest.fixture
def poetry_with_src_folder(
set_project_context: SetProjectContext,
) -> Iterator[Poetry]:
with set_project_context("project_with_src_folder", in_place=False) as cwd:
yield Factory().create_poetry(cwd)
@pytest.fixture()
def tester(
command_tester_factory: CommandTesterFactory, poetry_simple_project: Poetry
) -> CommandTester:
return command_tester_factory("check", poetry=poetry_simple_project)
def test_check_valid(tester: CommandTester) -> None:
tester.execute()
expected = """\
All set!
"""
assert tester.io.fetch_output() == expected
@pytest.mark.parametrize(
["args", "expected_status"],
[
([], 0),
(["--strict"], 1),
],
)
def test_check_valid_legacy(
args: list[str],
expected_status: int,
mocker: MockerFixture,
tester: CommandTester,
fixture_dir: FixtureDirGetter,
) -> None:
mocker.patch(
"poetry.poetry.Poetry.file",
return_value=TOMLFile(fixture_dir("simple_project_legacy") / "pyproject.toml"),
new_callable=mocker.PropertyMock,
)
tester.execute(" ".join(args))
expected = (
"Warning: [tool.poetry.name] is deprecated. Use [project.name] instead.\n"
"Warning: [tool.poetry.version] is set but 'version' is not in "
"[project.dynamic]. If it is static use [project.version]. If it is dynamic, "
"add 'version' to [project.dynamic].\n"
"If you want to set the version dynamically via `poetry build "
"--local-version` or you are using a plugin, which sets the version "
"dynamically, you should define the version in [tool.poetry] and add "
"'version' to [project.dynamic].\n"
"Warning: [tool.poetry.description] is deprecated. Use [project.description] "
"instead.\n"
"Warning: [tool.poetry.readme] is set but 'readme' is not in "
"[project.dynamic]. If it is static use [project.readme]. If it is dynamic, "
"add 'readme' to [project.dynamic].\n"
"If you want to define multiple readmes, you should define them in "
"[tool.poetry] and add 'readme' to [project.dynamic].\n"
"Warning: [tool.poetry.license] is deprecated. Use [project.license] instead.\n"
"Warning: [tool.poetry.authors] is deprecated. Use [project.authors] instead.\n"
"Warning: [tool.poetry.keywords] is deprecated. Use [project.keywords] "
"instead.\n"
"Warning: [tool.poetry.classifiers] is set but 'classifiers' is not in "
"[project.dynamic]. If it is static use [project.classifiers]. If it is "
"dynamic, add 'classifiers' to [project.dynamic].\n"
"ATTENTION: Per default Poetry determines classifiers for supported Python "
"versions and license automatically. If you define classifiers in [project], "
"you disable the automatic enrichment. In other words, you have to define all "
"classifiers manually. If you want to use Poetry's automatic enrichment of "
"classifiers, you should define them in [tool.poetry] and add 'classifiers' "
"to [project.dynamic].\n"
"Warning: [tool.poetry.homepage] is deprecated. Use [project.urls] instead.\n"
"Warning: [tool.poetry.repository] is deprecated. Use [project.urls] instead.\n"
"Warning: [tool.poetry.documentation] is deprecated. Use [project.urls] "
"instead.\n"
"Warning: Defining console scripts in [tool.poetry.scripts] is deprecated. "
"Use [project.scripts] instead. ([tool.poetry.scripts] should only be used "
"for scripts of type 'file').\n"
)
assert tester.io.fetch_error() == expected
assert tester.status_code == expected_status
def test_check_invalid_dep_name_same_as_project_name(
mocker: MockerFixture, tester: CommandTester, fixture_dir: FixtureDirGetter
) -> None:
mocker.patch(
"poetry.poetry.Poetry.file",
return_value=TOMLFile(
fixture_dir("invalid_pyproject_dep_name") / "pyproject.toml"
),
new_callable=mocker.PropertyMock,
)
tester.execute("")
expected = """\
Error: Project name (invalid) is same as one of its dependencies
"""
assert tester.io.fetch_error() == expected
def test_check_invalid(
tester: CommandTester,
fixture_dir: FixtureDirGetter,
command_tester_factory: CommandTesterFactory,
poetry_with_invalid_pyproject: Poetry,
) -> None:
tester = command_tester_factory("check", poetry=poetry_with_invalid_pyproject)
tester.execute("--lock")
expected = """\
Error: Unrecognized classifiers: ['Intended Audience :: Clowns'].
Error: Declared README file does not exist: never/exists.md
Error: Invalid source "not-exists" referenced in dependencies.
Error: Invalid source "not-exists2" referenced in dependencies.
Error: poetry.lock was not found.
Warning: A wildcard Python dependency is ambiguous.\
Consider specifying a more explicit one.
Warning: The "pendulum" dependency specifies the "allows-prereleases" property,\
which is deprecated. Use "allow-prereleases" instead.
Warning: Deprecated classifier 'Natural Language :: Ukranian'.\
Must be replaced by ['Natural Language :: Ukrainian'].
Warning: Deprecated classifier\
'Topic :: Communications :: Chat :: AOL Instant Messenger'.\
Must be removed.
"""
assert tester.io.fetch_error() == expected
def test_check_private(
mocker: MockerFixture, tester: CommandTester, fixture_dir: FixtureDirGetter
) -> None:
mocker.patch(
"poetry.poetry.Poetry.file",
return_value=TOMLFile(fixture_dir("private_pyproject") / "pyproject.toml"),
new_callable=mocker.PropertyMock,
)
tester.execute()
expected = """\
All set!
"""
assert tester.io.fetch_output() == expected
def test_check_non_package_mode(
mocker: MockerFixture, tester: CommandTester, fixture_dir: FixtureDirGetter
) -> None:
mocker.patch(
"poetry.poetry.Poetry.file",
return_value=TOMLFile(fixture_dir("non_package_mode") / "pyproject.toml"),
new_callable=mocker.PropertyMock,
)
tester.execute()
expected = """\
All set!
"""
assert tester.io.fetch_output() == expected
@pytest.mark.parametrize(
("options", "expected", "expected_status"),
[
("", "All set!\n", 0),
("--lock", "Error: poetry.lock was not found.\n", 1),
],
)
def test_check_lock_missing(
mocker: MockerFixture,
tester: CommandTester,
fixture_dir: FixtureDirGetter,
options: str,
expected: str,
expected_status: int,
) -> None:
mocker.patch(
"poetry.poetry.Poetry.file",
return_value=TOMLFile(fixture_dir("private_pyproject") / "pyproject.toml"),
new_callable=mocker.PropertyMock,
)
status_code = tester.execute(options)
assert status_code == expected_status
if status_code == 0:
assert tester.io.fetch_output() == expected
else:
assert tester.io.fetch_error() == expected
@pytest.mark.parametrize("options", ["", "--lock"])
def test_check_lock_outdated(
command_tester_factory: CommandTesterFactory,
poetry_with_outdated_lockfile: Poetry,
options: str,
) -> None:
locker = Locker(
lock=poetry_with_outdated_lockfile.pyproject.file.path.parent / "poetry.lock",
pyproject_data=poetry_with_outdated_lockfile.locker._pyproject_data,
)
poetry_with_outdated_lockfile.set_locker(locker)
tester = command_tester_factory("check", poetry=poetry_with_outdated_lockfile)
status_code = tester.execute(options)
expected = (
"Error: pyproject.toml changed significantly since poetry.lock was last generated. "
"Run `poetry lock` to fix the lock file.\n"
)
assert tester.io.fetch_error() == expected
# exit with an error
assert status_code == 1
@pytest.mark.parametrize("options", ["", "--lock"])
def test_check_lock_up_to_date(
command_tester_factory: CommandTesterFactory,
poetry_with_up_to_date_lockfile: Poetry,
options: str,
) -> None:
locker = Locker(
lock=poetry_with_up_to_date_lockfile.pyproject.file.path.parent / "poetry.lock",
pyproject_data=poetry_with_up_to_date_lockfile.locker._pyproject_data,
)
poetry_with_up_to_date_lockfile.set_locker(locker)
tester = command_tester_factory("check", poetry=poetry_with_up_to_date_lockfile)
status_code = tester.execute(options)
expected = "All set!\n"
assert tester.io.fetch_output() == expected
# exit with an error
assert status_code == 0
def test_check_does_not_error_on_pypi_reference(
command_tester_factory: CommandTesterFactory,
poetry_with_pypi_reference: Poetry,
) -> None:
tester = command_tester_factory("check", poetry=poetry_with_pypi_reference)
status_code = tester.execute("")
assert tester.io.fetch_output() == "All set!\n"
assert status_code == 0
def test_check_project_with_src_folder(
command_tester_factory: CommandTesterFactory,
poetry_with_src_folder: Poetry,
) -> None:
tester = command_tester_factory("check", poetry=poetry_with_src_folder)
poetry_file = poetry_with_src_folder.file.path
project_root = poetry_file.parent
project_dir = project_root / "project_with_src_folder"
os.makedirs(project_dir, exist_ok=True)
tester.execute("")
expected = """\
Warning: Found empty directory 'project_with_src_folder' in project root while the actual package is in\
'src/project_with_src_folder'. This may cause issues with package installation.\
Consider removing the empty directory.
"""
assert tester.io.fetch_error() == expected