forked from arkq/flake8-requirements
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_requirements.py
179 lines (154 loc) · 6.66 KB
/
test_requirements.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
174
175
176
177
178
179
import unittest
from collections import OrderedDict
from pkg_resources import parse_requirements
from flake8_requirements.checker import Flake8Checker
from flake8_requirements.checker import memoize
try:
from unittest import mock
builtins_open = 'builtins.open'
except ImportError:
import mock
builtins_open = '__builtin__.open'
def mock_open_with_name(read_data="", name="file.name"):
"""Mock open call with a specified `name` attribute."""
m = mock.mock_open(read_data=read_data)
m.return_value.name = name
return m
def mock_open_multiple(files=OrderedDict()):
"""Create a mock open object for multiple files."""
m = mock.mock_open()
m.side_effect = [
mock_open_with_name(read_data=content, name=name).return_value
for name, content in files.items()
]
return m
class RequirementsTestCase(unittest.TestCase):
def setUp(self):
memoize.mem = {}
def test_resolve_requirement(self):
self.assertEqual(
Flake8Checker.resolve_requirement("foo >= 1.0.0"),
["foo"],
)
def test_resolve_requirement_with_option(self):
self.assertEqual(
Flake8Checker.resolve_requirement("foo-bar.v1==1.0 --option"),
["foo-bar.v1"],
)
def test_resolve_requirement_standalone_option(self):
self.assertEqual(
Flake8Checker.resolve_requirement("--extra-index-url"),
[],
)
def test_resolve_requirement_with_file_beyond_max_depth(self):
with self.assertRaises(RuntimeError):
Flake8Checker.resolve_requirement("-r requirements.txt")
def test_resolve_requirement_with_file_empty(self):
with mock.patch(builtins_open, mock.mock_open()) as m:
self.assertEqual(
Flake8Checker.resolve_requirement("-r requirements.txt", 1),
[],
)
m.assert_called_once_with("requirements.txt")
def test_resolve_requirement_with_file_content(self):
with mock.patch(builtins_open, mock_open_multiple(files=OrderedDict((
("requirements.txt", "foo >= 1.0.0\nbar <= 1.0.0\n"),
)))):
self.assertEqual(
Flake8Checker.resolve_requirement("-r requirements.txt", 1),
["foo", "bar"],
)
def test_resolve_requirement_with_file_content_line_continuation(self):
with mock.patch(builtins_open, mock_open_multiple(files=OrderedDict((
("requirements.txt", "foo[bar] \\\n>= 1.0.0\n"),
)))):
self.assertEqual(
Flake8Checker.resolve_requirement("-r requirements.txt", 1),
["foo"],
)
def test_resolve_requirement_with_file_content_line_continuation_2(self):
with mock.patch(builtins_open, mock_open_multiple(files=OrderedDict((
("requirements.txt", "foo \\\n>= 1.0.0 \\\n# comment \\\nbar \\"),
)))):
self.assertEqual(
Flake8Checker.resolve_requirement("-r requirements.txt", 1),
["foo", "bar"],
)
def test_resolve_requirement_with_file_recursion_beyond_max_depth(self):
with mock.patch(builtins_open, mock_open_multiple(files=OrderedDict((
("requirements.txt", "-r requirements.txt\n"),
)))):
with self.assertRaises(RuntimeError):
Flake8Checker.resolve_requirement("-r requirements.txt", 1),
def test_resolve_requirement_with_file_recursion(self):
with mock.patch(builtins_open, mock_open_multiple(files=OrderedDict((
("requirements.txt", "--requirement inner.txt\nbar <= 1.0.0\n"),
("inner.txt", "# inner\nbaz\n\nqux\n"),
)))):
self.assertEqual(
Flake8Checker.resolve_requirement("-r requirements.txt", 2),
["baz", "qux", "bar"],
)
def test_resolve_requirement_with_relative_include(self):
with mock.patch(builtins_open, mock_open_multiple(files=OrderedDict((
("requirements.txt", "-r requirements/production.txt"),
("requirements/production.txt", "-r node/one.txt\nfoo"),
("requirements/node/one.txt", "-r common.txt\n-r /abs/path.txt"),
("requirements/node/common.txt", "bar"),
("/abs/path.txt", "bis"),
)))) as m:
self.assertEqual(
Flake8Checker.resolve_requirement("-r requirements.txt", 5),
["bar", "bis", "foo"],
)
m.assert_has_calls([
mock.call("requirements.txt"),
mock.call("requirements/production.txt"),
mock.call("requirements/node/one.txt"),
mock.call("requirements/node/common.txt"),
mock.call("/abs/path.txt"),
])
def test_init_with_no_requirements(self):
with mock.patch(builtins_open, mock.mock_open()) as m:
m.side_effect = IOError("No such file or directory"),
checker = Flake8Checker(None, None)
self.assertEqual(checker.get_requirements_txt(), ())
def test_init_with_simple_requirements(self):
with mock.patch(builtins_open, mock_open_multiple(files=OrderedDict((
("requirements.txt", "foo >= 1.0.0\nbar <= 1.0.0\n"),
)))):
checker = Flake8Checker(None, None)
self.assertEqual(
checker.get_requirements_txt(),
tuple(parse_requirements([
"foo",
"bar",
])),
)
def test_init_with_recursive_requirements_beyond_max_depth(self):
with mock.patch(builtins_open, mock_open_multiple(files=OrderedDict((
("requirements.txt", "foo >= 1.0.0\n-r inner.txt\nbar <= 1.0.0\n"),
("inner.txt", "# inner\nbaz\n\nqux\n"),
)))):
with self.assertRaises(RuntimeError):
try:
Flake8Checker.requirements_max_depth = 0
checker = Flake8Checker(None, None)
checker.get_requirements_txt()
finally:
Flake8Checker.requirements_max_depth = 1
def test_init_with_recursive_requirements(self):
with mock.patch(builtins_open, mock_open_multiple(files=OrderedDict((
("requirements.txt", "foo >= 1.0.0\n-r inner.txt\nbar <= 1.0.0\n"),
("inner.txt", "# inner\nbaz\n\nqux\n"),
)))):
checker = Flake8Checker(None, None)
self.assertEqual(
checker.get_requirements_txt(),
tuple(parse_requirements([
"foo",
"baz",
"qux",
"bar",
])),
)