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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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 (
43
# Files which are listed here will be skipped when testing for Copyright (or
45
COPYRIGHT_EXCEPTIONS = ['bzrlib/lsprof.py', 'bzrlib/_bencode_py.py']
47
LICENSE_EXCEPTIONS = ['bzrlib/lsprof.py', 'bzrlib/_bencode_py.py']
48
# Technically, 'bzrlib/lsprof.py' should be 'bzrlib/util/lsprof.py',
49
# (we do not check bzrlib/util/, since that is code bundled from elsewhere)
50
# but for compatibility with previous releases, we don't want to move it.
53
class TestSourceHelper(TestCase):
55
def source_file_name(self, package):
56
"""Return the path of the .py file for package."""
57
if getattr(sys, "frozen", None) is not None:
58
raise TestSkipped("can't test sources in frozen distributions.")
59
path = package.__file__
66
class TestApiUsage(TestSourceHelper):
68
def find_occurences(self, rule, filename):
69
"""Find the number of occurences of rule in a file."""
71
source = file(filename, 'r')
73
if line.find(rule) > -1:
77
def test_branch_working_tree(self):
78
"""Test that the number of uses of working_tree in branch is stable."""
79
occurences = self.find_occurences('self.working_tree()',
80
self.source_file_name(bzrlib.branch))
81
# do not even think of increasing this number. If you think you need to
82
# increase it, then you almost certainly are doing something wrong as
83
# the relationship from working_tree to branch is one way.
84
# Note that this is an exact equality so that when the number drops,
85
#it is not given a buffer but rather has this test updated immediately.
86
self.assertEqual(0, occurences)
88
def test_branch_WorkingTree(self):
89
"""Test that the number of uses of working_tree in branch is stable."""
90
occurences = self.find_occurences('WorkingTree',
91
self.source_file_name(bzrlib.branch))
92
# Do not even think of increasing this number. If you think you need to
93
# increase it, then you almost certainly are doing something wrong as
94
# the relationship from working_tree to branch is one way.
95
# As of 20070809, there are no longer any mentions at all.
96
self.assertEqual(0, occurences)
99
class TestSource(TestSourceHelper):
101
def get_bzrlib_dir(self):
102
"""Get the path to the root of bzrlib"""
103
source = self.source_file_name(bzrlib)
104
source_dir = os.path.dirname(source)
106
# Avoid the case when bzrlib is packaged in a zip file
107
if not os.path.isdir(source_dir):
108
raise TestSkipped('Cannot find bzrlib source directory. Expected %s'
112
def get_source_files(self, extensions=None):
113
"""Yield all source files for bzr and bzrlib
115
:param our_files_only: If true, exclude files from included libraries
118
bzrlib_dir = self.get_bzrlib_dir()
119
if extensions is None:
120
extensions = ('.py',)
122
# This is the front-end 'bzr' script
123
bzr_path = self.get_bzr_path()
126
for root, dirs, files in os.walk(bzrlib_dir):
128
if d.endswith('.tmp'):
131
for extension in extensions:
132
if f.endswith(extension):
135
# Did not match the accepted extensions
137
yield osutils.pathjoin(root, f)
139
def get_source_file_contents(self, extensions=None):
140
for fname in self.get_source_files(extensions=extensions):
141
f = open(fname, 'rb')
148
def is_our_code(self, fname):
149
"""Return true if it's a "real" part of bzrlib rather than external code"""
150
if '/util/' in fname or '/plugins/' in fname:
155
def is_copyright_exception(self, fname):
156
"""Certain files are allowed to be different"""
157
if not self.is_our_code(fname):
158
# We don't ask that external utilities or plugins be
161
for exc in COPYRIGHT_EXCEPTIONS:
162
if fname.endswith(exc):
166
def is_license_exception(self, fname):
167
"""Certain files are allowed to be different"""
168
if not self.is_our_code(fname):
170
for exc in LICENSE_EXCEPTIONS:
171
if fname.endswith(exc):
175
def test_tmpdir_not_in_source_files(self):
176
"""When scanning for source files, we don't descend test tempdirs"""
177
for filename in self.get_source_files():
178
if re.search(r'test....\.tmp', filename):
179
self.fail("get_source_file() returned filename %r "
180
"from within a temporary directory"
183
def test_copyright(self):
184
"""Test that all .py and .pyx files have a valid copyright statement"""
187
copyright_re = re.compile('#\\s*copyright.*(?=\n)', re.I)
188
copyright_canonical_re = re.compile(
189
r'# Copyright \(C\) ' # Opening "# Copyright (C)"
190
r'(\d+)(, \d+)*' # Followed by a series of dates
191
r'.*Canonical Ltd' # And containing 'Canonical Ltd'
194
for fname, text in self.get_source_file_contents(
195
extensions=('.py', '.pyx')):
196
if self.is_copyright_exception(fname):
198
match = copyright_canonical_re.search(text)
200
match = copyright_re.search(text)
202
incorrect.append((fname, 'found: %s' % (match.group(),)))
204
incorrect.append((fname, 'no copyright line found\n'))
206
if 'by Canonical' in match.group():
207
incorrect.append((fname,
208
'should not have: "by Canonical": %s'
212
help_text = ["Some files have missing or incorrect copyright"
215
"Please either add them to the list of"
216
" COPYRIGHT_EXCEPTIONS in"
217
" bzrlib/tests/test_source.py",
218
# this is broken to prevent a false match
219
"or add '# Copyright (C)"
220
" 2007 Canonical Ltd' to these files:",
223
for fname, comment in incorrect:
224
help_text.append(fname)
225
help_text.append((' '*4) + comment)
227
self.fail('\n'.join(help_text))
230
"""Test that all .py and .pyx files have a GPL disclaimer."""
234
# This program is free software; you can redistribute it and/or modify
235
# it under the terms of the GNU General Public License as published by
236
# the Free Software Foundation; either version 2 of the License, or
237
# (at your option) any later version.
239
# This program is distributed in the hope that it will be useful,
240
# but WITHOUT ANY WARRANTY; without even the implied warranty of
241
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
242
# GNU General Public License for more details.
244
# You should have received a copy of the GNU General Public License
245
# along with this program; if not, write to the Free Software
246
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
248
gpl_re = re.compile(re.escape(gpl_txt), re.MULTILINE)
250
for fname, text in self.get_source_file_contents(
251
extensions=('.py', '.pyx')):
252
if self.is_license_exception(fname):
254
if not gpl_re.search(text):
255
incorrect.append(fname)
258
help_text = ['Some files have missing or incomplete GPL statement',
260
"Please either add them to the list of"
261
" LICENSE_EXCEPTIONS in"
262
" bzrlib/tests/test_source.py",
263
"Or add the following text to the beginning:",
266
for fname in incorrect:
267
help_text.append((' '*4) + fname)
269
self.fail('\n'.join(help_text))
271
def _push_file(self, dict_, fname, line_no):
272
if fname not in dict_:
273
dict_[fname] = [line_no]
275
dict_[fname].append(line_no)
277
def _format_message(self, dict_, message):
278
files = ["%s: %s" % (f, ', '.join([str(i+1) for i in lines]))
279
for f, lines in dict_.items()]
281
return message + '\n\n %s' % ('\n '.join(files))
283
def test_coding_style(self):
284
"""Check if bazaar code conforms to some coding style conventions.
286
Currently we assert that the following is not present:
289
* no newline at end of files
291
Print how many files have
292
* trailing white space
293
* lines longer than 79 chars
297
illegal_newlines = {}
299
no_newline_at_eof = []
300
for fname, text in self.get_source_file_contents(
301
extensions=('.py', '.pyx')):
302
if not self.is_our_code(fname):
304
lines = text.splitlines(True)
305
last_line_no = len(lines) - 1
306
for line_no, line in enumerate(lines):
308
self._push_file(tabs, fname, line_no)
309
if not line.endswith('\n') or line.endswith('\r\n'):
310
if line_no != last_line_no: # not no_newline_at_eof
311
self._push_file(illegal_newlines, fname, line_no)
312
if line.endswith(' \n'):
313
self._push_file(trailing_ws, fname, line_no)
315
self._push_file(long_lines, fname, line_no)
316
if not lines[-1].endswith('\n'):
317
no_newline_at_eof.append(fname)
320
problems.append(self._format_message(tabs,
321
'Tab characters were found in the following source files.'
322
'\nThey should either be replaced by "\\t" or by spaces:'))
324
print ("There are %i lines with trailing white space in %i files."
325
% (sum([len(lines) for f, lines in trailing_ws.items()]),
328
problems.append(self._format_message(illegal_newlines,
329
'Non-unix newlines were found in the following source files:'))
331
print ("There are %i lines longer than 79 characters in %i files."
332
% (sum([len(lines) for f, lines in long_lines.items()]),
334
if no_newline_at_eof:
335
no_newline_at_eof.sort()
336
problems.append("The following source files doesn't have a "
337
"newline at the end:"
339
% ('\n '.join(no_newline_at_eof)))
341
self.fail('\n\n'.join(problems))
343
def test_no_asserts(self):
344
"""bzr shouldn't use the 'assert' statement."""
345
# assert causes too much variation between -O and not, and tends to
346
# give bad errors to the user
348
# scan down through x for assert statements, report any problems
349
# this is a bit cheesy; it may get some false positives?
350
if x[0] == symbol.assert_stmt:
352
elif x[0] == token.NAME:
353
# can't search further down
356
if sub and search(sub):
360
for fname, text in self.get_source_file_contents():
361
if not self.is_our_code(fname):
363
ast = parser.ast2tuple(parser.suite(''.join(text)))
365
badfiles.append(fname)
368
"these files contain an assert statement and should not:\n%s"
369
% '\n'.join(badfiles))