~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_source.py

(jameinel) Allow 'bzr serve' to interpret SIGHUP as a graceful shutdown.
 (bug #795025) (John A Meinel)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2006, 2008 Canonical Ltd
2
 
#   Authors: Robert Collins <robert.collins@canonical.com>
3
 
#            and others
 
1
# Copyright (C) 2005-2011 Canonical Ltd
4
2
#
5
3
# This program is free software; you can redistribute it and/or modify
6
4
# it under the terms of the GNU General Public License as published by
14
12
#
15
13
# You should have received a copy of the GNU General Public License
16
14
# along with this program; if not, write to the Free Software
17
 
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
16
 
19
17
"""These tests are tests about the source code of bzrlib itself.
20
18
 
21
19
They are useful for testing code quality, checking coverage metric etc.
22
20
"""
23
21
 
24
 
# import system imports here
25
22
import os
26
23
import parser
27
24
import re
28
 
from cStringIO import StringIO
29
25
import symbol
30
26
import sys
31
27
import token
32
28
 
33
 
#import bzrlib specific imports here
34
29
from bzrlib import (
35
 
    diff,
36
30
    osutils,
37
 
    patiencediff,
38
 
    textfile,
39
31
    )
40
32
import bzrlib.branch
41
33
from bzrlib.tests import (
42
 
    KnownFailure,
43
34
    TestCase,
44
35
    TestSkipped,
45
36
    )
46
 
from bzrlib.workingtree import WorkingTree
47
37
 
48
38
 
49
39
# Files which are listed here will be skipped when testing for Copyright (or
50
40
# GPL) statements.
51
 
COPYRIGHT_EXCEPTIONS = ['bzrlib/lsprof.py']
 
41
COPYRIGHT_EXCEPTIONS = [
 
42
    'bzrlib/_bencode_py.py',
 
43
    'bzrlib/doc_generate/conf.py',
 
44
    'bzrlib/lsprof.py',
 
45
    ]
52
46
 
53
 
LICENSE_EXCEPTIONS = ['bzrlib/lsprof.py']
 
47
LICENSE_EXCEPTIONS = [
 
48
    'bzrlib/_bencode_py.py',
 
49
    'bzrlib/doc_generate/conf.py',
 
50
    'bzrlib/lsprof.py',
 
51
    ]
54
52
# Technically, 'bzrlib/lsprof.py' should be 'bzrlib/util/lsprof.py',
55
53
# (we do not check bzrlib/util/, since that is code bundled from elsewhere)
56
54
# but for compatibility with previous releases, we don't want to move it.
57
 
 
58
 
 
59
 
def check_coding_style(old_filename, oldlines, new_filename, newlines, to_file,
60
 
                  allow_binary=False, sequence_matcher=None,
61
 
                  path_encoding='utf8'):
62
 
    """text_differ to be passed to diff.DiffText, which checks code style """
63
 
    if allow_binary is False:
64
 
        textfile.check_text_lines(oldlines)
65
 
        textfile.check_text_lines(newlines)
66
 
 
67
 
    if sequence_matcher is None:
68
 
        sequence_matcher = patiencediff.PatienceSequenceMatcher
69
 
 
70
 
    started = [False] #trick to access parent scoped variable
71
 
    def start_if_needed():
72
 
        if not started[0]:
73
 
            to_file.write('+++ %s\n' % new_filename)
74
 
            started[0] = True
75
 
 
76
 
    def check_newlines(j1, j2):
77
 
        for i, line in enumerate(newlines[j1:j2]):
78
 
            bad_ws_match = re.match(r'^(([\t]*)(.*?)([\t ]*))(\r?\n)?$', line)
79
 
            if bad_ws_match:
80
 
                line_content = bad_ws_match.group(1)
81
 
                has_leading_tabs = bool(bad_ws_match.group(2))
82
 
                has_trailing_whitespace = bool(bad_ws_match.group(4))
83
 
                if has_leading_tabs:
84
 
                    start_if_needed()
85
 
                    to_file.write('line %i has leading tabs: "%s"\n'% (
86
 
                        i+1+j1, line_content))
87
 
                if has_trailing_whitespace:
88
 
                    start_if_needed()
89
 
                    to_file.write('line %i has trailing whitespace: "%s"\n'% (
90
 
                        i+1+j1, line_content))
91
 
                if len(line_content) > 79:
92
 
                    print (
93
 
                        '\nFile %s\nline %i is longer than 79 characters:'
94
 
                        '\n"%s"'% (new_filename, i+1+j1, line_content))
95
 
 
96
 
    for group in sequence_matcher(None, oldlines, newlines
97
 
            ).get_grouped_opcodes(0):
98
 
        for tag, i1, i2, j1, j2 in group:
99
 
            if tag == 'replace' or tag == 'insert':
100
 
                check_newlines(j1, j2)
101
 
 
102
 
    if len(newlines) == j2 and not newlines[j2-1].endswith('\n'):
103
 
        start_if_needed()
104
 
        to_file.write("\\ No newline at end of file\n")
 
55
#
 
56
# sphinx_conf is semi-autogenerated.
105
57
 
106
58
 
107
59
class TestSourceHelper(TestCase):
135
87
        # do not even think of increasing this number. If you think you need to
136
88
        # increase it, then you almost certainly are doing something wrong as
137
89
        # the relationship from working_tree to branch is one way.
138
 
        # Note that this is an exact equality so that when the number drops, 
 
90
        # Note that this is an exact equality so that when the number drops,
139
91
        #it is not given a buffer but rather has this test updated immediately.
140
92
        self.assertEqual(0, occurences)
141
93
 
159
111
 
160
112
        # Avoid the case when bzrlib is packaged in a zip file
161
113
        if not os.path.isdir(source_dir):
162
 
            raise TestSkipped('Cannot find bzrlib source directory. Expected %s'
163
 
                              % source_dir)
 
114
            raise TestSkipped(
 
115
                'Cannot find bzrlib source directory. Expected %s'
 
116
                % source_dir)
164
117
        return source_dir
165
118
 
166
 
    def get_source_files(self):
 
119
    def get_source_files(self, extensions=None):
167
120
        """Yield all source files for bzr and bzrlib
168
 
        
 
121
 
169
122
        :param our_files_only: If true, exclude files from included libraries
170
123
            or plugins.
171
124
        """
172
125
        bzrlib_dir = self.get_bzrlib_dir()
 
126
        if extensions is None:
 
127
            extensions = ('.py',)
173
128
 
174
129
        # This is the front-end 'bzr' script
175
130
        bzr_path = self.get_bzr_path()
180
135
                if d.endswith('.tmp'):
181
136
                    dirs.remove(d)
182
137
            for f in files:
183
 
                if not f.endswith('.py'):
 
138
                for extension in extensions:
 
139
                    if f.endswith(extension):
 
140
                        break
 
141
                else:
 
142
                    # Did not match the accepted extensions
184
143
                    continue
185
144
                yield osutils.pathjoin(root, f)
186
145
 
187
 
    def get_source_file_contents(self):
188
 
        for fname in self.get_source_files():
 
146
    def get_source_file_contents(self, extensions=None):
 
147
        for fname in self.get_source_files(extensions=extensions):
189
148
            f = open(fname, 'rb')
190
149
            try:
191
150
                text = f.read()
194
153
            yield fname, text
195
154
 
196
155
    def is_our_code(self, fname):
197
 
        """Return true if it's a "real" part of bzrlib rather than external code"""
 
156
        """True if it's a "real" part of bzrlib rather than external code"""
198
157
        if '/util/' in fname or '/plugins/' in fname:
199
158
            return False
200
159
        else:
229
188
                          % filename)
