Skip to content

Commit 1b796c3

Browse files
committed
Add tests
Signed-off-by: Tushar Goel <tushar.goel.dav@gmail.com>
1 parent 7a11ccd commit 1b796c3

File tree

8 files changed

+176
-4
lines changed

8 files changed

+176
-4
lines changed

src/python_inspector/resolve_cli.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,8 @@ def resolve_dependencies(
247247
python_requires=python_requires,
248248
):
249249
click.secho(
250-
f"Python version {python_version} is not compatible with setup.py {setup_py_file} "
250+
f"Python version {get_python_version_from_env_tag(python_version)} "
251+
f"is not compatible with setup.py {setup_py_file} "
251252
f"python_requires {python_requires}",
252253
err=True,
253254
)

tests/data/setup/simple-setup.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
1+
# Copyright (C) 2017-2021 HERE Europe B.V.
2+
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# SPDX-License-Identifier: Apache-2.0
16+
# License-Filename: LICENSE
17+
118
from setuptools import find_packages
219
from setuptools import setup
320

@@ -8,7 +25,7 @@
825
url="https://example.org/app",
926
license="MIT License",
1027
classifiers=["License :: OSI Approved :: MIT License", "Programming Language :: Python :: 2"],
11-
python_requires=">=2, <3",
12-
install_requires=["Flask>=0.12, <1.1"],
28+
python_requires=">2, <=3",
29+
install_requires=["license-expression>=0.1, <1.2"],
1330
packages=find_packages(),
1431
)
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
{
2+
"headers": {
3+
"tool_name": "python-inspector",
4+
"tool_homepageurl": "https://github.com/nexB/python-inspector",
5+
"tool_version": "0.6.3",
6+
"options": [
7+
"--index-url https://pypi.org/simple",
8+
"--python-version 27",
9+
"--operating-system linux",
10+
"--json <file>"
11+
],
12+
"notice": "Dependency tree generated with python-inspector.\npython-inspector is a free software tool from nexB Inc. and others.\nVisit https://github.com/nexB/scancode-toolkit/ for support and download.",
13+
"warnings": [],
14+
"errors": []
15+
},
16+
"requirements": [
17+
{
18+
"purl": "pkg:pypi/license-expression",
19+
"extracted_requirement": "license-expression<1.2,>=0.1",
20+
"scope": "install",
21+
"is_runtime": true,
22+
"is_optional": false,
23+
"is_resolved": false,
24+
"resolved_package": {},
25+
"extra_data": {}
26+
}
27+
],
28+
"resolved_dependencies": [
29+
{
30+
"package": "pkg:pypi/boolean-py@3.8",
31+
"dependencies": [],
32+
"wheel_urls": [
33+
"https://files.pythonhosted.org/packages/eb/5b/f82983127b1a1a4db83ee290e00a94a2b08c566fa6c58466e82ed7b0a76f/boolean.py-3.8-py2.py3-none-any.whl"
34+
],
35+
"sdist_url": "https://files.pythonhosted.org/packages/a1/eb/b37ef5647243ade8308f7bb46b1a45e6204790c163cbd8cf6df990d5c1c1/boolean.py-3.8.tar.gz"
36+
},
37+
{
38+
"package": "pkg:pypi/license-expression@1.0",
39+
"dependencies": [
40+
"pkg:pypi/boolean-py@3.8"
41+
],
42+
"wheel_urls": [
43+
"https://files.pythonhosted.org/packages/12/7a/dd02b8ac8f0cd81557012b8f249ba4d0d32fe8f98e8d951ebc882187fd92/license_expression-1.0-py2.py3-none-any.whl"
44+
],
45+
"sdist_url": "https://files.pythonhosted.org/packages/27/71/a8d8d78f7866a75f6a940cc53d1557a5edf1ae4a281fe8797cf36077105d/license-expression-1.0.tar.gz"
46+
}
47+
]
48+
}

tests/test_cli.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525

2626
test_env = FileDrivenTesting()
2727
test_env.test_data_dir = os.path.join(os.path.dirname(__file__), "data")
28+
setup_test_env = FileDrivenTesting()
29+
setup_test_env.test_data_dir = os.path.join(os.path.dirname(__file__), "data", "setup")
2830

2931

3032
@pytest.mark.online
@@ -212,6 +214,31 @@ def test_cli_with_pinned_requirements_file():
212214
)
213215

214216

