summaryrefslogtreecommitdiff
path: root/tests/functional/test_check.py
blob: 45611e73ec158cf52a78de4eeba2e6e27ed04497 (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
from tests.lib import create_test_package_with_setup


def matches_expected_lines(string, expected_lines):
    # Ignore empty lines
    output_lines = set(filter(None, string.splitlines()))
    # Match regardless of order
    return set(output_lines) == set(expected_lines)


def test_basic_check_clean(script):
    """On a clean environment, check should print a helpful message.

    """
    result = script.pip('check')

    expected_lines = (
        "No broken requirements found.",
    )
    assert matches_expected_lines(result.stdout, expected_lines)
    assert result.returncode == 0


def test_basic_check_missing_dependency(script):
    # Setup a small project
    pkga_path = create_test_package_with_setup(
        script,
        name='pkga', version='1.0', install_requires=['missing==0.1'],
    )
    # Let's install pkga without its dependency
    res = script.pip('install', '--no-index', pkga_path, '--no-deps')
    assert "Successfully installed pkga-1.0" in res.stdout, str(res)

    result = script.pip('check', expect_error=True)

    expected_lines = (
        "pkga 1.0 requires missing, which is not installed.",
    )
    assert matches_expected_lines(result.stdout, expected_lines)
    assert result.returncode == 1


def test_basic_check_broken_dependency(script):
    # Setup pkga depending on pkgb>=1.0
    pkga_path = create_test_package_with_setup(
        script,
        name='pkga', version='1.0', install_requires=['broken>=1.0'],
    )
    # Let's install pkga without its dependency
    res = script.pip('install', '--no-index', pkga_path, '--no-deps')
    assert "Successfully installed pkga-1.0" in res.stdout, str(res)

    # Setup broken==0.1
    broken_path = create_test_package_with_setup(
        script,
        name='broken', version='0.1',
    )
    # Let's install broken==0.1
    res = script.pip(
        'install', '--no-index', broken_path, '--no-warn-conflicts',
    )
    assert "Successfully installed broken-0.1" in res.stdout, str(res)

    result = script.pip('check', expect_error=True)

    expected_lines = (
        "pkga 1.0 has requirement broken>=1.0, but you have broken 0.1.",
    )
    assert matches_expected_lines(result.stdout, expected_lines)
    assert result.returncode == 1


def test_basic_check_broken_dependency_and_missing_dependency(script):
    pkga_path = create_test_package_with_setup(
        script,
        name='pkga', version='1.0', install_requires=['broken>=1.0'],
    )
    # Let's install pkga without its dependency
    res = script.pip('install', '--no-index', pkga_path, '--no-deps')
    assert "Successfully installed pkga-1.0" in res.stdout, str(res)

    # Setup broken==0.1
    broken_path = create_test_package_with_setup(
        script,
        name='broken', version='0.1', install_requires=['missing'],
    )
    # Let's install broken==0.1
    res = script.pip('install', '--no-index', broken_path, '--no-deps')
    assert "Successfully installed broken-0.1" in res.stdout, str(res)

    result = script.pip('check', expect_error=True)

    expected_lines = (
        "broken 0.1 requires missing, which is not installed.",
        "pkga 1.0 has requirement broken>=1.0, but you have broken 0.1."
    )

    assert matches_expected_lines(result.stdout, expected_lines)
    assert result.returncode == 1


def test_check_complicated_name_missing(script):
    package_a_path = create_test_package_with_setup(
        script,
        name='package_A', version='1.0',
        install_requires=['Dependency-B>=1.0'],
    )

    # Without dependency
    result = script.pip('install', '--no-index', package_a_path, '--no-deps')
    assert "Successfully installed package-A-1.0" in result.stdout, str(result)

    result = script.pip('check', expect_error=True)
    expected_lines = (
        "package-a 1.0 requires dependency-b, which is not installed.",
    )
    assert matches_expected_lines(result.stdout, expected_lines)
    assert result.returncode == 1


def test_check_complicated_name_broken(script):
    package_a_path = create_test_package_with_setup(
        script,
        name='package_A', version='1.0',
        install_requires=['Dependency-B>=1.0'],
    )
    dependency_b_path_incompatible = create_test_package_with_setup(
        script,
        name='dependency-b', version='0.1',
    )

    # With broken dependency
    result = script.pip('install', '--no-index', package_a_path, '--no-deps')
    assert "Successfully installed package-A-1.0" in result.stdout, str(result)

    result = script.pip(
        'install', '--no-index', dependency_b_path_incompatible, '--no-deps',
    )
    assert "Successfully installed dependency-b-0.1" in result.stdout

    result = script.pip('check', expect_error=True)
    expected_lines = (
        "package-a 1.0 has requirement Dependency-B>=1.0, but you have "
        "dependency-b 0.1.",
    )
    assert matches_expected_lines(result.stdout, expected_lines)
    assert result.returncode == 1


def test_check_complicated_name_clean(script):
    package_a_path = create_test_package_with_setup(
        script,
        name='package_A', version='1.0',
        install_requires=['Dependency-B>=1.0'],
    )
    dependency_b_path = create_test_package_with_setup(
        script,
        name='dependency-b', version='1.0',
    )

    result = script.pip('install', '--no-index', package_a_path, '--no-deps')
    assert "Successfully installed package-A-1.0" in result.stdout, str(result)

    result = script.pip(
        'install', '--no-index', dependency_b_path, '--no-deps',
    )
    assert "Successfully installed dependency-b-1.0" in result.stdout

    result = script.pip('check', expect_error=True)
    expected_lines = (
        "No broken requirements found.",
    )
    assert matches_expected_lines(result.stdout, expected_lines)
    assert result.returncode == 0


def test_check_considers_conditional_reqs(script):
    package_a_path = create_test_package_with_setup(
        script,
        name='package_A', version='1.0',
        install_requires=[
            "Dependency-B>=1.0; python_version != '2.7'",
            "Dependency-B>=2.0; python_version == '2.7'",
        ],
    )

    result = script.pip('install', '--no-index', package_a_path, '--no-deps')
    assert "Successfully installed package-A-1.0" in result.stdout, str(result)

    result = script.pip('check', expect_error=True)
    expected_lines = (
        "package-a 1.0 requires dependency-b, which is not installed.",
    )
    assert matches_expected_lines(result.stdout, expected_lines)
    assert result.returncode == 1


def test_check_development_versions_are_also_considered(script):
    # Setup pkga depending on pkgb>=1.0
    pkga_path = create_test_package_with_setup(
        script,
        name='pkga', version='1.0', install_requires=['depend>=1.0'],
    )
    # Let's install pkga without its dependency
    res = script.pip('install', '--no-index', pkga_path, '--no-deps')
    assert "Successfully installed pkga-1.0" in res.stdout, str(res)

    # Setup depend==1.1.0.dev0
    depend_path = create_test_package_with_setup(
        script,
        name='depend', version='1.1.0.dev0',
    )
    # Let's install depend==1.1.0.dev0
    res = script.pip(
        'install', '--no-index', depend_path, '--no-warn-conflicts',
    )
    assert "Successfully installed depend-1.1.0.dev0" in res.stdout, str(res)

    result = script.pip('check')
    expected_lines = (
        "No broken requirements found.",
    )
    assert matches_expected_lines(result.stdout, expected_lines)
    assert result.returncode == 0


def test_basic_check_broken_metadata(script):
    # Create some corrupt metadata
    dist_info_dir = script.site_packages_path / 'pkga-1.0.dist-info'
    dist_info_dir.mkdir()
    with open(dist_info_dir / 'METADATA', 'w') as f:
        f.write('Metadata-Version: 2.1\n'
                'Name: pkga\n'
                'Version: 1.0\n'
                'Requires-Dist: pip; python_version == "3.4";extra == "test"\n'
                )

    result = script.pip('check', expect_error=True)

    assert 'Error parsing requirements' in result.stderr
    assert result.returncode == 1