24
23
# import system imports here
32
28
#import bzrlib specific imports here
33
29
from bzrlib import (
36
32
import bzrlib.branch
37
from bzrlib.tests import (
33
from bzrlib.tests import TestCase, TestSkipped
43
36
# Files which are listed here will be skipped when testing for Copyright (or
45
COPYRIGHT_EXCEPTIONS = ['bzrlib/lsprof.py', 'bzrlib/_bencode_py.py']
38
COPYRIGHT_EXCEPTIONS = ['bzrlib/lsprof.py']
47
LICENSE_EXCEPTIONS = ['bzrlib/lsprof.py', 'bzrlib/_bencode_py.py']
40
LICENSE_EXCEPTIONS = ['bzrlib/lsprof.py']
48
41
# Technically, 'bzrlib/lsprof.py' should be 'bzrlib/util/lsprof.py',
49
42
# (we do not check bzrlib/util/, since that is code bundled from elsewhere)
50
43
# but for compatibility with previous releases, we don't want to move it.
89
80
"""Test that the number of uses of working_tree in branch is stable."""
90
81
occurences = self.find_occurences('WorkingTree',
91
82
self.source_file_name(bzrlib.branch))
92
# Do not even think of increasing this number. If you think you need to
83
# do not even think of increasing this number. If you think you need to
93
84
# increase it, then you almost certainly are doing something wrong as
94
85
# 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)
86
# This number should be 4 (import NoWorkingTree and WorkingTree,
87
# raise NoWorkingTree from working_tree(), and construct a working tree
88
# there) but a merge that regressed this was done before this test was
89
# written. Note that this is an exact equality so that when the number
90
# drops, it is not given a buffer but rather this test updated
92
self.assertEqual(2, occurences)
99
95
class TestSource(TestSourceHelper):
146
132
yield fname, text
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
134
def is_copyright_exception(self, fname):
156
135
"""Certain files are allowed to be different"""
157
if not self.is_our_code(fname):
136
if '/util/' in fname or '/plugins/' in fname:
158
137
# We don't ask that external utilities or plugins be
159
138
# (C) Canonical Ltd
161
141
for exc in COPYRIGHT_EXCEPTIONS:
162
142
if fname.endswith(exc):
166
147
def is_license_exception(self, fname):
167
148
"""Certain files are allowed to be different"""
168
if not self.is_our_code(fname):
149
if '/util/' in fname or '/plugins/' in fname:
150
# We don't ask that external utilities or plugins be
170
154
for exc in LICENSE_EXCEPTIONS:
171
155
if fname.endswith(exc):
175
160
def test_tmpdir_not_in_source_files(self):
244
230
# You should have received a copy of the GNU General Public License
245
231
# along with this program; if not, write to the Free Software
246
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
232
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
248
234
gpl_re = re.compile(re.escape(gpl_txt), re.MULTILINE)
250
for fname, text in self.get_source_file_contents(
251
extensions=('.py', '.pyx')):
236
for fname, text in self.get_source_file_contents():
252
237
if self.is_license_exception(fname):
254
239
if not gpl_re.search(text):
269
254
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):
256
def test_no_tabs(self):
257
"""bzrlib source files should not contain any tab characters."""
360
260
for fname, text in self.get_source_file_contents():
361
if not self.is_our_code(fname):
261
if '/util/' in fname or '/plugins/' in 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))
264
incorrect.append(fname)
267
self.fail('Tab characters were found in the following source files.'
268
'\nThey should either be replaced by "\\t" or by spaces:'
270
% ('\n '.join(incorrect)))