forked from python-poetry/poetry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_install.py
293 lines (220 loc) · 8.15 KB
/
test_install.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
from __future__ import annotations
from typing import TYPE_CHECKING
import pytest
from poetry.core.masonry.utils.module import ModuleOrPackageNotFound
from poetry.core.packages.dependency_group import MAIN_GROUP
if TYPE_CHECKING:
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 ProjectFactory
PYPROJECT_CONTENT = """\
[tool.poetry]
name = "simple-project"
version = "1.2.3"
description = "Some description."
authors = [
"Python Poetry <tests@python-poetry.org>"
]
license = "MIT"
readme = "README.rst"
[tool.poetry.dependencies]
python = "~2.7 || ^3.4"
fizz = { version = "^1.0", optional = true }
buzz = { version = "^2.0", optional = true }
[tool.poetry.group.foo.dependencies]
foo = "^1.0"
[tool.poetry.group.bar.dependencies]
bar = "^1.1"
[tool.poetry.group.baz.dependencies]
baz = "^1.2"
[tool.poetry.group.bim.dependencies]
bim = "^1.3"
[tool.poetry.group.bam]
optional = true
[tool.poetry.group.bam.dependencies]
bam = "^1.4"
[tool.poetry.extras]
extras_a = [ "fizz" ]
extras_b = [ "buzz" ]
"""
@pytest.fixture
def poetry(project_factory: ProjectFactory) -> Poetry:
return project_factory(name="export", pyproject_content=PYPROJECT_CONTENT)
@pytest.fixture
def tester(
command_tester_factory: CommandTesterFactory, poetry: Poetry
) -> CommandTester:
return command_tester_factory("install")
@pytest.mark.parametrize(
("options", "groups"),
[
("", {MAIN_GROUP, "foo", "bar", "baz", "bim"}),
("--only-root", set()),
(f"--only {MAIN_GROUP}", {MAIN_GROUP}),
("--only foo", {"foo"}),
("--only foo,bar", {"foo", "bar"}),
("--only bam", {"bam"}),
("--with bam", {MAIN_GROUP, "foo", "bar", "baz", "bim", "bam"}),
("--without foo,bar", {MAIN_GROUP, "baz", "bim"}),
(f"--without {MAIN_GROUP}", {"foo", "bar", "baz", "bim"}),
("--with foo,bar --without baz --without bim --only bam", {"bam"}),
# net result zero options
("--with foo", {MAIN_GROUP, "foo", "bar", "baz", "bim"}),
("--without bam", {MAIN_GROUP, "foo", "bar", "baz", "bim"}),
("--with bam --without bam", {MAIN_GROUP, "foo", "bar", "baz", "bim"}),
("--with foo --without foo", {MAIN_GROUP, "bar", "baz", "bim"}),
# deprecated options
("--no-dev", {MAIN_GROUP}),
],
)
@pytest.mark.parametrize("with_root", [True, False])
def test_group_options_are_passed_to_the_installer(
options: str,
groups: set[str],
with_root: bool,
tester: CommandTester,
mocker: MockerFixture,
):
"""
Group options are passed properly to the installer.
"""
mocker.patch.object(tester.command.installer, "run", return_value=0)
editable_builder_mock = mocker.patch(
"poetry.masonry.builders.editable.EditableBuilder",
side_effect=ModuleOrPackageNotFound(),
)
if not with_root:
options = f"--no-root {options}"
status_code = tester.execute(options)
if options == "--no-root --only-root":
assert status_code == 1
return
else:
assert status_code == 0
package_groups = set(tester.command.poetry.package._dependency_groups.keys())
installer_groups = set(tester.command.installer._groups)
assert installer_groups <= package_groups
assert set(installer_groups) == groups
if with_root:
assert editable_builder_mock.call_count == 1
assert editable_builder_mock.call_args_list[0][0][0] == tester.command.poetry
else:
assert editable_builder_mock.call_count == 0
def test_sync_option_is_passed_to_the_installer(
tester: CommandTester, mocker: MockerFixture
):
"""
The --sync option is passed properly to the installer.
"""
mocker.patch.object(tester.command.installer, "run", return_value=1)
tester.execute("--sync")
assert tester.command.installer._requires_synchronization
def test_no_all_extras_doesnt_populate_installer(
tester: CommandTester, mocker: MockerFixture
):
"""
Not passing --all-extras means the installer doesn't see any extras.
"""
mocker.patch.object(tester.command.installer, "run", return_value=1)
tester.execute()
assert not tester.command.installer._extras
def test_all_extras_populates_installer(tester: CommandTester, mocker: MockerFixture):
"""
The --all-extras option results in extras passed to the installer.
"""
mocker.patch.object(tester.command.installer, "run", return_value=1)
tester.execute("--all-extras")
assert tester.command.installer._extras == ["extras-a", "extras-b"]
def test_extras_are_parsed_and_populate_installer(
tester: CommandTester,
mocker: MockerFixture,
):
mocker.patch.object(tester.command.installer, "run", return_value=0)
tester.execute('--extras "first second third"')
assert tester.command.installer._extras == ["first", "second", "third"]
def test_extras_conflicts_all_extras(tester: CommandTester, mocker: MockerFixture):
"""
The --extras doesn't make sense with --all-extras.
"""
mocker.patch.object(tester.command.installer, "run", return_value=0)
tester.execute("--extras foo --all-extras")
assert tester.status_code == 1
assert (
tester.io.fetch_error()
== "You cannot specify explicit `--extras` while installing using"
" `--all-extras`.\n"
)
@pytest.mark.parametrize(
"options",
[
"--with foo",
"--without foo",
"--with foo,bar --without baz",
"--only foo",
],
)
def test_only_root_conflicts_with_without_only(
options: str,
tester: CommandTester,
mocker: MockerFixture,
):
mocker.patch.object(tester.command.installer, "run", return_value=0)
tester.execute(f"{options} --only-root")
assert tester.status_code == 1
assert (
tester.io.fetch_error()
== "The `--with`, `--without` and `--only` options cannot be used with"
" the `--only-root` option.\n"
)
def test_remove_untracked_outputs_deprecation_warning(
tester: CommandTester,
mocker: MockerFixture,
):
mocker.patch.object(tester.command.installer, "run", return_value=0)
tester.execute("--remove-untracked")
assert tester.status_code == 0
assert (
tester.io.fetch_error()
== "The `--remove-untracked` option is deprecated, use the `--sync` option"
" instead.\n"
)
def test_dry_run_populates_installer(tester: CommandTester, mocker: MockerFixture):
"""
The --dry-run option results in extras passed to the installer.
"""
mocker.patch.object(tester.command.installer, "run", return_value=1)
tester.execute("--dry-run")
assert tester.command.installer._dry_run is True
def test_dry_run_does_not_build(tester: CommandTester, mocker: MockerFixture):
mocker.patch.object(tester.command.installer, "run", return_value=0)
mocked_editable_builder = mocker.patch(
"poetry.masonry.builders.editable.EditableBuilder"
)
tester.execute("--dry-run")
assert mocked_editable_builder.return_value.build.call_count == 0
def test_install_logs_output(tester: CommandTester, mocker: MockerFixture):
mocker.patch.object(tester.command.installer, "run", return_value=0)
mocker.patch("poetry.masonry.builders.editable.EditableBuilder")
tester.execute()
assert tester.status_code == 0
assert (
tester.io.fetch_output()
== "\nInstalling the current project: simple-project (1.2.3)\n"
)
def test_install_logs_output_decorated(tester: CommandTester, mocker: MockerFixture):
mocker.patch.object(tester.command.installer, "run", return_value=0)
mocker.patch("poetry.masonry.builders.editable.EditableBuilder")
tester.execute(decorated=True)
expected = (
"\n"
"\x1b[39;1mInstalling\x1b[39;22m the current project: "
"\x1b[36msimple-project\x1b[39m (\x1b[39;1m1.2.3\x1b[39;22m)"
"\x1b[1G\x1b[2K"
"\x1b[39;1mInstalling\x1b[39;22m the current project: "
"\x1b[36msimple-project\x1b[39m (\x1b[32m1.2.3\x1b[39m)"
"\n"
)
assert tester.status_code == 0
assert tester.io.fetch_output() == expected