230
189
 
231
190
    def test_copyright(self):
232
 
        """Test that all .py files have a valid copyright statement"""
233
 
        # These are files which contain a different copyright statement
234
 
        # and that is okay.
 
191
        """Test that all .py and .pyx files have a valid copyright statement"""
235
192
        incorrect = []
236
193
 
237
194
        copyright_re = re.compile('#\\s*copyright.*(?=\n)', re.I)
238
195
        copyright_canonical_re = re.compile(
239
 
            r'# Copyright \(C\) ' # Opening "# Copyright (C)"
240
 
            r'(\d+)(, \d+)*' # Followed by a series of dates
241
 
            r'.*Canonical Ltd' # And containing 'Canonical Ltd'
242
 
            )
 
196
            r'# Copyright \(C\) '  # Opening "# Copyright (C)"
 
197
            r'(\d+)(, \d+)*'       # followed by a series of dates
 
198
            r'.*Canonical Ltd')    # and containing 'Canonical Ltd'.
243
199
 
244
 
        for fname, text in self.get_source_file_contents():
 
200
        for fname, text in self.get_source_file_contents(
 
201
                extensions=('.py', '.pyx')):
245
202
            if self.is_copyright_exception(fname):
246
203
                continue
247
204
            match = copyright_canonical_re.search(text)
