Update docs for configuration
[third_party/pep8] / testsuite / test_shell.py
1 # -*- coding: utf-8 -*-
2 import os.path
3 import sys
4 import unittest
5
6 import pep8
7 from testsuite.support import ROOT_DIR, PseudoFile
8
9
10 class ShellTestCase(unittest.TestCase):
11     """Test the usual CLI options and output."""
12
13     def setUp(self):
14         self._saved_argv = sys.argv
15         self._saved_stdout = sys.stdout
16         self._saved_stderr = sys.stderr
17         self._saved_pconfig = pep8.PROJECT_CONFIG
18         self._saved_cpread = pep8.RawConfigParser._read
19         self._saved_stdin_get_value = pep8.stdin_get_value
20         self._config_filenames = []
21         self.stdin = ''
22         sys.argv = ['pep8']
23         sys.stdout = PseudoFile()
24         sys.stderr = PseudoFile()
25
26         def fake_config_parser_read(cp, fp, filename):
27             self._config_filenames.append(filename)
28         pep8.RawConfigParser._read = fake_config_parser_read
29         pep8.stdin_get_value = self.stdin_get_value
30
31     def tearDown(self):
32         sys.argv = self._saved_argv
33         sys.stdout = self._saved_stdout
34         sys.stderr = self._saved_stderr
35         pep8.PROJECT_CONFIG = self._saved_pconfig
36         pep8.RawConfigParser._read = self._saved_cpread
37         pep8.stdin_get_value = self._saved_stdin_get_value
38
39     def stdin_get_value(self):
40         return self.stdin
41
42     def pep8(self, *args):
43         del sys.stdout[:], sys.stderr[:]
44         sys.argv[1:] = args
45         try:
46             pep8._main()
47             errorcode = None
48         except SystemExit:
49             errorcode = sys.exc_info()[1].code
50         return sys.stdout.getvalue(), sys.stderr.getvalue(), errorcode
51
52     def test_print_usage(self):
53         stdout, stderr, errcode = self.pep8('--help')
54         self.assertFalse(errcode)
55         self.assertFalse(stderr)
56         self.assertTrue(stdout.startswith("Usage: pep8 [options] input"))
57
58         stdout, stderr, errcode = self.pep8('--version')
59         self.assertFalse(errcode)
60         self.assertFalse(stderr)
61         self.assertEqual(stdout.count('\n'), 1)
62
63         stdout, stderr, errcode = self.pep8('--obfuscated')
64         self.assertEqual(errcode, 2)
65         self.assertEqual(stderr.splitlines(),
66                          ["Usage: pep8 [options] input ...", "",
67                           "pep8: error: no such option: --obfuscated"])
68         self.assertFalse(stdout)
69
70         self.assertFalse(self._config_filenames)
71
72     def test_check_simple(self):
73         E11 = os.path.join(ROOT_DIR, 'testsuite', 'E11.py')
74         stdout, stderr, errcode = self.pep8(E11)
75         stdout = stdout.splitlines()
76         self.assertEqual(errcode, 1)
77         self.assertFalse(stderr)
78         self.assertEqual(len(stdout), 17)
79         for line, num, col in zip(stdout, (3, 6, 9, 12), (3, 6, 1, 5)):
80             path, x, y, msg = line.split(':')
81             self.assertTrue(path.endswith(E11))
82             self.assertEqual(x, str(num))
83             self.assertEqual(y, str(col))
84             self.assertTrue(msg.startswith(' E11'))
85         # Config file read from the pep8's setup.cfg
86         config_filenames = [os.path.basename(p)
87                             for p in self._config_filenames]
88         self.assertTrue('setup.cfg' in config_filenames)
89
90     def test_check_stdin(self):
91         pep8.PROJECT_CONFIG = ()
92         stdout, stderr, errcode = self.pep8('-')
93         self.assertFalse(errcode)
94         self.assertFalse(stderr)
95         self.assertFalse(stdout)
96
97         self.stdin = 'import os, sys\n'
98         stdout, stderr, errcode = self.pep8('-')
99         stdout = stdout.splitlines()
100         self.assertEqual(errcode, 1)
101         self.assertFalse(stderr)
102         self.assertEqual(stdout,
103                          ['stdin:1:10: E401 multiple imports on one line'])
104
105     def test_check_non_existent(self):
106         self.stdin = 'import os, sys\n'
107         stdout, stderr, errcode = self.pep8('fictitious.py')
108         self.assertEqual(errcode, 1)
109         self.assertFalse(stderr)
110         self.assertTrue(stdout.startswith('fictitious.py:1:1: E902 '))
111
112     def test_check_noarg(self):
113         # issue #170: do not read stdin by default
114         pep8.PROJECT_CONFIG = ()
115         stdout, stderr, errcode = self.pep8()
116         self.assertEqual(errcode, 2)
117         self.assertEqual(stderr.splitlines(),
118                          ["Usage: pep8 [options] input ...", "",
119                           "pep8: error: input not specified"])
120         self.assertFalse(self._config_filenames)
121
122     def test_check_diff(self):
123         pep8.PROJECT_CONFIG = ()
124         diff_lines = [
125             "--- testsuite/E11.py       2006-06-01 08:49:50 +0500",
126             "+++ testsuite/E11.py       2008-04-06 17:36:29 +0500",
127             "@@ -2,4 +2,7 @@",
128             " if x > 2:",
129             "   print x",
130             "+#: E111",
131             "+if True:",
132             "+     print",
133             " #: E112",
134             " if False:",
135             "",
136         ]
137
138         self.stdin = '\n'.join(diff_lines)
139         stdout, stderr, errcode = self.pep8('--diff')
140         stdout = stdout.splitlines()
141         self.assertEqual(errcode, 1)
142         self.assertFalse(stderr)
143         for line, num, col in zip(stdout, (3, 6), (3, 6)):
144             path, x, y, msg = line.split(':')
145             self.assertEqual(x, str(num))
146             self.assertEqual(y, str(col))
147             self.assertTrue(msg.startswith(' E11'))
148
149         diff_lines[:2] = ["--- a/testsuite/E11.py       2006-06-01 08:49 +0400",
150                           "+++ b/testsuite/E11.py       2008-04-06 17:36 +0400"]
151         self.stdin = '\n'.join(diff_lines)
152         stdout, stderr, errcode = self.pep8('--diff')
153         stdout = stdout.splitlines()
154         self.assertEqual(errcode, 1)
155         self.assertFalse(stderr)
156         for line, num, col in zip(stdout, (3, 6), (3, 6)):
157             path, x, y, msg = line.split(':')
158             self.assertEqual(x, str(num))
159             self.assertEqual(y, str(col))
160             self.assertTrue(msg.startswith(' E11'))
161
162         # issue #127, #137: one-line chunks
163         diff_lines[:-1] = ["diff --git a/testsuite/E11.py b/testsuite/E11.py",
164                            "index 8735e25..2ecb529 100644",
165                            "--- a/testsuite/E11.py",
166                            "+++ b/testsuite/E11.py",
167                            "@@ -5,0 +6 @@ if True:",
168                            "+     print"]
169         self.stdin = '\n'.join(diff_lines)
170         stdout, stderr, errcode = self.pep8('--diff')
171         (stdout,) = stdout.splitlines()
172         self.assertEqual(errcode, 1)
173         self.assertFalse(stderr)
174         self.assertTrue('testsuite/E11.py:6:6: E111 ' in stdout)
175
176         # missing '--diff'
177         self.stdin = '\n'.join(diff_lines)
178         stdout, stderr, errcode = self.pep8()
179         self.assertEqual(errcode, 2)
180         self.assertFalse(stdout)
181         self.assertTrue(stderr.startswith('Usage: pep8 [options] input ...'))
182
183         # no matching file in the diff
184         diff_lines[3] = "+++ b/testsuite/lost/E11.py"
185         self.stdin = '\n'.join(diff_lines)
186         stdout, stderr, errcode = self.pep8('--diff')
187         self.assertFalse(errcode)
188         self.assertFalse(stdout)
189         self.assertFalse(stderr)