-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathtest_install_wheel.py
637 lines (542 loc) · 20.3 KB
/
test_install_wheel.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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
# -*- coding: utf-8 -*-
import distutils
import glob
import os
import shutil
import pytest
from tests.lib import create_basic_wheel_for_package, skip_if_python2
from tests.lib.path import Path
from tests.lib.wheel import make_wheel
# assert_installed expects a package subdirectory, so give it to them
def make_wheel_with_file(name, version, **kwargs):
extra_files = kwargs.setdefault("extra_files", {})
extra_files["{}/__init__.py".format(name)] = "# example"
return make_wheel(name=name, version=version, **kwargs)
def test_install_from_future_wheel_version(script, tmpdir):
"""
Test installing a wheel with a WHEEL metadata version that is:
- a major version ahead of what we expect (not ok), and
- a minor version ahead of what we expect (ok)
"""
from tests.lib import TestFailure
package = make_wheel_with_file(
name="futurewheel",
version="3.0",
wheel_metadata_updates={"Wheel-Version": "3.0"},
).save_to_dir(tmpdir)
result = script.pip('install', package, '--no-index', expect_error=True)
with pytest.raises(TestFailure):
result.assert_installed('futurewheel', without_egg_link=True,
editable=False)
package = make_wheel_with_file(
name="futurewheel",
version="1.9",
wheel_metadata_updates={"Wheel-Version": "1.9"},
).save_to_dir(tmpdir)
result = script.pip(
'install', package, '--no-index', expect_stderr=True
)
result.assert_installed('futurewheel', without_egg_link=True,
editable=False)
def test_install_from_broken_wheel(script, data):
"""
Test that installing a broken wheel fails properly
"""
from tests.lib import TestFailure
package = data.packages.joinpath("brokenwheel-1.0-py2.py3-none-any.whl")
result = script.pip('install', package, '--no-index', expect_error=True)
with pytest.raises(TestFailure):
result.assert_installed('futurewheel', without_egg_link=True,
editable=False)
def test_basic_install_from_wheel(script, shared_data, tmpdir):
"""
Test installing from a wheel (that has a script)
"""
shutil.copy(
shared_data.packages / "has.script-1.0-py2.py3-none-any.whl", tmpdir
)
result = script.pip(
'install', 'has.script==1.0', '--no-index',
'--find-links', tmpdir,
)
dist_info_folder = script.site_packages / 'has.script-1.0.dist-info'
result.did_create(dist_info_folder)
script_file = script.bin / 'script.py'
result.did_create(script_file)
def test_basic_install_from_wheel_with_extras(script, shared_data, tmpdir):
"""
Test installing from a wheel with extras.
"""
shutil.copy(
shared_data.packages / "complex_dist-0.1-py2.py3-none-any.whl", tmpdir
)
shutil.copy(
shared_data.packages / "simple.dist-0.1-py2.py3-none-any.whl", tmpdir
)
result = script.pip(
'install', 'complex-dist[simple]', '--no-index',
'--find-links', tmpdir,
)
dist_info_folder = script.site_packages / 'complex_dist-0.1.dist-info'
result.did_create(dist_info_folder)
dist_info_folder = script.site_packages / 'simple.dist-0.1.dist-info'
result.did_create(dist_info_folder)
def test_basic_install_from_wheel_file(script, data):
"""
Test installing directly from a wheel file.
"""
package = data.packages.joinpath("simple.dist-0.1-py2.py3-none-any.whl")
result = script.pip('install', package, '--no-index')
dist_info_folder = script.site_packages / 'simple.dist-0.1.dist-info'
result.did_create(dist_info_folder)
installer = dist_info_folder / 'INSTALLER'
result.did_create(installer)
with open(script.base_path / installer, 'rb') as installer_file:
installer_details = installer_file.read()
assert installer_details == b'pip\n'
installer_temp = dist_info_folder / 'INSTALLER.pip'
result.did_not_create(installer_temp)
# Installation seems to work, but scripttest fails to check.
# I really don't care now since we're desupporting it soon anyway.
@skip_if_python2
def test_basic_install_from_unicode_wheel(script, data):
"""
Test installing from a wheel (that has a script)
"""
make_wheel(
'unicode_package',
'1.0',
extra_files={
'வணக்கம்/__init__.py': b'',
'வணக்கம்/નમસ્તે.py': b'',
},
).save_to_dir(script.scratch_path)
result = script.pip(
'install', 'unicode_package==1.0', '--no-index',
'--find-links', script.scratch_path,
)
dist_info_folder = script.site_packages / 'unicode_package-1.0.dist-info'
result.did_create(dist_info_folder)
file1 = script.site_packages.joinpath('வணக்கம்', '__init__.py')
result.did_create(file1)
file2 = script.site_packages.joinpath('வணக்கம்', 'નમસ્તે.py')
result.did_create(file2)
def test_install_from_wheel_with_headers(script, data):
"""
Test installing from a wheel file with headers
"""
package = data.packages.joinpath("headers.dist-0.1-py2.py3-none-any.whl")
result = script.pip('install', package, '--no-index')
dist_info_folder = script.site_packages / 'headers.dist-0.1.dist-info'
result.did_create(dist_info_folder)
def test_install_wheel_with_target(script, shared_data, with_wheel, tmpdir):
"""
Test installing a wheel using pip install --target
"""
shutil.copy(
shared_data.packages / "simple.dist-0.1-py2.py3-none-any.whl", tmpdir
)
target_dir = script.scratch_path / 'target'
result = script.pip(
'install', 'simple.dist==0.1', '-t', target_dir,
'--no-index', '--find-links', tmpdir,
)
result.did_create(Path('scratch') / 'target' / 'simpledist')
def test_install_wheel_with_target_and_data_files(script, data, with_wheel):
"""
Test for issue #4092. It will be checked that a data_files specification in
setup.py is handled correctly when a wheel is installed with the --target
option.
The setup() for the wheel 'prjwithdatafile-1.0-py2.py3-none-any.whl' is as
follows ::
setup(
name='prjwithdatafile',
version='1.0',
packages=['prjwithdatafile'],
data_files=[
(r'packages1', ['prjwithdatafile/README.txt']),
(r'packages2', ['prjwithdatafile/README.txt'])
]
)
"""
target_dir = script.scratch_path / 'prjwithdatafile'
package = data.packages.joinpath(
"prjwithdatafile-1.0-py2.py3-none-any.whl"
)
result = script.pip('install', package,
'-t', target_dir,
'--no-index')
project_path = Path('scratch') / 'prjwithdatafile'
result.did_create(project_path / 'packages1' / 'README.txt')
result.did_create(project_path / 'packages2' / 'README.txt')
result.did_not_create(project_path / 'lib' / 'python')
def test_install_wheel_with_root(script, shared_data, tmpdir):
"""
Test installing a wheel using pip install --root
"""
root_dir = script.scratch_path / 'root'
shutil.copy(
shared_data.packages / "simple.dist-0.1-py2.py3-none-any.whl", tmpdir
)
result = script.pip(
'install', 'simple.dist==0.1', '--root', root_dir,
'--no-index', '--find-links', tmpdir,
)
result.did_create(Path('scratch') / 'root')
def test_install_wheel_with_prefix(script, shared_data, tmpdir):
"""
Test installing a wheel using pip install --prefix
"""
prefix_dir = script.scratch_path / 'prefix'
shutil.copy(
shared_data.packages / "simple.dist-0.1-py2.py3-none-any.whl", tmpdir
)
result = script.pip(
'install', 'simple.dist==0.1', '--prefix', prefix_dir,
'--no-index', '--find-links', tmpdir,
)
lib = distutils.sysconfig.get_python_lib(prefix=Path('scratch') / 'prefix')
result.did_create(lib)
def test_install_from_wheel_installs_deps(script, data, tmpdir):
"""
Test can install dependencies of wheels
"""
# 'requires_source' depends on the 'source' project
package = data.packages.joinpath(
"requires_source-1.0-py2.py3-none-any.whl"
)
shutil.copy(data.packages / "source-1.0.tar.gz", tmpdir)
result = script.pip(
'install', '--no-index', '--find-links', tmpdir, package,
)
result.assert_installed('source', editable=False)
def test_install_from_wheel_no_deps(script, data, tmpdir):
"""
Test --no-deps works with wheel installs
"""
# 'requires_source' depends on the 'source' project
package = data.packages.joinpath(
"requires_source-1.0-py2.py3-none-any.whl"
)
shutil.copy(data.packages / "source-1.0.tar.gz", tmpdir)
result = script.pip(
'install', '--no-index', '--find-links', tmpdir, '--no-deps',
package,
)
pkg_folder = script.site_packages / 'source'
result.did_not_create(pkg_folder)
def test_wheel_record_lines_in_deterministic_order(script, data):
to_install = data.packages.joinpath("simplewheel-1.0-py2.py3-none-any.whl")
result = script.pip('install', to_install)
dist_info_folder = script.site_packages / 'simplewheel-1.0.dist-info'
record_path = dist_info_folder / 'RECORD'
result.did_create(dist_info_folder)
result.did_create(record_path)
record_path = result.files_created[record_path].full
record_lines = [
p for p in Path(record_path).read_text().split('\n') if p
]
assert record_lines == sorted(record_lines)
@pytest.mark.incompatible_with_test_venv
def test_install_user_wheel(script, shared_data, with_wheel, tmpdir):
"""
Test user install from wheel (that has a script)
"""
shutil.copy(
shared_data.packages / "has.script-1.0-py2.py3-none-any.whl", tmpdir
)
result = script.pip(
'install', 'has.script==1.0', '--user', '--no-index',
'--find-links', tmpdir,
)
egg_info_folder = script.user_site / 'has.script-1.0.dist-info'
result.did_create(egg_info_folder)
script_file = script.user_bin / 'script.py'
result.did_create(script_file)
def test_install_from_wheel_gen_entrypoint(script, shared_data, tmpdir):
"""
Test installing scripts (entry points are generated)
"""
shutil.copy(
shared_data.packages / "script.wheel1a-0.1-py2.py3-none-any.whl",
tmpdir,
)
result = script.pip(
'install', 'script.wheel1a==0.1', '--no-index',
'--find-links', tmpdir,
)
if os.name == 'nt':
wrapper_file = script.bin / 't1.exe'
else:
wrapper_file = script.bin / 't1'
result.did_create(wrapper_file)
if os.name != "nt":
assert bool(os.access(script.base_path / wrapper_file, os.X_OK))
def test_install_from_wheel_gen_uppercase_entrypoint(
script, shared_data, tmpdir
):
"""
Test installing scripts with uppercase letters in entry point names
"""
shutil.copy(
shared_data.packages /
"console_scripts_uppercase-1.0-py2.py3-none-any.whl",
tmpdir,
)
result = script.pip(
'install', 'console-scripts-uppercase==1.0', '--no-index',
'--find-links', tmpdir,
)
if os.name == 'nt':
# Case probably doesn't make any difference on NT
wrapper_file = script.bin / 'cmdName.exe'
else:
wrapper_file = script.bin / 'cmdName'
result.did_create(wrapper_file)
if os.name != "nt":
assert bool(os.access(script.base_path / wrapper_file, os.X_OK))
# pkg_resources.EntryPoint() does not parse unicode correctly on Python 2.
@skip_if_python2
def test_install_from_wheel_gen_unicode_entrypoint(script):
make_wheel(
"script_wheel_unicode",
"1.0",
console_scripts=["進入點 = 模組:函式"],
).save_to_dir(script.scratch_path)
result = script.pip(
"install",
"--no-index",
"--find-links",
script.scratch_path,
"script_wheel_unicode",
)
if os.name == "nt":
result.did_create(script.bin.joinpath("進入點.exe"))
else:
result.did_create(script.bin.joinpath("進入點"))
def test_install_from_wheel_with_legacy(script, shared_data, tmpdir):
"""
Test installing scripts (legacy scripts are preserved)
"""
shutil.copy(
shared_data.packages / "script.wheel2a-0.1-py2.py3-none-any.whl",
tmpdir,
)
result = script.pip(
'install', 'script.wheel2a==0.1', '--no-index',
'--find-links', tmpdir,
)
legacy_file1 = script.bin / 'testscript1.bat'
legacy_file2 = script.bin / 'testscript2'
result.did_create(legacy_file1)
result.did_create(legacy_file2)
def test_install_from_wheel_no_setuptools_entrypoint(
script, shared_data, tmpdir
):
"""
Test that when we generate scripts, any existing setuptools wrappers in
the wheel are skipped.
"""
shutil.copy(
shared_data.packages / "script.wheel1-0.1-py2.py3-none-any.whl", tmpdir
)
result = script.pip(
'install', 'script.wheel1==0.1', '--no-index',
'--find-links', tmpdir,
)
if os.name == 'nt':
wrapper_file = script.bin / 't1.exe'
else:
wrapper_file = script.bin / 't1'
wrapper_helper = script.bin / 't1-script.py'
# The wheel has t1.exe and t1-script.py. We will be generating t1 or
# t1.exe depending on the platform. So we check that the correct wrapper
# is present and that the -script.py helper has been skipped. We can't
# easily test that the wrapper from the wheel has been skipped /
# overwritten without getting very platform-dependent, so omit that.
result.did_create(wrapper_file)
result.did_not_create(wrapper_helper)
def test_skipping_setuptools_doesnt_skip_legacy(script, shared_data, tmpdir):
"""
Test installing scripts (legacy scripts are preserved even when we skip
setuptools wrappers)
"""
shutil.copy(
shared_data.packages / "script.wheel2-0.1-py2.py3-none-any.whl", tmpdir
)
result = script.pip(
'install', 'script.wheel2==0.1', '--no-index',
'--find-links', tmpdir,
)
legacy_file1 = script.bin / 'testscript1.bat'
legacy_file2 = script.bin / 'testscript2'
wrapper_helper = script.bin / 't1-script.py'
result.did_create(legacy_file1)
result.did_create(legacy_file2)
result.did_not_create(wrapper_helper)
def test_install_from_wheel_gui_entrypoint(script, shared_data, tmpdir):
"""
Test installing scripts (gui entry points are generated)
"""
shutil.copy(
shared_data.packages / "script.wheel3-0.1-py2.py3-none-any.whl", tmpdir
)
result = script.pip(
'install', 'script.wheel3==0.1', '--no-index',
'--find-links', tmpdir,
)
if os.name == 'nt':
wrapper_file = script.bin / 't1.exe'
else:
wrapper_file = script.bin / 't1'
result.did_create(wrapper_file)
def test_wheel_compiles_pyc(script, shared_data, tmpdir):
"""
Test installing from wheel with --compile on
"""
shutil.copy(
shared_data.packages / "simple.dist-0.1-py2.py3-none-any.whl", tmpdir
)
script.pip(
"install", "--compile", "simple.dist==0.1", "--no-index",
"--find-links", tmpdir,
)
# There are many locations for the __init__.pyc file so attempt to find
# any of them
exists = [
os.path.exists(script.site_packages_path / "simpledist/__init__.pyc"),
]
exists += glob.glob(
script.site_packages_path / "simpledist/__pycache__/__init__*.pyc"
)
assert any(exists)
def test_wheel_no_compiles_pyc(script, shared_data, tmpdir):
"""
Test installing from wheel with --compile on
"""
shutil.copy(
shared_data.packages / "simple.dist-0.1-py2.py3-none-any.whl", tmpdir
)
script.pip(
"install", "--no-compile", "simple.dist==0.1", "--no-index",
"--find-links", tmpdir,
)
# There are many locations for the __init__.pyc file so attempt to find
# any of them
exists = [
os.path.exists(script.site_packages_path / "simpledist/__init__.pyc"),
]
exists += glob.glob(
script.site_packages_path / "simpledist/__pycache__/__init__*.pyc"
)
assert not any(exists)
def test_install_from_wheel_uninstalls_old_version(script, data):
# regression test for https://github.com/pypa/pip/issues/1825
package = data.packages.joinpath("simplewheel-1.0-py2.py3-none-any.whl")
result = script.pip('install', package, '--no-index')
package = data.packages.joinpath("simplewheel-2.0-py2.py3-none-any.whl")
result = script.pip('install', package, '--no-index')
dist_info_folder = script.site_packages / 'simplewheel-2.0.dist-info'
result.did_create(dist_info_folder)
dist_info_folder = script.site_packages / 'simplewheel-1.0.dist-info'
result.did_not_create(dist_info_folder)
def test_wheel_compile_syntax_error(script, data):
package = data.packages.joinpath("compilewheel-1.0-py2.py3-none-any.whl")
result = script.pip('install', '--compile', package, '--no-index')
assert 'yield from' not in result.stdout
assert 'SyntaxError: ' not in result.stdout
def test_wheel_install_with_no_cache_dir(script, tmpdir, data):
"""Check wheel installations work, even with no cache.
"""
package = data.packages.joinpath("simple.dist-0.1-py2.py3-none-any.whl")
result = script.pip('install', '--no-cache-dir', '--no-index', package)
result.assert_installed('simpledist', editable=False)
def test_wheel_install_fails_with_extra_dist_info(script):
package = create_basic_wheel_for_package(
script,
"simple",
"0.1.0",
extra_files={
"unrelated-2.0.0.dist-info/WHEEL": "Wheel-Version: 1.0",
"unrelated-2.0.0.dist-info/METADATA": (
"Name: unrelated\nVersion: 2.0.0\n"
),
},
)
result = script.pip(
"install", "--no-cache-dir", "--no-index", package, expect_error=True
)
assert "multiple .dist-info directories" in result.stderr
def test_wheel_install_fails_with_unrelated_dist_info(script):
package = create_basic_wheel_for_package(script, "simple", "0.1.0")
new_name = "unrelated-2.0.0-py2.py3-none-any.whl"
new_package = os.path.join(os.path.dirname(package), new_name)
shutil.move(package, new_package)
result = script.pip(
"install",
"--no-cache-dir",
"--no-index",
new_package,
expect_error=True,
)
assert (
"'simple-0.1.0.dist-info' does not start with 'unrelated'"
in result.stderr
)
def test_wheel_installs_ok_with_nested_dist_info(script):
package = create_basic_wheel_for_package(
script,
"simple",
"0.1.0",
extra_files={
"subdir/unrelated-2.0.0.dist-info/WHEEL": "Wheel-Version: 1.0",
"subdir/unrelated-2.0.0.dist-info/METADATA": (
"Name: unrelated\nVersion: 2.0.0\n"
),
},
)
script.pip(
"install", "--no-cache-dir", "--no-index", package
)
def test_wheel_installs_ok_with_badly_encoded_irrelevant_dist_info_file(
script
):
package = create_basic_wheel_for_package(
script,
"simple",
"0.1.0",
extra_files={
"simple-0.1.0.dist-info/AUTHORS.txt": b"\xff"
},
)
script.pip(
"install", "--no-cache-dir", "--no-index", package
)
# Metadata is not decoded on Python 2.
@skip_if_python2
def test_wheel_install_fails_with_badly_encoded_metadata(script):
package = create_basic_wheel_for_package(
script,
"simple",
"0.1.0",
extra_files={
"simple-0.1.0.dist-info/METADATA": b"\xff"
},
)
result = script.pip(
"install", "--no-cache-dir", "--no-index", package, expect_error=True
)
assert "Error decoding metadata for" in result.stderr
assert "simple-0.1.0-py2.py3-none-any.whl" in result.stderr
assert "METADATA" in result.stderr
@pytest.mark.parametrize(
'package_name',
['simple-package', 'simple_package'],
)
def test_correct_package_name_while_creating_wheel_bug(script, package_name):
"""Check that the package name is correctly named while creating
a .whl file with a given format
"""
package = create_basic_wheel_for_package(script, package_name, '1.0')
wheel_name = os.path.basename(package)
assert wheel_name == 'simple_package-1.0-py2.py3-none-any.whl'