/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
5967.5.2 by Martin Pool
Make test_source actually comply with pep8 itself
1
# Copyright (C) 2005-2011 Canonical Ltd
1523 by Robert Collins
Test for the number of uses of self.working_tree() in branch.py
2
#
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
7
#
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
12
#
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
4183.7.1 by Sabin Iacob
update FSF mailing address
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
1523 by Robert Collins
Test for the number of uses of self.working_tree() in branch.py
16
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
17
"""These tests are tests about the source code of breezy itself.
1523 by Robert Collins
Test for the number of uses of self.working_tree() in branch.py
18
19
They are useful for testing code quality, checking coverage metric etc.
20
"""
21
22
import os
3376.2.1 by Martin Pool
Add test_no_asserts and cleanup test_source
23
import parser
2052.3.1 by John Arbash Meinel
Add tests to cleanup the copyright of all source files
24
import re
3376.2.1 by Martin Pool
Add test_no_asserts and cleanup test_source
25
import symbol
1523 by Robert Collins
Test for the number of uses of self.working_tree() in branch.py
26
import sys
3376.2.1 by Martin Pool
Add test_no_asserts and cleanup test_source
27
import token
1523 by Robert Collins
Test for the number of uses of self.working_tree() in branch.py
28
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
29
from breezy import (
2052.3.1 by John Arbash Meinel
Add tests to cleanup the copyright of all source files
30
    osutils,
31
    )
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
32
import breezy.branch
33
from breezy.tests import (
3376.2.1 by Martin Pool
Add test_no_asserts and cleanup test_source
34
    TestCase,
35
    TestSkipped,
36
    )
2052.3.1 by John Arbash Meinel
Add tests to cleanup the copyright of all source files
37
38
2052.3.8 by John Arbash Meinel
Better documentation about the exception variables
39
# Files which are listed here will be skipped when testing for Copyright (or
40
# GPL) statements.
5193.6.24 by Vincent Ladeuil
Add copyrights in placeholder __init__.py files and fix the fallout from bzrlib/doc_generate/conf.py rename.
41
COPYRIGHT_EXCEPTIONS = [
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
42
    'breezy/_bencode_py.py',
43
    'breezy/doc_generate/conf.py',
44
    'breezy/lsprof.py',
5193.6.24 by Vincent Ladeuil
Add copyrights in placeholder __init__.py files and fix the fallout from bzrlib/doc_generate/conf.py rename.
45
    ]
2052.3.5 by John Arbash Meinel
Guide people to how to add files to the list of exceptions
46
5193.6.24 by Vincent Ladeuil
Add copyrights in placeholder __init__.py files and fix the fallout from bzrlib/doc_generate/conf.py rename.
47
LICENSE_EXCEPTIONS = [
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
48
    'breezy/_bencode_py.py',
49
    'breezy/doc_generate/conf.py',
50
    'breezy/lsprof.py',
5193.6.24 by Vincent Ladeuil
Add copyrights in placeholder __init__.py files and fix the fallout from bzrlib/doc_generate/conf.py rename.
51
    ]
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
52
# Technically, 'breezy/lsprof.py' should be 'breezy/util/lsprof.py',
53
# (we do not check breezy/util/, since that is code bundled from elsewhere)
2052.3.8 by John Arbash Meinel
Better documentation about the exception variables
54
# but for compatibility with previous releases, we don't want to move it.
4634.37.3 by Martin Pool
sphinx autogenerate configuration is exempt from test_source
55
#
56
# sphinx_conf is semi-autogenerated.
2052.3.5 by John Arbash Meinel
Guide people to how to add files to the list of exceptions
57
3572.1.7 by Marius Kruger
Code style and minor changes as per review.
58
2052.3.1 by John Arbash Meinel
Add tests to cleanup the copyright of all source files
59
class TestSourceHelper(TestCase):
60
61
    def source_file_name(self, package):
62
        """Return the path of the .py file for package."""
