-
Notifications
You must be signed in to change notification settings - Fork 54
/
test_sync.py
483 lines (387 loc) · 14.6 KB
/
test_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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
import os
import sys
import tempfile
from collections import Counter
import mock
import pytest
from .constants import PACKAGES_PATH
from piptools._compat import path_to_url
from piptools.exceptions import IncompatibleRequirements
from piptools.sync import dependency_tree, diff, merge, sync
@pytest.fixture
def mocked_tmp_file():
with mock.patch.object(tempfile, "NamedTemporaryFile") as m:
yield m.return_value
@pytest.fixture
def mocked_tmp_req_file(mocked_tmp_file):
with mock.patch("os.unlink"):
mocked_tmp_file.name = "requirements.txt"
yield mocked_tmp_file
@pytest.mark.parametrize(
("installed", "root", "expected"),
[
([], "pip-tools", []),
([("pip-tools==1", [])], "pip-tools", ["pip-tools"]),
([("pip-tools==1", []), ("django==1.7", [])], "pip-tools", ["pip-tools"]),
(
[("pip-tools==1", ["click>=2"]), ("django==1.7", []), ("click==3", [])],
"pip-tools",
["pip-tools", "click"],
),
(
[("pip-tools==1", ["click>=2"]), ("django==1.7", []), ("click==1", [])],
"pip-tools",
["pip-tools"],
),
(
[("root==1", ["child==2"]), ("child==2", ["grandchild==3"])],
"root",
["root", "child"],
),
(
[
("root==1", ["child==2"]),
("child==2", ["grandchild==3"]),
("grandchild==3", []),
],
"root",
["root", "child", "grandchild"],
),
(
[("root==1", ["child==2"]), ("child==2", ["root==1"])],
"root",
["root", "child"],
),
],
)
def test_dependency_tree(fake_dist, installed, root, expected):
installed = {
distribution.key: distribution
for distribution in (fake_dist(name, deps) for name, deps in installed)
}
actual = dependency_tree(installed, root)
assert actual == set(expected)
def test_merge_detect_conflicts(from_line):
requirements = [from_line("flask==1"), from_line("flask==2")]
with pytest.raises(IncompatibleRequirements):
merge(requirements, ignore_conflicts=False)
def test_merge_ignore_conflicts(from_line):
requirements = [from_line("flask==1"), from_line("flask==2")]
assert Counter(requirements[1:2]) == Counter(
merge(requirements, ignore_conflicts=True)
)
def test_merge(from_line):
requirements = [
from_line("flask==1"),
from_line("flask==1"),
from_line("django==2"),
]
assert Counter(requirements[1:3]) == Counter(
merge(requirements, ignore_conflicts=False)
)
def test_merge_urls(from_line):
requirements = [
from_line("file:///example.zip#egg=example==1.0"),
from_line("example==1.0"),
from_line("file:///unrelated.zip"),
]
assert Counter(requirements[1:]) == Counter(
merge(requirements, ignore_conflicts=False)
)
def test_diff_should_do_nothing():
installed = [] # empty env
reqs = [] # no requirements
to_install, to_uninstall = diff(reqs, installed)
assert to_install == set()
assert to_uninstall == set()
def test_diff_should_install(from_line):
installed = [] # empty env
reqs = [from_line("django==1.8")]
to_install, to_uninstall = diff(reqs, installed)
assert {str(x.req) for x in to_install} == {"django==1.8"}
assert to_uninstall == set()
def test_diff_should_uninstall(fake_dist):
installed = [fake_dist("django==1.8")]
reqs = []
to_install, to_uninstall = diff(reqs, installed)
assert to_install == set()
assert to_uninstall == {"django"} # no version spec when uninstalling
def test_diff_should_not_uninstall(fake_dist):
ignored = (
"pip==7.1.0",
"pip-tools==1.1.1",
"pip-review==1.1.1",
"pkg-resources==0.0.0",
"setuptools==34.0.0",
"wheel==0.29.0",
"python==3.0",
"distribute==0.1",
"wsgiref==0.1",
"argparse==0.1",
)
installed = [fake_dist(pkg) for pkg in ignored]
reqs = []
to_uninstall = diff(reqs, installed)[1]
assert to_uninstall == set()
def test_diff_should_update(fake_dist, from_line):
installed = [fake_dist("django==1.7")]
reqs = [from_line("django==1.8")]
to_install, to_uninstall = diff(reqs, installed)
assert {str(x.req) for x in to_install} == {"django==1.8"}
assert to_uninstall == set()
def test_diff_should_install_with_markers(from_line):
installed = []
reqs = [from_line("subprocess32==3.2.7 ; python_version=='2.7'")]
to_install, to_uninstall = diff(reqs, installed)
assert {str(x.req) for x in to_install} == (
{"subprocess32==3.2.7"} if sys.version.startswith("2.7") else set()
)
assert to_uninstall == set()
def test_diff_should_uninstall_with_markers(fake_dist, from_line):
installed = [fake_dist("subprocess32==3.2.7")]
reqs = [from_line("subprocess32==3.2.7 ; python_version=='2.7'")]
to_install, to_uninstall = diff(reqs, installed)
assert to_install == set()
assert to_uninstall == (
set() if sys.version.startswith("2.7") else {"subprocess32"}
)
def test_diff_leave_packaging_packages_alone(fake_dist, from_line):
# Suppose an env contains Django, and pip itself
installed = [
fake_dist("django==1.7"),
fake_dist("first==2.0.1"),
fake_dist("pip==7.1.0"),
]
# Then this Django-only requirement should keep pip around (i.e. NOT
# uninstall it), but uninstall first
reqs = [from_line("django==1.7")]
to_install, to_uninstall = diff(reqs, installed)
assert to_install == set()
assert to_uninstall == {"first"}
def test_diff_leave_piptools_alone(fake_dist, from_line):
# Suppose an env contains Django, and pip-tools itself (including all of
# its dependencies)
installed = [
fake_dist("django==1.7"),
fake_dist("first==2.0.1"),
fake_dist("pip-tools==1.1.1", ["click>=4", "first", "six"]),
fake_dist("six==1.9.0"),
fake_dist("click==4.1"),
fake_dist("foobar==0.3.6"),
]
# Then this Django-only requirement should keep pip around (i.e. NOT
# uninstall it), but uninstall first
reqs = [from_line("django==1.7")]
to_install, to_uninstall = diff(reqs, installed)
assert to_install == set()
assert to_uninstall == {"foobar"}
def test_diff_with_editable(fake_dist, from_editable):
installed = [fake_dist("small-fake-with-deps==0.0.1"), fake_dist("six==1.10.0")]
path_to_package = os.path.join(PACKAGES_PATH, "small_fake_with_deps")
reqs = [from_editable(path_to_package)]
to_install, to_uninstall = diff(reqs, installed)
# FIXME: The editable package is uninstalled and reinstalled, including
# all its dependencies, even if the version numbers match.
assert to_uninstall == {"six", "small-fake-with-deps"}
assert len(to_install) == 1
package = list(to_install)[0]
assert package.editable
assert package.link.url == path_to_url(path_to_package)
def test_diff_with_matching_url_versions(fake_dist, from_line):
# if URL version is explicitly provided, use it to avoid reinstalling
installed = [fake_dist("example==1.0")]
reqs = [from_line("file:///example.zip#egg=example==1.0")]
to_install, to_uninstall = diff(reqs, installed)
assert to_install == set()
assert to_uninstall == set()
def test_diff_with_no_url_versions(fake_dist, from_line):
# if URL version is not provided, assume the contents have
# changed and reinstall
installed = [fake_dist("example==1.0")]
reqs = [from_line("file:///example.zip#egg=example")]
to_install, to_uninstall = diff(reqs, installed)
assert to_install == set(reqs)
assert to_uninstall == {"example"}
def test_sync_install_temporary_requirement_file(
from_line, from_editable, mocked_tmp_req_file
):
with mock.patch("piptools.sync.check_call") as check_call:
to_install = {from_line("django==1.8")}
sync(to_install, set())
check_call.assert_called_once_with(
[
sys.executable,
"-m",
"pip",
"install",
"-r",
mocked_tmp_req_file.name,
"-q",
]
)
def test_temporary_requirement_file_deleted(from_line, from_editable, mocked_tmp_file):
with mock.patch("piptools.sync.check_call"):
to_install = {from_line("django==1.8")}
with mock.patch("os.unlink") as unlink:
sync(to_install, set())
unlink.assert_called_once_with(mocked_tmp_file.name)
def test_sync_requirement_file(from_line, from_editable, mocked_tmp_req_file):
with mock.patch("piptools.sync.check_call"):
to_install = {
from_line("django==1.8"),
from_editable("git+git://fake.org/x/y.git#egg=y"),
from_line("click==4.0"),
from_editable("git+git://fake.org/i/j.git#egg=j"),
from_line("pytz==2017.2"),
}
sync(to_install, set())
expected = (
"click==4.0\n"
"django==1.8\n"
"-e git+git://fake.org/i/j.git#egg=j\n"
"pytz==2017.2\n"
"-e git+git://fake.org/x/y.git#egg=y"
)
mocked_tmp_req_file.write.assert_called_once_with(expected)
def test_sync_requirement_file_with_hashes(
from_line, from_editable, mocked_tmp_req_file
):
with mock.patch("piptools.sync.check_call"):
to_install = {
from_line(
"django==1.8",
options={
"hashes": {
"sha256": [
"6a03ce2feafdd193a0ba8a26dbd9773e"
"757d2e5d5e7933a62eac129813bd381a"
]
}
},
),
from_line(
"click==4.0",
options={
"hashes": {
"sha256": [
"9ab1d313f99b209f8f71a629f3683303"
"0c8d7c72282cf7756834baf567dca662"
]
}
},
),
from_line(
"pytz==2017.2",
options={
"hashes": {
"sha256": [
"d1d6729c85acea542367138286862712"
"9432fba9a89ecbb248d8d1c7a9f01c67",
"f5c056e8f62d45ba8215e5cb8f50dfcc"
"b198b4b9fbea8500674f3443e4689589",
]
}
},
),
}
sync(to_install, set())
expected = (
"click==4.0 \\\n"
" --hash=sha256:9ab1d313f99b209f8f71a629"
"f36833030c8d7c72282cf7756834baf567dca662\n"
"django==1.8 \\\n"
" --hash=sha256:6a03ce2feafdd193a0ba8a26"
"dbd9773e757d2e5d5e7933a62eac129813bd381a\n"
"pytz==2017.2 \\\n"
" --hash=sha256:d1d6729c85acea542367138286"
"8627129432fba9a89ecbb248d8d1c7a9f01c67 \\\n"
" --hash=sha256:f5c056e8f62d45ba8215e5cb8f"
"50dfccb198b4b9fbea8500674f3443e4689589"
)
mocked_tmp_req_file.write.assert_called_once_with(expected)
@mock.patch("piptools.sync.click.echo")
def test_sync_up_to_date(echo):
"""
Everything up-to-date should be printed.
"""
sync(set(), set(), verbose=True)
echo.assert_called_once_with("Everything up-to-date")
@mock.patch("piptools.sync.check_call")
def test_sync_verbose(check_call, from_line):
"""
The -q option has to be passed to every pip calls.
"""
sync({from_line("django==1.8")}, {from_line("click==4.0")}, verbose=True)
assert check_call.call_count == 2
for call in check_call.call_args_list:
check_call_args = call[0][0]
assert "-q" not in check_call_args
@pytest.mark.parametrize(
("to_install", "to_uninstall", "expected_message"),
[
({"django==1.8", "click==4.0"}, set(), "Would install:"),
(set(), {"django==1.8", "click==4.0"}, "Would uninstall:"),
],
)
@mock.patch("piptools.sync.click.echo")
def test_sync_dry_run(echo, from_line, to_install, to_uninstall, expected_message):
"""
Sync with --dry-run option prints what's is going to be installed/uninstalled.
"""
to_install = set(from_line(pkg) for pkg in to_install)
to_uninstall = set(from_line(pkg) for pkg in to_uninstall)
sync(to_install, to_uninstall, dry_run=True)
expected_calls = [
mock.call(expected_message),
mock.call(" django==1.8"),
mock.call(" click==4.0"),
]
echo.assert_has_calls(expected_calls, any_order=True)
@pytest.mark.parametrize(
("to_install", "to_uninstall", "expected_message"),
[
({"django==1.8", "click==4.0"}, set(), "Would install:"),
(set(), {"django==1.8", "click==4.0"}, "Would uninstall:"),
],
)
@mock.patch("piptools.sync.check_call")
@mock.patch("piptools.sync.click.confirm")
@mock.patch("piptools.sync.click.echo")
def test_sync_ask_declined(
echo, confirm, check_call, from_line, to_install, to_uninstall, expected_message
):
"""
Sync with --ask option does a dry run if the user declines
"""
confirm.return_value = False
to_install = set(from_line(pkg) for pkg in to_install)
to_uninstall = set(from_line(pkg) for pkg in to_uninstall)
sync(to_install, to_uninstall, ask=True)
expected_calls = [
mock.call(expected_message),
mock.call(" django==1.8"),
mock.call(" click==4.0"),
]
echo.assert_has_calls(expected_calls, any_order=True)
confirm.assert_called_once_with("Would you like to proceed with these changes?")
check_call.assert_not_called()
@pytest.mark.parametrize("dry_run", [True, False])
@mock.patch("piptools.sync.click.confirm")
@mock.patch("piptools.sync.check_call")
def test_sync_ask_accepted(check_call, confirm, from_line, dry_run):
"""
pip should be called as normal when the user confirms, even with dry_run
"""
confirm.return_value = True
sync(
{from_line("django==1.8")}, {from_line("click==4.0")}, ask=True, dry_run=dry_run
)
assert check_call.call_count == 2
confirm.assert_called_once_with("Would you like to proceed with these changes?")
@mock.patch("piptools.sync.check_call")
def test_sync_uninstall_pip_command(check_call):
to_uninstall = ["six", "django", "pytz", "click"]
sync(set(), to_uninstall)
check_call.assert_called_once_with(
[sys.executable, "-m", "pip", "uninstall", "-y", "-q"] + sorted(to_uninstall)
)