271
228
                        ]
272
229
            for fname, comment in incorrect:
273
230
                help_text.append(fname)
274
 
                help_text.append((' '*4) + comment)
 
231
                help_text.append((' ' * 4) + comment)
275
232
 
276
233
            self.fail('\n'.join(help_text))
277
234
 
278
235
    def test_gpl(self):
279
 
        """Test that all .py files have a GPL disclaimer"""
 
236
        """Test that all .py and .pyx files have a GPL disclaimer."""
280
237
        incorrect = []
281
238
 
282
239
        gpl_txt = """
292
249
#
293
250
# You should have received a copy of the GNU General Public License
294
251
# along with this program; if not, write to the Free Software
295
 
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
252
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
296
253
"""
297
254
        gpl_re = re.compile(re.escape(gpl_txt), re.MULTILINE)
298
255
 
299
 
        for fname, text in self.get_source_file_contents():
 
256
        for fname, text in self.get_source_file_contents(
 
257
                extensions=('.py', '.pyx')):
300
258
            if self.is_license_exception(fname):
301
259
                continue
302
260
            if not gpl_re.search(text):
309
267
                         " LICENSE_EXCEPTIONS in"
310
268
                         " bzrlib/tests/test_source.py",
311
269
                         "Or add the following text to the beginning:",
312
 
                         gpl_txt
313
 
                        ]
 
270
                         gpl_txt]
314
271
            for fname in incorrect:
315
 
                help_text.append((' '*4) + fname)
 
272
                help_text.append((' ' * 4) + fname)
316
273
 
317
274
            self.fail('\n'.join(help_text))
318
275
 
319
 
    def test_no_tabs(self):
320
 
        """bzrlib source files should not contain any tab characters."""
321
 
        incorrect = []
322
 
 
323
 
        for fname, text in self.get_source_file_contents():
324
 
            if not self.is_our_code(fname):
325
 
                continue
326
 
            if '\t' in text:
327
 
                incorrect.append(fname)
328
 
 
329
 
        if incorrect:
330
 
            self.fail('Tab characters were found in the following source files.'
331
 
              '\nThey should either be replaced by "\\t" or by spaces:'
332
 
              '\n\n    %s'
333
 
              % ('\n    '.join(incorrect)))
 
276
    def _push_file(self, dict_, fname, line_no):
 
277
        if fname not in dict_:
 
278
            dict_[fname] = [line_no]
 
279
        else:
 
280
            dict_[fname].append(line_no)
 
281
 
 
282
    def _format_message(self, dict_, message):
 
283
        files = ["%s: %s" % (f, ', '.join([str(i + 1) for i in lines]))
 
284
                for f, lines in dict_.items()]
 
285
        files.sort()
 
286
        return message + '\n\n    %s' % ('\n    '.join(files))