3616.2.7 by Mark Hammond
prefer getattr() over hasattr()
63
        if getattr(sys, "frozen", None) is not None:
3616.2.5 by Mark Hammond
don't try and test source code when we are frozen.
64
            raise TestSkipped("can't test sources in frozen distributions.")
2052.3.1 by John Arbash Meinel
Add tests to cleanup the copyright of all source files
65
        path = package.__file__
66
        if path[-1] in 'co':
67
            return path[:-1]
68
        else:
69
            return path
70
71
72
class TestApiUsage(TestSourceHelper):
1523 by Robert Collins
Test for the number of uses of self.working_tree() in branch.py
73
1524 by Robert Collins
Test the uses of WorkingTree from branch.py
74
    def find_occurences(self, rule, filename):
75
        """Find the number of occurences of rule in a file."""
1523 by Robert Collins
Test for the number of uses of self.working_tree() in branch.py
76
        occurences = 0
6973.7.5 by Jelmer Vernooij
s/file/open.
77
        source = open(filename, 'r')
1523 by Robert Collins
Test for the number of uses of self.working_tree() in branch.py
78
        for line in source:
1524 by Robert Collins
Test the uses of WorkingTree from branch.py
79
            if line.find(rule) > -1:
1523 by Robert Collins
Test for the number of uses of self.working_tree() in branch.py
80
                occurences += 1
1524 by Robert Collins
Test the uses of WorkingTree from branch.py
81
        return occurences
82
83
    def test_branch_working_tree(self):
84
        """Test that the number of uses of working_tree in branch is stable."""
85
        occurences = self.find_occurences('self.working_tree()',
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
86
                                          self.source_file_name(breezy.branch))
1523 by Robert Collins
Test for the number of uses of self.working_tree() in branch.py
87
        # do not even think of increasing this number. If you think you need to
88
        # increase it, then you almost certainly are doing something wrong as
89
        # the relationship from working_tree to branch is one way.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
90
        # Note that this is an exact equality so that when the number drops,
1534.4.35 by Robert Collins
Give branch its own basis tree and last_revision methods; deprecated branch.working_tree()
91
        #it is not given a buffer but rather has this test updated immediately.
92
        self.assertEqual(0, occurences)
1524 by Robert Collins
Test the uses of WorkingTree from branch.py
93
94
    def test_branch_WorkingTree(self):
95
        """Test that the number of uses of working_tree in branch is stable."""
96
        occurences = self.find_occurences('WorkingTree',
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
97
                                          self.source_file_name(breezy.branch))
2696.1.1 by Martin Pool
Remove things deprecated in 0.11 and earlier
98
        # Do not even think of increasing this number. If you think you need to
1524 by Robert Collins
Test the uses of WorkingTree from branch.py
99
        # increase it, then you almost certainly are doing something wrong as
100
        # the relationship from working_tree to branch is one way.
2696.1.1 by Martin Pool
Remove things deprecated in 0.11 and earlier
101
        # As of 20070809, there are no longer any mentions at all.
102
        self.assertEqual(0, occurences)
2052.3.1 by John Arbash Meinel
Add tests to cleanup the copyright of all source files
103
104
105
class TestSource(TestSourceHelper):
106
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
107
    def get_breezy_dir(self):
108
        """Get the path to the root of breezy"""
109
        source = self.source_file_name(breezy)
2052.3.1 by John Arbash Meinel
Add tests to cleanup the copyright of all source files
110
        source_dir = os.path.dirname(source)
111
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
112
        # Avoid the case when breezy is packaged in a zip file
2052.3.1 by John Arbash Meinel
Add tests to cleanup the copyright of all source files
113
        if not os.path.isdir(source_dir):
5967.5.2 by Martin Pool
Make test_source actually comply with pep8 itself
114
            raise TestSkipped(
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
115
                'Cannot find breezy source directory. Expected %s'
5967.5.2 by Martin Pool
Make test_source actually comply with pep8 itself
116
                % source_dir)
