summaryrefslogtreecommitdiff
path: root/tests/functional/test_install_reqs.py
blob: 7d8d9a66e0614733acafcd97f355aeba154306ff (plain)
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
import os.path
import textwrap

import pytest

from tests.lib import (
    _create_test_package_with_subdirectory, pyversion, requirements_file
)
from tests.lib.local_repos import local_checkout


@pytest.mark.network
def test_requirements_file(script):
    """
    Test installing from a requirements file.

    """
    other_lib_name, other_lib_version = 'anyjson', '0.3'
    script.scratch_path.join("initools-req.txt").write(textwrap.dedent("""\
        INITools==0.2
        # and something else to test out:
        %s<=%s
        """ % (other_lib_name, other_lib_version)))
    result = script.pip(
        'install', '-r', script.scratch_path / 'initools-req.txt'
    )
    assert (
        script.site_packages / 'INITools-0.2-py%s.egg-info' %
        pyversion in result.files_created
    )
    assert script.site_packages / 'initools' in result.files_created
    assert result.files_created[script.site_packages / other_lib_name].dir
    fn = '%s-%s-py%s.egg-info' % (other_lib_name, other_lib_version, pyversion)
    assert result.files_created[script.site_packages / fn].dir


def test_schema_check_in_requirements_file(script):
    """
    Test installing from a requirements file with an invalid vcs schema..

    """
    script.scratch_path.join("file-egg-req.txt").write(
        "\n%s\n" % (
            "git://github.com/alex/django-fixture-generator.git"
            "#egg=fixture_generator"
        )
    )

    with pytest.raises(AssertionError):
        script.pip(
            "install", "-vvv", "-r", script.scratch_path / "file-egg-req.txt"
        )


def test_relative_requirements_file(script, data):
    """
    Test installing from a requirements file with a relative path. For path
    URLs, use an egg= definition.

    """
    egg_info_file = (
        script.site_packages / 'FSPkg-0.1.dev0-py%s.egg-info' % pyversion
    )
    egg_link_file = (
        script.site_packages / 'FSPkg.egg-link'
    )
    package_folder = script.site_packages / 'fspkg'

    # Compute relative install path to FSPkg from scratch path.
    full_rel_path = data.packages.join('FSPkg') - script.scratch_path
    embedded_rel_path = script.scratch_path.join(full_rel_path)

    # For each relative path, install as either editable or not using either
    # URLs with egg links or not.
    for req_path in (full_rel_path,
                     'file:' + full_rel_path + '#egg=FSPkg',
                     embedded_rel_path):
        # Regular install.
        with requirements_file(req_path + '\n',
                               script.scratch_path) as reqs_file:
            result = script.pip('install', '-vvv', '-r', reqs_file.name,
                                cwd=script.scratch_path)
            assert egg_info_file in result.files_created, str(result)
            assert package_folder in result.files_created, str(result)
            script.pip('uninstall', '-y', 'fspkg')

        # Editable install.
        with requirements_file('-e ' + req_path + '\n',
                               script.scratch_path) as reqs_file:
            result = script.pip('install', '-vvv', '-r', reqs_file.name,
                                cwd=script.scratch_path)
            assert egg_link_file in result.files_created, str(result)
            script.pip('uninstall', '-y', 'fspkg')


@pytest.mark.network
def test_multiple_requirements_files(script, tmpdir):
    """
    Test installing from multiple nested requirements files.

    """
    other_lib_name, other_lib_version = 'anyjson', '0.3'
    script.scratch_path.join("initools-req.txt").write(
        textwrap.dedent("""
            -e %s@10#egg=INITools
            -r %s-req.txt
        """) %
        (
            local_checkout(
                'svn+http://svn.colorstudy.com/INITools/trunk',
                tmpdir.join("cache"),
            ),
            other_lib_name
        ),
    )
    script.scratch_path.join("%s-req.txt" % other_lib_name).write(
        "%s<=%s" % (other_lib_name, other_lib_version)
    )
    result = script.pip(
        'install', '-r', script.scratch_path / 'initools-req.txt'
    )
    assert result.files_created[script.site_packages / other_lib_name].dir
    fn = '%s-%s-py%s.egg-info' % (other_lib_name, other_lib_version, pyversion)
    assert result.files_created[script.site_packages / fn].dir
    assert script.venv / 'src' / 'initools' in result.files_created


def test_package_in_constraints_and_dependencies(script, data):
    script.scratch_path.join("constraints.txt").write(
        "TopoRequires2==0.0.1\nTopoRequires==0.0.1"
    )
    result = script.pip('install', '--no-index', '-f',
                        data.find_links, '-c', script.scratch_path /
                        'constraints.txt', 'TopoRequires2')
    assert 'installed TopoRequires-0.0.1' in result.stdout


