forked from aboutcode-org/python-inspector
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_api.py
More file actions
159 lines (122 loc) · 5.33 KB
/
test_api.py
File metadata and controls
159 lines (122 loc) · 5.33 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
#!/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 os
import pytest
from commoncode.testcase import FileDrivenTesting
from test_cli import check_data_results
from python_inspector.api import get_index_urls
from python_inspector.api import resolver_api
test_env = FileDrivenTesting()
test_env.test_data_dir = os.path.join(os.path.dirname(__file__), "data")
def test_api_with_specifier():
expected_file = test_env.get_test_loc("test-api-expected.json", must_exist=False)
results = resolver_api(
specifiers=["flask==2.1.2"],
python_version="3.10",
operating_system="linux",
)
check_data_results(results=results.to_dict(generic_paths=True), expected_file=expected_file)
def test_api_with_specifier_pdt():
expected_file = test_env.get_test_loc("test-api-pdt-expected.json", must_exist=False)
results = resolver_api(
specifiers=["flask==2.1.2"],
python_version="3.10",
operating_system="linux",
pdt_output=True,
)
check_data_results(results=results.to_dict(generic_paths=True), expected_file=expected_file)
def test_api_with_requirement_file():
expected_file = test_env.get_test_loc("test-api-with-requirement-file.json", must_exist=False)
results = resolver_api(
python_version="3.10",
operating_system="linux",
requirement_files=[test_env.get_test_loc("frozen-requirements.txt")],
)
check_data_results(results=results.to_dict(generic_paths=True), expected_file=expected_file)
def test_api_with_prefer_source():
expected_file = test_env.get_test_loc("test-api-with-prefer-source.json", must_exist=False)
results = resolver_api(
specifiers=["flask==2.1.2"],
python_version="3.10",
operating_system="linux",
prefer_source=True,
)
check_data_results(results=results.to_dict(generic_paths=True), expected_file=expected_file)
def test_api_with_recursive_requirement_file():
requirement_file = test_env.get_test_loc("recursive_requirements/r.txt")
expected_file = test_env.get_test_loc(
"test-api-with-recursive-requirement-file.json", must_exist=False
)
results = resolver_api(
python_version="3.8",
operating_system="linux",
requirement_files=[requirement_file],
)
check_data_results(results=results.to_dict(generic_paths=True), expected_file=expected_file)
def test_api_with_no_os():
with pytest.raises(Exception):
resolver_api(specifiers=["flask==2.1.2"], python_version="3.10")
def test_api_with_no_pyver():
with pytest.raises(Exception):
resolver_api(specifiers=["flask==2.1.2"], operating_system="linux")
def test_api_with_unsupported_os():
with pytest.raises(ValueError):
resolver_api(specifiers=["flask==2.1.2"], python_version="3.10", operating_system="foo-bar")
def test_api_with_wrong_pyver():
with pytest.raises(ValueError):
resolver_api(specifiers=["flask==2.1.2"], python_version="3.14", operating_system="linux")
def test_api_with_python_311():
expected_file = test_env.get_test_loc("test-api-with-python-311.json", must_exist=False)
results = resolver_api(
specifiers=["flask==2.1.2"],
python_version="3.11",
operating_system="linux",
prefer_source=True,
)
check_data_results(results=results.to_dict(generic_paths=True), expected_file=expected_file)
def test_api_with_lief_python_312():
expected_file = test_env.get_test_loc("test-api-with-lief-python-312.json", must_exist=False)
results = resolver_api(
specifiers=["lief==0.15.1"],
python_version="3.12",
operating_system="linux",
prefer_source=True,
)
check_data_results(results=results.to_dict(generic_paths=True), expected_file=expected_file)
def test_api_with_partial_setup_py():
expected_file = test_env.get_test_loc("test-api-with-partial-setup-py.json", must_exist=False)
results = resolver_api(
python_version="3.11",
operating_system="linux",
setup_py_file=test_env.get_test_loc("partial-setup.py"),
prefer_source=True,
analyze_setup_py_insecurely=True,
)
check_data_results(results=results.to_dict(generic_paths=True), expected_file=expected_file)
def test_get_index_urls():
# pass as a tuple
index_urls = get_index_urls(
index_urls=("https://pypi.org/simple",),
extra_data={"extra_index_urls": ["https://pypi.org/simple", "https://example.com/simple"]},
)
expected = ["https://example.com/simple", "https://pypi.org/simple"]
assert sorted(list(index_urls)) == expected
# pass as a string
index_urls = get_index_urls(
index_urls=("https://pypi.org/simple"),
extra_data={"extra_index_urls": ["https://pypi.org/simple", "https://example.com/simple"]},
)
assert sorted(list(index_urls)) == expected
index_urls = get_index_urls(
index_urls=("https://pypi.org/simple",),
extra_data={"index_url": "https://pypi.org/simple"},
)
assert index_urls == ("https://pypi.org/simple",)