1
# Copyright (C) 2005, 2006, 2008 Canonical Ltd
 
 
2
#   Authors: Robert Collins <robert.collins@canonical.com>
 
 
5
# This program is free software; you can redistribute it and/or modify
 
 
6
# it under the terms of the GNU General Public License as published by
 
 
7
# the Free Software Foundation; either version 2 of the License, or
 
 
8
# (at your option) any later version.
 
 
10
# This program is distributed in the hope that it will be useful,
 
 
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
 
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
 
13
# GNU General Public License for more details.
 
 
15
# You should have received a copy of the GNU General Public License
 
 
16
# along with this program; if not, write to the Free Software
 
 
17
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
 
19
"""These tests are tests about the source code of bzrlib itself.
 
 
21
They are useful for testing code quality, checking coverage metric etc.
 
 
24
# import system imports here
 
 
32
#import bzrlib specific imports here
 
 
37
from bzrlib.tests import (
 
 
44
# Files which are listed here will be skipped when testing for Copyright (or
 
 
46
COPYRIGHT_EXCEPTIONS = ['bzrlib/lsprof.py']
 
 
48
LICENSE_EXCEPTIONS = ['bzrlib/lsprof.py']
 
 
49
# Technically, 'bzrlib/lsprof.py' should be 'bzrlib/util/lsprof.py',
 
 
50
# (we do not check bzrlib/util/, since that is code bundled from elsewhere)
 
 
51
# but for compatibility with previous releases, we don't want to move it.
 
 
54
class TestSourceHelper(TestCase):
 
 
56
    def source_file_name(self, package):
 
 
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.")
 
 
60
        path = package.__file__
 
 
67
class TestApiUsage(TestSourceHelper):
 
 
69
    def find_occurences(self, rule, filename):
 
 
70
        """Find the number of occurences of rule in a file."""
 
 
72
        source = file(filename, 'r')
 
 
74
            if line.find(rule) > -1:
 
 
78
    def test_branch_working_tree(self):
 
 
79
        """Test that the number of uses of working_tree in branch is stable."""
 
 
80
        occurences = self.find_occurences('self.working_tree()',
 
 
81
                                          self.source_file_name(bzrlib.branch))
 
 
82
        # do not even think of increasing this number. If you think you need to
 
 
83
        # increase it, then you almost certainly are doing something wrong as
 
 
84
        # the relationship from working_tree to branch is one way.
 
 
85
        # Note that this is an exact equality so that when the number drops, 
 
 
86
        #it is not given a buffer but rather has this test updated immediately.
 
 
87
        self.assertEqual(0, occurences)
 
 
89
    def test_branch_WorkingTree(self):
 
 
90
        """Test that the number of uses of working_tree in branch is stable."""
 
 
91
        occurences = self.find_occurences('WorkingTree',
 
 
92
                                          self.source_file_name(bzrlib.branch))
 
 
93
        # Do not even think of increasing this number. If you think you need to
 
 
94
        # increase it, then you almost certainly are doing something wrong as
 
 
95
        # 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)
 
 
100
class TestSource(TestSourceHelper):
 
 
102
    def get_bzrlib_dir(self):
 
 
103
        """Get the path to the root of bzrlib"""
 
 
104
        source = self.source_file_name(bzrlib)
 
 
105
        source_dir = os.path.dirname(source)
 
 
107
        # Avoid the case when bzrlib is packaged in a zip file
 
 
108
        if not os.path.isdir(source_dir):
 
 
109
            raise TestSkipped('Cannot find bzrlib source directory. Expected %s'
 
 
113
    def get_source_files(self):
 
 
114
        """Yield all source files for bzr and bzrlib
 
 
116
        :param our_files_only: If true, exclude files from included libraries
 
 
119
        bzrlib_dir = self.get_bzrlib_dir()
 
 
121
        # This is the front-end 'bzr' script
 
 
122
        bzr_path = self.get_bzr_path()
 
 
125
        for root, dirs, files in os.walk(bzrlib_dir):
 
 
127
                if d.endswith('.tmp'):
 
 
130
                if not f.endswith('.py'):
 
 
132
                yield osutils.pathjoin(root, f)
 
 
134
    def get_source_file_contents(self):
 
 
135
        for fname in self.get_source_files():
 
 
136
            f = open(fname, 'rb')
 
 
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:
 
 
150
    def is_copyright_exception(self, fname):
 
 
151
        """Certain files are allowed to be different"""
 
 
152
        if not self.is_our_code(fname):
 
 
153
            # We don't ask that external utilities or plugins be
 
 
156
        for exc in COPYRIGHT_EXCEPTIONS:
 
 
157
            if fname.endswith(exc):
 
 
161
    def is_license_exception(self, fname):
 
 
