forked from gforcada/flake8-pep3101
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_tests.py
158 lines (134 loc) · 4.95 KB
/
run_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
# -*- coding: utf-8 -*-
from flake8_pep3101 import Flake8Pep3101
from flake8.main import application
from tempfile import mkdtemp
from testfixtures import OutputCapture
import os
import unittest
class TestFlake8Pep3101(unittest.TestCase):
@staticmethod
def _given_a_file_in_test_dir(contents):
test_dir = os.path.realpath(mkdtemp())
file_path = os.path.join(test_dir, 'test.py')
with open(file_path, 'w') as a_file:
a_file.write(contents)
return file_path
def test_no_old_formatter(self):
file_path = self._given_a_file_in_test_dir(
'b = 3\n'
)
app = application.Application()
with OutputCapture() as output:
app.run([file_path, ])
self.assertEqual(
output.captured,
''
)
def test_new_formatting_no_problem(self):
file_path = self._given_a_file_in_test_dir('\n'.join([
'print("hello {0:s}".format("world"))\n',
]))
app = application.Application()
with OutputCapture() as output:
app.run([file_path, ])
self.assertEqual(
output.captured,
''
)
def test_s_formatter(self):
file_path = self._given_a_file_in_test_dir('\n'.join([
'print("hello %s" % (\'lo\'))',
]))
checker = Flake8Pep3101(None, file_path)
ret = list(checker.run())
self.assertEqual(len(ret), 1)
self.assertEqual(ret[0][0], 1)
self.assertEqual(ret[0][1], 6)
self.assertEqual(ret[0][2], 'S001 found module formatter')
def test_multiline_formatter(self):
file_path = self._given_a_file_in_test_dir('\n'.join([
'print("hello %s"',
'% (\'world\'))',
]))
app = application.Application()
with OutputCapture() as output:
app.run([file_path, ])
self.assertIn(
'1:7: S001 found module formatter',
output.captured
)
def test_multiline_aligned_formatter(self):
file_path = self._given_a_file_in_test_dir('\n'.join([
'print("hello %s"',
' % (\'world\'))',
]))
checker = Flake8Pep3101(None, file_path)
ret = list(checker.run())
self.assertEqual(len(ret), 1)
self.assertEqual(ret[0][0], 1)
self.assertEqual(ret[0][1], 6)
self.assertEqual(ret[0][2], 'S001 found module formatter')
def test_logging_module(self):
file_path = self._given_a_file_in_test_dir('\n'.join([
'import logging',
'logger = logging.getLogger()',
'logger.info("%s is bad", "me")'
]))
checker = Flake8Pep3101(None, file_path)
ret = list(checker.run())
self.assertEqual(len(ret), 0)
def test_multiple_strings(self):
file_path = self._given_a_file_in_test_dir('\n'.join([
'"""""""1" if "%" else "2"'
]))
checker = Flake8Pep3101(None, file_path)
ret = list(checker.run())
self.assertEqual(len(ret), 0)
def test_multiple_single_quotes_strings(self):
file_path = self._given_a_file_in_test_dir('\n'.join([
"'''''''1' if '%' else '2'"
]))
checker = Flake8Pep3101(None, file_path)
ret = list(checker.run())
self.assertEqual(len(ret), 0)
def test_multiple_strings_with_old_formatting(self):
"""Check that multiple quoting is handled properly.
In this case correctly detecting it.
"""
file_path = self._given_a_file_in_test_dir('\n'.join([
'"""""""1" if "%" else "2%s" % "x"'
]))
checker = Flake8Pep3101(None, file_path)
ret = list(checker.run())
self.assertEqual(len(ret), 1)
self.assertEqual(ret[0][0], 1)
self.assertEqual(ret[0][1], 22)
self.assertEqual(ret[0][2], 'S001 found module formatter')
def test_percent_on_string(self):
"""Check that multiple quoting is handled properly.
In this case no string substitution is happening.
Found in plone.app.drafts.tests
"""
file_path = self._given_a_file_in_test_dir('\n'.join([
'a = \'"%2B%2Badd%2B%2BMyDocument"\''
]))
checker = Flake8Pep3101(None, file_path)
ret = list(checker.run())
self.assertEqual(len(ret), 0)
def test_percent_as_last_character_on_line(self):
"""Check that a percent symbol as the last character on a line is
handled properly.
"""
file_path = self._given_a_file_in_test_dir('\n'.join([
"a = 'my string %s %s' \\",
" %\\",
" ('3', '4', )",
]))
checker = Flake8Pep3101(None, file_path)
ret = list(checker.run())
self.assertEqual(len(ret), 1)
self.assertEqual(ret[0][0], 1)
self.assertEqual(ret[0][1], 4)
self.assertEqual(ret[0][2], 'S001 found module formatter')
if __name__ == '__main__':
unittest.main()