forked from arkq/flake8-requirements
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_setup.py
114 lines (101 loc) · 3.58 KB
/
test_setup.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
import ast
import os
import unittest
from pkg_resources import parse_requirements
from flake8_requirements.checker import Flake8Checker
from flake8_requirements.checker import SetupVisitor
try:
from unittest import mock
builtins_open = 'builtins.open'
except ImportError:
import mock
builtins_open = '__builtin__.open'
class SetupTestCase(unittest.TestCase):
def test_detect_setup(self):
code = "setup({})".format(",".join((
"name='A'",
"version='1'",
"author='A'",
"packages=['']",
"url='URL'",
)))
setup = SetupVisitor(ast.parse(code), "")
self.assertEqual(setup.redirected, True)
self.assertDictEqual(setup.keywords, {
'name': 'A',
'version': '1',
'packages': [''],
'author': 'A',
'url': 'URL',
})
code = "setup({})".format(",".join(
"{}='{}'".format(x, x)
for x in SetupVisitor.attributes
))
setup = SetupVisitor(ast.parse(code), "")
self.assertEqual(setup.redirected, True)
self.assertDictEqual(setup.keywords, {
x: x for x in SetupVisitor.attributes
})
code = "setup({})".format(",".join((
"name='A'",
"version='1'",
"package='ABC'",
"processing=True",
"verbose=True",
)))
setup = SetupVisitor(ast.parse(code), "")
self.assertEqual(setup.redirected, False)
def test_detect_setup_wrong_num_of_args(self):
setup = SetupVisitor(ast.parse("setup(name='A')"), "")
self.assertEqual(setup.redirected, False)
def test_detect_setup_wrong_function(self):
setup = SetupVisitor(ast.parse("setup(1, name='A')"), "")
self.assertEqual(setup.redirected, False)
def test_detect_setup_oops(self):
setup = SetupVisitor(ast.parse("\n".join((
"from .myModule import setup",
"setup({})".format(",".join((
"name='A'",
"version='1'",
"author='A'",
"packages=['']",
"url='URL'",
))),
))), "")
self.assertEqual(setup.redirected, False)
def test_get_requirements(self):
setup = SetupVisitor(ast.parse("setup(**{})".format(str({
'name': 'A',
'version': '1',
'packages': [''],
'install_requires': ["ABC > 1.0.0", "bar.cat > 2, < 3"],
'extras_require': {
'extra': ["extra < 10"],
},
}))), "")
self.assertEqual(setup.redirected, True)
self.assertEqual(
sorted(setup.get_requirements(), key=lambda x: x.project_name),
sorted(parse_requirements([
"ABC > 1.0.0",
"bar.cat > 2, < 3",
"extra < 10",
]), key=lambda x: x.project_name),
)
def test_get_setup_cfg_requirements(self):
curdir = os.path.abspath(os.path.dirname(__file__))
with open(os.path.join(curdir, "test_setup.cfg")) as f:
content = f.read()
with mock.patch(builtins_open, mock.mock_open(read_data=content)):
checker = Flake8Checker(None, None)
self.assertEqual(
checker.get_setup_cfg_requirements(False),
list(parse_requirements([
"requests",
"importlib; python_version == \"2.6\"",
"pytest",
"ReportLab>=1.2",
"docutils>=0.3",
])),
)