def test_multiple_constraints_files(script, data):
    script.scratch_path.join("outer.txt").write("-c inner.txt")
    script.scratch_path.join("inner.txt").write(
        "Upper==1.0")
    result = script.pip(
        'install', '--no-index', '-f', data.find_links, '-c',
        script.scratch_path / 'outer.txt', 'Upper')
    assert 'installed Upper-1.0' in result.stdout


def test_respect_order_in_requirements_file(script, data):
    script.scratch_path.join("frameworks-req.txt").write(textwrap.dedent("""\
        parent
        child
        simple
        """))

    result = script.pip(
        'install', '--no-index', '-f', data.find_links, '-r',
        script.scratch_path / 'frameworks-req.txt'
    )

    downloaded = [line for line in result.stdout.split('\n')
                  if 'Collecting' in line]

    assert 'parent' in downloaded[0], (
        'First download should be "parent" but was "%s"' % downloaded[0]
    )
    assert 'child' in downloaded[1], (
        'Second download should be "child" but was "%s"' % downloaded[1]
    )
    assert 'simple' in downloaded[2], (
        'Third download should be "simple" but was "%s"' % downloaded[2]
    )


def test_install_local_editable_with_extras(script, data):
    to_install = data.packages.join("LocalExtras")
    res = script.pip(
        'install', '-e', to_install + '[bar]', '--process-dependency-links',
        expect_error=False,
        expect_stderr=True,
    )
    assert script.site_packages / 'easy-install.pth' in res.files_updated, (
        str(res)
    )
    assert (
        script.site_packages / 'LocalExtras.egg-link' in res.files_created
    ), str(res)
    assert script.site_packages / 'simple' in res.files_created, str(res)


def test_install_collected_dependencies_first(script):
    result = script.pip_install_local(
        'toporequires2',
    )
    text = [line for line in result.stdout.split('\n')
            if 'Installing' in line][0]
    assert text.endswith('toporequires2')


@pytest.mark.network
def test_install_local_editable_with_subdirectory(script):
    version_pkg_path = _create_test_package_with_subdirectory(script,
                                                              'version_subdir')
    result = script.pip(
        'install', '-e',
        '%s#egg=version_subpkg&subdirectory=version_subdir' %
        ('git+file://%s' % version_pkg_path,)
    )

    result.assert_installed('version-subpkg', sub_dir='version_subdir')


@pytest.mark.network
def test_install_local_with_subdirectory(script):
    version_pkg_path = _create_test_package_with_subdirectory(script,
                                                              'version_subdir')
    result = script.pip(
        'install',
        '%s#egg=version_subpkg&subdirectory=version_subdir' %
        ('git+file://%s' % version_pkg_path,)
    )

    result.assert_installed('version_subpkg.py', editable=False)


@pytest.mark.network
def test_wheel_user_with_prefix_in_pydistutils_cfg(script, data, virtualenv):
    # Make sure wheel is available in the virtualenv
    script.pip('install', 'wheel')
    script.pip('download', 'setuptools', 'wheel', '-d', data.packages)
    virtualenv.system_site_packages = True
    homedir = script.environ["HOME"]
    script.scratch_path.join("bin").mkdir()
    with open(os.path.join(homedir, ".pydistutils.cfg"), "w") as cfg:
        cfg.write(textwrap.dedent("""
            [install]
            prefix=%s""" % script.scratch_path))

    result = script.pip('install', '--user', '--no-index', '-f',
                        data.find_links, 'requiresupper')
    # Check that we are really installing a wheel
    assert 'Running setup.py install for requiresupper' not in result.stdout
    assert 'installed requiresupper' in result.stdout


def test_install_option_in_requirements_file(script, data, virtualenv):
    """
    Test --install-option in requirements file overrides same option in cli
    """

    script.scratch_path.join("home1").mkdir()
    script.scratch_path.join("home2").mkdir()

    script.scratch_path.join("reqs.txt").write(
        textwrap.dedent(
            """simple --install-option='--home=%s'"""
            % script.scratch_path.join("home1")))

    result = script.pip(
        'install', '--no-index', '-f', data.find_links, '-r',
        script.scratch_path / 'reqs.txt',
        '--install-option=--home=%s' % script.scratch_path.join("home2"),
        expect_stderr=True)

    package_dir = script.scratch / 'home1' / 'lib' / 'python' / 'simple'
    assert package_dir in result.files_created


