forked from aboutcode-org/python-inspector
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_utils.py
More file actions
166 lines (137 loc) · 5.96 KB
/
test_utils.py
File metadata and controls
166 lines (137 loc) · 5.96 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
#!/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 collections
import json
import os
import sys
from netrc import netrc
from unittest import mock
import pytest
from commoncode.testcase import FileDrivenTesting
from test_cli import check_json_file_results
from _packagedcode.pypi import SetupCfgHandler
from python_inspector.resolution import fetch_and_extract_sdist
from python_inspector.utils import get_netrc_auth
from python_inspector.utils_pypi import PypiSimpleRepository
from python_inspector.utils_pypi import valid_python_version
test_env = FileDrivenTesting()
test_env.test_data_dir = os.path.join(os.path.dirname(__file__), "data")
Candidate = collections.namedtuple("Candidate", "name version extras")
def test_get_netrc_auth():
netrc_file = test_env.get_test_loc("test.netrc")
parsed_netrc = netrc(netrc_file)
assert get_netrc_auth(url="https://pyp1.org/simple", netrc=parsed_netrc) == ("test", "test123")
assert get_netrc_auth(url="https://pyp1.org/different/path", netrc=parsed_netrc) == (
"test",
"test123",
)
assert get_netrc_auth(url="https://pyp1.org", netrc=parsed_netrc) == ("test", "test123")
def test_get_netrc_auth_with_ports_and_schemes():
netrc_file = test_env.get_test_loc("test.netrc")
parsed_netrc = netrc(netrc_file)
assert get_netrc_auth(url="https://pyp1.org:443/path", netrc=parsed_netrc) == (
"test",
"test123",
)
assert get_netrc_auth(url="http://pyp1.org:80/simple", netrc=parsed_netrc) == (
"test",
"test123",
)
def test_get_commented_netrc_auth():
netrc_file = test_env.get_test_loc("test-commented.netrc")
parsed_netrc = netrc(netrc_file)
assert get_netrc_auth(url="https://pyp2.org/simple", netrc=parsed_netrc) == ("test", "test123")
def test_get_netrc_auth_with_no_matching_url():
netrc_file = test_env.get_test_loc("test.netrc")
parsed_netrc = netrc(netrc_file)
assert get_netrc_auth(url="https://pypi2.org/simple", netrc=parsed_netrc) == (None, None)
def test_get_netrc_auth_with_with_subdomains():
netrc_file = test_env.get_test_loc("test.netrc")
parsed_netrc = netrc(netrc_file)
assert get_netrc_auth(url="https://subdomain.example.com/simple", netrc=parsed_netrc) == (
"subdomain-user",
"subdomain-secret",
)
assert get_netrc_auth(url="https://another.example.com/simple", netrc=parsed_netrc) == (
None,
None,
)
def test_get_netrc_auth_with_default():
netrc_file = test_env.get_test_loc("test-default.netrc")
parsed_netrc = netrc(netrc_file)
assert get_netrc_auth(url="https://example.com/simple", netrc=parsed_netrc) == (
"test",
"test123",
)
assert get_netrc_auth(url="https://non-existing.org/simple", netrc=parsed_netrc) == (
"defaultuser",
"defaultpass",
)
@pytest.mark.asyncio
@pytest.mark.skipif(sys.version_info < (3, 8), reason="requires python3.8 or higher")
@mock.patch("python_inspector.utils_pypi.CACHE.get")
async def test_fetch_links(mock_get):
file_name = test_env.get_test_loc("psycopg2.html")
with open(file_name) as file:
mock_get.return_value = file.read(), file_name
links = await PypiSimpleRepository().fetch_links(normalized_name="psycopg2")
result_file = test_env.get_temp_file("json")
expected_file = test_env.get_test_loc("psycopg2-links-expected.json", must_exist=False)
with open(result_file, "w") as file:
json.dump(links, file, indent=4)
check_json_file_results(result_file, expected_file)
# Testing relative links
relative_links_file = test_env.get_test_loc("fetch_links_test.html")
with open(relative_links_file) as relative_file:
mock_get.return_value = relative_file.read(), relative_links_file
relative_links = await PypiSimpleRepository().fetch_links(normalized_name="sources.whl")
relative_links_result_file = test_env.get_temp_file("json")
relative_links_expected_file = test_env.get_test_loc(
"relative-links-expected.json", must_exist=False
)
with open(relative_links_result_file, "w") as file:
json.dump(relative_links, file, indent=4)
check_json_file_results(relative_links_result_file, relative_links_expected_file)
def test_parse_reqs():
results = [
package.to_dict() for package in SetupCfgHandler.parse(test_env.get_test_loc("setup.cfg"))
]
result_file = test_env.get_temp_file("json")
expected_file = test_env.get_test_loc("parse-reqs.json", must_exist=False)
with open(result_file, "w") as file:
json.dump(results, file, indent=4)
check_json_file_results(result_file, expected_file)
@pytest.mark.online
@pytest.mark.asyncio
async def test_get_sdist_file():
sdist_file = await fetch_and_extract_sdist(
repos=tuple([PypiSimpleRepository()]),
candidate=Candidate(name="psycopg2", version="2.7.5", extras=None),
python_version="3.8",
)
assert os.path.basename(os.path.normpath(sdist_file)) == "psycopg2-2.7.5"
def test_parse_reqs_with_setup_requires_and_python_requires():
results = [
package.to_dict()
for package in SetupCfgHandler.parse(
test_env.get_test_loc("setup_with_setup_requires_and_python_requires.cfg")
)
]
result_file = test_env.get_temp_file("json")
expected_file = test_env.get_test_loc(
"parse-reqs-with-setup_requires-and-python-requires.json", must_exist=False
)
with open(result_file, "w") as file:
json.dump(results, file, indent=4)
check_json_file_results(result_file, expected_file)
def test_valid_python_version():
assert valid_python_version("3.8", ">3.1")
assert not valid_python_version("3.8.1", ">3.9")