162
        """Certain files are allowed to be different"""
 
 
163
        if not self.is_our_code(fname):
 
 
165
        for exc in LICENSE_EXCEPTIONS:
 
 
166
            if fname.endswith(exc):
 
 
170
    def test_tmpdir_not_in_source_files(self):
 
 
171
        """When scanning for source files, we don't descend test tempdirs"""
 
 
172
        for filename in self.get_source_files():
 
 
173
            if re.search(r'test....\.tmp', filename):
 
 
174
                self.fail("get_source_file() returned filename %r "
 
 
175
                          "from within a temporary directory"
 
 
178
    def test_copyright(self):
 
 
179
        """Test that all .py files have a valid copyright statement"""
 
 
180
        # These are files which contain a different copyright statement
 
 
184
        copyright_re = re.compile('#\\s*copyright.*(?=\n)', re.I)
 
 
185
        copyright_canonical_re = re.compile(
 
 
186
            r'# Copyright \(C\) ' # Opening "# Copyright (C)"
 
 
187
            r'(\d+)(, \d+)*' # Followed by a series of dates
 
 
188
            r'.*Canonical Ltd' # And containing 'Canonical Ltd'
 
 
191
        for fname, text in self.get_source_file_contents():
 
 
192
            if self.is_copyright_exception(fname):
 
 
194
            match = copyright_canonical_re.search(text)
 
 
196
                match = copyright_re.search(text)
 
 
198
                    incorrect.append((fname, 'found: %s' % (match.group(),)))
 
 
200
                    incorrect.append((fname, 'no copyright line found\n'))
 
 
202
                if 'by Canonical' in match.group():
 
 
203
                    incorrect.append((fname,
 
 
204
                        'should not have: "by Canonical": %s'
 
 
208
            help_text = ["Some files have missing or incorrect copyright"
 
 
211
                         "Please either add them to the list of"
 
 
212
                         " COPYRIGHT_EXCEPTIONS in"
 
 
213
                         " bzrlib/tests/test_source.py",
 
 
214
                         # this is broken to prevent a false match
 
 
215
                         "or add '# Copyright (C)"
 
 
216
                         " 2007 Canonical Ltd' to these files:",
 
 
219
            for fname, comment in incorrect:
 
 
220
                help_text.append(fname)
 
 
221
                help_text.append((' '*4) + comment)
 
 
223
            self.fail('\n'.join(help_text))
 
 
226
        """Test that all .py files have a GPL disclaimer"""
 
 
230
# This program is free software; you can redistribute it and/or modify
 
 
231
# it under the terms of the GNU General Public License as published by
 
 
232
# the Free Software Foundation; either version 2 of the License, or
 
 
233
# (at your option) any later version.
 
 
235
# This program is distributed in the hope that it will be useful,
 
 
236
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
 
237
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
 
238
# GNU General Public License for more details.
 
 
240
# You should have received a copy of the GNU General Public License
 
 
241
# along with this program; if not, write to the Free Software
 
 
242
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
 
244
        gpl_re = re.compile(re.escape(gpl_txt), re.MULTILINE)
 
 
246
        for fname, text in self.get_source_file_contents():
 
 
247
            if self.is_license_exception(fname):
 
 
249
            if not gpl_re.search(text):
 
 
250
                incorrect.append(fname)
 
 
253
            help_text = ['Some files have missing or incomplete GPL statement',
 
 
255
                         "Please either add them to the list of"
 
 
256
                         " LICENSE_EXCEPTIONS in"
 
 
257
                         " bzrlib/tests/test_source.py",
 
 
258
                         "Or add the following text to the beginning:",
 
 
261
            for fname in incorrect:
 
 
262
                help_text.append((' '*4) + fname)
 
 
264
            self.fail('\n'.join(help_text))
 
 
266
    def test_no_tabs(self):
 
 
267
        """bzrlib source files should not contain any tab characters."""
 
 
270
        for fname, text in self.get_source_file_contents():
 
 
271
            if not self.is_our_code(fname):
 
 
274
                incorrect.append(fname)
 
 
277
            self.fail('Tab characters were found in the following source files.'
 
 
278
              '\nThey should either be replaced by "\\t" or by spaces:'
 
 
280
              % ('\n    '.join(incorrect)))
 
 
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
 
 
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:
 
 
291
            elif x[0] == token.NAME:
 
 
292
                # can't search further down
 
 
295
                if sub and search(sub):
 
 
299
        for fname, text in self.get_source_file_contents():
 
 
300
            if not self.is_our_code(fname):
 
 
302
            ast = parser.ast2tuple(parser.suite(''.join(text)))
 
 
304
                badfiles.append(fname)
 
 
307
                "these files contain an assert statement and should not:\n%s"
 
 
308
                % '\n'.join(badfiles))