~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_source.py

  • Committer: John Arbash Meinel
  • Date: 2007-04-28 15:04:17 UTC
  • mfrom: (2466 +trunk)
  • mto: This revision was merged to the branch mainline in revision 2566.
  • Revision ID: john@arbash-meinel.com-20070428150417-trp3pi0pzd411pu4
[merge] bzr.dev 2466

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2006, 2008 Canonical Ltd
 
1
# Copyright (C) 2005, 2006 Canonical Ltd
2
2
#   Authors: Robert Collins <robert.collins@canonical.com>
3
 
#            and others
4
3
#
5
4
# This program is free software; you can redistribute it and/or modify
6
5
# it under the terms of the GNU General Public License as published by
23
22
 
24
23
# import system imports here
25
24
import os
26
 
import parser
27
25
import re
28
 
import symbol
29
26
import sys
30
 
import token
31
27
 
32
28
#import bzrlib specific imports here
33
29
from bzrlib import (
34
30
    osutils,
35
31
    )
36
32
import bzrlib.branch
37
 
from bzrlib.tests import (
38
 
    KnownFailure,
39
 
    TestCase,
40
 
    TestSkipped,
41
 
    )
 
33
from bzrlib.tests import TestCase, TestSkipped
42
34
 
43
35
 
44
36
# Files which are listed here will be skipped when testing for Copyright (or
55
47
 
56
48
    def source_file_name(self, package):
57
49
        """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.")
60
50
        path = package.__file__
61
51
        if path[-1] in 'co':
62
52
            return path[:-1]
90
80
        """Test that the number of uses of working_tree in branch is stable."""
91
81
        occurences = self.find_occurences('WorkingTree',
92
82
                                          self.source_file_name(bzrlib.branch))
93
 
        # Do not even think of increasing this number. If you think you need to
 
83
        # do not even think of increasing this number. If you think you need to
94
84
        # increase it, then you almost certainly are doing something wrong as
95
85
        # the relationship from working_tree to branch is one way.
96
 
        # As of 20070809, there are no longer any mentions at all.
97
 
        self.assertEqual(0, occurences)
 
86
        # This number should be 4 (import NoWorkingTree and WorkingTree, 
 
87
        # raise NoWorkingTree from working_tree(), and construct a working tree
 
88
        # there) but a merge that regressed this was done before this test was
 
89
        # written. Note that this is an exact equality so that when the number
 
90
        # drops, it is not given a buffer but rather this test updated
 
91
        # immediately.
 
92
        self.assertEqual(2, occurences)
98
93
 
99
94
 
100
95
class TestSource(TestSourceHelper):
111
106
        return source_dir
112
107
 
113
108
    def get_source_files(self):
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
 
        """
 
109
        """yield all source files for bzr and bzrlib"""
119
110
        bzrlib_dir = self.get_bzrlib_dir()
120
111
 
121
112
        # This is the front-end 'bzr' script
140
131
                f.close()
141
132
            yield fname, text
142
133
 
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
 
 
150
134
    def is_copyright_exception(self, fname):
151
135
        """Certain files are allowed to be different"""
152
 
        if not self.is_our_code(fname):
 
136
        if '/util/' in fname or '/plugins/' in fname:
153
137
            # We don't ask that external utilities or plugins be
154
138
            # (C) Canonical Ltd
155
139
            return True
 
140
 
156
141
        for exc in COPYRIGHT_EXCEPTIONS:
157
142
            if fname.endswith(exc):
158
143
                return True
 
144
 
159
145
        return False
160
146
 
161
147
    def is_license_exception(self, fname):
162
148
        """Certain files are allowed to be different"""
163
 
        if not self.is_our_code(fname):
 
149
        if '/util/' in fname or '/plugins/' in fname:
 
150
            # We don't ask that external utilities or plugins be
 
151
            # (C) Canonical Ltd
164
152
            return True
 
153
 
165
154
        for exc in LICENSE_EXCEPTIONS:
166
155
            if fname.endswith(exc):
167
156
                return True
 
157
 
168
158
        return False
169
159
 
170
160
    def test_tmpdir_not_in_source_files(self):
213
203
                         " bzrlib/tests/test_source.py",
214
204
                         # this is broken to prevent a false match
215
205
                         "or add '# Copyright (C)"
216
 
                         " 2007 Canonical Ltd' to these files:",
 
206
                         " 2006 Canonical Ltd' to these files:",
217
207
                         "",
218
208
                        ]
219
209
            for fname, comment in incorrect:
268
258
        incorrect = []
269
259
 
270
260
        for fname, text in self.get_source_file_contents():
271
 
            if not self.is_our_code(fname):
 
261
            if '/util/' in fname or '/plugins/' in fname:
272
262
                continue
273
263
            if '\t' in text:
274
264
                incorrect.append(fname)
278
268
              '\nThey should either be replaced by "\\t" or by spaces:'
279
269
              '\n\n    %s'
280
270
              % ('\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))