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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
|
# Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""Test the lint module."""
from __future__ import print_function
import collections
import StringIO
from chromite.cli.cros import lint
from chromite.lib import cros_test_lib
# pylint: disable=protected-access
class TestNode(object):
"""Object good enough to stand in for lint funcs"""
Args = collections.namedtuple('Args', ('args', 'vararg', 'kwarg'))
Arg = collections.namedtuple('Arg', ('name',))
def __init__(self, doc='', fromlineno=0, path='foo.py', args=(), vararg='',
kwarg='', names=None, lineno=0, name='module'):
if names is None:
names = [('name', None)]
self.doc = doc
self.lines = doc.split('\n')
self.fromlineno = fromlineno
self.lineno = lineno
self.file = path
self.args = self.Args(args=[self.Arg(name=x) for x in args],
vararg=vararg, kwarg=kwarg)
self.names = names
self.name = name
def argnames(self):
return self.args
class CheckerTestCase(cros_test_lib.TestCase):
"""Helpers for Checker modules"""
def add_message(self, msg_id, node=None, line=None, args=None):
"""Capture lint checks"""
# We include node.doc here explicitly so the pretty assert message
# inclues it in the output automatically.
doc = node.doc if node else ''
self.results.append((msg_id, doc, line, args))
def setUp(self):
assert hasattr(self, 'CHECKER'), 'TestCase must set CHECKER'
self.results = []
self.checker = self.CHECKER()
self.checker.add_message = self.add_message
class DocStringCheckerTest(CheckerTestCase):
"""Tests for DocStringChecker module"""
GOOD_FUNC_DOCSTRINGS = (
'Some string',
"""Short summary
Body of text.
""",
"""line o text
Body and comments on
more than one line.
Args:
moo: cow
Returns:
some value
Raises:
something else
""",
"""Short summary.
Args:
fat: cat
Yields:
a spoon
""",
)
BAD_FUNC_DOCSTRINGS = (
"""
bad first line
""",
"""The first line is good
but the second one isn't
""",
""" whitespace is wrong""",
"""whitespace is wrong """,
""" whitespace is wrong
Multiline tickles differently.
""",
"""Should be no trailing blank lines
Returns:
a value
""",
"""ok line
cuddled end""",
"""we want Args/Returns not Arguments/Return
Arguments:
Return:
""",
"""section order is wrong here
Raises:
Returns:
""",
"""sections lack whitespace between them
Args:
foo: bar
Returns:
yeah
""",
"""yields is misspelled
Yield:
a car
""",
"""Section name has bad spacing
Args:\x20\x20\x20
key: here
""",
"""too many blank lines
Returns:
None
""",
"""wrongly uses javadoc
@returns None
""",
"""the indentation is incorrect
Args:
some: day
""",
)
# The current linter isn't good enough yet to detect these.
TODO_BAD_FUNC_DOCSTRINGS = (
"""The returns section isn't a proper section
Args:
bloop: de
returns something
""",
)
CHECKER = lint.DocStringChecker
def testGood_visit_function(self):
"""Allow known good docstrings"""
for dc in self.GOOD_FUNC_DOCSTRINGS:
self.results = []
node = TestNode(doc=dc)
self.checker.visit_function(node)
self.assertEqual(self.results, [],
msg='docstring was not accepted:\n"""%s"""' % dc)
def testBad_visit_function(self):
"""Reject known bad docstrings"""
for dc in self.BAD_FUNC_DOCSTRINGS:
self.results = []
node = TestNode(doc=dc)
self.checker.visit_function(node)
self.assertNotEqual(self.results, [],
msg='docstring was not rejected:\n"""%s"""' % dc)
def testSmoke_visit_module(self):
"""Smoke test for modules"""
self.checker.visit_module(TestNode(doc='foo'))
self.assertEqual(self.results, [])
self.checker.visit_module(TestNode(doc='', path='/foo/__init__.py'))
self.assertEqual(self.results, [])
def testSmoke_visit_class(self):
"""Smoke test for classes"""
self.checker.visit_class(TestNode(doc='bar'))
def testGood_check_first_line(self):
"""Verify _check_first_line accepts good inputs"""
docstrings = (
'Some string',
)
for dc in docstrings:
self.results = []
node = TestNode(doc=dc)
self.checker._check_first_line(node, node.lines)
self.assertEqual(self.results, [],
msg='docstring was not accepted:\n"""%s"""' % dc)
def testBad_check_first_line(self):
"""Verify _check_first_line rejects bad inputs"""
docstrings = (
'\nSome string\n',
)
for dc in docstrings:
self.results = []
node = TestNode(doc=dc)
self.checker._check_first_line(node, node.lines)
self.assertEqual(len(self.results), 1)
def testGood_check_second_line_blank(self):
"""Verify _check_second_line_blank accepts good inputs"""
docstrings = (
'Some string\n\nThis is the third line',
'Some string',
)
for dc in docstrings:
self.results = []
node = TestNode(doc=dc)
self.checker._check_second_line_blank(node, node.lines)
self.assertEqual(self.results, [],
msg='docstring was not accepted:\n"""%s"""' % dc)
def testBad_check_second_line_blank(self):
"""Verify _check_second_line_blank rejects bad inputs"""
docstrings = (
'Some string\nnonempty secondline',
)
for dc in docstrings:
self.results = []
node = TestNode(doc=dc)
self.checker._check_second_line_blank(node, node.lines)
self.assertEqual(len(self.results), 1)
def testGoodFuncVarKwArg(self):
"""Check valid inputs for *args and **kwargs"""
for vararg in (None, 'args', '_args'):
for kwarg in (None, 'kwargs', '_kwargs'):
self.results = []
node = TestNode(vararg=vararg, kwarg=kwarg)
self.checker._check_func_signature(node)
self.assertEqual(len(self.results), 0)
def testMisnamedFuncVarKwArg(self):
"""Reject anything but *args and **kwargs"""
for vararg in ('arg', 'params', 'kwargs', '_moo'):
self.results = []
node = TestNode(vararg=vararg)
self.checker._check_func_signature(node)
self.assertEqual(len(self.results), 1)
for kwarg in ('kwds', '_kwds', 'args', '_moo'):
self.results = []
node = TestNode(kwarg=kwarg)
self.checker._check_func_signature(node)
self.assertEqual(len(self.results), 1)
def testGoodFuncArgs(self):
"""Verify normal args in Args are allowed"""
datasets = (
("""args are correct, and cls is ignored
Args:
moo: cow
""",
('cls', 'moo',), None, None,
),
("""args are correct, and self is ignored
Args:
moo: cow
*args: here
""",
('self', 'moo',), 'args', 'kwargs',
),
("""args are allowed to wrap
Args:
moo:
a big fat cow
that takes many lines
to describe its fatness
""",
('moo',), None, 'kwargs',
),
)
for dc, args, vararg, kwarg in datasets:
self.results = []
node = TestNode(doc=dc, args=args, vararg=vararg, kwarg=kwarg)
self.checker._check_all_args_in_doc(node, node.lines)
self.assertEqual(len(self.results), 0)
def testBadFuncArgs(self):
"""Verify bad/missing args in Args are caught"""
datasets = (
("""missing 'bar'
Args:
moo: cow
""",
('moo', 'bar',),
),
("""missing 'cow' but has 'bloop'
Args:
moo: cow
""",
('bloop',),
),
("""too much space after colon
Args:
moo: cow
""",
('moo',),
),
("""not enough space after colon
Args:
moo:cow
""",
('moo',),
),
)
for dc, args in datasets:
self.results = []
node = TestNode(doc=dc, args=args)
self.checker._check_all_args_in_doc(node, node.lines)
self.assertEqual(len(self.results), 1)
class ChromiteLoggingCheckerTest(CheckerTestCase):
"""Tests for ChromiteLoggingChecker module"""
CHECKER = lint.ChromiteLoggingChecker
def testLoggingImported(self):
"""Test that import logging is flagged."""
node = TestNode(names=[('logging', None)], lineno=15)
self.checker.visit_import(node)
self.assertEqual(self.results, [('R9301', '', 15, None)])
def testLoggingNotImported(self):
"""Test that importing something else (not logging) is not flagged."""
node = TestNode(names=[('myModule', None)], lineno=15)
self.checker.visit_import(node)
self.assertEqual(self.results, [])
class SourceCheckerTest(CheckerTestCase):
"""Tests for SourceChecker module"""
CHECKER = lint.SourceChecker
def _testShebang(self, shebangs, exp, fileno):
"""Helper for shebang tests"""
for shebang in shebangs:
self.results = []
node = TestNode()
stream = StringIO.StringIO(shebang)
stream.fileno = lambda: fileno
self.checker._check_shebang(node, stream)
self.assertEqual(len(self.results), exp,
msg='processing shebang failed: %r' % shebang)
def testBadShebangNoExec(self):
"""Verify _check_shebang rejects bad shebangs"""
shebangs = (
'#!/usr/bin/python\n',
'#! /usr/bin/python2 \n',
'#!/usr/bin/env python3\n',
)
with open('/dev/null') as f:
self._testShebang(shebangs, 2, f.fileno())
def testGoodShebang(self):
"""Verify _check_shebang accepts good shebangs"""
shebangs = (
'#!/usr/bin/python2\n',
'#!/usr/bin/python2 \n',
'#!/usr/bin/python3\n',
'#!/usr/bin/python3\t\n',
)
with open('/bin/sh') as f:
self._testShebang(shebangs, 0, f.fileno())
def testGoodUnittestName(self):
"""Verify _check_module_name accepts good unittest names"""
module_names = (
'lint_unittest',
)
for name in module_names:
node = TestNode(name=name)
self.results = []
self.checker._check_module_name(node)
self.assertEqual(len(self.results), 0)
def testBadUnittestName(self):
"""Verify _check_module_name accepts good unittest names"""
module_names = (
'lint_unittests',
)
for name in module_names:
node = TestNode(name=name)
self.results = []
self.checker._check_module_name(node)
self.assertEqual(len(self.results), 1)
|