2052.3.1 by John Arbash Meinel
Add tests to cleanup the copyright of all source files
117
        return source_dir
118
4198.1.1 by John Arbash Meinel
Include pyrex files in our source testing for GPL and Copyright checks.
119
    def get_source_files(self, extensions=None):
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
120
        """Yield all source files for bzr and breezy
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
121
3376.2.1 by Martin Pool
Add test_no_asserts and cleanup test_source
122
        :param our_files_only: If true, exclude files from included libraries
123
            or plugins.
124
        """
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
125
        breezy_dir = self.get_breezy_dir()
4198.1.1 by John Arbash Meinel
Include pyrex files in our source testing for GPL and Copyright checks.
126
        if extensions is None:
127
            extensions = ('.py',)
2052.3.1 by John Arbash Meinel
Add tests to cleanup the copyright of all source files
128
129
        # This is the front-end 'bzr' script
6622.1.1 by Jelmer Vernooij
Rename bzrlib => brzlib, bzr => brz.
130
        bzr_path = self.get_brz_path()
2052.3.1 by John Arbash Meinel
Add tests to cleanup the copyright of all source files
131
        yield bzr_path
132
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
133
        for root, dirs, files in os.walk(breezy_dir):
2102.3.1 by mbp at sourcefrog
test_source should avoid walking into tempdirs
134
            for d in dirs:
135
                if d.endswith('.tmp'):
136
                    dirs.remove(d)
2052.3.1 by John Arbash Meinel
Add tests to cleanup the copyright of all source files
137
            for f in files:
4198.1.1 by John Arbash Meinel
Include pyrex files in our source testing for GPL and Copyright checks.
138
                for extension in extensions:
139
                    if f.endswith(extension):
140
                        break
141
                else:
142
                    # Did not match the accepted extensions
2052.3.1 by John Arbash Meinel
Add tests to cleanup the copyright of all source files
143
                    continue
144
                yield osutils.pathjoin(root, f)
145
4198.1.1 by John Arbash Meinel
Include pyrex files in our source testing for GPL and Copyright checks.
146
    def get_source_file_contents(self, extensions=None):
147
        for fname in self.get_source_files(extensions=extensions):
7045.4.10 by Jelmer Vernooij
Fix a couple more tests.
148
            with open(fname, 'r') as f:
149
                yield fname, f.read()
2052.3.1 by John Arbash Meinel
Add tests to cleanup the copyright of all source files
150
3376.2.1 by Martin Pool
Add test_no_asserts and cleanup test_source
151
    def is_our_code(self, fname):
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
152
        """True if it's a "real" part of breezy rather than external code"""
3376.2.1 by Martin Pool
Add test_no_asserts and cleanup test_source
153
        if '/util/' in fname or '/plugins/' in fname:
154
            return False
155
        else:
156
            return True
157
2052.3.5 by John Arbash Meinel
Guide people to how to add files to the list of exceptions
158
    def is_copyright_exception(self, fname):
159
        """Certain files are allowed to be different"""
3376.2.1 by Martin Pool
Add test_no_asserts and cleanup test_source
160
        if not self.is_our_code(fname):
2052.3.5 by John Arbash Meinel
Guide people to how to add files to the list of exceptions
161
            return True
162
        for exc in COPYRIGHT_EXCEPTIONS:
163
            if fname.endswith(exc):
164
                return True
165
        return False
166
167
    def is_license_exception(self, fname):
168
        """Certain files are allowed to be different"""
3376.2.4 by Martin Pool
Remove every assert statement from bzrlib!
169
        if not self.is_our_code(fname):
2052.3.5 by John Arbash Meinel
Guide people to how to add files to the list of exceptions
170
            return True
171
        for exc in LICENSE_EXCEPTIONS:
2052.3.1 by John Arbash Meinel
Add tests to cleanup the copyright of all source files
172
            if fname.endswith(exc):