217+
@pytest.mark.online
218+
def test_cli_with_setup_py_failure():
219+
setup_py_file = setup_test_env.get_test_loc("simple-setup.py")
220+
expected_file = setup_test_env.get_test_loc("simple-setup.py-expected.json", must_exist=False)
221+
check_setup_py_resolution(
222+
setup_py=setup_py_file,
223+
expected_file=expected_file,
224+
regen=REGEN_TEST_FIXTURES,
225+
expected_rc=1,
226+
message=f"Python version 3.8 is not compatible with setup.py {setup_py_file} python_requires >2, <=3",
227+
)
228+
229+
230+
@pytest.mark.online
231+
def test_cli_with_setup_py():
232+
setup_py_file = setup_test_env.get_test_loc("simple-setup.py")
233+
expected_file = setup_test_env.get_test_loc("simple-setup.py-expected.json", must_exist=False)
234+
check_setup_py_resolution(
235+
setup_py=setup_py_file,
236+
expected_file=expected_file,
237+
regen=REGEN_TEST_FIXTURES,
238+
extra_options=["--python-version", "27"],
239+
)
240+
241+
215242
def check_specs_resolution(
216243
specifier,
217244
expected_file,
@@ -276,6 +303,30 @@ def check_requirements_resolution(
276303
)
277304

278305

306+
def check_setup_py_resolution(
307+
setup_py,
308+
expected_file,
309+
extra_options=tuple(),
310+
regen=REGEN_TEST_FIXTURES,
311+
pdt_output=False,
312+
expected_rc=0,
313+
message="",
314+
):
315+
result_file = setup_test_env.get_temp_file(file_name="json")
316+
if pdt_output:
317+
options = ["--setup-py", setup_py, "--json-pdt", result_file]
318+
else:
319+
options = ["--setup-py", setup_py, "--json", result_file]
320+
options.extend(extra_options)
321+
result = run_cli(options=options, expected_rc=expected_rc)
322+
if message:
323+
assert message in result.output
324+
if expected_rc == 0:
325+
check_json_results(
326+
result_file=result_file, expected_file=expected_file, regen=regen, clean=not pdt_output
327+
)
328+
329+
279330
def check_json_results(result_file, expected_file, clean=True, regen=REGEN_TEST_FIXTURES):
280331
"""
281332
Check the ``result_file`` JSON results against the ``expected_file``

tests/test_packagecode_pypi.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#
2+
# Copyright (c) nexB Inc. and others. All rights reserved.
3+
# ScanCode is a trademark of nexB Inc.
4+
# SPDX-License-Identifier: Apache-2.0
5+
# See http://www.apache.org/licenses/LICENSE-2.0 for the license text.
6+
# See https://github.com/nexB/skeleton for support or download.
7+
# See https://aboutcode.org for more information about nexB OSS projects.
8+
#
9+
10+
from _packagedcode.models import DependentPackage
11+
from _packagedcode.pypi import create_dependency_for_python_requires
12+
13+
14+
def test_create_dependency_for_python_requires():
15+
assert create_dependency_for_python_requires(
16+
python_requires_specifier=">=3.6"
17+
) == DependentPackage(
18+
purl="pkg:generic/python", extracted_requirement="python_requires>=3.6", scope="python"
19+
)

tests/test_resolution.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ def test_without_supported_wheels():
145145
"pkg:pypi/hyperlink@21.0.0",
146146
"pkg:pypi/idna@3.3",
147147
"pkg:pypi/pycparser@2.21",
148-
"pkg:pypi/setuptools@65.1.0",
148+
"pkg:pypi/setuptools@65.2.0",
149149
"pkg:pypi/txaio@22.2.1",
150150
]
151151

tests/test_setup_py_parsing.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#
2+
# Copyright (c) nexB Inc. and others. All rights reserved.
3+
# ScanCode is a trademark of nexB Inc.
4+
# SPDX-License-Identifier: Apache-2.0
5+
# See http://www.apache.org/licenses/LICENSE-2.0 for the license text.
6+
# See https://github.com/nexB/skeleton for support or download.
7+
# See https://aboutcode.org for more information about nexB OSS projects.
8+
#
9+
10+
import os
11+
12+
from _packagedcode.models import DependentPackage
13+
from _packagedcode.pypi import PythonSetupPyHandler
14+
15+
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
16+
17+
18+
def test_setup_py_parsing():
19+
setup_py_file = os.path.join(BASE_DIR, "data", "setup", "simple-setup.py")
20+
package_data = list(PythonSetupPyHandler.parse(location=setup_py_file))
21+
deps = []
22+
for pkg in package_data:
23+
deps.extend(pkg.dependencies)
24+
assert deps == [
25+
DependentPackage(
26+
purl="pkg:pypi/license-expression",
27+
extracted_requirement="license-expression<1.2,>=0.1",
28+
scope="install",
29+
),
30+
]

tests/test_utils.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from python_inspector.resolution import fetch_and_extract_sdist
2222
from python_inspector.utils import get_netrc_auth
2323
from python_inspector.utils_pypi import PypiSimpleRepository
24+
from python_inspector.utils_pypi import valid_python_version
2425

2526
test_env = FileDrivenTesting()
2627
test_env.test_data_dir = os.path.join(os.path.dirname(__file__), "data")
@@ -87,3 +88,8 @@ def test_parse_reqs_with_setup_requires_and_python_requires():
8788
with open(result_file, "w") as file:
8889
json.dump(results, file, indent=4)
8990
check_json_results(result_file, expected_file, clean=False)
91+
92+
93+
def test_valid_python_version():
94+
assert valid_python_version("3.8", ">3.1")
95+
assert not valid_python_version("3.8.1", ">3.9")

0 commit comments

Comments
 (0)