~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_source.py

  • Committer: Martin Pool
  • Date: 2005-07-06 01:04:08 UTC
  • Revision ID: mbp@sourcefrog.net-20050706010408-6a5f429ee8eb3824
- Merge3.find_sync_regions() - avoid problems with iters on python2.3 by 
  just stepping through arrays; also make this return a list rather than 
  being a generator.

  Thanks very much to John for the report and help debugging.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2006 Canonical Ltd
2
 
#   Authors: Robert Collins <robert.collins@canonical.com>
3
 
#
4
 
# This program is free software; you can redistribute it and/or modify
5
 
# it under the terms of the GNU General Public License as published by
6
 
# the Free Software Foundation; either version 2 of the License, or
7
 
# (at your option) any later version.
8
 
#
9
 
# This program is distributed in the hope that it will be useful,
10
 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 
# GNU General Public License for more details.
13
 
#
14
 
# You should have received a copy of the GNU General Public License
15
 
# along with this program; if not, write to the Free Software
16
 
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17
 
 
18
 
"""These tests are tests about the source code of bzrlib itself.
19
 
 
20
 
They are useful for testing code quality, checking coverage metric etc.
21
 
"""
22
 
 
23
 
# import system imports here
24
 
import os
25
 
import re
26
 
import sys
27
 
 
28
 
#import bzrlib specific imports here
29
 
from bzrlib import (
30
 
    osutils,
31
 
    )
32
 
import bzrlib.branch
33
 
from bzrlib.tests import TestCase, TestSkipped
34
 
 
35
 
 
36
 
# Files which are listed here will be skipped when testing for Copyright (or
37
 
# GPL) statements.
38
 
COPYRIGHT_EXCEPTIONS = ['bzrlib/lsprof.py']
39
 
 
40
 
LICENSE_EXCEPTIONS = ['bzrlib/lsprof.py']
41
 
# Technically, 'bzrlib/lsprof.py' should be 'bzrlib/util/lsprof.py',
42
 
# (we do not check bzrlib/util/, since that is code bundled from elsewhere)
43
 
# but for compatibility with previous releases, we don't want to move it.
44
 
 
45
 
 
46
 
class TestSourceHelper(TestCase):
47
 
 
48
 
    def source_file_name(self, package):
49
 
        """Return the path of the .py file for package."""
50
 
        path = package.__file__
51
 
        if path[-1] in 'co':
52
 
            return path[:-1]
53
 
        else:
54
 
            return path
55
 
 
56
 
 
57
 
class TestApiUsage(TestSourceHelper):
58
 
 
59
 
    def find_occurences(self, rule, filename):
60
 
        """Find the number of occurences of rule in a file."""
61
 
        occurences = 0
62
 
        source = file(filename, 'r')
63
 
        for line in source:
64
 
            if line.find(rule) > -1:
65
 
                occurences += 1
66
 
        return occurences
67
 
 
68
 
    def test_branch_working_tree(self):
69
 
        """Test that the number of uses of working_tree in branch is stable."""
70
 
        occurences = self.find_occurences('self.working_tree()',
71
 
                                          self.source_file_name(bzrlib.branch))
72
 
        # do not even think of increasing this number. If you think you need to
73
 
        # increase it, then you almost certainly are doing something wrong as
74
 
        # the relationship from working_tree to branch is one way.
75
 
        # Note that this is an exact equality so that when the number drops, 
76
 
        #it is not given a buffer but rather has this test updated immediately.
77
 
        self.assertEqual(0, occurences)
78
 
 
79
 
    def test_branch_WorkingTree(self):
80
 
        """Test that the number of uses of working_tree in branch is stable."""
81
 
        occurences = self.find_occurences('WorkingTree',
82
 
                                          self.source_file_name(bzrlib.branch))
83
 
        # Do not even think of increasing this number. If you think you need to
84
 
        # increase it, then you almost certainly are doing something wrong as
85
 
        # the relationship from working_tree to branch is one way.
86
 
        # As of 20070809, there are no longer any mentions at all.
87
 
        self.assertEqual(0, occurences)
88
 
 
89
 
 
90
 
class TestSource(TestSourceHelper):
91
 
 
92
 
    def get_bzrlib_dir(self):
93
 
        """Get the path to the root of bzrlib"""
94
 
        source = self.source_file_name(bzrlib)
95
 
        source_dir = os.path.dirname(source)
96
 
 
97
 
        # Avoid the case when bzrlib is packaged in a zip file
98
 
        if not os.path.isdir(source_dir):
99
 
            raise TestSkipped('Cannot find bzrlib source directory. Expected %s'
100
 
                              % source_dir)
101
 
        return source_dir
102
 
 
103
 
    def get_source_files(self):
104
 
        """yield all source files for bzr and bzrlib"""
105
 
        bzrlib_dir = self.get_bzrlib_dir()
106
 
 
107
 
        # This is the front-end 'bzr' script
108
 
        bzr_path = self.get_bzr_path()
109
 
        yield bzr_path
110
 
 
111
 
        for root, dirs, files in os.walk(bzrlib_dir):
112
 
            for d in dirs:
113
 
                if d.endswith('.tmp'):
114
 
                    dirs.remove(d)
115
 
            for f in files:
116
 
                if not f.endswith('.py'):
117
 
                    continue
118
 
                yield osutils.pathjoin(root, f)
119
 
 
120
 
    def get_source_file_contents(self):
121
 
        for fname in self.get_source_files():
122
 
            f = open(fname, 'rb')
123
 
            try:
124
 
                text = f.read()
125
 
            finally:
