forked from arkq/flake8-requirements
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_checker.py
173 lines (140 loc) · 5.29 KB
/
test_checker.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
170
171
172
173
import ast
import unittest
from flake8_requirements import checker
class SetupVisitorMock(checker.SetupVisitor):
def __init__(self):
self.redirected = True
self.keywords = {
'name': "flake8-requires",
'install_requires': [
"foo",
"bar",
"hyp-hen",
"python-boom",
"pillow",
"space.module",
],
}
class Flake8Checker(checker.Flake8Checker):
@classmethod
def get_setup_py(cls):
return SetupVisitorMock()
@property
def processing_setup_py(self):
return self.filename == "setup.py"
class Flake8OptionManagerMock(dict):
def add_option(self, name, **kw):
self[name] = kw
class Flake8Options:
known_modules = ""
requirements_file = None
requirements_max_depth = 1
scan_host_site_packages = False
def check(code, filename="<unknown>", options=None):
if options is None:
options = Flake8Options
checker.memoize.mem = {}
Flake8Checker.parse_options(options)
return list(Flake8Checker(ast.parse(code), filename).run())
class Flake8CheckerTestCase(unittest.TestCase):
def test_add_options(self):
manager = Flake8OptionManagerMock()
Flake8Checker.add_options(manager)
self.assertEqual(
sorted(manager.keys()),
['--known-modules', '--requirements-file',
'--requirements-max-depth', '--scan-host-site-packages'],
)
def test_stdlib(self):
errors = check("import os\nfrom unittest import TestCase")
self.assertEqual(len(errors), 0)
def test_stdlib_case(self):
errors = check("from cProfile import Profile")
self.assertEqual(len(errors), 0)
errors = check("from cprofile import Profile")
self.assertEqual(len(errors), 1)
self.assertEqual(
errors[0][2],
"I900 'cprofile' not listed as a requirement",
)
def test_1st_party(self):
errors = check("import flake8_requires")
self.assertEqual(len(errors), 0)
def test_3rd_party(self):
errors = check("import foo\nfrom bar import Bar")
self.assertEqual(len(errors), 0)
def test_3rd_party_python_prefix(self):
errors = check("from boom import blast")
self.assertEqual(len(errors), 0)
def test_3rd_party_missing(self):
errors = check("import os\nfrom cat import Cat")
self.assertEqual(len(errors), 1)
self.assertEqual(
errors[0][2],
"I900 'cat' not listed as a requirement",
)
def test_3rd_party_hyphen(self):
errors = check("from hyp_hen import Hyphen")
self.assertEqual(len(errors), 0)
def test_3rd_party_known_module(self):
errors = check("import PIL")
self.assertEqual(len(errors), 0)
def test_non_top_level_import(self):
errors = check("def function():\n import cat")
self.assertEqual(len(errors), 1)
self.assertEqual(
errors[0][2],
"I900 'cat' not listed as a requirement",
)
def test_namespace(self):
errors = check("import space.module")
self.assertEqual(len(errors), 0)
errors = check("from space import module")
self.assertEqual(len(errors), 0)
errors = check("import space")
self.assertEqual(len(errors), 1)
def test_relative(self):
errors = check("from . import local")
self.assertEqual(len(errors), 0)
errors = check("from ..local import local")
self.assertEqual(len(errors), 0)
def test_discover_host_3rd_party_modules(self):
class Options(Flake8Options):
scan_host_site_packages = True
Flake8Checker.parse_options(Options)
self.assertEqual(
type(Flake8Checker.known_host_3rd_parties),
dict,
)
# Since flake8-requirements (this package) is a plugin for flake8, it
# is very likely that one will have flake8 installed in the host
# site-packages. However, that is not the case for our GitHub Actions
# runners, so we can not enforce this assertion.
if 'flake8' in Flake8Checker.known_host_3rd_parties:
self.assertEqual(
Flake8Checker.known_host_3rd_parties['flake8'],
['flake8'],
)
def test_custom_mapping_parser(self):
class Options(Flake8Options):
known_modules = ":[pydrmcodec],mylib:[mylib.drm,mylib.ex]"
Flake8Checker.parse_options(Options)
self.assertEqual(
Flake8Checker.known_modules,
{"": ["pydrmcodec"], "mylib": ["mylib.drm", "mylib.ex"]},
)
def test_custom_mapping(self):
class Options(Flake8Options):
known_modules = "flake8-requires:[flake8req]"
errors = check("from flake8req import mymodule", options=Options)
self.assertEqual(len(errors), 0)
def test_setup_py(self):
errors = check("from setuptools import setup", "setup.py")
self.assertEqual(len(errors), 0)
# mods_3rd_party
errors = check("from setuptools import setup", "xxx.py")
self.assertEqual(len(errors), 1)
self.assertEqual(
errors[0][2],
"I900 'setuptools' not listed as a requirement",
)