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
|
# -*- encoding: utf-8 -*-
# Copyright (C) 2012-2014 Red Hat, Inc. All rights reserved.
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
#
# Authors: Michal Minar <miminar@redhat.com>
#
"""Pylint plugin to check OpenLMI unittest modules."""
from logilab.astng import scoped_nodes
from pylint.interfaces import IASTNGChecker
from pylint.checkers import BaseChecker
import unittest
def is_test_case_method(node):
"""
Check, whether node method is defined by TestCase base class.
"""
if ( isinstance(node, scoped_nodes.Function)
and node.name in unittest.TestCase.__dict__):
return True
return False
def is_test_case_subclass(node):
"""
Check, whether node is a subclass of TestCase base class.
"""
if not isinstance(node, scoped_nodes.Class):
return False
for ancestor in node.ancestors():
if ( ancestor.name == 'TestCase'
and ancestor.getattr('__module__')[0].value.startswith('unittest')):
return True
return False
class TestCaseChecker(BaseChecker):
"""
Checker for OpenLMI unittest modules.
Right now it just suppresses unwanted messages.
"""
__implements__ = IASTNGChecker
name = 'unittest_case'
# When we do have some real warning messages/reports,
# remote W9920. We need it just to get registered.
msgs = { 'W9920': ('Dummy', "This is a dummy message.")}
priority = -1
def visit_class(self, node):
"""
Suppress Naming and 'Attribute creation out of __init__'
errors for every TestCase subclass.
"""
if not is_test_case_subclass(node):
return
if 'setUp' in node:
self.linter.disable('W0201',
scope='module', line=node['setUp'].lineno)
for child in node.get_children():
if not is_test_case_method(child):
continue
self.linter.disable('C0103',
scope='module', line=child.lineno)
def register(linter):
"""required method to auto register our checker"""
linter.register_checker(TestCaseChecker(linter))
|