forked from arkq/flake8-requirements
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_checker.py
140 lines (112 loc) · 4.13 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
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 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_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_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",
)