def test_constraints_not_installed_by_default(script, data):
    script.scratch_path.join("c.txt").write("requiresupper")
    result = script.pip(
        'install', '--no-index', '-f', data.find_links, '-c',
        script.scratch_path / 'c.txt', 'Upper')
    assert 'requiresupper' not in result.stdout


def test_constraints_only_causes_error(script, data):
    script.scratch_path.join("c.txt").write("requiresupper")
    result = script.pip(
        'install', '--no-index', '-f', data.find_links, '-c',
        script.scratch_path / 'c.txt', expect_error=True)
    assert 'installed requiresupper' not in result.stdout


def test_constraints_local_editable_install_causes_error(script, data):
    script.scratch_path.join("constraints.txt").write(
        "singlemodule==0.0.0"
    )
    to_install = data.src.join("singlemodule")
    result = script.pip(
        'install', '--no-index', '-f', data.find_links, '-c',
        script.scratch_path / 'constraints.txt', '-e',
        to_install, expect_error=True)
    assert 'Could not satisfy constraints for' in result.stderr


def test_constraints_local_install_causes_error(script, data):
    script.scratch_path.join("constraints.txt").write(
        "singlemodule==0.0.0"
    )
    to_install = data.src.join("singlemodule")
    result = script.pip(
        'install', '--no-index', '-f', data.find_links, '-c',
        script.scratch_path / 'constraints.txt',
        to_install, expect_error=True)
    assert 'Could not satisfy constraints for' in result.stderr


def test_constraints_constrain_to_local_editable(script, data):
    to_install = data.src.join("singlemodule")
    script.scratch_path.join("constraints.txt").write(
        "-e file://%s#egg=singlemodule" % to_install
    )
    result = script.pip(
        'install', '--no-index', '-f', data.find_links, '-c',
        script.scratch_path / 'constraints.txt', 'singlemodule')
    assert 'Running setup.py develop for singlemodule' in result.stdout


def test_constraints_constrain_to_local(script, data):
    to_install = data.src.join("singlemodule")
    script.scratch_path.join("constraints.txt").write(
        "file://%s#egg=singlemodule" % to_install
    )
    result = script.pip(
        'install', '--no-index', '-f', data.find_links, '-c',
        script.scratch_path / 'constraints.txt', 'singlemodule')
    assert 'Running setup.py install for singlemodule' in result.stdout


def test_constrained_to_url_install_same_url(script, data):
    to_install = data.src.join("singlemodule")
    script.scratch_path.join("constraints.txt").write(
        "file://%s#egg=singlemodule" % to_install
    )
    result = script.pip(
        'install', '--no-index', '-f', data.find_links, '-c',
        script.scratch_path / 'constraints.txt', to_install)
    assert 'Running setup.py install for singlemodule' in result.stdout


@pytest.mark.network
def test_double_install_spurious_hash_mismatch(script, tmpdir, data):
    """Make sure installing the same hashed sdist twice doesn't throw hash
    mismatch errors.

    Really, this is a test that we disable reads from the wheel cache in
    hash-checking mode. Locally, implicitly built wheels of sdists obviously
    have different hashes from the original archives. Comparing against those
    causes spurious mismatch errors.

    """
    script.pip('install', 'wheel')  # Otherwise, it won't try to build wheels.
    script.pip('download', 'setuptools', 'wheel', '-d', data.packages)
    with requirements_file('simple==1.0 --hash=sha256:393043e672415891885c9a2a'
                           '0929b1af95fb866d6ca016b42d2e6ce53619b653',
                           tmpdir) as reqs_file:
        # Install a package (and build its wheel):
        result = script.pip_install_local(
            '--find-links', data.find_links,
            '-r', reqs_file.abspath, expect_error=False)
        assert 'Successfully installed simple-1.0' in str(result)

        # Uninstall it:
        script.pip('uninstall', '-y', 'simple', expect_error=False)

        # Then install it again. We should not hit a hash mismatch, and the
        # package should install happily.
        result = script.pip_install_local(
            '--find-links', data.find_links,
            '-r', reqs_file.abspath, expect_error=False)
        assert 'Successfully installed simple-1.0' in str(result)


def test_install_with_extras_from_constraints(script, data):
    to_install = data.packages.join("LocalExtras")
    script.scratch_path.join("constraints.txt").write(
        "file://%s#egg=LocalExtras[bar]" % to_install
    )
    result = script.pip_install_local(
        '-c', script.scratch_path / 'constraints.txt', 'LocalExtras')
    assert script.site_packages / 'simple' in result.files_created


