~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_source.py

  • Committer: John Arbash Meinel
  • Author(s): Mark Hammond
  • Date: 2008-09-09 17:02:21 UTC
  • mto: This revision was merged to the branch mainline in revision 3697.
  • Revision ID: john@arbash-meinel.com-20080909170221-svim3jw2mrz0amp3
An updated transparent icon for bzr.

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
47
55
 
48
56
    def source_file_name(self, package):
49
57
        """Return the path of the .py file for package."""
 
58
        if getattr(sys, "frozen", None) is not None:
 
59
            raise TestSkipped("can't test sources in frozen distributions.")
50
60
        path = package.__file__
51
61
        if path[-1] in 'co':
52
62
            return path[:-1]
101
111
        return source_dir
102
112
 
103
113
    def get_source_files(self):
104
 
        """yield all source files for bzr and bzrlib"""
 
114
        """Yield all source files for bzr and bzrlib
 
115
        
 
116
        :param our_files_only: If true, exclude files from included libraries
 
117
            or plugins.
 
118
        """
105
119
        bzrlib_dir = self.get_bzrlib_dir()
106
120
 
107
121
        # This is the front-end 'bzr' script
126
140
                f.close()
127
141
            yield fname, text
128
142
 
 
143
    def is_our_code(self, fname):
 
144
        """Return true if it's a "real" part of bzrlib rather than external code"""
 
145
        if '/util/' in fname or '/plugins/' in fname:
 
146
            return False
 
147
        else:
 
148
            return True
 
149
 
129
150
    def is_copyright_exception(self, fname):
130
151
        """Certain files are allowed to be different"""
131
 
        if '/util/' in fname or '/plugins/' in fname:
 
152
        if not self.is_our_code(fname):
132
153
            # We don't ask that external utilities or plugins be
133
154
            # (C) Canonical Ltd
134
155
            return True
135
 
 
136
156
        for exc in COPYRIGHT_EXCEPTIONS:
137
157
            if fname.endswith(exc):
138
158
                return True
139
 
 
140
159
        return False
141
160
 
142
161
    def is_license_exception(self, fname):
143
162
        """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
 
163
        if not self.is_our_code(fname):
147
164
            return True
148
 
 
149
165
        for exc in LICENSE_EXCEPTIONS:
150
166
            if fname.endswith(exc):
151
167
                return True
152
 
 
153
168
        return False
154
169
 
155
170
    def test_tmpdir_not_in_source_files(self):
253
268
        incorrect = []
254
269
 
255
270
        for fname, text in self.get_source_file_contents():
256
 
            if '/util/' in fname or '/plugins/' in fname:
 
271
            if not self.is_our_code(fname):
257
272
                continue
258
273
            if '\t' in text:
259
274
                incorrect.append(fname)
263
278
              '\nThey should either be replaced by "\\t" or by spaces:'
264
279
              '\n\n    %s'
265
280
              % ('\n    '.join(incorrect)))
 
281
 
 
282
    def test_no_asserts(self):
 
283
        """bzr shouldn't use the 'assert' statement."""
 
284
        # assert causes too much variation between -O and not, and tends to
 
285
        # give bad errors to the user
 
286
        def search(x):
 
287
            # scan down through x for assert statements, report any problems
 
288
            # this is a bit cheesy; it may get some false positives?
 
289
            if x[0] == symbol.assert_stmt:
 
290
                return True
 
291
            elif x[0] == token.NAME:
 
292
                # can't search further down
 
293
                return False
 
294
            for sub in x[1:]:
 
295
                if sub and search(sub):
 
296
                    return True
 
297
            return False
 
298
        badfiles = []
 
299
        for fname, text in self.get_source_file_contents():
 
300
            if not self.is_our_code(fname):
 
301
                continue
 
302
            ast = parser.ast2tuple(parser.suite(''.join(text)))
 
303
            if search(ast):
 
304
                badfiles.append(fname)
 
305
        if badfiles:
 
306
            self.fail(
 
307
                "these files contain an assert statement and should not:\n%s"
 
308
                % '\n'.join(badfiles))