-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathtest_pep621.py
169 lines (148 loc) · 5.85 KB
/
test_pep621.py
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
import unittest
from unittest import mock
from unittest.mock import mock_open
from unittest.mock import patch
from pkg_resources import parse_requirements
from flake8_requirements.checker import Flake8Checker
from flake8_requirements.checker import ModuleSet
from flake8_requirements.checker import memoize
class Flake8Options:
known_modules = ""
requirements_file = None
requirements_max_depth = 1
scan_host_site_packages = False
class Pep621TestCase(unittest.TestCase):
content = b"""
[project]
name="test"
dependencies=["tools==1.0"]
[project.optional-dependencies]
dev = ["dev-tools==1.0"]
"""
def setUp(self):
memoize.mem = {}
def tearDown(self):
Flake8Checker.root_dir = ""
def test_pyproject_custom_mapping_parser(self):
class Options(Flake8Options):
known_modules = {"mylib": ["mylib.drm", "mylib.ex"]}
Flake8Checker.parse_options(Options)
self.assertEqual(
Flake8Checker.known_modules,
{"mylib": ["mylib.drm", "mylib.ex"]},
)
def test_get_pyproject_toml_pep621(self):
with mock.patch('builtins.open', mock_open(read_data=self.content)):
pep621 = Flake8Checker.get_pyproject_toml_pep621()
expected = {
"name": "test",
"dependencies": ["tools==1.0"],
"optional-dependencies": {
"dev": ["dev-tools==1.0"]
},
}
self.assertDictEqual(pep621, expected)
def test_get_pyproject_toml_invalid(self):
content = self.content + b"invalid"
with mock.patch('builtins.open', mock_open(read_data=content)):
self.assertDictEqual(Flake8Checker.get_pyproject_toml_pep621(), {})
def test_1st_party(self):
with mock.patch('builtins.open', mock_open()) as m:
m.side_effect = (
IOError("No such file or directory: 'setup.py'"),
IOError("No such file or directory: 'setup.cfg'"),
mock_open(read_data=self.content).return_value,
)
checker = Flake8Checker(None, None)
mods = checker.get_mods_1st_party()
self.assertEqual(mods, ModuleSet({"test": {}}))
def test_3rd_party(self):
with mock.patch('builtins.open', mock_open()) as m:
m.side_effect = (
IOError("No such file or directory: 'setup.py'"),
IOError("No such file or directory: 'setup.cfg'"),
mock_open(read_data=self.content).return_value,
)
checker = Flake8Checker(None, None)
mods = checker.get_mods_3rd_party(False)
self.assertEqual(mods, ModuleSet({"tools": {}, "dev_tools": {}}))
def test_dynamic_requirements(self):
requirements_content = "package1\npackage2>=2.0"
data = {
"project": {"dynamic": ["dependencies"]},
"tool": {
"setuptools": {
"dynamic": {"dependencies": {"file": ["requirements.txt"]}}
}
},
}
with patch(
'flake8_requirements.checker.Flake8Checker.get_pyproject_toml',
return_value=data,
):
with patch(
'builtins.open', mock_open(read_data=requirements_content)
):
result = Flake8Checker.get_setuptools_dynamic_requirements()
expected_results = ['package1', 'package2>=2.0']
parsed_results = [str(req) for req in result]
self.assertEqual(parsed_results, expected_results)
def test_dynamic_optional_dependencies(self):
data = {
"project": {"dynamic": ["dependencies", "optional-dependencies"]},
"tool": {
"setuptools": {
"dynamic": {
"dependencies": {"file": ["requirements.txt"]},
"optional-dependencies": {
"test": {"file": ["optional-requirements.txt"]}
},
}
}
},
}
requirements_content = """
package1
package2>=2.0
"""
optional_requirements_content = "package3[extra] >= 3.0"
with mock.patch(
'flake8_requirements.checker.Flake8Checker.get_pyproject_toml',
return_value=data,
):
with mock.patch('builtins.open', mock.mock_open()) as mocked_file:
mocked_file.side_effect = [
mock.mock_open(
read_data=requirements_content
).return_value,
mock.mock_open(
read_data=optional_requirements_content
).return_value,
]
result = Flake8Checker.get_setuptools_dynamic_requirements()
expected = list(parse_requirements(requirements_content))
expected += list(
parse_requirements(optional_requirements_content)
)
self.assertEqual(len(result), len(expected))
for i in range(len(result)):
self.assertEqual(result[i], expected[i])
def test_missing_requirements_file(self):
data = {
"project": {"dynamic": ["dependencies"]},
"tool": {
"setuptools": {
"dynamic": {
"dependencies": {
"file": ["nonexistent-requirements.txt"]
}
}
}
},
}
with mock.patch(
'flake8_requirements.checker.Flake8Checker.get_pyproject_toml',
return_value=data,
):
result = Flake8Checker.get_setuptools_dynamic_requirements()
self.assertEqual(result, [])