-
-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathtest_api.py
More file actions
200 lines (153 loc) · 5.7 KB
/
Copy pathtest_api.py
File metadata and controls
200 lines (153 loc) · 5.7 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
#!/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 commoncode.testcase import FileDrivenTesting
from test_cli import check_json_results
from python_inspector.resolve_cli import resolver_api
test_env = FileDrivenTesting()
test_env.test_data_dir = os.path.join(os.path.dirname(__file__), "data")
def test_api_with_specifier():
result_file = test_env.get_temp_file("json")
expected_file = test_env.get_test_loc("test-api-expected.json", must_exist=False)
resolution = resolver_api(
specifiers=["flask==2.1.2"], python_version="3.10", operating_system="linux"
)
with open(result_file, "w") as result:
json.dump(resolution.to_dict(), result, indent=2, separators=(",", ": "))
check_json_results(
result_file=result_file,
expected_file=expected_file,
clean=True,
)
def test_api_with_specifier_pdt():
result_file = test_env.get_temp_file("json")
expected_file = test_env.get_test_loc("test-api-pdt-expected.json", must_exist=False)
resolution = resolver_api(
specifiers=["flask==2.1.2"],
python_version="3.10",
operating_system="linux",
pdt_output=True,
)
with open(result_file, "w") as result:
json.dump(resolution.to_dict(), result, indent=2, separators=(",", ": "))
check_json_results(
result_file=result_file,
expected_file=expected_file,
clean=True,
)
def test_api_with_requirement_file():
result_file = test_env.get_temp_file("json")
requirement_file = test_env.get_test_loc("frozen-requirements.txt")
expected_file = test_env.get_test_loc("test-api-with-requirement-file.json", must_exist=False)
resolution = resolver_api(
python_version="3.10", operating_system="linux", requirement_files=[requirement_file]
)
with open(result_file, "w") as result:
json.dump(resolution.to_dict(), result, indent=2, separators=(",", ": "))
check_json_results(
result_file=result_file,
expected_file=expected_file,
clean=True,
)
def test_api_with_prefer_source():
result_file = test_env.get_temp_file("json")
expected_file = test_env.get_test_loc("test-api-with-prefer-source.json", must_exist=False)
resolution = resolver_api(
specifiers=["flask==2.1.2"],
python_version="3.10",
operating_system="linux",
prefer_source=True,
)
with open(result_file, "w") as result:
json.dump(resolution.to_dict(), result, indent=2, separators=(",", ": "))
check_json_results(
result_file=result_file,
expected_file=expected_file,
clean=True,
)
def test_api_with_recursive_requirement_file():
result_file = test_env.get_temp_file("json")
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
)
resolution = resolver_api(
python_version="3.8", operating_system="linux", requirement_files=[requirement_file]
)
with open(result_file, "w") as result:
json.dump(resolution.to_dict(), result, indent=2, separators=(",", ": "))
check_json_results(
result_file=result_file,
expected_file=expected_file,
clean=True,
)
def test_api_with_no_os():
with pytest.raises(Exception) as e:
resolver_api(
specifiers=["flask==2.1.2"],
python_version="3.10",
)
def test_api_with_no_pyver():
with pytest.raises(Exception) as e:
resolver_api(
specifiers=["flask==2.1.2"],
operating_system="linux",
)
def test_api_with_unsupported_os():
with pytest.raises(ValueError) as e:
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) as e:
resolver_api(
specifiers=["flask==2.1.2"],
python_version="3.12",
operating_system="linux",
)
def test_api_with_python_311():
result_file = test_env.get_temp_file("json")
expected_file = test_env.get_test_loc("test-api-with-python-311.json", must_exist=False)
resolution = resolver_api(
specifiers=["flask==2.1.2"],
python_version="3.11",
operating_system="linux",
prefer_source=True,
)
with open(result_file, "w") as result:
json.dump(resolution.to_dict(), result, indent=2, separators=(",", ": "))
check_json_results(
result_file=result_file,
expected_file=expected_file,
clean=True,
)
def test_api_with_partial_setup_py():
result_file = test_env.get_temp_file("json")
setup_py_file = test_env.get_test_loc("partial-setup.py")
expected_file = test_env.get_test_loc("test-api-with-partial-setup-py.json", must_exist=False)
resolution = resolver_api(
python_version="3.11",
operating_system="linux",
setup_py_file=setup_py_file,
prefer_source=True,
analyze_setup_py_insecurely=True,
)
with open(result_file, "w") as result:
json.dump(resolution.to_dict(), result, indent=2, separators=(",", ": "))
check_json_results(
result_file=result_file,
expected_file=expected_file,
clean=True,
)