~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_source.py

  • Committer: Aaron Bentley
  • Date: 2007-02-06 14:52:16 UTC
  • mfrom: (2266 +trunk)
  • mto: This revision was merged to the branch mainline in revision 2268.
  • Revision ID: abentley@panoramicfeedback.com-20070206145216-fcpi8o3ufvuzwbp9
Merge bzr.dev

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
88
80
        """Test that the number of uses of working_tree in branch is stable."""
89
81
        occurences = self.find_occurences('WorkingTree',
90
82
                                          self.source_file_name(bzrlib.branch))
91
 
        # 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
92
84
        # increase it, then you almost certainly are doing something wrong as
93
85
        # the relationship from working_tree to branch is one way.
94
 
        # As of 20070809, there are no longer any mentions at all.
95
 
        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)
96
93
 
97
94
 
98
95
class TestSource(TestSourceHelper):
109
106
        return source_dir
110
107
 
111
108
    def get_source_files(self):
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
 
        """
 
109
        """yield all source files for bzr and bzrlib"""
117
110
        bzrlib_dir = self.get_bzrlib_dir()
118
111
 
119
112
        # This is the front-end 'bzr' script
138
131
                f.close()
139
132
            yield fname, text
140
133
 
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
 
 
148
134
    def is_copyright_exception(self, fname):
149
135
        """Certain files are allowed to be different"""
150
 
        if not self.is_our_code(fname):
 
136
        if '/util/' in fname or '/plugins/' in fname:
151
137
            # We don't ask that external utilities or plugins be
152
138
            # (C) Canonical Ltd
153
139
            return True
 
140
 
154
141
        for exc in COPYRIGHT_EXCEPTIONS:
155
142
            if fname.endswith(exc):
156
143
                return True
 
144
 
157
145
        return False
158
146
 
159
147
    def is_license_exception(self, fname):
160
148
        """Certain files are allowed to be different"""
161
 
        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
162
152
            return True
 
153
 
163
154
        for exc in LICENSE_EXCEPTIONS:
164
155
            if fname.endswith(exc):
165
156
                return True
 
157
 
166
158
        return False
167
159
 
168
160
    def test_tmpdir_not_in_source_files(self):
211
203
                         " bzrlib/tests/test_source.py",
212
204
                         # this is broken to prevent a false match
213
205
                         "or add '# Copyright (C)"
214
 
                         " 2007 Canonical Ltd' to these files:",
 
206
                         " 2006 Canonical Ltd' to these files:",
215
207
                         "",
216
208
                        ]
217
209
            for fname, comment in incorrect:
266
258
        incorrect = []
267
259
 
268
260
        for fname, text in self.get_source_file_contents():
269
 
            if not self.is_our_code(fname):
 
261
            if '/util/' in fname or '/plugins/' in fname:
270
262
                continue
271
263
            if '\t' in text:
272
264
                incorrect.append(fname)
276
268
              '\nThey should either be replaced by "\\t" or by spaces:'
277
269
              '\n\n    %s'
278
270
              % ('\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))