forked from klen/pylama
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_pylama.py
169 lines (124 loc) · 4.65 KB
/
test_pylama.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
import os.path as op
from pylama.async import check_async
from pylama.config import parse_options, get_config
from pylama.core import filter_errors, parse_modeline, prepare_params, run
from pylama.errors import Error, remove_duplicates
from pylama.hook import git_hook, hg_hook
from pylama.lint.extensions import LINTERS
from pylama.main import shell, check_path
def test_filter_errors():
assert list(filter_errors([Error(text='E1')], select=['E'], ignore=['E101']))
assert not list(filter_errors([Error(text='W1')], select=['W100'], ignore=['W']))
def test_remove_duplicates():
errors = [Error(linter='pycodestyle', text='E701'), Error(linter='pylint', text='C0321')]
errors = list(remove_duplicates(errors))
assert len(errors) == 1
def test_parser_modeline():
code = """
bla bla bla
# pylama: ignore=W12,E14:select=R:skip=0
"""
params = parse_modeline(code)
assert params == dict(ignore='W12,E14', select='R', skip='0')
def test_prepare_params():
p1 = dict(ignore='W', select='R01', skip='0')
p2 = dict(ignore='E34,R45', select='E')
options = parse_options(ignore=['D'], config=False)
params = prepare_params(p1, p2, options)
assert params == {
'ignore': set(['R45', 'E34', 'W', 'D']),
'select': set(['R01', 'E']),
'skip': False, 'linters': []}
def test_checkpath():
path = op.abspath('dummy.py')
options = parse_options([path])
result = check_path(options)
assert result
assert result[0].filename == 'dummy.py'
def test_mccabe():
mccabe = LINTERS.get('mccabe')
errors = mccabe.run('dummy.py', '', params={})
assert errors == []
def test_pyflakes():
options = parse_options(linters=['pyflakes'], config=False)
assert options.linters
errors = run('dummy.py', code="\n".join([
"import sys",
"def test():",
" unused = 1"
]), options=options)
assert len(errors) == 2
def test_pycodestyle():
options = parse_options(linters=['pycodestyle'], config=False)
assert len(options.linters) == 1
errors = run('dummy.py', options=options)
assert len(errors) == 2
options.linters_params['pycodestyle'] = dict(max_line_length=60)
errors = run('dummy.py', options=options)
assert len(errors) == 11
def test_pydocstyle():
options = parse_options(linters=['pydocstyle'])
assert len(options.linters) == 1
errors = run('dummy.py', options=options)
assert errors
# def test_radon():
# options = parse_options(linters=['radon'])
# options.linters_params['radon'] = dict(complexity=1)
# assert len(options.linters) == 1
# errors = run('dummy.py', options=options)
# assert errors
def test_linters_params():
options = parse_options(linters='mccabe', config=False)
options.linters_params['mccabe'] = dict(complexity=1)
errors = run('dummy.py', options=options)
assert len(errors) == 1
options.linters_params['mccabe'] = dict(complexity=20)
errors = run('dummy.py', options=options)
assert not errors
def test_sort():
options = parse_options()
options.sort = ['C', 'D']
errors = run('dummy.py', options=options)
assert errors[0].type == 'C'
def test_ignore_select():
options = parse_options()
options.ignore = ['E301', 'D102']
options.linters = ['pycodestyle', 'pydocstyle', 'pyflakes', 'mccabe']
errors = run('dummy.py', options=options)
assert len(errors) == 17
options.ignore = ['E3', 'D']
errors = run('dummy.py', options=options)
assert len(errors) == 0
options.select = ['E301']
errors = run('dummy.py', options=options)
assert len(errors) == 1
assert errors[0]['col']
def test_shell():
errors = shell('-o dummy dummy.py'.split(), error=False)
assert errors
errors = shell(['unknown.py'], error=False)
assert not errors
def test_git_hook():
assert not git_hook(False)
def test_hg_hook():
assert not hg_hook(None, dict())
def test_config():
config = get_config()
assert config
options = parse_options()
assert options
assert options.skip
assert not options.verbose
assert options.paths == ['pylama']
options = parse_options(['-l', 'pydocstyle,pycodestyle', '-i', 'E'])
linters, _ = zip(*options.linters)
assert set(linters) == set(['pydocstyle', 'pycodestyle'])
assert options.ignore == ['E']
options = parse_options('-o dummy dummy.py'.split())
linters, _ = zip(*options.linters)
assert set(linters) == set(['pycodestyle', 'mccabe', 'pyflakes'])
assert options.skip == []
def test_async():
options = parse_options(config=False)
errors = check_async(['dummy.py'], options=options, rootdir='.')
assert errors