23
23
# import system imports here
27
28
#import bzrlib specific imports here
28
from bzrlib.tests import TestCase
29
32
import bzrlib.branch
32
class TestApiUsage(TestCase):
33
from bzrlib.tests import TestCase, TestSkipped
36
class TestSourceHelper(TestCase):
38
def source_file_name(self, package):
39
"""Return the path of the .py file for package."""
40
path = package.__file__
47
class TestApiUsage(TestSourceHelper):
34
49
def find_occurences(self, rule, filename):
35
50
"""Find the number of occurences of rule in a file."""
73
80
# drops, it is not given a buffer but rather this test updated
75
82
self.assertEqual(2, occurences)
85
class TestSource(TestSourceHelper):
87
def get_bzrlib_dir(self):
88
"""Get the path to the root of bzrlib"""
89
source = self.source_file_name(bzrlib)
90
source_dir = os.path.dirname(source)
92
# Avoid the case when bzrlib is packaged in a zip file
93
if not os.path.isdir(source_dir):
94
raise TestSkipped('Cannot find bzrlib source directory. Expected %s'
98
def get_source_files(self):
99
"""yield all source files for bzr and bzrlib"""
100
bzrlib_dir = self.get_bzrlib_dir()
102
# This is the front-end 'bzr' script
103
bzr_path = self.get_bzr_path()
106
for root, dirs, files in os.walk(bzrlib_dir):
108
if not f.endswith('.py'):
110
yield osutils.pathjoin(root, f)
112
def get_source_file_contents(self):
113
for fname in self.get_source_files():
114
f = open(fname, 'rb')
121
def is_exception(self, fname):
122
"""Certain files are allowed to be different"""
123
if '/util/' in fname:
124
# We don't require external utilities to be (C) Canonical Ltd
127
exceptions = ['bzrlib/lsprof.py',
128
'bzrlib/conflicts.py',
129
'bzrlib/iterablefile.py',
131
'bzrlib/tests/test_patches.py',
133
for exc in exceptions:
134
if fname.endswith(exc):
139
def test_copyright(self):
140
"""Test that all .py files have a valid copyright statement"""
141
# These are files which contain a different copyright statement
145
copyright_re = re.compile('#\\s*copyright.*\n', re.I)
146
copyright_canonical_re = re.compile(
147
r'# Copyright \(C\) ' # Opening "# Copyright (C)"
148
r'(\d+)(, \d+)*' # Followed by a series of dates
149
r'.*Canonical Ltd' # And containing 'Canonical Ltd'
152
for fname, text in self.get_source_file_contents():
153
if self.is_exception(fname):
155
if not copyright_canonical_re.search(text):
156
m = copyright_re.search(text)
158
incorrect.append((fname, 'found: %s' % (m.group(),)))
160
incorrect.append((fname, 'no copyright line found\n'))
163
help_text = ["Some files have missing or incorrect copyright"
165
"Please add '# Copyright (C) 2006 Canonical Ltd'"
169
for fname, comment in incorrect:
170
help_text.append(fname)
171
help_text.append((' '*4) + comment)
173
self.fail('\n'.join(help_text))
176
"""Test that all .py files have a GPL disclaimer"""
180
# This program is free software; you can redistribute it and/or modify
181
# it under the terms of the GNU General Public License as published by
182
# the Free Software Foundation; either version 2 of the License, or
183
# (at your option) any later version.
185
# This program is distributed in the hope that it will be useful,
186
# but WITHOUT ANY WARRANTY; without even the implied warranty of
187
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
188
# GNU General Public License for more details.
190
# You should have received a copy of the GNU General Public License
191
# along with this program; if not, write to the Free Software
192
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
194
gpl_re = re.compile(re.escape(gpl_txt), re.MULTILINE)
196
for fname, text in self.get_source_file_contents():
197
if self.is_exception(fname):
199
if not gpl_re.search(text):
200
incorrect.append(fname)
203
help_text = ['Some files have missing or incomplete GPL statement',
204
'Please fix the following files to have text:',
207
for fname in incorrect:
208
help_text.append((' '*4) + fname)
210
self.fail('\n'.join(help_text))