~ubuntu-branches/ubuntu/raring/pyflakes/raring-proposed

« back to all changes in this revision

Viewing changes to pyflakes/test/test_script.py

  • Committer: Package Import Robot
  • Author(s): Dmitrijs Ledkovs
  • Date: 2013-02-13 19:07:52 UTC
  • mfrom: (3.1.9 experimental)
  • Revision ID: package-import@ubuntu.com-20130213190752-pr06y05rz8n06tmg
Tags: 0.6.1-1~exp1
* New upstream release:
  + rebase add_main_function.diff.
  - drop always_close_fd.diff, merged upstream.
  - drop check_encoding_errors.diff, merged upstream.
* Bump standards version
* Switch to dh_python2
* Bump debhelper to 9
* Adjust Vcs URL to canonical format.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
 
2
 
"""
3
 
Tests for L{pyflakes.scripts.pyflakes}.
4
 
"""
5
 
 
6
 
import sys
7
 
from StringIO import StringIO
8
 
 
9
 
from twisted.python.filepath import FilePath
10
 
from twisted.trial.unittest import TestCase
11
 
 
12
 
from pyflakes.scripts.pyflakes import checkPath
13
 
 
14
 
def withStderrTo(stderr, f):
15
 
    """
16
 
    Call C{f} with C{sys.stderr} redirected to C{stderr}.
17
 
    """
18
 
    (outer, sys.stderr) = (sys.stderr, stderr)
19
 
    try:
20
 
        return f()
21
 
    finally:
22
 
        sys.stderr = outer
23
 
 
24
 
 
25
 
 
26
 
class CheckTests(TestCase):
27
 
    """
28
 
    Tests for L{check} and L{checkPath} which check a file for flakes.
29
 
    """
30
 
    def test_missingTrailingNewline(self):
31
 
        """
32
 
        Source which doesn't end with a newline shouldn't cause any
33
 
        exception to be raised nor an error indicator to be returned by
34
 
        L{check}.
35
 
        """
36
 
        fName = self.mktemp()
37
 
        FilePath(fName).setContent("def foo():\n\tpass\n\t")
38
 
        self.assertFalse(checkPath(fName))
39
 
 
40
 
 
41
 
    def test_checkPathNonExisting(self):
42
 
        """
43
 
        L{checkPath} handles non-existing files.
44
 
        """
45
 
        err = StringIO()
46
 
        count = withStderrTo(err, lambda: checkPath('extremo'))
47
 
        self.assertEquals(err.getvalue(), 'extremo: No such file or directory\n')
48
 
        self.assertEquals(count, 1)
49
 
 
50
 
 
51
 
    def test_multilineSyntaxError(self):
52
 
        """
53
 
        Source which includes a syntax error which results in the raised
54
 
        L{SyntaxError.text} containing multiple lines of source are reported
55
 
        with only the last line of that source.
56
 
        """
57
 
        source = """\
58
 
def foo():
59
 
    '''
60
 
 
61
 
def bar():
62
 
    pass
63
 
 
64
 
def baz():
65
 
    '''quux'''
66
 
"""
67
 
 
68
 
        # Sanity check - SyntaxError.text should be multiple lines, if it
69
 
        # isn't, something this test was unprepared for has happened.
70
 
        def evaluate(source):
71
 
            exec source
72
 
        exc = self.assertRaises(SyntaxError, evaluate, source)
73
 
        self.assertTrue(exc.text.count('\n') > 1)
74
 
 
75
 
        sourcePath = FilePath(self.mktemp())
76
 
        sourcePath.setContent(source)
77
 
        err = StringIO()
78
 
        count = withStderrTo(err, lambda: checkPath(sourcePath.path))
79
 
        self.assertEqual(count, 1)
80
 
 
81
 
        self.assertEqual(
82
 
            err.getvalue(),
83
 
            """\
84
 
%s:8: invalid syntax
85
 
    '''quux'''
86
 
           ^
87
 
""" % (sourcePath.path,))
88
 
 
89
 
 
90
 
    def test_eofSyntaxError(self):
