-
-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathtest_cli.py
More file actions
439 lines (371 loc) · 13.5 KB
/
Copy pathtest_cli.py
File metadata and controls
439 lines (371 loc) · 13.5 KB
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright (c) nexB Inc. and others. All rights reserved.
# ScanCode is a trademark of nexB Inc.
# SPDX-License-Identifier: Apache-2.0
# See http://www.apache.org/licenses/LICENSE-2.0 for the license text.
# See https://github.com/nexB/python-inspector for support or download.
# See https://aboutcode.org for more information about nexB OSS projects.
#
import json
import os
import pytest
from click.testing import CliRunner
from commoncode.testcase import FileDrivenTesting
from _packagedcode import models
from python_inspector.resolve_cli import get_requirements_from_direct_dependencies
from python_inspector.resolve_cli import resolve_dependencies
# Used for tests to regenerate fixtures with regen=True
REGEN_TEST_FIXTURES = os.getenv("PYINSP_REGEN_TEST_FIXTURES", False)
test_env = FileDrivenTesting()
test_env.test_data_dir = os.path.join(os.path.dirname(__file__), "data")
setup_test_env = FileDrivenTesting()
setup_test_env.test_data_dir = os.path.join(os.path.dirname(__file__), "data", "setup")
@pytest.mark.online
def test_cli_with_default_urls():
expected_file = test_env.get_test_loc("default-url-expected.json", must_exist=False)
specifier = "zipp==3.8.0"
extra_options = [
"--use-pypi-json-api",
]
check_specs_resolution(
specifier=specifier,
expected_file=expected_file,
extra_options=extra_options,
regen=REGEN_TEST_FIXTURES,
)
@pytest.mark.online
def test_pdt_output():
requirements_file = test_env.get_test_loc("pdt-requirements.txt")
expected_file = test_env.get_test_loc("pdt-requirements.txt-expected.json", must_exist=False)
extra_options = []
check_requirements_resolution(
requirements_file=requirements_file,
expected_file=expected_file,
extra_options=extra_options,
pdt_output=True,
regen=REGEN_TEST_FIXTURES,
)
@pytest.mark.online
def test_pdt_output_with_pinned_requirements():
requirements_file = test_env.get_test_loc("pinned-pdt-requirements.txt")
expected_file = test_env.get_test_loc(
"pinned-pdt-requirements.txt-expected.json", must_exist=False
)
extra_options = []
check_requirements_resolution(
requirements_file=requirements_file,
expected_file=expected_file,
extra_options=extra_options,
pdt_output=True,
regen=REGEN_TEST_FIXTURES,
)
@pytest.mark.online
def test_pdt_output_with_frozen_requirements():
requirements_file = test_env.get_test_loc("frozen-requirements.txt")
expected_file = test_env.get_test_loc("frozen-requirements.txt-expected.json", must_exist=False)
extra_options = []
check_requirements_resolution(
requirements_file=requirements_file,
expected_file=expected_file,
extra_options=extra_options,
pdt_output=True,
regen=REGEN_TEST_FIXTURES,
)
@pytest.mark.online
def test_cli_with_single_index_url():
expected_file = test_env.get_test_loc("single-url-expected.json", must_exist=False)
specifier = "zipp==3.8.0"
extra_options = [
"--index-url",
"https://pypi.org/simple",
]
check_specs_resolution(
specifier=specifier,
expected_file=expected_file,
extra_options=extra_options,
regen=REGEN_TEST_FIXTURES,
)
@pytest.mark.online
def test_cli_with_single_index_url_except_pypi_simple():
expected_file = test_env.get_test_loc(
"single-url-except-simple-expected.json", must_exist=False
)
# using flask since it's not present in thirdparty
specifier = "flask"
extra_options = [
"--index-url",
"https://thirdparty.aboutcode.org/pypi/simple/",
]
check_specs_resolution(
specifier=specifier,
expected_file=expected_file,
extra_options=extra_options,
regen=REGEN_TEST_FIXTURES,
)
@pytest.mark.online
def test_cli_with_multiple_index_url_and_tilde_req():
expected_file = test_env.get_test_loc("tilde_req-expected.json", must_exist=False)
specifier = "zipp~=3.8.0"
extra_options = [
"--index-url",
"https://pypi.org/simple",
"--index-url",
"https://thirdparty.aboutcode.org/pypi/simple/",
]
check_specs_resolution(
specifier=specifier,
expected_file=expected_file,
extra_options=extra_options,
regen=REGEN_TEST_FIXTURES,
)
@pytest.mark.online
def test_cli_with_environment_marker_and_complex_ranges():
requirements_file = test_env.get_test_loc("environment-marker-test-requirements.txt")
expected_file = test_env.get_test_loc(
"environment-marker-test-requirements.txt-expected.json", must_exist=False
)
extra_options = [
"--operating-system",
"linux",
"--python-version",
"37",
]
check_requirements_resolution(
requirements_file=requirements_file,
expected_file=expected_file,
extra_options=extra_options,
pdt_output=True,
regen=REGEN_TEST_FIXTURES,
)
@pytest.mark.online
def test_cli_with_multiple_index_url_and_tilde_req_with_max_rounds():
expected_file = test_env.get_test_loc("tilde_req-expected.json", must_exist=False)
specifier = "zipp~=3.8.0"
extra_options = [
"--index-url",
"https://pypi.org/simple",
"--index-url",
"https://thirdparty.aboutcode.org/pypi/simple/",
"--max-rounds",
"100",
]
check_specs_resolution(
specifier=specifier,
expected_file=expected_file,
extra_options=extra_options,
regen=REGEN_TEST_FIXTURES,
)
@pytest.mark.online
def test_cli_with_multiple_index_url_and_tilde_req_and_netrc_file_without_matching_url():
expected_file = test_env.get_test_loc("tilde_req-expected.json", must_exist=False)
netrc_file = test_env.get_test_loc("test.netrc", must_exist=False)
specifier = "zipp~=3.8.0"
extra_options = [
"--index-url",
"https://pypi.org/simple",
"--index-url",
"https://thirdparty.aboutcode.org/pypi/simple/",
"--netrc",
netrc_file,
]
check_specs_resolution(
specifier=specifier,
expected_file=expected_file,
extra_options=extra_options,
regen=REGEN_TEST_FIXTURES,
)
@pytest.mark.online
def test_cli_with_pinned_requirements_file():
requirements_file = test_env.get_test_loc("pinned-requirements.txt")
expected_file = test_env.get_test_loc("pinned-requirements.txt-expected.json", must_exist=False)
check_requirements_resolution(
requirements_file=requirements_file,
expected_file=expected_file,
regen=REGEN_TEST_FIXTURES,
)
@pytest.mark.online
def test_cli_with_setup_py_failure():
setup_py_file = setup_test_env.get_test_loc("simple-setup.py")
expected_file = setup_test_env.get_test_loc("simple-setup.py-expected.json", must_exist=False)
check_setup_py_resolution(
setup_py=setup_py_file,
expected_file=expected_file,
regen=REGEN_TEST_FIXTURES,
expected_rc=1,
message=f"Python version 3.8 is not compatible with setup.py {setup_py_file} python_requires >2, <=3",
)
@pytest.mark.online
def test_cli_with_insecure_option():
setup_py_file = setup_test_env.get_test_loc("spdx-setup.py")
expected_file = setup_test_env.get_test_loc("spdx-setup.py-expected.json", must_exist=False)
check_setup_py_resolution(
setup_py=setup_py_file,
expected_file=expected_file,
regen=REGEN_TEST_FIXTURES,
extra_options=["--python-version", "27", "--analyze-setup-py-insecurely"],
pdt_output=True,
)
@pytest.mark.online
def test_cli_with_insecure_option_testpkh():
setup_py_file = test_env.get_test_loc("insecure-setup-2/setup.py")
expected_file = test_env.get_test_loc(
"insecure-setup-2/setup.py-expected.json", must_exist=False
)
check_setup_py_resolution(
setup_py=setup_py_file,
expected_file=expected_file,
regen=REGEN_TEST_FIXTURES,
extra_options=["--python-version", "27", "--analyze-setup-py-insecurely"],
)
@pytest.mark.online
def test_cli_with_insecure_option_testrdflib():
setup_py_file = test_env.get_test_loc("insecure-setup/setup.py")
expected_file = test_env.get_test_loc("insecure-setup/setup.py-expected.json", must_exist=False)
check_setup_py_resolution(
setup_py=setup_py_file,
expected_file=expected_file,
regen=REGEN_TEST_FIXTURES,
extra_options=["--python-version", "27", "--analyze-setup-py-insecurely"],
)
@pytest.mark.online
def test_cli_with_setup_py():
setup_py_file = setup_test_env.get_test_loc("simple-setup.py")
expected_file = setup_test_env.get_test_loc("simple-setup.py-expected.json", must_exist=False)
check_setup_py_resolution(
setup_py=setup_py_file,
expected_file=expected_file,
regen=REGEN_TEST_FIXTURES,
extra_options=["--python-version", "27"],
)
def check_specs_resolution(
specifier,
expected_file,
extra_options=tuple(),
regen=REGEN_TEST_FIXTURES,
):
result_file = test_env.get_temp_file("json")
options = ["--specifier", specifier, "--json", result_file]
options.extend(extra_options)
run_cli(options=options)
check_json_results(
result_file=result_file,
expected_file=expected_file,
regen=regen,
)
def test_passing_of_json_pdt_and_json_flags():
result_file = test_env.get_temp_file("json")
options = ["--specifier", "foo", "--json", result_file, "--json-pdt", result_file]
run_cli(options=options, expected_rc=1)
def test_version_option():
options = ["--version"]
result = run_cli(options=options)
assert "0.7.1" in result.output
def test_passing_of_netrc_file_that_does_not_exist():
options = ["--specifier", "foo", "--netrc", "bar.txt", "--json", "-"]
run_cli(options=options, expected_rc=2)
def test_passing_of_wrong_requirements_file():
test_file = test_env.get_temp_file(file_name="pdt.txt", extension="")
with open(test_file, "w") as f:
f.write("")
test_file_2 = test_env.get_temp_file(file_name="setup.py", extension="")
with open(test_file_2, "w") as f:
f.write("")
options = ["--requirement", test_file, "--json", "-", "--requirement", test_file_2]
result = run_cli(options=options, expected_rc=1)
assert "Error: no requirements requested" in result.output
def test_passing_of_no_json_output_flag():
options = ["--specifier", "foo"]
run_cli(options=options, expected_rc=1)
def check_requirements_resolution(
requirements_file,
expected_file,
extra_options=tuple(),
regen=REGEN_TEST_FIXTURES,
pdt_output=False,
):
result_file = test_env.get_temp_file("json")
if pdt_output:
options = ["--requirement", requirements_file, "--json-pdt", result_file]
else:
options = ["--requirement", requirements_file, "--json", result_file]
options.extend(extra_options)
run_cli(options=options)
check_json_results(
result_file=result_file, expected_file=expected_file, regen=regen, clean=not pdt_output
)
def check_setup_py_resolution(
setup_py,
expected_file,
extra_options=tuple(),
regen=REGEN_TEST_FIXTURES,
pdt_output=False,
expected_rc=0,
message="",
):
result_file = setup_test_env.get_temp_file(file_name="json")
if pdt_output:
options = ["--setup-py", setup_py, "--json-pdt", result_file]
else:
options = ["--setup-py", setup_py, "--json", result_file]
options.extend(extra_options)
result = run_cli(options=options, expected_rc=expected_rc)
if message:
assert message in result.output
if expected_rc == 0:
check_json_results(
result_file=result_file, expected_file=expected_file, regen=regen, clean=not pdt_output
)
def check_json_results(result_file, expected_file, clean=True, regen=REGEN_TEST_FIXTURES):
"""
Check the ``result_file`` JSON results against the ``expected_file``
expected JSON results.
If ``clean`` is True, remove headers data that can change across runs to
provide stable test resultys.
If ``regen`` is True the expected_file WILL BE overwritten with the new
results from ``results_file``. This is convenient for updating tests
expectations.
"""
with open(result_file) as res:
results = json.load(res)
if clean:
clean_results(results)
if regen:
with open(expected_file, "w") as reg:
json.dump(results, reg, indent=2, separators=(",", ": "))
expected = results
else:
with open(expected_file) as res:
expected = json.load(res)
if clean:
clean_results(expected)
assert results == expected
def clean_results(results):
"""
Return cleaned results removing transient values that can change across test
runs.
"""
headers = results.get("headers", {})
options = headers.get("options", [])
headers["options"] = [o for o in options if not o.startswith("--requirement")]
return results
def run_cli(options, cli=resolve_dependencies, expected_rc=0, env=None):
"""
Run a command line resolution. Return a click.testing.Result object.
"""
if not env:
env = dict(os.environ)
runner = CliRunner()
result = runner.invoke(cli, options, catch_exceptions=False, env=env)
if result.exit_code != expected_rc:
output = result.output
error = f"""
Failure to run:
rc: {result.exit_code}
python-inspector {options}
output:
{output}
"""
assert result.exit_code == expected_rc, error
return result