23
24
# import system imports here
28
32
#import bzrlib specific imports here
29
33
from bzrlib import (
32
36
import bzrlib.branch
33
from bzrlib.tests import TestCase, TestSkipped
37
from bzrlib.tests import (
36
43
# Files which are listed here will be skipped when testing for Copyright (or
38
COPYRIGHT_EXCEPTIONS = ['bzrlib/lsprof.py']
45
COPYRIGHT_EXCEPTIONS = ['bzrlib/lsprof.py', 'bzrlib/_bencode_py.py']
40
LICENSE_EXCEPTIONS = ['bzrlib/lsprof.py']
47
LICENSE_EXCEPTIONS = ['bzrlib/lsprof.py', 'bzrlib/_bencode_py.py']
41
48
# Technically, 'bzrlib/lsprof.py' should be 'bzrlib/util/lsprof.py',
42
49
# (we do not check bzrlib/util/, since that is code bundled from elsewhere)
43
50
# but for compatibility with previous releases, we don't want to move it.
127
146
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:
129
155
def is_copyright_exception(self, fname):
130
156
"""Certain files are allowed to be different"""
131
if '/util/' in fname or '/plugins/' in fname:
157
if not self.is_our_code(fname):
132
158
# We don't ask that external utilities or plugins be
133
159
# (C) Canonical Ltd
136
161
for exc in COPYRIGHT_EXCEPTIONS:
137
162
if fname.endswith(exc):
142
166
def is_license_exception(self, fname):
143
167
"""Certain files are allowed to be different"""
144
if '/util/' in fname or '/plugins/' in fname:
145
# We don't ask that external utilities or plugins be
168
if not self.is_our_code(fname):
149
170
for exc in LICENSE_EXCEPTIONS:
150
171
if fname.endswith(exc):
155
175
def test_tmpdir_not_in_source_files(self):
225
244
# You should have received a copy of the GNU General Public License
226
245
# along with this program; if not, write to the Free Software
227
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
246
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
229
248
gpl_re = re.compile(re.escape(gpl_txt), re.MULTILINE)
231
for fname, text in self.get_source_file_contents():
250
for fname, text in self.get_source_file_contents(
251
extensions=('.py', '.pyx')):
232
252
if self.is_license_exception(fname):
234
254
if not gpl_re.search(text):
249
269
self.fail('\n'.join(help_text))
251
def test_no_tabs(self):
252
"""bzrlib source files should not contain any tab characters."""
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):
255
360
for fname, text in self.get_source_file_contents():
256
if '/util/' in fname or '/plugins/' in fname:
361
if not self.is_our_code(fname):
259
incorrect.append(fname)
262
self.fail('Tab characters were found in the following source files.'
263
'\nThey should either be replaced by "\\t" or by spaces:'
265
% ('\n '.join(incorrect)))
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))