91
 
        """
92
 
        The error reported for source files which end prematurely causing a
93
 
        syntax error reflects the cause for the syntax error.
94
 
        """
95
 
        source = "def foo("
96
 
        sourcePath = FilePath(self.mktemp())
97
 
        sourcePath.setContent(source)
98
 
        err = StringIO()
99
 
        count = withStderrTo(err, lambda: checkPath(sourcePath.path))
100
 
        self.assertEqual(count, 1)
101
 
        self.assertEqual(
102
 
            err.getvalue(),
103
 
            """\
104
 
%s:1: unexpected EOF while parsing
105
 
def foo(
106
 
         ^
107
 
""" % (sourcePath.path,))
108
 
 
109
 
 
110
 
    def test_nonDefaultFollowsDefaultSyntaxError(self):
111
 
        """
112
 
        Source which has a non-default argument following a default argument
113
 
        should include the line number of the syntax error.  However these
114
 
        exceptions do not include an offset.
115
 
        """
116
 
        source = """\
117
 
def foo(bar=baz, bax):
118
 
    pass
119
 
"""
120
 
        sourcePath = FilePath(self.mktemp())
121
 
        sourcePath.setContent(source)
122
 
        err = StringIO()
123
 
        count = withStderrTo(err, lambda: checkPath(sourcePath.path))
124
 
        self.assertEqual(count, 1)
125
 
        self.assertEqual(
126
 
            err.getvalue(),
127
 
            """\
128
 
%s:1: non-default argument follows default argument
129
 
def foo(bar=baz, bax):
130
 
""" % (sourcePath.path,))
131
 
 
132
 
 
133
 
    def test_nonKeywordAfterKeywordSyntaxError(self):
134
 
        """
135
 
        Source which has a non-keyword argument after a keyword argument should
136
 
        include the line number of the syntax error.  However these exceptions
137
 
        do not include an offset.
138
 
        """
139
 
        source = """\
140
 
foo(bar=baz, bax)
141
 
"""
142
 
        sourcePath = FilePath(self.mktemp())
143
 
        sourcePath.setContent(source)
144
 
        err = StringIO()
145
 
        count = withStderrTo(err, lambda: checkPath(sourcePath.path))
146
 
        self.assertEqual(count, 1)
147
 
        self.assertEqual(
148
 
            err.getvalue(),
149
 
            """\
150
 
%s:1: non-keyword arg after keyword arg
151
 
foo(bar=baz, bax)
152
 
""" % (sourcePath.path,))
153
 
 
154
 
 
155
 
    def test_permissionDenied(self):
156
 
        """
157
 
        If the a source file is not readable, this is reported on standard
158
 
        error.
159
 
        """
160
 
        sourcePath = FilePath(self.mktemp())
161
 
        sourcePath.setContent('')
162
 
        sourcePath.chmod(0)
163
 
        err = StringIO()
164
 
        count = withStderrTo(err, lambda: checkPath(sourcePath.path))
165
 
        self.assertEquals(count, 1)
166
 
        self.assertEquals(
167
 
            err.getvalue(), "%s: Permission denied\n" % (sourcePath.path,))
168
 
 
169
 
 
170
 
    def test_misencodedFile(self):
171
 
        """
172
 
        If a source file contains bytes which cannot be decoded, this is
173
 
        reported on stderr.
174
 
        """
175
 
        source = u"""\
176
 
# coding: ascii
177
 
x = "\N{SNOWMAN}"
178
 
""".encode('utf-8')
179
 
        sourcePath = FilePath(self.mktemp())
180
 
        sourcePath.setContent(source)
181
 
        err = StringIO()
182
 
        count = withStderrTo(err, lambda: checkPath(sourcePath.path))
183
 
        self.assertEquals(count, 1)
184
 
        self.assertEquals(
185
 
            err.getvalue(), "%s: problem decoding source\n" % (sourcePath.path,))