~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_source.py

  • Committer: Martin Pool
  • Date: 2008-06-11 02:36:40 UTC
  • mfrom: (3490 +trunk)
  • mto: This revision was merged to the branch mainline in revision 3492.
  • Revision ID: mbp@sourcefrog.net-20080611023640-db0lqd75yueksdw7
Merge news

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2006 Canonical Ltd
 
1
# Copyright (C) 2005, 2006, 2008 Canonical Ltd
2
2
#   Authors: Robert Collins <robert.collins@canonical.com>
 
3
#            and others
3
4
#
4
5
# This program is free software; you can redistribute it and/or modify
5
6
# it under the terms of the GNU General Public License as published by
22
23
 
23
24
# import system imports here
24
25
import os
 
26
import parser
25
27
import re
 
28
import symbol
26
29
import sys
 
30
import token
27
31
 
28
32
#import bzrlib specific imports here
29
33
from bzrlib import (
30
34
    osutils,
31
35
    )
32
36
import bzrlib.branch
33
 
from bzrlib.tests import TestCase, TestSkipped
 
37
from bzrlib.tests import (
 
38
    KnownFailure,
 
39
    TestCase,
 
40
    TestSkipped,
 
41
    )
34
42
 
35
43
 
36
44
# Files which are listed here will be skipped when testing for Copyright (or
101
109
        return source_dir
102
110
 
103
111
    def get_source_files(self):
104
 
        """yield all source files for bzr and bzrlib"""
 
112
        """Yield all source files for bzr and bzrlib
 
113
        
 
114
        :param our_files_only: If true, exclude files from included libraries
 
115
            or plugins.
 
116
        """
105
117
        bzrlib_dir = self.get_bzrlib_dir()
106
118
 
107
119
        # This is the front-end 'bzr' script
126
138
                f.close()
127
139
            yield fname, text
128
140
 
 
141
    def is_our_code(self, fname):
 
142
        """Return true if it's a "real" part of bzrlib rather than external code"""
 
143
        if '/util/' in fname or '/plugins/' in fname:
 
144
            return False
 
145
        else:
 
146
            return True
 
147
 
129
148
    def is_copyright_exception(self, fname):
130
149
        """Certain files are allowed to be different"""
131
 
        if '/util/' in fname or '/plugins/' in fname:
 
150
        if not self.is_our_code(fname):
132
151
            # We don't ask that external utilities or plugins be
133
152
            # (C) Canonical Ltd
134
153
            return True
135
 
 
136
154
        for exc in COPYRIGHT_EXCEPTIONS:
137
155
            if fname.endswith(exc):
138
156
                return True
139
 
 
140
157
        return False
141
158
 
142
159
    def is_license_exception(self, fname):
143
160
        """Certain files are allowed to be different"""
144
 
        if '/util/' in fname or '/plugins/' in fname:
145
 
            # We don't ask that external utilities or plugins be
146
 
            # (C) Canonical Ltd
 
161
        if not self.is_our_code(fname):
147
162
            return True
148
 
 
149
163
        for exc in LICENSE_EXCEPTIONS:
150
164
            if fname.endswith(exc):
151
165
                return True
152
 
 
153
166
        return False
154
167
 
155
168
    def test_tmpdir_not_in_source_files(self):
253
266
        incorrect = []
254
267
 
255
268
        for fname, text in self.get_source_file_contents():
256
 
            if '/util/' in fname or '/plugins/' in fname:
 
269
            if not self.is_our_code(fname):
257
270
                continue
258
271
            if '\t' in text:
259
272
                incorrect.append(fname)
263
276
              '\nThey should either be replaced by "\\t" or by spaces:'
264
277
              '\n\n    %s'
265
278
              % ('\n    '.join(incorrect)))
 
279
 
 
280
    def test_no_asserts(self):
 
281
        """bzr shouldn't use the 'assert' statement."""
 
282
        # assert causes too much variation between -O and not, and tends to
 
283
        # give bad errors to the user
 
284
        def search(x):
 
285
            # scan down through x for assert statements, report any problems
 
286
            # this is a bit cheesy; it may get some false positives?
 
287
            if x[0] == symbol.assert_stmt:
 
288
                return True
 
289
            elif x[0] == token.NAME:
 
290
                # can't search further down
 
291
                return False
 
292
            for sub in x[1:]:
 
293
                if sub and search(sub):
 
294
                    return True
 
295
            return False
 
296
        badfiles = []
 
297
        for fname, text in self.get_source_file_contents():
 
298
            if not self.is_our_code(fname):
 
299
                continue
 
300
            ast = parser.ast2tuple(parser.suite(''.join(text)))
 
301
            if search(ast):
 
302
                badfiles.append(fname)
 
303
        if badfiles:
 
304
            self.fail(
 
305
                "these files contain an assert statement and should not:\n%s"
 
306
                % '\n'.join(badfiles))