173
                return True
174
        return False
175
2102.3.1 by mbp at sourcefrog
test_source should avoid walking into tempdirs
176
    def test_tmpdir_not_in_source_files(self):
177
        """When scanning for source files, we don't descend test tempdirs"""
178
        for filename in self.get_source_files():
179
            if re.search(r'test....\.tmp', filename):
180
                self.fail("get_source_file() returned filename %r "
181
                          "from within a temporary directory"
182
                          % filename)
183
2052.3.1 by John Arbash Meinel
Add tests to cleanup the copyright of all source files
184
    def test_copyright(self):
4198.1.1 by John Arbash Meinel
Include pyrex files in our source testing for GPL and Copyright checks.
185
        """Test that all .py and .pyx files have a valid copyright statement"""
2052.3.1 by John Arbash Meinel
Add tests to cleanup the copyright of all source files
186
        incorrect = []
187
2052.3.7 by John Arbash Meinel
Use positive lookahead to avoid extra newlines
188
        copyright_re = re.compile('#\\s*copyright.*(?=\n)', re.I)
6621.21.1 by Martin
Update bt.test_source copyright test to accept other copyright holders
189
        copyright_statement_re = re.compile(
5967.5.2 by Martin Pool
Make test_source actually comply with pep8 itself
190
            r'# Copyright \(C\) '  # Opening "# Copyright (C)"
6621.21.1 by Martin
Update bt.test_source copyright test to accept other copyright holders
191
            r'(\d+?)((, |-)\d+)*'  # followed by a series of dates
192
            r' [^ ]*')             # and then whoever.
2052.3.1 by John Arbash Meinel
Add tests to cleanup the copyright of all source files
193
4198.1.1 by John Arbash Meinel
Include pyrex files in our source testing for GPL and Copyright checks.
194
        for fname, text in self.get_source_file_contents(
195
                extensions=('.py', '.pyx')):
2052.3.5 by John Arbash Meinel
Guide people to how to add files to the list of exceptions
196
            if self.is_copyright_exception(fname):
2052.3.1 by John Arbash Meinel
Add tests to cleanup the copyright of all source files
197
                continue
6621.21.1 by Martin
Update bt.test_source copyright test to accept other copyright holders
198
            match = copyright_statement_re.search(text)
2052.3.2 by John Arbash Meinel
Change Copyright .. by Canonical to Copyright ... Canonical
199
            if not match:
200
                match = copyright_re.search(text)
201
                if match:
202
                    incorrect.append((fname, 'found: %s' % (match.group(),)))
2052.3.1 by John Arbash Meinel
Add tests to cleanup the copyright of all source files
203
                else:
204
                    incorrect.append((fname, 'no copyright line found\n'))
2052.3.2 by John Arbash Meinel
Change Copyright .. by Canonical to Copyright ... Canonical
205
            else:
206
                if 'by Canonical' in match.group():
207
                    incorrect.append((fname,
208
                        'should not have: "by Canonical": %s'
209
                        % (match.group(),)))
2052.3.1 by John Arbash Meinel
Add tests to cleanup the copyright of all source files
210
211
        if incorrect:
212
            help_text = ["Some files have missing or incorrect copyright"
213
                         " statements.",
2052.3.5 by John Arbash Meinel
Guide people to how to add files to the list of exceptions
214
                         "",
215
                         "Please either add them to the list of"
216
                         " COPYRIGHT_EXCEPTIONS in"
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
217
                         " breezy/tests/test_source.py",
2052.3.5 by John Arbash Meinel
Guide people to how to add files to the list of exceptions
218
                         # this is broken to prevent a false match
219
                         "or add '# Copyright (C)"
6621.21.1 by Martin
Update bt.test_source copyright test to accept other copyright holders
220
                         " 2007 Bazaar hackers' to these files:",
2052.3.1 by John Arbash Meinel
Add tests to cleanup the copyright of all source files
221
                         "",
222
                        ]