126
 
                f.close()
127
 
            yield fname, text
128
 
 
129
 
    def is_copyright_exception(self, fname):
130
 
        """Certain files are allowed to be different"""
131
 
        if '/util/' in fname or '/plugins/' in fname:
132
 
            # We don't ask that external utilities or plugins be
133
 
            # (C) Canonical Ltd
134
 
            return True
135
 
 
136
 
        for exc in COPYRIGHT_EXCEPTIONS:
137
 
            if fname.endswith(exc):
138
 
                return True
139
 
 
140
 
        return False
141
 
 
142
 
    def is_license_exception(self, fname):
143
 
        """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
147
 
            return True
148
 
 
149
 
        for exc in LICENSE_EXCEPTIONS:
150
 
            if fname.endswith(exc):
151
 
                return True
152
 
 
153
 
        return False
154
 
 
155
 
    def test_tmpdir_not_in_source_files(self):
156
 
        """When scanning for source files, we don't descend test tempdirs"""
157
 
        for filename in self.get_source_files():
158
 
            if re.search(r'test....\.tmp', filename):
159
 
                self.fail("get_source_file() returned filename %r "
160
 
                          "from within a temporary directory"
161
 
                          % filename)
162
 
 
163
 
    def test_copyright(self):
164
 
        """Test that all .py files have a valid copyright statement"""
165
 
        # These are files which contain a different copyright statement
166
 
        # and that is okay.
167
 
        incorrect = []
168
 
 
169
 
        copyright_re = re.compile('#\\s*copyright.*(?=\n)', re.I)
170
 
        copyright_canonical_re = re.compile(
171
 
            r'# Copyright \(C\) ' # Opening "# Copyright (C)"
172
 
            r'(\d+)(, \d+)*' # Followed by a series of dates
173
 
            r'.*Canonical Ltd' # And containing 'Canonical Ltd'
174
 
            )
175
 
 
176
 
        for fname, text in self.get_source_file_contents():
177
 
            if self.is_copyright_exception(fname):
178
 
                continue
179
 
            match = copyright_canonical_re.search(text)
180
 
            if not match:
181
 
                match = copyright_re.search(text)
182
 
                if match:
183
 
                    incorrect.append((fname, 'found: %s' % (match.group(),)))
184
 
                else:
185
 
                    incorrect.append((fname, 'no copyright line found\n'))
186
 
            else:
187
 
                if 'by Canonical' in match.group():
188
 
                    incorrect.append((fname,
189
 
                        'should not have: "by Canonical": %s'
190
 
                        % (match.group(),)))
191
 
 
192
 
        if incorrect:
193
 
            help_text = ["Some files have missing or incorrect copyright"
194
 
                         " statements.",
195
 
                         "",
196
 
                         "Please either add them to the list of"
197
 
                         " COPYRIGHT_EXCEPTIONS in"
198
 
                         " bzrlib/tests/test_source.py",
199
 
                         # this is broken to prevent a false match
200
 
                         "or add '# Copyright (C)"
201
 
                         " 2007 Canonical Ltd' to these files:",
202
 
                         "",
203
 
                        ]
204
 
            for fname, comment in incorrect:
205
 
                help_text.append(fname)
206
 
                help_text.append((' '*4) + comment)
207
 
 
208
 
            self.fail('\n'.join(help_text))
209
 
 
210
 
    def test_gpl(self):
211
 
        """Test that all .py files have a GPL disclaimer"""
212
 
        incorrect = []
213
 
 
214
 
        gpl_txt = """
215
 
# This program is free software; you can redistribute it and/or modify
216
 
# it under the terms of the GNU General Public License as published by
217
 
# the Free Software Foundation; either version 2 of the License, or
218
 
# (at your option) any later version.
219
 
#
220
 
# This program is distributed in the hope that it will be useful,
221
 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
222
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
223
 
# GNU General Public License for more details.
224
 
#
225
 
# You should have received a copy of the GNU General Public License
226
 
# along with this program; if not, write to the Free Software
227
 
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
228
 
"""
229
 
        gpl_re = re.compile(re.escape(gpl_txt), re.MULTILINE)
230
 
 
231
 
        for fname, text in self.get_source_file_contents():
232
 
            if self.is_license_exception(fname):
233
 
                continue
234
 
            if not gpl_re.search(text):
235
 
                incorrect.append(fname)
236
 
 
237
 
        if incorrect:
238
 
            help_text = ['Some files have missing or incomplete GPL statement',
239
 
                         "",
240
 
                         "Please either add them to the list of"
241
 
                         " LICENSE_EXCEPTIONS in"
242
 
                         " bzrlib/tests/test_source.py",
243
 
                         "Or add the following text to the beginning:",
244
 
                         gpl_txt
245
 
                        ]
246
 
            for fname in incorrect:
247
 
                help_text.append((' '*4) + fname)
248
 
 
249
 
            self.fail('\n'.join(help_text))
250
 
 
251
 
    def test_no_tabs(self):
252
 
        """bzrlib source files should not contain any tab characters."""
253
 
        incorrect = []
254
 
 
255
 
        for fname, text in self.get_source_file_contents():
256
 
            if '/util/' in fname or '/plugins/' in fname:
257
 
                continue
258
 
            if '\t' in text:
259
 
                incorrect.append(fname)
260
 
 
261
 
        if incorrect:
262
 
            self.fail('Tab characters were found in the following source files.'
263
 
              '\nThey should either be replaced by "\\t" or by spaces:'
264
 
              '\n\n    %s'
265
 
              % ('\n    '.join(incorrect)))