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
|
# Copyright (c) 2010, 2012, 2014 LOGILAB S.A. (Paris, FRANCE) <contact@logilab.fr>
# Copyright (c) 2012 Ry4an Brase <ry4an-hg@ry4an.org>
# Copyright (c) 2014 Google, Inc.
# Copyright (c) 2015 Ionel Cristian Maries <contact@ionelmc.ro>
# Copyright (c) 2016-2017 Claudiu Popa <pcmanticore@gmail.com>
# Copyright (c) 2016 Derek Gustafson <degustaf@gmail.com>
# Copyright (c) 2018 Sushobhit <31987769+sushobhit27@users.noreply.github.com>
# Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
# For details: https://github.com/PyCQA/pylint/blob/master/COPYING
import sys
from contextlib import redirect_stdout
from io import StringIO
from os.path import abspath, dirname, join
import pytest
from pylint.checkers import similar
SIMILAR1 = join(dirname(abspath(__file__)), "input", "similar1")
SIMILAR2 = join(dirname(abspath(__file__)), "input", "similar2")
MULTILINE = join(dirname(abspath(__file__)), "input", "multiline-import")
HIDE_CODE_WITH_IMPORTS = join(
dirname(abspath(__file__)), "input", "hide_code_with_imports.py"
)
def test_ignore_comments():
output = StringIO()
with redirect_stdout(output), pytest.raises(SystemExit) as ex:
similar.Run(["--ignore-comments", SIMILAR1, SIMILAR2])
assert ex.value.code == 0
assert (
output.getvalue().strip()
== (
"""
10 similar lines in 2 files
==%s:0
==%s:0
import one
from two import two
three
four
five
six
seven
eight
nine
''' ten
TOTAL lines=60 duplicates=10 percent=16.67
"""
% (SIMILAR1, SIMILAR2)
).strip()
)
def test_ignore_docsrings():
output = StringIO()
with redirect_stdout(output), pytest.raises(SystemExit) as ex:
similar.Run(["--ignore-docstrings", SIMILAR1, SIMILAR2])
assert ex.value.code == 0
assert (
output.getvalue().strip()
== (
"""
8 similar lines in 2 files
==%s:6
==%s:6
seven
eight
nine
''' ten
ELEVEN
twelve '''
thirteen
fourteen
5 similar lines in 2 files
==%s:0
==%s:0
import one
from two import two
three
four
five
TOTAL lines=60 duplicates=13 percent=21.67
"""
% ((SIMILAR1, SIMILAR2) * 2)
).strip()
)
def test_ignore_imports():
output = StringIO()
with redirect_stdout(output), pytest.raises(SystemExit) as ex:
similar.Run(["--ignore-imports", SIMILAR1, SIMILAR2])
assert ex.value.code == 0
assert (
output.getvalue().strip()
== """
TOTAL lines=60 duplicates=0 percent=0.00
""".strip()
)
def test_multiline_imports():
output = StringIO()
with redirect_stdout(output), pytest.raises(SystemExit) as ex:
similar.Run([MULTILINE, MULTILINE])
assert ex.value.code == 0
assert (
output.getvalue().strip()
== (
"""
8 similar lines in 2 files
==%s:0
==%s:0
from foo import (
bar,
baz,
quux,
quuux,
quuuux,
quuuuux,
)
TOTAL lines=16 duplicates=8 percent=50.00
"""
% (MULTILINE, MULTILINE)
).strip()
)
def test_ignore_multiline_imports():
output = StringIO()
with redirect_stdout(output), pytest.raises(SystemExit) as ex:
similar.Run(["--ignore-imports", MULTILINE, MULTILINE])
assert ex.value.code == 0
assert (
output.getvalue().strip()
== """
TOTAL lines=16 duplicates=0 percent=0.00
""".strip()
)
def test_no_hide_code_with_imports():
output = StringIO()
with redirect_stdout(output), pytest.raises(SystemExit) as ex:
similar.Run(["--ignore-imports"] + 2 * [HIDE_CODE_WITH_IMPORTS])
assert ex.value.code == 0
assert "TOTAL lines=32 duplicates=16 percent=50.00" in output.getvalue()
def test_ignore_nothing():
output = StringIO()
with redirect_stdout(output), pytest.raises(SystemExit) as ex:
similar.Run([SIMILAR1, SIMILAR2])
assert ex.value.code == 0
assert (
output.getvalue().strip()
== (
"""
5 similar lines in 2 files
==%s:0
==%s:0
import one
from two import two
three
four
five
TOTAL lines=60 duplicates=5 percent=8.33
"""
% (SIMILAR1, SIMILAR2)
).strip()
)
def test_help():
output = StringIO()
with redirect_stdout(output):
try:
similar.Run(["--help"])
except SystemExit as ex:
assert ex.code == 0
else:
pytest.fail("not system exit")
def test_no_args():
output = StringIO()
with redirect_stdout(output):
try:
similar.Run([])
except SystemExit as ex:
assert ex.code == 1
else:
pytest.fail("not system exit")
|