223
            for fname, comment in incorrect:
224
                help_text.append(fname)
5967.5.2 by Martin Pool
Make test_source actually comply with pep8 itself
225
                help_text.append((' ' * 4) + comment)
2052.3.1 by John Arbash Meinel
Add tests to cleanup the copyright of all source files
226
227
            self.fail('\n'.join(help_text))
228
229
    def test_gpl(self):
4198.1.1 by John Arbash Meinel
Include pyrex files in our source testing for GPL and Copyright checks.
230
        """Test that all .py and .pyx files have a GPL disclaimer."""
2052.3.1 by John Arbash Meinel
Add tests to cleanup the copyright of all source files
231
        incorrect = []
232
233
        gpl_txt = """
234
# This program is free software; you can redistribute it and/or modify
235
# it under the terms of the GNU General Public License as published by
236
# the Free Software Foundation; either version 2 of the License, or
237
# (at your option) any later version.
238
#
239
# This program is distributed in the hope that it will be useful,
240
# but WITHOUT ANY WARRANTY; without even the implied warranty of
241
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
242
# GNU General Public License for more details.
243
#
244
# You should have received a copy of the GNU General Public License
245
# along with this program; if not, write to the Free Software
4183.7.1 by Sabin Iacob
update FSF mailing address
246
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
2052.3.1 by John Arbash Meinel
Add tests to cleanup the copyright of all source files
247
"""
248
        gpl_re = re.compile(re.escape(gpl_txt), re.MULTILINE)
249
4198.1.1 by John Arbash Meinel
Include pyrex files in our source testing for GPL and Copyright checks.
250
        for fname, text in self.get_source_file_contents(
251
                extensions=('.py', '.pyx')):
2052.3.5 by John Arbash Meinel
Guide people to how to add files to the list of exceptions
252
            if self.is_license_exception(fname):
2052.3.1 by John Arbash Meinel
Add tests to cleanup the copyright of all source files
253
                continue
254
            if not gpl_re.search(text):
255
                incorrect.append(fname)
256
257
        if incorrect:
258
            help_text = ['Some files have missing or incomplete GPL statement',
2052.3.5 by John Arbash Meinel
Guide people to how to add files to the list of exceptions
259
                         "",
260
                         "Please either add them to the list of"
261
                         " LICENSE_EXCEPTIONS in"
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
262
                         " breezy/tests/test_source.py",
2052.3.5 by John Arbash Meinel
Guide people to how to add files to the list of exceptions
263
                         "Or add the following text to the beginning:",
5967.5.2 by Martin Pool
Make test_source actually comply with pep8 itself
264
                         gpl_txt]
2052.3.1 by John Arbash Meinel
Add tests to cleanup the copyright of all source files
265
            for fname in incorrect:
5967.5.2 by Martin Pool
Make test_source actually comply with pep8 itself
266
                help_text.append((' ' * 4) + fname)
2052.3.1 by John Arbash Meinel
Add tests to cleanup the copyright of all source files
267
268
            self.fail('\n'.join(help_text))
2120.2.1 by John Arbash Meinel
Remove tabs from source files, and add a test to keep it that way.
269
3943.7.2 by Marius Kruger
* also check for unix style newlines and note in HACKING that this is what we use.
270
    def _push_file(self, dict_, fname, line_no):
271
        if fname not in dict_:
272
            dict_[fname] = [line_no]
273
        else:
274
            dict_[fname].append(line_no)
275
276
    def _format_message(self, dict_, message):
6619.3.18 by Jelmer Vernooij
Run 2to3 idioms fixer.
277
        files = sorted(["%s: %s" % (f, ', '.join([str(i + 1) for i in lines]))
278
                for f, lines in dict_.items()])
3943.7.2 by Marius Kruger
* also check for unix style newlines and note in HACKING that this is what we use.
279
        return message + '\n\n    %s' % ('\n    '.join(files))
