-
Notifications
You must be signed in to change notification settings - Fork 54
/
test_cli.py
403 lines (308 loc) · 12.8 KB
/
test_cli.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
import os
from textwrap import dedent
from six.moves.urllib.request import pathname2url
import subprocess
import sys
import mock
from click.testing import CliRunner
import pytest
from piptools.scripts.compile import cli
from piptools.scripts.sync import cli as sync_cli
from pip._vendor.packaging.version import parse as parse_version
from pip import __version__ as pip_version
PIP_VERSION = parse_version(os.environ.get('PIP', pip_version))
fail_below_pip9 = pytest.mark.xfail(
PIP_VERSION < parse_version('9'),
reason="needs pip 9 or greater"
)
@pytest.yield_fixture
def pip_conf(tmpdir):
test_conf = dedent("""\
[global]
index-url = http://example.com
trusted-host = example.com
""")
pip_conf_file = 'pip.conf' if os.name != 'nt' else 'pip.ini'
path = (tmpdir / pip_conf_file).strpath
with open(path, 'w') as f:
f.write(test_conf)
old_value = os.environ.get('PIP_CONFIG_FILE')
try:
os.environ['PIP_CONFIG_FILE'] = path
yield path
finally:
if old_value is not None:
os.environ['PIP_CONFIG_FILE'] = old_value
else:
del os.environ['PIP_CONFIG_FILE']
os.remove(path)
def test_default_pip_conf_read(pip_conf):
assert os.path.exists(pip_conf)
runner = CliRunner()
with runner.isolated_filesystem():
# preconditions
with open('requirements.in', 'w'):
pass
out = runner.invoke(cli, ['-v'])
# check that we have our index-url as specified in pip.conf
assert 'Using indexes:\n http://example.com' in out.output
assert '--index-url http://example.com' in out.output
def test_command_line_overrides_pip_conf(pip_conf):
assert os.path.exists(pip_conf)
runner = CliRunner()
with runner.isolated_filesystem():
# preconditions
with open('requirements.in', 'w'):
pass
out = runner.invoke(cli, ['-v', '-i', 'http://override.com'])
# check that we have our index-url as specified in pip.conf
assert 'Using indexes:\n http://override.com' in out.output
def test_command_line_setuptools_read(pip_conf):
runner = CliRunner()
with runner.isolated_filesystem():
package = open('setup.py', 'w')
package.write(dedent("""\
from setuptools import setup
setup(install_requires=[])
"""))
package.close()
out = runner.invoke(cli)
# check that pip-compile generated a configuration
assert 'This file is autogenerated by pip-compile' in out.output
def test_find_links_option(pip_conf):
assert os.path.exists(pip_conf)
runner = CliRunner()
with runner.isolated_filesystem():
with open('requirements.in', 'w'):
pass
out = runner.invoke(cli, ['-v', '-f', './libs1', '-f', './libs2'])
# Check that find-links has been passed to pip
assert 'Configuration:\n -f ./libs1\n -f ./libs2' in out.output
def test_extra_index_option(pip_conf):
assert os.path.exists(pip_conf)
runner = CliRunner()
with runner.isolated_filesystem():
with open('requirements.in', 'w'):
pass
out = runner.invoke(cli, ['-v',
'--extra-index-url', 'http://extraindex1.com',
'--extra-index-url', 'http://extraindex2.com'])
assert ('Using indexes:\n'
' http://example.com\n'
' http://extraindex1.com\n'
' http://extraindex2.com' in out.output)
assert ('--index-url http://example.com\n'
'--extra-index-url http://extraindex1.com\n'
'--extra-index-url http://extraindex2.com' in out.output)
def test_trusted_host(pip_conf):
assert os.path.exists(pip_conf)
runner = CliRunner()
with runner.isolated_filesystem():
with open('requirements.in', 'w'):
pass
out = runner.invoke(cli, ['-v',
'--trusted-host', 'example.com',
'--trusted-host', 'example2.com'])
assert ('--trusted-host example.com\n'
'--trusted-host example2.com\n' in out.output)
def test_trusted_host_no_emit(pip_conf):
assert os.path.exists(pip_conf)
runner = CliRunner()
with runner.isolated_filesystem():
with open('requirements.in', 'w'):
pass
out = runner.invoke(cli, ['-v',
'--trusted-host', 'example.com',
'--no-emit-trusted-host'])
assert '--trusted-host example.com' not in out.output
assert '--no-emit-trusted-host' in out.output
def test_realistic_complex_sub_dependencies(tmpdir):
# make a temporary wheel of a fake package
subprocess.check_output(['pip', 'wheel',
'--no-deps',
'-w', str(tmpdir),
os.path.join('.', 'tests', 'test_data', 'fake_package', '.')])
runner = CliRunner()
with runner.isolated_filesystem():
with open('requirements.in', 'w') as req_in:
req_in.write('fake_with_deps') # require fake package
out = runner.invoke(cli, ['-v',
'-n', '--rebuild',
'-f', str(tmpdir)])
assert out.exit_code == 0
def _invoke(command):
"""Invoke sub-process."""
try:
output = subprocess.check_output(
command,
stderr=subprocess.STDOUT,
)
status = 0
except subprocess.CalledProcessError as error:
output = error.output
status = error.returncode
return status, output
def test_run_as_module_compile(tmpdir):
"""piptools can be run as ``python -m piptools ...``."""
status, output = _invoke([
sys.executable, '-m', 'piptools', 'compile', '--help',
])
# Should have run pip-compile successfully.
output = output.decode('utf-8')
assert output.startswith('Usage:')
assert 'Compiles requirements.txt from requirements.in' in output
assert status == 0
def test_run_as_module_sync():
"""piptools can be run as ``python -m piptools ...``."""
status, output = _invoke([
sys.executable, '-m', 'piptools', 'sync', '--help',
])
# Should have run pip-compile successfully.
output = output.decode('utf-8')
assert output.startswith('Usage:')
assert 'Synchronize virtual environment with' in output
assert status == 0
def test_sync_quiet(tmpdir):
"""sync command can be run with `--quiet` or `-q` flag."""
runner = CliRunner()
with runner.isolated_filesystem():
with open('requirements.txt', 'w') as req_in:
req_in.write('six==1.10.0')
with mock.patch('piptools.sync.check_call') as check_call:
out = runner.invoke(sync_cli, ['-q'])
assert out.output == ''
assert out.exit_code == 0
# for every call to pip ensure the `-q` flag is set
for call in check_call.call_args_list:
assert '-q' in call[0][0]
def test_editable_package(tmpdir):
""" piptools can compile an editable """
fake_package_dir = os.path.join(os.path.split(__file__)[0], 'test_data', 'small_fake_package')
fake_package_dir = 'file:' + pathname2url(fake_package_dir)
runner = CliRunner()
with runner.isolated_filesystem():
with open('requirements.in', 'w') as req_in:
req_in.write('-e ' + fake_package_dir) # require editable fake package
out = runner.invoke(cli, ['-n'])
assert out.exit_code == 0
assert fake_package_dir in out.output
assert 'six==1.10.0' in out.output
def test_editable_package_vcs(tmpdir):
vcs_package = (
'git+git://github.com/pytest-dev/pytest-django'
'@21492afc88a19d4ca01cd0ac392a5325b14f95c7'
'#egg=pytest-django'
)
runner = CliRunner()
with runner.isolated_filesystem():
with open('requirements.in', 'w') as req_in:
req_in.write('-e ' + vcs_package)
out = runner.invoke(cli, ['-n',
'--rebuild'])
print(out.output)
assert out.exit_code == 0
assert vcs_package in out.output
assert 'pytest' in out.output # dependency of pytest-django
def test_locally_available_editable_package_is_not_archived_in_cache_dir(tmpdir):
""" piptools will not create an archive for a locally available editable requirement """
cache_dir = tmpdir.mkdir('cache_dir')
fake_package_dir = os.path.join(os.path.split(__file__)[0], 'test_data', 'small_fake_package')
fake_package_dir = 'file:' + pathname2url(fake_package_dir)
with mock.patch('piptools.repositories.pypi.CACHE_DIR', new=str(cache_dir)):
runner = CliRunner()
with runner.isolated_filesystem():
with open('requirements.in', 'w') as req_in:
req_in.write('-e ' + fake_package_dir) # require editable fake package
out = runner.invoke(cli, ['-n'])
assert out.exit_code == 0
assert fake_package_dir in out.output
assert 'six==1.10.0' in out.output
# we should not find any archived file in {cache_dir}/pkgs
assert not os.listdir(os.path.join(str(cache_dir), 'pkgs'))
def test_input_file_without_extension(tmpdir):
"""
piptools can compile a file without an extension,
and add .txt as the defaut output file extension.
"""
runner = CliRunner()
with runner.isolated_filesystem():
with open('requirements', 'w') as req_in:
req_in.write('six==1.10.0')
out = runner.invoke(cli, ['-n', 'requirements'])
assert out.exit_code == 0
assert '--output-file requirements.txt' in out.output
assert 'six==1.10.0' in out.output
def test_upgrade_packages_option(tmpdir):
"""
piptools respects --upgrade-package/-P inline list.
"""
fake_package_dir = os.path.join(os.path.split(__file__)[0], 'test_data', 'minimal_wheels')
runner = CliRunner()
with runner.isolated_filesystem():
with open('requirements.in', 'w') as req_in:
req_in.write('small-fake-a\nsmall-fake-b')
with open('requirements.txt', 'w') as req_in:
req_in.write('small-fake-a==0.1\nsmall-fake-b==0.1')
out = runner.invoke(cli, [
'-P', 'small_fake_b',
'-f', fake_package_dir,
])
assert out.exit_code == 0
assert 'small-fake-a==0.1' in out.output
assert 'small-fake-b==0.2' in out.output
def test_generate_hashes_with_editable():
small_fake_package_dir = os.path.join(
os.path.split(__file__)[0], 'test_data', 'small_fake_package')
small_fake_package_url = 'file:' + pathname2url(small_fake_package_dir)
runner = CliRunner()
with runner.isolated_filesystem():
with open('requirements.in', 'w') as fp:
fp.write('-e {}\n'.format(small_fake_package_url))
fp.write('pytz==2017.2\n')
out = runner.invoke(cli, ['--generate-hashes'])
expected = (
'#\n'
'# This file is autogenerated by pip-compile\n'
'# To update, run:\n'
'#\n'
'# pip-compile --generate-hashes --output-file requirements.txt requirements.in\n'
'#\n'
'-e {}\n'
'pytz==2017.2 \\\n'
' --hash=sha256:d1d6729c85acea5423671382868627129432fba9a89ecbb248d8d1c7a9f01c67 \\\n'
' --hash=sha256:f5c056e8f62d45ba8215e5cb8f50dfccb198b4b9fbea8500674f3443e4689589\n'
).format(small_fake_package_url)
assert out.exit_code == 0
assert expected in out.output
@fail_below_pip9
def test_filter_pip_markes():
"""
Check that pip-compile works with pip environment markers (PEP496)
"""
runner = CliRunner()
with runner.isolated_filesystem():
with open('requirements', 'w') as req_in:
req_in.write(
"six==1.10.0\n"
"unknown_package==0.1; python_version == '1'")
out = runner.invoke(cli, ['-n', 'requirements'])
assert out.exit_code == 0
assert '--output-file requirements.txt' in out.output
assert 'six==1.10.0' in out.output
assert 'unknown_package' not in out.output
def test_no_candidates():
runner = CliRunner()
with runner.isolated_filesystem():
with open('requirements', 'w') as req_in:
req_in.write('six>1.0b0,<1.0b0')
out = runner.invoke(cli, ['-n', 'requirements'])
assert out.exit_code == 2
assert 'Skipped pre-versions:' in out.output
def test_no_candidates_pre():
runner = CliRunner()
with runner.isolated_filesystem():
with open('requirements', 'w') as req_in:
req_in.write('six>1.0b0,<1.0b0')
out = runner.invoke(cli, ['-n', 'requirements', '--pre'])
assert out.exit_code == 2
assert 'Tried pre-versions:' in out.output