15
15
# You should have received a copy of the GNU General Public License
16
16
# along with this program; if not, write to the Free Software
17
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
19
"""These tests are tests about the source code of bzrlib itself.
44
43
# Files which are listed here will be skipped when testing for Copyright (or
46
COPYRIGHT_EXCEPTIONS = ['bzrlib/lsprof.py']
45
COPYRIGHT_EXCEPTIONS = ['bzrlib/lsprof.py', 'bzrlib/_bencode_py.py']
48
LICENSE_EXCEPTIONS = ['bzrlib/lsprof.py']
47
LICENSE_EXCEPTIONS = ['bzrlib/lsprof.py', 'bzrlib/_bencode_py.py']
49
48
# Technically, 'bzrlib/lsprof.py' should be 'bzrlib/util/lsprof.py',
50
49
# (we do not check bzrlib/util/, since that is code bundled from elsewhere)
51
50
# but for compatibility with previous releases, we don't want to move it.
82
81
# do not even think of increasing this number. If you think you need to
83
82
# increase it, then you almost certainly are doing something wrong as
84
83
# the relationship from working_tree to branch is one way.
85
# Note that this is an exact equality so that when the number drops,
84
# Note that this is an exact equality so that when the number drops,
86
85
#it is not given a buffer but rather has this test updated immediately.
87
86
self.assertEqual(0, occurences)
127
128
if d.endswith('.tmp'):
130
if not f.endswith('.py'):
131
for extension in extensions:
132
if f.endswith(extension):
135
# Did not match the accepted extensions
132
137
yield osutils.pathjoin(root, f)
134
def get_source_file_contents(self):
135
for fname in self.get_source_files():
139
def get_source_file_contents(self, extensions=None):
140
for fname in self.get_source_files(extensions=extensions):
136
141
f = open(fname, 'rb')
240
244
# You should have received a copy of the GNU General Public License
241
245
# along with this program; if not, write to the Free Software
242
# 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
244
248
gpl_re = re.compile(re.escape(gpl_txt), re.MULTILINE)
246
for fname, text in self.get_source_file_contents():
250
for fname, text in self.get_source_file_contents(
251
extensions=('.py', '.pyx')):
247
252
if self.is_license_exception(fname):
249
254
if not gpl_re.search(text):
264
269
self.fail('\n'.join(help_text))
266
def test_no_tabs(self):
267
"""bzrlib source files should not contain any tab characters."""
270
for fname, text in self.get_source_file_contents():
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')):
271
302
if not self.is_our_code(fname):
274
incorrect.append(fname)
277
self.fail('Tab characters were found in the following source files.'
278
'\nThey should either be replaced by "\\t" or by spaces:'
280
% ('\n '.join(incorrect)))
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))
282
343
def test_no_asserts(self):
283
344
"""bzr shouldn't use the 'assert' statement."""