280
3943.7.1 by Marius Kruger
* Change test_no_tabs to test_coding_style and let it check for trailing newlines too.
281
    def test_coding_style(self):
282
        """Check if bazaar code conforms to some coding style conventions.
283
5967.5.1 by Martin Pool
Don't print a message about long lines or trailing newlines.
284
        Generally we expect PEP8, but we do not generally strictly enforce
285
        this, and there are existing files that do not comply.  The 'pep8'
286
        tool, available separately, will check for more cases.
4210.5.1 by Marius Kruger
don't raise KnownFailure for all coding_style regressions,
287
5967.5.1 by Martin Pool
Don't print a message about long lines or trailing newlines.
288
        This test only enforces conditions that are globally true at the
289
        moment, and that should cause a patch to be rejected: spaces rather
290
        than tabs, unix newlines, and a newline at the end of the file.
3943.7.1 by Marius Kruger
* Change test_no_tabs to test_coding_style and let it check for trailing newlines too.
291
        """
3943.7.2 by Marius Kruger
* also check for unix style newlines and note in HACKING that this is what we use.
292
        tabs = {}
293
        illegal_newlines = {}
3943.7.4 by Marius Kruger
now also check for 'no newline at end of files'
294
        no_newline_at_eof = []
4198.1.1 by John Arbash Meinel
Include pyrex files in our source testing for GPL and Copyright checks.
295
        for fname, text in self.get_source_file_contents(
296
                extensions=('.py', '.pyx')):
3376.2.1 by Martin Pool
Add test_no_asserts and cleanup test_source
297
            if not self.is_our_code(fname):
