-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathtest_requirements.py
145 lines (117 loc) · 5.48 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
import unittest
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'
class RequirementsTestCase(unittest.TestCase):
def setUp(self):
memoize.mem = {}
def test_resolve_requirement_with_blank(self):
self.assertEqual(Flake8Checker.resolve_requirement(""), [])
def test_resolve_requirement_with_comment(self):
self.assertEqual(
Flake8Checker.resolve_requirement("#-r requirements.txt"),
[],
)
def test_resolve_requirement_with_simple(self):
self.assertEqual(
Flake8Checker.resolve_requirement("foo >= 1.0.0"),
["foo >= 1.0.0"],
)
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):
content = "foo >= 1.0.0\nbar <= 1.0.0\n"
with mock.patch(builtins_open, mock.mock_open(read_data=content)):
self.assertEqual(
Flake8Checker.resolve_requirement("-r requirements.txt", 1),
["foo >= 1.0.0", "bar <= 1.0.0"],
)
def test_resolve_requirement_with_file_recursion_beyond_max_depth(self):
content = "-r requirements.txt\n"
with mock.patch(builtins_open, mock.mock_open(read_data=content)):
with self.assertRaises(RuntimeError):
Flake8Checker.resolve_requirement("-r requirements.txt", 1),
def test_resolve_requirement_with_file_recursion(self):
content = "foo >= 1.0.0\n-r inner.txt\nbar <= 1.0.0\n"
inner_content = "# inner\nbaz\n\nqux\n"
with mock.patch(builtins_open, mock.mock_open()) as m:
m.side_effect = (
mock.mock_open(read_data=content).return_value,
mock.mock_open(read_data=inner_content).return_value,
)
self.assertEqual(
Flake8Checker.resolve_requirement("-r requirements.txt", 2),
["foo >= 1.0.0", "baz", "qux", "bar <= 1.0.0"],
)
def test_init_with_no_requirements(self):
with mock.patch("os.path.exists", return_value=False) as exists:
checker = Flake8Checker(None, None)
requirements = checker.get_requirements_txt()
self.assertEqual(requirements, ())
exists.assert_called_once_with("requirements.txt")
def test_init_with_simple_requirements(self):
content = "foo >= 1.0.0\nbar <= 1.0.0\n"
with mock.patch("os.path.exists", return_value=True):
with mock.patch(builtins_open, mock.mock_open()) as m:
m.side_effect = (
mock.mock_open(read_data=content).return_value,
)
checker = Flake8Checker(None, None)
requirements = checker.get_requirements_txt()
self.assertEqual(
sorted(requirements, key=lambda x: x.project_name),
sorted(parse_requirements([
"foo >= 1.0.0",
"bar <= 1.0.0",
]), key=lambda x: x.project_name),
)
def test_init_with_recursive_requirements_beyond_max_depth(self):
content = "foo >= 1.0.0\n-r inner.txt\nbar <= 1.0.0\n"
inner_content = "# inner\nbaz\n\nqux\n"
with mock.patch("os.path.exists", return_value=True):
with mock.patch(builtins_open, mock.mock_open()) as m:
m.side_effect = (
mock.mock_open(read_data=content).return_value,
mock.mock_open(read_data=inner_content).return_value,
)
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):
content = "foo >= 1.0.0\n-r inner.txt\nbar <= 1.0.0\n"
inner_content = "# inner\nbaz\n\nqux\n"
with mock.patch("os.path.exists", return_value=True):
with mock.patch(builtins_open, mock.mock_open()) as m:
m.side_effect = (
mock.mock_open(read_data=content).return_value,
mock.mock_open(read_data=inner_content).return_value,
)
checker = Flake8Checker(None, None)
requirements = checker.get_requirements_txt()
self.assertEqual(
sorted(requirements, key=lambda x: x.project_name),
sorted(parse_requirements([
"foo >= 1.0.0",
"baz",
"qux",
"bar <= 1.0.0",
]), key=lambda x: x.project_name),
)