-
-
Notifications
You must be signed in to change notification settings - Fork 166
/
main.py
156 lines (140 loc) · 5.46 KB
/
main.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
import os
import sys
import unittest
from contextlib import contextmanager
from cProfile import Profile
from pylint.lint import Run
from pylint_odoo import misc
EXPECTED_ERRORS = {
'api-one-deprecated': 4,
'api-one-multi-together': 2,
'attribute-deprecated': 2,
'class-camelcase': 1,
'consider-merging-classes-inherited': 2,
'copy-wo-api-one': 2,
'create-user-wo-reset-password': 1,
'dangerous-filter-wo-user': 1,
'dangerous-view-replace-wo-priority': 5,
'deprecated-openerp-xml-node': 5,
'duplicate-id-csv': 2,
'duplicate-xml-fields': 6,
'duplicate-xml-record-id': 2,
'file-not-used': 8,
'incoherent-interpreter-exec-perm': 3,
'invalid-commit': 4,
'javascript-lint': 2,
'license-allowed': 1,
'manifest-author-string': 1,
'manifest-deprecated-key': 1,
'manifest-required-author': 1,
'manifest-required-key': 1,
'manifest-version-format': 2,
'method-compute': 1,
'method-inverse': 1,
'method-required-super': 8,
'method-search': 1,
'missing-newline-extrafiles': 3,
'missing-readme': 1,
'no-utf8-coding-comment': 3,
'odoo-addons-relative-import': 4,
'old-api7-method-defined': 2,
'openerp-exception-warning': 3,
'redundant-modulename-xml': 1,
'rst-syntax-error': 2,
'sql-injection': 6,
'translation-field': 2,
'translation-required': 4,
'use-vim-comment': 1,
'wrong-tabs-instead-of-spaces': 2,
'xml-syntax-error': 2,
}
@contextmanager
def profiling(profile):
profile.enable()
yield
profile.disable()
class MainTest(unittest.TestCase):
def setUp(self):
self.default_options = [
'--load-plugins=pylint_odoo', '--reports=no', '--msg-template='
'"{path}:{line}: [{msg_id}({symbol}), {obj}] {msg}"',
'--output-format=colorized',
]
path_modules = os.path.join(
os.path.dirname(os.path.dirname(os.path.realpath(__file__))),
'test_repo')
self.paths_modules = []
root, dirs, _ = os.walk(path_modules).next()
for path in dirs:
self.paths_modules.append(os.path.join(root, path))
self.default_extra_params = [
'--disable=all',
'--enable=odoolint,pointless-statement,trailing-newlines',
]
self.profile = Profile()
self.sys_path_origin = list(sys.path)
def tearDown(self):
sys.path = list(self.sys_path_origin)
test = self._testMethodName
prefix = os.path.expanduser(os.environ.get('PYLINT_ODOO_STATS',
'~/pylint_odoo_cprofile'))
fstats = prefix + '_' + test + '.stats'
if test != 'test_10_path_dont_exist':
self.profile.dump_stats(fstats)
def run_pylint(self, paths, extra_params=None):
for path in paths:
if not os.path.exists(path):
raise OSError('Path "{path}" not found.'.format(path=path))
if extra_params is None:
extra_params = self.default_extra_params
sys.path.extend(paths)
cmd = self.default_options + extra_params + paths
with profiling(self.profile):
res = Run(cmd, exit=False)
return res
def test_10_path_dont_exist(self):
"self-test if path don't exist"
path_unexist = u'/tmp/____unexist______'
with self.assertRaisesRegexp(
OSError,
r'Path "{path}" not found.$'.format(path=path_unexist)):
self.run_pylint([path_unexist])
def test_20_expected_errors(self):
pylint_res = self.run_pylint(self.paths_modules)
# Expected vs found errors
real_errors = pylint_res.linter.stats['by_msg']
self.assertEqual(sorted(real_errors.items()),
sorted(EXPECTED_ERRORS.items()))
# All odoolint name errors vs found
msgs_found = pylint_res.linter.stats['by_msg'].keys()
plugin_msgs = misc.get_plugin_msgs(pylint_res)
test_missed_msgs = sorted(list(set(plugin_msgs) - set(msgs_found)))
self.assertEqual(
test_missed_msgs, [],
"Checks without test case: {test_missed_msgs}".format(
test_missed_msgs=test_missed_msgs))
sum_fails_found = misc.get_sum_fails(pylint_res.linter.stats)
sum_fails_expected = sum(EXPECTED_ERRORS.values())
self.assertEqual(sum_fails_found, sum_fails_expected)
def test_30_disabling_errors(self):
# Disabling
self.default_extra_params.append('--disable=dangerous-filter-wo-user')
pylint_res = self.run_pylint(self.paths_modules)
real_errors = pylint_res.linter.stats['by_msg']
global EXPECTED_ERRORS
EXPECTED_ERRORS.pop('dangerous-filter-wo-user')
self.assertEqual(sorted(real_errors.items()),
sorted(EXPECTED_ERRORS.items()))
sum_fails_found = misc.get_sum_fails(pylint_res.linter.stats)
sum_fails_expected = sum(EXPECTED_ERRORS.values())
self.assertEqual(sum_fails_found, sum_fails_expected)
def test_40_deprecated_modules(self):
"""Test deprecated modules"""
extra_params = ['--disable=all',
'--enable=deprecated-module',
'--deprecated-modules=openerp.osv']
pylint_res = self.run_pylint(self.paths_modules, extra_params)
real_errors = pylint_res.linter.stats['by_msg']
self.assertEqual(real_errors.items(), [('deprecated-module', 4)])
if __name__ == '__main__':
unittest.main()