2234.4.1 by Wouter van Heyst
(John Arbash Meinel) Fix selftest for installed bzr (#80330)
298
                continue
3943.7.2 by Marius Kruger
* also check for unix style newlines and note in HACKING that this is what we use.
299
            lines = text.splitlines(True)
3943.7.4 by Marius Kruger
now also check for 'no newline at end of files'
300
            last_line_no = len(lines) - 1
3943.7.2 by Marius Kruger
* also check for unix style newlines and note in HACKING that this is what we use.
301
            for line_no, line in enumerate(lines):
302
                if '\t' in line:
303
                    self._push_file(tabs, fname, line_no)
304
                if not line.endswith('\n') or line.endswith('\r\n'):
5967.5.2 by Martin Pool
Make test_source actually comply with pep8 itself
305
                    if line_no != last_line_no:  # not no_newline_at_eof
3943.7.4 by Marius Kruger
now also check for 'no newline at end of files'
306
                        self._push_file(illegal_newlines, fname, line_no)
307
            if not lines[-1].endswith('\n'):
308
                no_newline_at_eof.append(fname)
3943.7.1 by Marius Kruger
* Change test_no_tabs to test_coding_style and let it check for trailing newlines too.
309
        problems = []
310
        if tabs:
3943.7.2 by Marius Kruger
* also check for unix style newlines and note in HACKING that this is what we use.
311
            problems.append(self._format_message(tabs,
3943.7.1 by Marius Kruger
* Change test_no_tabs to test_coding_style and let it check for trailing newlines too.
312
                'Tab characters were found in the following source files.'
3943.7.2 by Marius Kruger
* also check for unix style newlines and note in HACKING that this is what we use.
313
                '\nThey should either be replaced by "\\t" or by spaces:'))
314
        if illegal_newlines:
315
            problems.append(self._format_message(illegal_newlines,
3943.7.4 by Marius Kruger
now also check for 'no newline at end of files'
316
                'Non-unix newlines were found in the following source files:'))
317
        if no_newline_at_eof:
318
            no_newline_at_eof.sort()
319
            problems.append("The following source files doesn't have a "
320
                "newline at the end:"
321
               '\n\n    %s'
322
               % ('\n    '.join(no_newline_at_eof)))
3943.7.1 by Marius Kruger
* Change test_no_tabs to test_coding_style and let it check for trailing newlines too.
323
        if problems:
3943.7.4 by Marius Kruger
now also check for 'no newline at end of files'
324
            self.fail('\n\n'.join(problems))
3572.1.3 by Marius Kruger
* move test_coding_style into test_source
325
3376.2.1 by Martin Pool
Add test_no_asserts and cleanup test_source
326
    def test_no_asserts(self):
327
        """bzr shouldn't use the 'assert' statement."""
328
        # assert causes too much variation between -O and not, and tends to
329
        # give bad errors to the user
330
        def search(x):
331
            # scan down through x for assert statements, report any problems
332
            # this is a bit cheesy; it may get some false positives?
333
            if x[0] == symbol.assert_stmt:
334
                return True
335
            elif x[0] == token.NAME:
336
                # can't search further down
337
                return False
338
            for sub in x[1:]:
339
                if sub and search(sub):
340
                    return True
341
            return False
342
        badfiles = []
4699.1.1 by Harald Meland
Make test_no_asserts quicker by doing a regexp check for the presence of any 'assert' word before doing any expensive AST stuff
343
        assert_re = re.compile(r'\bassert\b')
3376.2.1 by Martin Pool
Add test_no_asserts and cleanup test_source
344
        for fname, text in self.get_source_file_contents():
345
            if not self.is_our_code(fname):
346
                continue
4699.1.1 by Harald Meland
Make test_no_asserts quicker by doing a regexp check for the presence of any 'assert' word before doing any expensive AST stuff
347
            if not assert_re.search(text):
348
                continue
7045.4.10 by Jelmer Vernooij
Fix a couple more tests.
349
            st = parser.suite(text)
350
            code = parser.st2tuple(st)
351
            if search(code):
3376.2.1 by Martin Pool
Add test_no_asserts and cleanup test_source
352
                badfiles.append(fname)
353
        if badfiles:
3376.2.7 by Martin Pool
Treat assert statements in our code as a hard error
354
            self.fail(
3376.2.1 by Martin Pool
Add test_no_asserts and cleanup test_source
355
                "these files contain an assert statement and should not:\n%s"
356
                % '\n'.join(badfiles))
4634.117.5 by John Arbash Meinel
Add a test_source test, to ensure that all funcs have either an except
357
358
    def test_extension_exceptions(self):
359
        """Extension functions should propagate exceptions.
360
5967.5.2 by Martin Pool
Make test_source actually comply with pep8 itself
361
        Either they should return an object, have an 'except' clause, or
362
        have a "# cannot_raise" to indicate that we've audited them and
363
        defined them as not raising exceptions.
4634.117.5 by John Arbash Meinel
Add a test_source test, to ensure that all funcs have either an except
364
        """
365
        both_exc_and_no_exc = []
366
        missing_except = []
7038.1.5 by Martin
Fix test_source failure after removing magic comment
367
        common_classes = ('StaticTuple',)
5365.5.29 by John Arbash Meinel
Handle test_source and extensions. Also define an 'extern' protocol, to allow
368
        class_re = re.compile(r'^(cdef\s+)?(public\s+)?'
369
                              r'(api\s+)?class (\w+).*:', re.MULTILINE)
5967.5.2 by Martin Pool
Make test_source actually comply with pep8 itself
370
        except_re = re.compile(
371
            r'cdef\s+'        # start with cdef
372
            r'([\w *]*?)\s*'  # this is the return signature
373
            r'(\w+)\s*\('     # the function name
374
            r'[^)]*\)\s*'     # parameters
375
            r'(.*)\s*:'       # the except clause
376
            r'\s*(#\s*cannot[- _]raise)?')  # cannot raise comment
4634.117.5 by John Arbash Meinel
Add a test_source test, to ensure that all funcs have either an except
377
        for fname, text in self.get_source_file_contents(
378
                extensions=('.pyx',)):
6619.3.12 by Jelmer Vernooij
Use 2to3 set_literal fixer.
379
            known_classes = {m[-1] for m in class_re.findall(text)}
7038.1.5 by Martin
Fix test_source failure after removing magic comment
380
            known_classes.update(common_classes)
4634.117.5 by John Arbash Meinel
Add a test_source test, to ensure that all funcs have either an except
381
            cdefs = except_re.findall(text)
382
            for sig, func, exc_clause, no_exc_comment in cdefs:
4932.1.2 by John Arbash Meinel
Clean up _simple_set.pyx, and handle 'nogil'.
383
                if sig.startswith('api '):
384
                    sig = sig[4:]
4634.117.8 by John Arbash Meinel
Use a class regex to find other signatures to exclude.
385
                if not sig or sig in known_classes:
4634.117.5 by John Arbash Meinel
Add a test_source test, to ensure that all funcs have either an except
386
                    sig = 'object'
4932.1.2 by John Arbash Meinel
Clean up _simple_set.pyx, and handle 'nogil'.
387
                if 'nogil' in exc_clause:
388
                    exc_clause = exc_clause.replace('nogil', '').strip()
4634.117.5 by John Arbash Meinel
Add a test_source test, to ensure that all funcs have either an except
389
                if exc_clause and no_exc_comment:
390
                    both_exc_and_no_exc.append((fname, func))
391
                if sig != 'object' and not (exc_clause or no_exc_comment):
392
                    missing_except.append((fname, func))
393
        error_msg = []
394
        if both_exc_and_no_exc:
5967.5.2 by Martin Pool
Make test_source actually comply with pep8 itself
395
            error_msg.append(
396
                'The following functions had "cannot raise" comments'
397
                ' but did have an except clause set:')
4634.117.5 by John Arbash Meinel
Add a test_source test, to ensure that all funcs have either an except
398
            for fname, func in both_exc_and_no_exc:
399
                error_msg.append('%s:%s' % (fname, func))
400
            error_msg.extend(('', ''))
401
        if missing_except:
5967.5.2 by Martin Pool
Make test_source actually comply with pep8 itself
402
            error_msg.append(
403
                'The following functions have fixed return types,'
404
                ' but no except clause.')
405
            error_msg.append(
406
                'Either add an except or append "# cannot_raise".')
4634.117.5 by John Arbash Meinel
Add a test_source test, to ensure that all funcs have either an except
407
            for fname, func in missing_except:
408
                error_msg.append('%s:%s' % (fname, func))
409
            error_msg.extend(('', ''))
410
        if error_msg:
411
            self.fail('\n'.join(error_msg))
6379.6.2 by Jelmer Vernooij
add test to make sure absolute_import is enabled.
412
413
    def test_feature_absolute_import(self):
414
        """Using absolute imports means avoiding unnecesary stat and
415
        open calls.
416
417
        Make sure that all non-test files have absolute imports enabled.
418
        """
419
        missing_absolute_import = []
420
        for fname, text in self.get_source_file_contents(
6665.1.2 by Jelmer Vernooij
Require absolute_import in pyx files.
421
                extensions=('.py', '.pyx')):
6379.6.3 by Jelmer Vernooij
Use absolute_import.
422
            if "/tests/" in fname or "test_" in fname:
6379.6.2 by Jelmer Vernooij
add test to make sure absolute_import is enabled.
423
                # We don't really care about tests
424
                continue
6379.6.3 by Jelmer Vernooij
Use absolute_import.
425
            if not "from __future__ import absolute_import" in text:
6379.6.2 by Jelmer Vernooij
add test to make sure absolute_import is enabled.
426
                missing_absolute_import.append(fname)
427
428
        if missing_absolute_import:
429
            self.fail(
430
                'The following files do not have absolute_import enabled:\n'
431
                '\n' + '\n'.join(missing_absolute_import))