~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: 2006-10-31 21:29:02 UTC
  • mfrom: (2104 +trunk)
  • mto: This revision was merged to the branch mainline in revision 2110.
  • Revision ID: john@arbash-meinel.com-20061031212902-4b33920b90e9ce92
[merge] bzr.dev 2104

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005 by Canonical Ltd
 
1
# Copyright (C) 2005 Canonical Ltd
2
2
#   Authors: Robert Collins <robert.collins@canonical.com>
3
3
#
4
4
# This program is free software; you can redistribute it and/or modify
22
22
 
23
23
# import system imports here
24
24
import os
 
25
import re
25
26
import sys
26
27
 
27
28
#import bzrlib specific imports here
28
 
from bzrlib.tests import TestCase
 
29
from bzrlib import (
 
30
    osutils,
 
31
    )
29
32
import bzrlib.branch
30
 
 
31
 
 
32
 
class TestApiUsage(TestCase):
 
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):
33
58
 
34
59
    def find_occurences(self, rule, filename):
35
60
        """Find the number of occurences of rule in a file."""
40
65
                occurences += 1
41
66
        return occurences
42
67
 
43
 
    def source_file_name(self, package):
44
 
        """Return the path of the .py file for package."""
45
 
        path = package.__file__
46
 
        if path[-1] in 'co':
47
 
            return path[:-1]
48
 
        else:
49
 
            return path
50
 
 
51
68
    def test_branch_working_tree(self):
52
69
        """Test that the number of uses of working_tree in branch is stable."""
