forked from jazzband/pip-tools
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_cli_sync.py
342 lines (270 loc) · 10.2 KB
/
test_cli_sync.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
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
import os
import subprocess
import sys
from unittest import mock
import pytest
from pip._vendor.packaging.version import Version
from piptools.scripts.sync import DEFAULT_REQUIREMENTS_FILE, cli
def test_run_as_module_sync():
"""piptools can be run as ``python -m piptools ...``."""
result = subprocess.run(
[sys.executable, "-m", "piptools", "sync", "--help"],
stdout=subprocess.PIPE,
check=True,
)
# Should have run pip-compile successfully.
assert result.stdout.startswith(b"Usage:")
assert b"Synchronize virtual environment with" in result.stdout
@mock.patch("piptools.sync.run")
def test_quiet_option(run, runner):
"""sync command can be run with `--quiet` or `-q` flag."""
with open("requirements.txt", "w") as req_in:
req_in.write("six==1.10.0")
out = runner.invoke(cli, ["-q"])
assert not out.stderr_bytes
assert out.exit_code == 0
# for every call to pip ensure the `-q` flag is set
assert run.call_count == 2
for call in run.call_args_list:
assert "-q" in call[0][0]
@mock.patch("piptools.sync.run")
def test_quiet_option_when_up_to_date(run, runner):
"""
Sync should output nothing when everything is up to date and quiet option is set.
"""
with open("requirements.txt", "w"):
pass
with mock.patch("piptools.sync.diff", return_value=(set(), set())):
out = runner.invoke(cli, ["-q"])
assert not out.stderr_bytes
assert out.exit_code == 0
run.assert_not_called()
def test_no_requirements_file(runner):
"""
It should raise an error if there are no input files
or a requirements.txt file does not exist.
"""
out = runner.invoke(cli)
assert "No requirement files given" in out.stderr
assert out.exit_code == 2
def test_input_files_with_dot_in_extension(runner):
"""
It should raise an error if some of the input files have .in extension.
"""
with open("requirements.in", "w") as req_in:
req_in.write("six==1.10.0")
out = runner.invoke(cli, ["requirements.in"])
assert "ERROR: Some input files have the .in extension" in out.stderr
assert out.exit_code == 2
def test_force_files_with_dot_in_extension(runner):
"""
It should print a warning and sync anyway if some of the input files
have .in extension.
"""
with open("requirements.in", "w") as req_in:
req_in.write("six==1.10.0")
with mock.patch("piptools.sync.run"):
out = runner.invoke(cli, ["requirements.in", "--force"])
assert "WARNING: Some input files have the .in extension" in out.stderr
assert out.exit_code == 0
@pytest.mark.parametrize(
("req_lines", "should_raise"),
(
(["six>1.10.0", "six<1.10.0"], True),
(
["six>1.10.0 ; python_version>='3.0'", "six<1.10.0 ; python_version<'3.0'"],
False,
),
),
)
def test_merge_error(req_lines, should_raise, runner):
"""
Sync command should raise an error if there are merge errors.
It should not raise an error if otherwise incompatible requirements
are isolated by exclusive environment markers.
"""
with open("requirements.txt", "w") as req_in:
for line in req_lines:
req_in.write(line + "\n")
with mock.patch("piptools.sync.run"):
out = runner.invoke(cli, ["-n"])
if should_raise:
assert out.exit_code == 2
assert "Incompatible requirements found" in out.stderr
else:
assert out.exit_code == 1
@pytest.mark.parametrize(
("cli_flags", "expected_install_flags"),
(
(
["--find-links", "./libs1", "--find-links", "./libs2"],
["--find-links", "./libs1", "--find-links", "./libs2"],
),
(["--no-index"], ["--no-index"]),
(
["--index-url", "https://example.com"],
["--index-url", "https://example.com"],
),
(
["--extra-index-url", "https://foo", "--extra-index-url", "https://bar"],
["--extra-index-url", "https://foo", "--extra-index-url", "https://bar"],
),
(
["--trusted-host", "foo", "--trusted-host", "bar"],
["--trusted-host", "foo", "--trusted-host", "bar"],
),
(["--user"], ["--user"]),
(["--cert", "foo.crt"], ["--cert", "foo.crt"]),
(["--client-cert", "foo.pem"], ["--client-cert", "foo.pem"]),
(
["--pip-args", "--no-cache-dir --no-deps --no-warn-script-location"],
["--no-cache-dir", "--no-deps", "--no-warn-script-location"],
),
(["--pip-args='--cache-dir=/tmp'"], ["--cache-dir=/tmp"]),
(
["--pip-args=\"--cache-dir='/tmp/cache dir with spaces/'\""],
["--cache-dir='/tmp/cache dir with spaces/'"],
),
),
)
@mock.patch("piptools.sync.run")
def test_pip_install_flags(run, cli_flags, expected_install_flags, runner):
"""
Test the cli flags have to be passed to the pip install command.
"""
with open("requirements.txt", "w") as req_in:
req_in.write("six==1.10.0")
runner.invoke(cli, cli_flags)
call_args = [call[0][0] for call in run.call_args_list]
called_install_options = [args[6:] for args in call_args if args[3] == "install"]
assert called_install_options == [expected_install_flags], "Called args: {}".format(
call_args
)
@pytest.mark.parametrize(
"install_flags",
(
["--no-index"],
["--index-url", "https://example.com"],
["--extra-index-url", "https://example.com"],
["--find-links", "./libs1"],
["--trusted-host", "example.com"],
["--no-binary", ":all:"],
["--only-binary", ":all:"],
),
)
@mock.patch("piptools.sync.run")
def test_pip_install_flags_in_requirements_file(run, runner, install_flags):
"""
Test the options from requirements.txt file pass to the pip install command.
"""
with open(DEFAULT_REQUIREMENTS_FILE, "w") as reqs:
reqs.write(" ".join(install_flags) + "\n")
reqs.write("six==1.10.0")
out = runner.invoke(cli)
assert out.exit_code == 0, out
# Make sure pip install command has expected options
call_args = [call[0][0] for call in run.call_args_list]
called_install_options = [args[6:] for args in call_args if args[3] == "install"]
assert called_install_options == [install_flags], f"Called args: {call_args}"
@mock.patch("piptools.sync.run")
def test_sync_ask_declined(run, runner):
"""
Make sure nothing is installed if the confirmation is declined
"""
with open("requirements.txt", "w") as req_in:
req_in.write("small-fake-a==1.10.0")
runner.invoke(cli, ["--ask"], input="n\n")
run.assert_not_called()
@mock.patch("piptools.sync.run")
def test_sync_ask_accepted(run, runner):
"""
Make sure pip is called when the confirmation is accepted (even if
--dry-run is given)
"""
with open("requirements.txt", "w") as req_in:
req_in.write("small-fake-a==1.10.0")
runner.invoke(cli, ["--ask", "--dry-run"], input="y\n")
assert run.call_count == 2
def test_sync_dry_run_returns_non_zero_exit_code(runner):
"""
Make sure non-zero exit code is returned when --dry-run is given.
"""
with open("requirements.txt", "w") as req_in:
req_in.write("small-fake-a==1.10.0")
out = runner.invoke(cli, ["--dry-run"])
assert out.exit_code == 1
@mock.patch("piptools.sync.run")
def test_python_executable_option(
run,
runner,
fake_dist,
):
"""
Make sure sync command can run with `--python-executable` option.
"""
with open("requirements.txt", "w") as req_in:
req_in.write("small-fake-a==1.10.0")
custom_executable = os.path.abspath(sys.executable)
runner.invoke(cli, ["--python-executable", custom_executable])
assert run.call_count == 2
call_args = [call[0][0] for call in run.call_args_list]
called_uninstall_options = [
args[:5] for args in call_args if args[3] == "uninstall"
]
called_install_options = [args[:-1] for args in call_args if args[3] == "install"]
assert called_uninstall_options == [
[custom_executable, "-m", "pip", "uninstall", "-y"]
]
assert called_install_options == [[custom_executable, "-m", "pip", "install", "-r"]]
@pytest.mark.parametrize(
"python_executable",
(
"/tmp/invalid_executable",
"invalid_python",
),
)
def test_invalid_python_executable(runner, python_executable):
with open("requirements.txt", "w") as req_in:
req_in.write("small-fake-a==1.10.0")
out = runner.invoke(cli, ["--python-executable", python_executable])
assert out.exit_code == 2, out
message = "Could not resolve '{}' as valid executable path or alias.\n"
assert out.stderr == message.format(python_executable)
@mock.patch("piptools.scripts.sync.get_pip_version_for_python_executable")
def test_invalid_pip_version_in_python_executable(
get_pip_version_for_python_executable, runner
):
with open("requirements.txt", "w") as req_in:
req_in.write("small-fake-a==1.10.0")
custom_executable = os.path.abspath("custom_executable")
with open(custom_executable, "w") as exec_file:
exec_file.write("")
os.chmod(custom_executable, 0o700)
get_pip_version_for_python_executable.return_value = Version("19.1")
out = runner.invoke(cli, ["--python-executable", custom_executable])
assert out.exit_code == 2, out
message = (
"Target python executable '{}' has pip version 19.1 installed. "
"Version" # ">=20.3 is expected.\n" part is omitted
)
assert out.stderr.startswith(message.format(custom_executable))
@mock.patch("piptools.sync.run")
def test_default_python_executable_option(run, runner):
"""
Make sure sys.executable is used when --python-executable is not provided.
"""
with open("requirements.txt", "w") as req_in:
req_in.write("small-fake-a==1.10.0")
runner.invoke(cli)
assert run.call_count == 2
call_args = [call[0][0] for call in run.call_args_list]
called_install_options = [args[:-1] for args in call_args if args[3] == "install"]
assert called_install_options == [
[
sys.executable,
"-m",
"pip",
"install",
"-r",
]
]