63
88
"""Test that the number of uses of working_tree in branch is stable."""
64
89
occurences = self.find_occurences('WorkingTree',
65
90
self.source_file_name(bzrlib.branch))
66
# do not even think of increasing this number. If you think you need to
91
# Do not even think of increasing this number. If you think you need to
67
92
# increase it, then you almost certainly are doing something wrong as
68
93
# the relationship from working_tree to branch is one way.
69
# This number should be 4 (import NoWorkingTree and WorkingTree,
70
# raise NoWorkingTree from working_tree(), and construct a working tree
71
# there) but a merge that regressed this was done before this test was
72
# written. Note that this is an exact equality so that when the number
73
# drops, it is not given a buffer but rather this test updated
75
self.assertEqual(4, occurences)
94
# As of 20070809, there are no longer any mentions at all.
95
self.assertEqual(0, occurences)
98
class TestSource(TestSourceHelper):
100
def get_bzrlib_dir(self):
101
"""Get the path to the root of bzrlib"""
102
source = self.source_file_name(bzrlib)
103
source_dir = os.path.dirname(source)
105
# Avoid the case when bzrlib is packaged in a zip file
106
if not os.path.isdir(source_dir):
107
raise TestSkipped('Cannot find bzrlib source directory. Expected %s'
111
def get_source_files(self):
112
"""Yield all source files for bzr and bzrlib
114
:param our_files_only: If true, exclude files from included libraries
117
bzrlib_dir = self.get_bzrlib_dir()
119
# This is the front-end 'bzr' script
120
bzr_path = self.get_bzr_path()
123
for root, dirs, files in os.walk(bzrlib_dir):
125
if d.endswith('.tmp'):
128
if not f.endswith('.py'):
130
yield osutils.pathjoin(root, f)
132
def get_source_file_contents(self):
133
for fname in self.get_source_files():
134
f = open(fname, 'rb')
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:
148
def is_copyright_exception(self, fname):
149
"""Certain files are allowed to be different"""
150
if not self.is_our_code(fname):
151
# We don't ask that external utilities or plugins be
154
for exc in COPYRIGHT_EXCEPTIONS:
155
if fname.endswith(exc):
159
def is_license_exception(self, fname):
160
"""Certain files are allowed to be different"""
161
if not self.is_our_code(fname):
163
for exc in LICENSE_EXCEPTIONS:
164
if fname.endswith(exc):
168
def test_tmpdir_not_in_source_files(self):
169
"""When scanning for source files, we don't descend test tempdirs"""
170
for filename in self.get_source_files():
171
if re.search(r'test....\.tmp', filename):
172
self.fail("get_source_file() returned filename %r "
173
"from within a temporary directory"
176
def test_copyright(self):
177
"""Test that all .py files have a valid copyright statement"""
178
# These are files which contain a different copyright statement
182
copyright_re = re.compile('#\\s*copyright.*(?=\n)', re.I)
183
copyright_canonical_re = re.compile(
184
r'# Copyright \(C\) ' # Opening "# Copyright (C)"
185
r'(\d+)(, \d+)*' # Followed by a series of dates
186
r'.*Canonical Ltd' # And containing 'Canonical Ltd'
189
for fname, text in self.get_source_file_contents():
190
if self.is_copyright_exception(fname):
192
match = copyright_canonical_re.search(text)
194
match = copyright_re.search(text)
196
incorrect.append((fname, 'found: %s' % (match.group(),)))
198
incorrect.append((fname, 'no copyright line found\n'))
200
if 'by Canonical' in match.group():
201
incorrect.append((fname,
202
'should not have: "by Canonical": %s'
206
help_text = ["Some files have missing or incorrect copyright"
209
"Please either add them to the list of"
210
" COPYRIGHT_EXCEPTIONS in"
211
" bzrlib/tests/test_source.py",
212
# this is broken to prevent a false match
213
"or add '# Copyright (C)"
214
" 2007 Canonical Ltd' to these files:",
217
for fname, comment in incorrect:
218
help_text.append(fname)
219
help_text.append((' '*4) + comment)
221
self.fail('\n'.join(help_text))
224
"""Test that all .py files have a GPL disclaimer"""
228
# This program is free software; you can redistribute it and/or modify
229
# it under the terms of the GNU General Public License as published by
230
# the Free Software Foundation; either version 2 of the License, or
231
# (at your option) any later version.
233
# This program is distributed in the hope that it will be useful,
234
# but WITHOUT ANY WARRANTY; without even the implied warranty of
235
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
236
# GNU General Public License for more details.
238
# You should have received a copy of the GNU General Public License
239
# along with this program; if not, write to the Free Software
240
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
242
gpl_re = re.compile(re.escape(gpl_txt), re.MULTILINE)
244
for fname, text in self.get_source_file_contents():
245
if self.is_license_exception(fname):
247
if not gpl_re.search(text):
248
incorrect.append(fname)
251
help_text = ['Some files have missing or incomplete GPL statement',
253
"Please either add them to the list of"
254
" LICENSE_EXCEPTIONS in"
255
" bzrlib/tests/test_source.py",
256
"Or add the following text to the beginning:",
259
for fname in incorrect:
260
help_text.append((' '*4) + fname)
262
self.fail('\n'.join(help_text))
264
def test_no_tabs(self):
265
"""bzrlib source files should not contain any tab characters."""
268
for fname, text in self.get_source_file_contents():
269
if not self.is_our_code(fname):
272
incorrect.append(fname)
275
self.fail('Tab characters were found in the following source files.'
276
'\nThey should either be replaced by "\\t" or by spaces:'
278
% ('\n '.join(incorrect)))
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
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:
289
elif x[0] == token.NAME:
290
# can't search further down
293
if sub and search(sub):
297
for fname, text in self.get_source_file_contents():
298
if not self.is_our_code(fname):
300
ast = parser.ast2tuple(parser.suite(''.join(text)))
302
badfiles.append(fname)
305
"these files contain an assert statement and should not:\n%s"
306
% '\n'.join(badfiles))