334
287
 
335
288
    def test_coding_style(self):
336
 
        """ Check if bazaar code conforms to some coding style conventions.
337
 
 
338
 
        Currently we check all .py files for:
339
 
         * new trailing white space
340
 
         * new leading tabs
341
 
         * new long lines (give warning only)
342
 
         * no newline at end of files
 
289
        """Check if bazaar code conforms to some coding style conventions.
 
290
 
 
291
        Generally we expect PEP8, but we do not generally strictly enforce
 
292
        this, and there are existing files that do not comply.  The 'pep8'
 
293
        tool, available separately, will check for more cases.
 
294
 
 
295
        This test only enforces conditions that are globally true at the
 
296
        moment, and that should cause a patch to be rejected: spaces rather
 
297
        than tabs, unix newlines, and a newline at the end of the file.
343
298
        """
344
 
        bzr_dir = osutils.dirname(self.get_bzrlib_dir())
345
 
        try:
346
 
            wt = WorkingTree.open(bzr_dir)
347
 
        except:
348
 
            raise TestSkipped(
349
 
                'Could not open bazaar working tree %s'
350
 
                % bzr_dir)
351
 
        diff_output = StringIO()
352
 
        wt.lock_read()
353
 
        try:
354
 
            new_tree = wt
355
 
            old_tree = new_tree.basis_tree()
356
 
 
357
 
            old_tree.lock_read()
358
 
            new_tree.lock_read()
359
 
            try:
360
 
                iterator = new_tree.iter_changes(old_tree)
361
 
                for (file_id, paths, changed_content, versioned, parent,
362
 
                    name, kind, executable) in iterator:
363
 
                    if (changed_content and paths[1].endswith('.py')):
364
 
                        if kind == ('file', 'file'):
365
 
                            diff_text = diff.DiffText(old_tree, new_tree,
366
 
                                to_file=diff_output,
367
 
                                text_differ=check_coding_style)
368
 
                            diff_text.diff(file_id, paths[0], paths[1],
369
 
                                kind[0], kind[1])
370
 
                        else:
371
 
                            check_coding_style(name[0], (), name[1],
372
 
                                new_tree.get_file(file_id).readlines(),
373
 
                                diff_output)
374
 
            finally:
375
 
                old_tree.unlock()
376
 
                new_tree.unlock()
377
 
        finally:
378
 
            wt.unlock()
379
 
        if len(diff_output.getvalue()) > 0:
380
 
            self.fail("Unacceptable coding style:\n" + diff_output.getvalue())
 
299
        tabs = {}
 
300
        illegal_newlines = {}
 
301
        no_newline_at_eof = []
 
302
        for fname, text in self.get_source_file_contents(
 
303
                extensions=('.py', '.pyx')):
 
304
            if not self.is_our_code(fname):
 
305
                continue
 
306
            lines = text.splitlines(True)
 
307
            last_line_no = len(lines) - 1
 
308
            for line_no, line in enumerate(lines):
 
309
                if '\t' in line:
 
310
                    self._push_file(tabs, fname, line_no)
 
311
                if not line.endswith('\n') or line.endswith('\r\n'):
 
312
                    if line_no != last_line_no:  # not no_newline_at_eof
 
313
                        self._push_file(illegal_newlines, fname, line_no)
 
314
            if not lines[-1].endswith('\n'):
 
315
                no_newline_at_eof.append(fname)
 
316
        problems = []
 
317
        if tabs:
 
318
            problems.append(self._format_message(tabs,
 
319
                'Tab characters were found in the following source files.'
 
320
                '\nThey should either be replaced by "\\t" or by spaces:'))
 
321
        if illegal_newlines:
 
322
            problems.append(self._format_message(illegal_newlines,
 
323
                'Non-unix newlines were found in the following source files:'))
 
324
        if no_newline_at_eof:
 
325
            no_newline_at_eof.sort()
 
326
            problems.append("The following source files doesn't have a "
 
327
                "newline at the end:"
 
328
               '\n\n    %s'
 
329
               % ('\n    '.join(no_newline_at_eof)))
 
330
        if problems:
 
331
            self.fail('\n\n'.join(problems))
381
332
 
382
333
    def test_no_asserts(self):
383
334
        """bzr shouldn't use the 'assert' statement."""
