-
Notifications
You must be signed in to change notification settings - Fork 750
/
Copy pathtest_shell.py
229 lines (194 loc) · 8.33 KB
/
test_shell.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
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# -*- coding: utf-8 -*-
import os.path
import sys
import unittest
import pycodestyle
from testsuite.support import ROOT_DIR, PseudoFile
def safe_line_split(line):
r"""Parses output lines of the form
[location & file]:[row]:[column]:[message]
and returns a tuple of the form
(path, row, column, message)
This function handles the OS-issues related to a ":" appearing in some
Windows paths.
In Windows, the location is usually denoted as "[DriveLetter]:[location]".
On all other platforms, there is no colon in the location.
The majority of time on windows, the line will look like:
C:\projects\pycodestyle\testsuite\E11.py:3:3: E111 indentation is ...
Or if somebody is using a networked location
\\projects\pycodestyle\testsuite\E11.py:3:3: E111 indentation is ...
On non-windows, the same line would looke like:
/home/projects/pycodestyle/testsuite/E11.py:3:3: E111 indentation is ...
"""
split = line.split(':')
if sys.platform == 'win32': # Note, even 64 bit platforms return 'win32'
if len(split) == 5: # This will be the most common
drive, path, x, y, msg = split
path = drive + ':' + path
elif len(split) == 4: # If the location is a network share
path, x, y, msg = split
else:
raise Exception("Unhandled edge case parsing message: " + line)
else:
path, x, y, msg = split
return path, x, y, msg
class ShellTestCase(unittest.TestCase):
"""Test the usual CLI options and output."""
def setUp(self):
self._saved_argv = sys.argv
self._saved_stdout = sys.stdout
self._saved_stderr = sys.stderr
self._saved_pconfig = pycodestyle.PROJECT_CONFIG
self._saved_cpread = pycodestyle.RawConfigParser._read
self._saved_stdin_get_value = pycodestyle.stdin_get_value
self._config_filenames = []
self.stdin = ''
sys.argv = ['pep8']
sys.stdout = PseudoFile()
sys.stderr = PseudoFile()
def fake_config_parser_read(cp, fp, filename):
self._config_filenames.append(filename)
pycodestyle.RawConfigParser._read = fake_config_parser_read
pycodestyle.stdin_get_value = self.stdin_get_value
def tearDown(self):
sys.argv = self._saved_argv
sys.stdout = self._saved_stdout
sys.stderr = self._saved_stderr
pycodestyle.PROJECT_CONFIG = self._saved_pconfig
pycodestyle.RawConfigParser._read = self._saved_cpread
pycodestyle.stdin_get_value = self._saved_stdin_get_value
def stdin_get_value(self):
return self.stdin
def pep8(self, *args):
del sys.stdout[:], sys.stderr[:]
sys.argv[1:] = args
try:
pycodestyle._main()
errorcode = None
except SystemExit:
errorcode = sys.exc_info()[1].code
return sys.stdout.getvalue(), sys.stderr.getvalue(), errorcode
def test_print_usage(self):
stdout, stderr, errcode = self.pep8('--help')
self.assertFalse(errcode)
self.assertFalse(stderr)
self.assertTrue(stdout.startswith("Usage: pep8 [options] input"))
stdout, stderr, errcode = self.pep8('--version')
self.assertFalse(errcode)
self.assertFalse(stderr)
self.assertEqual(stdout.count('\n'), 1)
stdout, stderr, errcode = self.pep8('--obfuscated')
self.assertEqual(errcode, 2)
self.assertEqual(stderr.splitlines(),
["Usage: pep8 [options] input ...", "",
"pep8: error: no such option: --obfuscated"])
self.assertFalse(stdout)
self.assertFalse(self._config_filenames)
def test_check_simple(self):
E11 = os.path.join(ROOT_DIR, 'testsuite', 'E11.py')
stdout, stderr, errcode = self.pep8(E11)
stdout = stdout.splitlines()
self.assertEqual(errcode, 1)
self.assertFalse(stderr)
self.assertEqual(len(stdout), 17)
for line, num, col in zip(stdout, (3, 6, 9, 12), (3, 6, 1, 5)):
path, x, y, msg = safe_line_split(line)
self.assertTrue(path.endswith(E11))
self.assertEqual(x, str(num))
self.assertEqual(y, str(col))
self.assertTrue(msg.startswith(' E11'))
# Config file read from the pep8's setup.cfg
config_filenames = [os.path.basename(p)
for p in self._config_filenames]
self.assertTrue('setup.cfg' in config_filenames)
def test_check_stdin(self):
pycodestyle.PROJECT_CONFIG = ()
stdout, stderr, errcode = self.pep8('-')
self.assertFalse(errcode)
self.assertFalse(stderr)
self.assertFalse(stdout)
self.stdin = 'import os, sys\n'
stdout, stderr, errcode = self.pep8('-')
stdout = stdout.splitlines()
self.assertEqual(errcode, 1)
self.assertFalse(stderr)
self.assertEqual(stdout,
['stdin:1:10: E401 multiple imports on one line'])
def test_check_non_existent(self):
self.stdin = 'import os, sys\n'
stdout, stderr, errcode = self.pep8('fictitious.py')
self.assertEqual(errcode, 1)
self.assertFalse(stderr)
self.assertTrue(stdout.startswith('fictitious.py:1:1: E902 '))
def test_check_noarg(self):
# issue #170: do not read stdin by default
pycodestyle.PROJECT_CONFIG = ()
stdout, stderr, errcode = self.pep8()
self.assertEqual(errcode, 2)
self.assertEqual(stderr.splitlines(),
["Usage: pep8 [options] input ...", "",
"pep8: error: input not specified"])
self.assertFalse(self._config_filenames)
def test_check_diff(self):
pycodestyle.PROJECT_CONFIG = ()
diff_lines = [
"--- testsuite/E11.py 2006-06-01 08:49:50 +0500",
"+++ testsuite/E11.py 2008-04-06 17:36:29 +0500",
"@@ -2,4 +2,7 @@",
" if x > 2:",
" print x",
"+#: E111",
"+if True:",
"+ print",
" #: E112",
" if False:",
"",
]
self.stdin = '\n'.join(diff_lines)
stdout, stderr, errcode = self.pep8('--diff')
stdout = stdout.splitlines()
self.assertEqual(errcode, 1)
self.assertFalse(stderr)
for line, num, col in zip(stdout, (3, 6), (3, 6)):
path, x, y, msg = safe_line_split(line)
self.assertEqual(x, str(num))
self.assertEqual(y, str(col))
self.assertTrue(msg.startswith(' E11'))
diff_lines[:2] = ["--- a/testsuite/E11.py 2006-06-01 08:49 +0400",
"+++ b/testsuite/E11.py 2008-04-06 17:36 +0400"]
self.stdin = '\n'.join(diff_lines)
stdout, stderr, errcode = self.pep8('--diff')
stdout = stdout.splitlines()
self.assertEqual(errcode, 1)
self.assertFalse(stderr)
for line, num, col in zip(stdout, (3, 6), (3, 6)):
path, x, y, msg = safe_line_split(line)
self.assertEqual(x, str(num))
self.assertEqual(y, str(col))
self.assertTrue(msg.startswith(' E11'))
# issue #127, #137: one-line chunks
diff_lines[:-1] = ["diff --git a/testsuite/E11.py b/testsuite/E11.py",
"index 8735e25..2ecb529 100644",
"--- a/testsuite/E11.py",
"+++ b/testsuite/E11.py",
"@@ -5,0 +6 @@ if True:",
"+ print"]
self.stdin = '\n'.join(diff_lines)
stdout, stderr, errcode = self.pep8('--diff')
(stdout,) = stdout.splitlines()
self.assertEqual(errcode, 1)
self.assertFalse(stderr)
self.assertTrue('testsuite/E11.py:6:6: E111 ' in stdout)
# missing '--diff'
self.stdin = '\n'.join(diff_lines)
stdout, stderr, errcode = self.pep8()
self.assertEqual(errcode, 2)
self.assertFalse(stdout)
self.assertTrue(stderr.startswith('Usage: pep8 [options] input ...'))
# no matching file in the diff
diff_lines[3] = "+++ b/testsuite/lost/E11.py"
self.stdin = '\n'.join(diff_lines)
stdout, stderr, errcode = self.pep8('--diff')
self.assertFalse(errcode)
self.assertFalse(stdout)
self.assertFalse(stderr)