def test_install_with_extras_from_install(script, data):
    to_install = data.packages.join("LocalExtras")
    script.scratch_path.join("constraints.txt").write(
        "file://%s#egg=LocalExtras" % to_install
    )
    result = script.pip_install_local(
        '-c', script.scratch_path / 'constraints.txt', 'LocalExtras[baz]')
    assert script.site_packages / 'singlemodule.py'in result.files_created


def test_install_with_extras_joined(script, data):
    to_install = data.packages.join("LocalExtras")
    script.scratch_path.join("constraints.txt").write(
        "file://%s#egg=LocalExtras[bar]" % to_install
    )
    result = script.pip_install_local(
        '-c', script.scratch_path / 'constraints.txt', 'LocalExtras[baz]'
    )
    assert script.site_packages / 'simple' in result.files_created
    assert script.site_packages / 'singlemodule.py'in result.files_created


def test_install_with_extras_editable_joined(script, data):
    to_install = data.packages.join("LocalExtras")
    script.scratch_path.join("constraints.txt").write(
        "-e file://%s#egg=LocalExtras[bar]" % to_install
    )
    result = script.pip_install_local(
        '-c', script.scratch_path / 'constraints.txt', 'LocalExtras[baz]')
    assert script.site_packages / 'simple' in result.files_created
    assert script.site_packages / 'singlemodule.py'in result.files_created


def test_install_distribution_full_union(script, data):
    to_install = data.packages.join("LocalExtras")
    result = script.pip_install_local(
        to_install, to_install + "[bar]", to_install + "[baz]")
    assert 'Running setup.py install for LocalExtras' in result.stdout
    assert script.site_packages / 'simple' in result.files_created
    assert script.site_packages / 'singlemodule.py' in result.files_created


def test_install_distribution_duplicate_extras(script, data):
    to_install = data.packages.join("LocalExtras")
    package_name = to_install + "[bar]"
    with pytest.raises(AssertionError):
        result = script.pip_install_local(package_name, package_name)
        assert 'Double requirement given: %s' % package_name in result.stderr


def test_install_distribution_union_with_constraints(script, data):
    to_install = data.packages.join("LocalExtras")
    script.scratch_path.join("constraints.txt").write(
        "%s[bar]" % to_install)
    result = script.pip_install_local(
        '-c', script.scratch_path / 'constraints.txt', to_install + '[baz]')
    assert 'Running setup.py install for LocalExtras' in result.stdout
    assert script.site_packages / 'singlemodule.py' in result.files_created


def test_install_distribution_union_with_versions(script, data):
    to_install_001 = data.packages.join("LocalExtras")
    to_install_002 = data.packages.join("LocalExtras-0.0.2")
    result = script.pip_install_local(
        to_install_001 + "[bar]", to_install_002 + "[baz]")
    assert ("Successfully installed LocalExtras-0.0.1 simple-3.0 " +
            "singlemodule-0.0.1" in result.stdout)


@pytest.mark.xfail
def test_install_distribution_union_conflicting_extras(script, data):
    # LocalExtras requires simple==1.0, LocalExtras[bar] requires simple==2.0;
    # without a resolver, pip does not detect the conflict between simple==1.0
    # and simple==2.0. Once a resolver is added, this conflict should be
    # detected.
    to_install = data.packages.join("LocalExtras-0.0.2")
    result = script.pip_install_local(to_install, to_install + "[bar]",
                                      expect_error=True)
    assert 'installed' not in result.stdout
    assert "Conflict" in result.stderr


def test_install_unsupported_wheel_link_with_marker(script, data):
    script.scratch_path.join("with-marker.txt").write(
        textwrap.dedent("""\
            %s; %s
        """) %
        (
            'https://github.com/a/b/c/asdf-1.5.2-cp27-none-xyz.whl',
            'sys_platform == "xyz"'
        )
    )
    result = script.pip(
        'install', '-r', script.scratch_path / 'with-marker.txt',
        expect_error=False,
        expect_stderr=True,
    )

    assert ("Ignoring asdf: markers 'sys_platform == \"xyz\"' don't match "
            "your environment") in result.stderr
    assert len(result.files_created) == 0


def test_install_unsupported_wheel_file(script, data):
    # Trying to install a local wheel with an incompatible version/type
    # should fail.
    script.scratch_path.join("wheel-file.txt").write(textwrap.dedent("""\
        %s
        """ % data.packages.join("simple.dist-0.1-py1-none-invalid.whl")))
    result = script.pip(
        'install', '-r', script.scratch_path / 'wheel-file.txt',
        expect_error=True,
        expect_stderr=True,
    )
    assert ("simple.dist-0.1-py1-none-invalid.whl is not a supported " +
            "wheel on this platform" in result.stderr)
    assert len(result.files_created) == 0