396
347
                    return True
397
348
            return False
398
349
        badfiles = []
 
350
        assert_re = re.compile(r'\bassert\b')
399
351
        for fname, text in self.get_source_file_contents():
400
352
            if not self.is_our_code(fname):
401
353
                continue
402
 
            ast = parser.ast2tuple(parser.suite(''.join(text)))
 
354
            if not assert_re.search(text):
 
355
                continue
 
356
            ast = parser.ast2tuple(parser.suite(text))
403
357
            if search(ast):
404
358
                badfiles.append(fname)
405
359
        if badfiles:
406
360
            self.fail(
407
361
                "these files contain an assert statement and should not:\n%s"
408
362
                % '\n'.join(badfiles))
 
363
 
 
364
    def test_extension_exceptions(self):
 
365
        """Extension functions should propagate exceptions.
 
366
 
 
367
        Either they should return an object, have an 'except' clause, or
 
368
        have a "# cannot_raise" to indicate that we've audited them and
 
369
        defined them as not raising exceptions.
 
370
        """
 
371
        both_exc_and_no_exc = []
 
372
        missing_except = []
 
373
        class_re = re.compile(r'^(cdef\s+)?(public\s+)?'
 
374
                              r'(api\s+)?class (\w+).*:', re.MULTILINE)
 
375
        extern_class_re = re.compile(r'## extern cdef class (\w+)',
 
376
                                     re.MULTILINE)
 
377
        except_re = re.compile(
 
378
            r'cdef\s+'        # start with cdef
 
379
            r'([\w *]*?)\s*'  # this is the return signature
 
380
            r'(\w+)\s*\('     # the function name
 
381
            r'[^)]*\)\s*'     # parameters
 
382
            r'(.*)\s*:'       # the except clause
 
383
            r'\s*(#\s*cannot[- _]raise)?')  # cannot raise comment
 
384
        for fname, text in self.get_source_file_contents(
 
385
                extensions=('.pyx',)):
 
386
            known_classes = set([m[-1] for m in class_re.findall(text)])
 
387
            known_classes.update(extern_class_re.findall(text))
 
388
            cdefs = except_re.findall(text)
 
389
            for sig, func, exc_clause, no_exc_comment in cdefs:
 
390
                if sig.startswith('api '):
 
391
                    sig = sig[4:]
 
392
                if not sig or sig in known_classes:
 
393
                    sig = 'object'
 
394
                if 'nogil' in exc_clause:
 
395
                    exc_clause = exc_clause.replace('nogil', '').strip()
 
396
                if exc_clause and no_exc_comment:
 
397
                    both_exc_and_no_exc.append((fname, func))
 
398
                if sig != 'object' and not (exc_clause or no_exc_comment):
 
399
                    missing_except.append((fname, func))
 
400
        error_msg = []
 
401
        if both_exc_and_no_exc:
 
402
            error_msg.append(
 
403
                'The following functions had "cannot raise" comments'
 
404
                ' but did have an except clause set:')
 
405
            for fname, func in both_exc_and_no_exc:
 
406
                error_msg.append('%s:%s' % (fname, func))
 
407
            error_msg.extend(('', ''))
 
408
        if missing_except:
 
409
            error_msg.append(
 
410
                'The following functions have fixed return types,'
 
411
                ' but no except clause.')
 
412
            error_msg.append(
 
413
                'Either add an except or append "# cannot_raise".')
 
414
            for fname, func in missing_except:
 
415
                error_msg.append('%s:%s' % (fname, func))
 
416
            error_msg.extend(('', ''))
 
417
        if error_msg:
 
418
            self.fail('\n'.join(error_msg))