forked from klen/pylama
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
163 lines (120 loc) · 4.34 KB
/
tests.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
import pytest
import os.path as op
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
from pylama.async import check_async
def test_filter_errors():
assert list(filter_errors([Error(text='E')], select=['E'], ignore=['E101']))
assert not list(filter_errors([Error(text='W')], select=['W100'], ignore=['W']))
def test_remove_duplicates():
errors = [Error(linter='pep8', 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_pep8():
options = parse_options(linters=['pep8'], config=False)
errors = run('dummy.py', options=options)
assert len(errors) == 2
options.linters_params['pep8'] = dict(max_line_length=60)
errors = run('dummy.py', options=options)
assert len(errors) == 11
def test_pep257():
options = parse_options(linters=['pep257'])
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=2)
errors = run('dummy.py', options=options)
assert len(errors) == 13
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 = ['pep8', 'pep257', 'pyflakes', 'mccabe']
errors = run('dummy.py', options=options)
assert len(errors) == 15
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():
with pytest.raises(SystemExit):
git_hook()
def test_hg_hook():
with pytest.raises(SystemExit):
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.path == 'pylama'
options = parse_options(['-l', 'pep257,pep8', '-i', 'E'])
linters, _ = zip(*options.linters)
assert set(linters) == set(['pep257', 'pep8'])
assert options.ignore == ['E']
options = parse_options('-o dummy dummy.py'.split())
linters, _ = zip(*options.linters)
assert set(linters) == set(['pep8', 'mccabe', 'pyflakes'])
assert options.skip == []
def test_async():
options = parse_options(config=False)
errors = check_async(['dummy.py'], options=options, rootdir='.')
assert errors