53
70
        occurences = self.find_occurences('self.working_tree()',
73
90
        # drops, it is not given a buffer but rather this test updated
74
91
        # immediately.
75
92
        self.assertEqual(2, occurences)
 
93
 
 
94
 
 
95
class TestSource(TestSourceHelper):
 
96
 
 
97
    def get_bzrlib_dir(self):
 
98
        """Get the path to the root of bzrlib"""
 
99
        source = self.source_file_name(bzrlib)
 
100
        source_dir = os.path.dirname(source)
 
101
 
 
102
        # Avoid the case when bzrlib is packaged in a zip file
 
103
        if not os.path.isdir(source_dir):
 
104
            raise TestSkipped('Cannot find bzrlib source directory. Expected %s'
 
105
                              % source_dir)
 
106
        return source_dir
 
107
 
 
108
    def get_source_files(self):
 
109
        """yield all source files for bzr and bzrlib"""
 
110
        bzrlib_dir = self.get_bzrlib_dir()
 
111
 
 
112
        # This is the front-end 'bzr' script
 
113
        bzr_path = self.get_bzr_path()
 
114
        yield bzr_path
 
115
 
 
116
        for root, dirs, files in os.walk(bzrlib_dir):
 
117
            for f in files:
 
118
                if not f.endswith('.py'):
 
119
                    continue
 
120
                yield osutils.pathjoin(root, f)
 
121
 
 
122
    def get_source_file_contents(self):
 
123
        for fname in self.get_source_files():
 
124
            f = open(fname, 'rb')
 
125
            try:
 
126
                text = f.read()
 
127
            finally:
 
128
                f.close()
 
129
            yield fname, text
 
130
 
 
131
    def is_copyright_exception(self, fname):
 
132
        """Certain files are allowed to be different"""
 
133
        if '/util/' in fname or '/plugins/' in fname:
 
134
            # We don't ask that external utilities or plugins be
 
135
            # (C) Canonical Ltd
 
136
            return True
 
137
 
 
138
        for exc in COPYRIGHT_EXCEPTIONS:
 
139
            if fname.endswith(exc):
 
140
                return True
 
141
 
 
142
        return False
 
143
 
 
144
    def is_license_exception(self, fname):
 
145
        """Certain files are allowed to be different"""
 
146
        if '/util/' in fname or '/plugins/' in fname:
 
147
            # We don't ask that external utilities or plugins be
 
148
            # (C) Canonical Ltd
 
149
            return True
 
150
 
 
151
        for exc in LICENSE_EXCEPTIONS:
 
152
            if fname.endswith(exc):
 
153
                return True
 
154
 
 
155
        return False
 
156
 
 
157
    def test_copyright(self):
 
158
        """Test that all .py files have a valid copyright statement"""
 
159
        # These are files which contain a different copyright statement
 
160
        # and that is okay.
 
161
        incorrect = []
 
162
 
 
163
        copyright_re = re.compile('#\\s*copyright.*(?=\n)', re.I)
 
164
        copyright_canonical_re = re.compile(
 
165
            r'# Copyright \(C\) ' # Opening "# Copyright (C)"
 
166
            r'(\d+)(, \d+)*' # Followed by a series of dates
 
167
            r'.*Canonical Ltd' # And containing 'Canonical Ltd'
 
168
            )
 
169
 
 
170
        for fname, text in self.get_source_file_contents():
 
171
            if self.is_copyright_exception(fname):
 
172
                continue
 
173
            match = copyright_canonical_re.search(text)
 
174
            if not match:
 
175
                match = copyright_re.search(text)
 
176
                if match:
 
177
                    incorrect.append((fname, 'found: %s' % (match.group(),)))
 
178
                else:
 
179
                    incorrect.append((fname, 'no copyright line found\n'))
 
180
            else:
 
181
                if 'by Canonical' in match.group():
 
182
                    incorrect.append((fname,
 
183
                        'should not have: "by Canonical": %s'
 
184
                        % (match.group(),)))
 
185
 
 
186
        if incorrect:
 
187
            help_text = ["Some files have missing or incorrect copyright"
 
188
                         " statements.",
 
189
                         "",
 
190
                         "Please either add them to the list of"
 
191
                         " COPYRIGHT_EXCEPTIONS in"
 
192
                         " bzrlib/tests/test_source.py",
 
193
                         # this is broken to prevent a false match
 
194
                         "or add '# Copyright (C)"
 
195
                         " 2006 Canonical Ltd' to these files:",
 
196
                         "",
 
197
                        ]
 
198
            for fname, comment in incorrect:
 
199
                help_text.append(fname)
 
200
                help_text.append((' '*4) + comment)
 
201
 
 
202
            self.fail('\n'.join(help_text))
 
203
 
 
204
    def test_gpl(self):
 
205
        """Test that all .py files have a GPL disclaimer"""
 
206
        incorrect = []
 
207
 
 
208
        gpl_txt = """
 
209
# This program is free software; you can redistribute it and/or modify
 
210
# it under the terms of the GNU General Public License as published by
 
211
# the Free Software Foundation; either version 2 of the License, or
 
212
# (at your option) any later version.
 
213
#
 
214
# This program is distributed in the hope that it will be useful,
 
215
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
216
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
217
# GNU General Public License for more details.
 
218
#
 
219
# You should have received a copy of the GNU General Public License
 
220
# along with this program; if not, write to the Free Software
 
221
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
222
"""
 
223
        gpl_re = re.compile(re.escape(gpl_txt), re.MULTILINE)
 
224
 
 
225
        for fname, text in self.get_source_file_contents():
 
226
            if self.is_license_exception(fname):
 
227
                continue
 
228
            if not gpl_re.search(text):
 
229
                incorrect.append(fname)
 
230
 
 
231
        if incorrect:
 
232
            help_text = ['Some files have missing or incomplete GPL statement',
 
233
                         "",
 
234
                         "Please either add them to the list of"
 
235
                         " LICENSE_EXCEPTIONS in"
 
236
                         " bzrlib/tests/test_source.py",
 
237
                         "Or add the following text to the beginning:",
 
238
                         gpl_txt
 
239
                        ]
 
240
            for fname in incorrect:
 
241
                help_text.append((' '*4) + fname)
 
242
 
 
243
            self.fail('\n'.join(help_text))