/
test_hacking.py
228 lines (205 loc) · 11.2 KB
/
test_hacking.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# Copyright 2014 Red Hat, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from cinder.hacking import checks
from cinder import test
class HackingTestCase(test.TestCase):
"""This class tests the hacking checks in cinder.hacking.checks
This class ensures that Cinder's hacking checks are working by passing
strings to the check methods like the pep8/flake8 parser would. The parser
loops over each line in the file and then passes the parameters to the
check method. The parameter names in the check method dictate what type of
object is passed to the check method. The parameter types are::
logical_line: A processed line with the following modifications:
- Multi-line statements converted to a single line.
- Stripped left and right.
- Contents of strings replaced with "xxx" of same length.
- Comments removed.
physical_line: Raw line of text from the input file.
lines: a list of the raw lines from the input file
tokens: the tokens that contribute to this logical line
line_number: line number in the input file
total_lines: number of lines in the input file
blank_lines: blank lines before this one
indent_char: indentation character in this file (" " or "\t")
indent_level: indentation (with tabs expanded to multiples of 8)
previous_indent_level: indentation on previous line
previous_logical: previous logical line
filename: Path of the file being run through pep8
When running a test on a check method the return will be False/None if
there is no violation in the sample input. If there is an error a tuple is
returned with a position in the line, and a message. So to check the result
just assertTrue if the check is expected to fail and assertFalse if it
should pass.
"""
def test_no_vi_headers(self):
lines = ['Line 1\n', 'Line 2\n', 'Line 3\n', 'Line 4\n', 'Line 5\n',
'Line 6\n', 'Line 7\n', 'Line 8\n', 'Line 9\n', 'Line 10\n',
'Line 11\n']
self.assertEqual(None, checks.no_vi_headers(
"Test string foo", 1, lines))
self.assertEqual(2, len(list(checks.no_vi_headers(
"# vim: et tabstop=4 shiftwidth=4 softtabstop=4",
2, lines))))
self.assertEqual(2, len(list(checks.no_vi_headers(
"# vim: et tabstop=4 shiftwidth=4 softtabstop=4",
8, lines))))
self.assertEqual(None, checks.no_vi_headers(
"Test end string for vi",
9, lines))
# vim header outside of boundary (first/last 5 lines)
self.assertEqual(None, checks.no_vi_headers(
"# vim: et tabstop=4 shiftwidth=4 softtabstop=4",
6, lines))
def test_no_translate_debug_logs(self):
self.assertEqual(1, len(list(checks.no_translate_debug_logs(
"LOG.debug(_('foo'))", "cinder/scheduler/foo.py"))))
self.assertEqual(0, len(list(checks.no_translate_debug_logs(
"LOG.debug('foo')", "cinder/scheduler/foo.py"))))
self.assertEqual(0, len(list(checks.no_translate_debug_logs(
"LOG.info(_('foo'))", "cinder/scheduler/foo.py"))))
def test_check_explicit_underscore_import(self):
self.assertEqual(1, len(list(checks.check_explicit_underscore_import(
"LOG.info(_('My info message'))",
"cinder/tests/other_files.py"))))
self.assertEqual(1, len(list(checks.check_explicit_underscore_import(
"msg = _('My message')",
"cinder/tests/other_files.py"))))
self.assertEqual(0, len(list(checks.check_explicit_underscore_import(
"from cinder.i18n import _",
"cinder/tests/other_files.py"))))
self.assertEqual(0, len(list(checks.check_explicit_underscore_import(
"LOG.info(_('My info message'))",
"cinder/tests/other_files.py"))))
self.assertEqual(0, len(list(checks.check_explicit_underscore_import(
"msg = _('My message')",
"cinder/tests/other_files.py"))))
self.assertEqual(0, len(list(checks.check_explicit_underscore_import(
"from cinder.i18n import _, _LW",
"cinder/tests/other_files2.py"))))
self.assertEqual(0, len(list(checks.check_explicit_underscore_import(
"msg = _('My message')",
"cinder/tests/other_files2.py"))))
self.assertEqual(0, len(list(checks.check_explicit_underscore_import(
"_ = translations.ugettext",
"cinder/tests/other_files3.py"))))
self.assertEqual(0, len(list(checks.check_explicit_underscore_import(
"msg = _('My message')",
"cinder/tests/other_files3.py"))))
# Complete code coverage by falling through all checks
self.assertEqual(0, len(list(checks.check_explicit_underscore_import(
"LOG.info('My info message')",
"cinder/tests/other_files4.py"))))
def test_check_no_log_audit(self):
self.assertEqual(1, len(list(checks.check_no_log_audit(
"LOG.audit('My test audit log')"))))
self.assertEqual(0, len(list(checks.check_no_log_audit(
"LOG.info('My info test log.')"))))
def test_no_mutable_default_args(self):
self.assertEqual(0, len(list(checks.no_mutable_default_args(
"def foo (bar):"))))
self.assertEqual(1, len(list(checks.no_mutable_default_args(
"def foo (bar=[]):"))))
self.assertEqual(1, len(list(checks.no_mutable_default_args(
"def foo (bar={}):"))))
def test_check_assert_called_once(self):
self.assertEqual(0, len(list(checks.check_assert_called_once(
".assert_called_with(", "cinder/tests/test1.py"))))
self.assertEqual(0, len(list(checks.check_assert_called_once(
".assert_called_with(", "cinder/blah.py"))))
self.assertEqual(1, len(list(checks.check_assert_called_once(
".assert_called_once(", "cinder/tests/test1.py"))))
self.assertEqual(0, len(list(checks.check_assert_called_once(
".assertEqual(", "cinder/tests/test1.py"))))
def test_oslo_namespace_imports_check(self):
self.assertEqual(1, len(list(checks.check_oslo_namespace_imports(
"from oslo.concurrency import foo"))))
self.assertEqual(0, len(list(checks.check_oslo_namespace_imports(
"from oslo_concurrency import bar"))))
self.assertEqual(1, len(list(checks.check_oslo_namespace_imports(
"from oslo.db import foo"))))
self.assertEqual(0, len(list(checks.check_oslo_namespace_imports(
"from oslo_db import bar"))))
self.assertEqual(1, len(list(checks.check_oslo_namespace_imports(
"from oslo.config import foo"))))
self.assertEqual(0, len(list(checks.check_oslo_namespace_imports(
"from oslo_config import bar"))))
self.assertEqual(1, len(list(checks.check_oslo_namespace_imports(
"from oslo.utils import foo"))))
self.assertEqual(0, len(list(checks.check_oslo_namespace_imports(
"from oslo_utils import bar"))))
self.assertEqual(1, len(list(checks.check_oslo_namespace_imports(
"from oslo.serialization import foo"))))
self.assertEqual(0, len(list(checks.check_oslo_namespace_imports(
"from oslo_serialization import bar"))))
self.assertEqual(1, len(list(checks.check_oslo_namespace_imports(
"from oslo.log import foo"))))
self.assertEqual(0, len(list(checks.check_oslo_namespace_imports(
"from oslo_log import bar"))))
def test_no_contextlib_nested(self):
self.assertEqual(1, len(list(checks.check_no_contextlib_nested(
"with contextlib.nested("))))
self.assertEqual(1, len(list(checks.check_no_contextlib_nested(
" with nested("))))
self.assertEqual(0, len(list(checks.check_no_contextlib_nested(
"with my.nested("))))
self.assertEqual(0, len(list(checks.check_no_contextlib_nested(
"with foo as bar"))))
def test_check_datetime_now(self):
self.assertEqual(1, len(list(checks.check_datetime_now(
"datetime.now", False))))
self.assertEqual(0, len(list(checks.check_datetime_now(
"timeutils.utcnow", False))))
def test_check_datetime_now_noqa(self):
self.assertEqual(0, len(list(checks.check_datetime_now(
"datetime.now() # noqa", True))))
def test_validate_log_translations(self):
self.assertEqual(1, len(list(checks.validate_log_translations(
"LOG.info('foo')", "foo.py"))))
self.assertEqual(1, len(list(checks.validate_log_translations(
"LOG.warning('foo')", "foo.py"))))
self.assertEqual(1, len(list(checks.validate_log_translations(
"LOG.error('foo')", "foo.py"))))
self.assertEqual(1, len(list(checks.validate_log_translations(
"LOG.exception('foo')", "foo.py"))))
self.assertEqual(0, len(list(checks.validate_log_translations(
"LOG.info('foo')", "cinder/tests/foo.py"))))
self.assertEqual(0, len(list(checks.validate_log_translations(
"LOG.info(_LI('foo')", "foo.py"))))
self.assertEqual(0, len(list(checks.validate_log_translations(
"LOG.warning(_LW('foo')", "foo.py"))))
self.assertEqual(0, len(list(checks.validate_log_translations(
"LOG.error(_LE('foo')", "foo.py"))))
self.assertEqual(0, len(list(checks.validate_log_translations(
"LOG.exception(_LE('foo')", "foo.py"))))
def test_check_unicode_usage(self):
self.assertEqual(1, len(list(checks.check_unicode_usage(
"unicode(msg)", False))))
self.assertEqual(0, len(list(checks.check_unicode_usage(
"unicode(msg) # noqa", True))))
def test_no_print_statements(self):
self.assertEqual(0, len(list(checks.check_no_print_statements(
"a line with no print statement",
"cinder/file.py", False))))
self.assertEqual(1, len(list(checks.check_no_print_statements(
"print('My print statement')",
"cinder/file.py", False))))
self.assertEqual(0, len(list(checks.check_no_print_statements(
"print('My print statement in cinder/cmd, which is ok.')",
"cinder/cmd/file.py", False))))
self.assertEqual(0, len(list(checks.check_no_print_statements(
"print('My print statement that I just must have.')",
"cinder/tests/file.py", True))))
self.assertEqual(1, len(list(checks.check_no_print_statements(
"print ('My print with space')",
"cinder/volume/anotherFile.py", False))))