summaryrefslogtreecommitdiff
path: root/tests/functional/test_install_config.py
blob: bf25166852a46d545d91b846d64edc138d3292b4 (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
import os
import tempfile
import textwrap

import pytest


def test_options_from_env_vars(script):
    """
    Test if ConfigOptionParser reads env vars (e.g. not using PyPI here)

    """
    script.environ['PIP_NO_INDEX'] = '1'
    result = script.pip('install', '-vvv', 'INITools', expect_error=True)
    assert "Ignoring indexes:" in result.stdout, str(result)
    assert (
        "DistributionNotFound: No matching distribution found for INITools"
        in result.stdout
    )


def test_command_line_options_override_env_vars(script, virtualenv):
    """
    Test that command line options override environmental variables.

    """
    script.environ['PIP_INDEX_URL'] = 'https://example.com/simple/'
    result = script.pip('install', '-vvv', 'INITools', expect_error=True)
    assert (
        "Getting page https://example.com/simple/initools"
        in result.stdout
    )
    virtualenv.clear()
    result = script.pip(
        'install', '-vvv', '--index-url', 'https://download.zope.org/ppix',
        'INITools',
        expect_error=True,
    )
    assert "example.com" not in result.stdout
    assert "Getting page https://download.zope.org/ppix" in result.stdout


@pytest.mark.network
def test_env_vars_override_config_file(script, virtualenv):
    """
    Test that environmental variables override settings in config files.

    """
    fd, config_file = tempfile.mkstemp('-pip.cfg', 'test-')
    try:
        _test_env_vars_override_config_file(script, virtualenv, config_file)
    finally:
        # `os.close` is a workaround for a bug in subprocess
        # https://bugs.python.org/issue3210
        os.close(fd)
        os.remove(config_file)


def _test_env_vars_override_config_file(script, virtualenv, config_file):
    # set this to make pip load it
    script.environ['PIP_CONFIG_FILE'] = config_file
    # It's important that we test this particular config value ('no-index')
    # because there is/was a bug which only shows up in cases in which
    # 'config-item' and 'config_item' hash to the same value modulo the size
    # of the config dictionary.
    (script.scratch_path / config_file).write(textwrap.dedent("""\
        [global]
        no-index = 1
        """))
    result = script.pip('install', '-vvv', 'INITools', expect_error=True)
    assert (
        "DistributionNotFound: No matching distribution found for INITools"
        in result.stdout
    )
    script.environ['PIP_NO_INDEX'] = '0'
    virtualenv.clear()
    result = script.pip('install', '-vvv', 'INITools', expect_error=True)
    assert "Successfully installed INITools" in result.stdout


@pytest.mark.network
def test_command_line_append_flags(script, virtualenv, data):
    """
    Test command line flags that append to defaults set by environmental
    variables.

    """
    script.environ['PIP_FIND_LINKS'] = 'https://test.pypi.org'
    result = script.pip(
        'install', '-vvv', 'INITools', '--trusted-host',
        'test.pypi.org',
        expect_error=True,
    )
    assert (
        "Analyzing links from page https://test.pypi.org"
        in result.stdout
    ), str(result)
    virtualenv.clear()
    result = script.pip(
        'install', '-vvv', '--find-links', data.find_links, 'INITools',
        '--trusted-host', 'test.pypi.org',
        expect_error=True,
    )
    assert (
        "Analyzing links from page https://test.pypi.org"
        in result.stdout
    )
    assert "Skipping link %s" % data.find_links in result.stdout


@pytest.mark.network
def test_command_line_appends_correctly(script, data):
    """
    Test multiple appending options set by environmental variables.

    """
    script.environ['PIP_FIND_LINKS'] = (
        'https://test.pypi.org %s' % data.find_links
    )
    result = script.pip(
        'install', '-vvv', 'INITools', '--trusted-host',
        'test.pypi.org',
        expect_error=True,
    )

    assert (
        "Analyzing links from page https://test.pypi.org"
        in result.stdout
    ), result.stdout
    assert "Skipping link %s" % data.find_links in result.stdout


def test_config_file_override_stack(script, virtualenv):
    """
    Test config files (global, overriding a global config with a
    local, overriding all with a command line flag).

    """
    fd, config_file = tempfile.mkstemp('-pip.cfg', 'test-')
    try:
        _test_config_file_override_stack(script, virtualenv, config_file)
    finally:
        # `os.close` is a workaround for a bug in subprocess
        # https://bugs.python.org/issue3210
        os.close(fd)
        os.remove(config_file)


def _test_config_file_override_stack(script, virtualenv, config_file):
    # set this to make pip load it
    script.environ['PIP_CONFIG_FILE'] = config_file
    (script.scratch_path / config_file).write(textwrap.dedent("""\
        [global]
        index-url = https://download.zope.org/ppix
        """))
    result = script.pip('install', '-vvv', 'INITools', expect_error=True)
    assert (
        "Getting page https://download.zope.org/ppix/initools" in result.stdout
    )
    virtualenv.clear()
    (script.scratch_path / config_file).write(textwrap.dedent("""\
        [global]
        index-url = https://download.zope.org/ppix
        [install]
        index-url = https://pypi.gocept.com/
        """))
    result = script.pip('install', '-vvv', 'INITools', expect_error=True)
    assert "Getting page https://pypi.gocept.com/initools" in result.stdout
    result = script.pip(
        'install', '-vvv', '--index-url', 'https://pypi.org/simple/',
        'INITools',
        expect_error=True,
    )
    assert (
        "Getting page http://download.zope.org/ppix/INITools"
        not in result.stdout
    )
    assert "Getting page https://pypi.gocept.com/INITools" not in result.stdout
    assert (
        "Getting page https://pypi.org/simple/initools" in result.stdout
    )


def test_options_from_venv_config(script, virtualenv):
    """
    Test if ConfigOptionParser reads a virtualenv-local config file

    """
    from pip._internal.locations import config_basename
    conf = "[global]\nno-index = true"
    ini = virtualenv.location / config_basename
    with open(ini, 'w') as f:
        f.write(conf)
    result = script.pip('install', '-vvv', 'INITools', expect_error=True)
    assert "Ignoring indexes:" in result.stdout, str(result)
    assert (
        "DistributionNotFound: No matching distribution found for INITools"
        in result.stdout
    )


@pytest.mark.network
def test_install_no_binary_via_config_disables_cached_wheels(
        script, data, common_wheels):
    script.pip('install', 'wheel', '--no-index', '-f', common_wheels)
    config_file = tempfile.NamedTemporaryFile(mode='wt', delete=False)
    try:
        script.environ['PIP_CONFIG_FILE'] = config_file.name
        config_file.write(textwrap.dedent("""\
            [global]
            no-binary = :all:
            """))
        config_file.close()
        res = script.pip(
            'install', '--no-index', '-f', data.find_links,
            'upper', expect_stderr=True)
    finally:
        os.unlink(config_file.name)
    assert "Successfully installed upper-2.0" in str(res), str(res)
    # No wheel building for upper, which was blacklisted
    assert "Running setup.py bdist_wheel for upper" not in str(res), str(res)
    # Must have used source, not a cached wheel to install upper.
    assert "Running setup.py install for upper" in str(res), str(res)