90
80
"""Test that the number of uses of working_tree in branch is stable."""
91
81
occurences = self.find_occurences('WorkingTree',
92
82
self.source_file_name(bzrlib.branch))
93
# 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
94
84
# increase it, then you almost certainly are doing something wrong as
95
85
# the relationship from working_tree to branch is one way.
96
# As of 20070809, there are no longer any mentions at all.
97
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)
100
95
class TestSource(TestSourceHelper):
147
132
yield fname, text
149
def is_our_code(self, fname):
150
"""Return true if it's a "real" part of bzrlib rather than external code"""
151
if '/util/' in fname or '/plugins/' in fname:
156
134
def is_copyright_exception(self, fname):
157
135
"""Certain files are allowed to be different"""
158
if not self.is_our_code(fname):
136
if '/util/' in fname or '/plugins/' in fname:
159
137
# We don't ask that external utilities or plugins be
160
138
# (C) Canonical Ltd
162
141
for exc in COPYRIGHT_EXCEPTIONS:
163
142
if fname.endswith(exc):
167
147
def is_license_exception(self, fname):
168
148
"""Certain files are allowed to be different"""
169
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
171
154
for exc in LICENSE_EXCEPTIONS:
172
155
if fname.endswith(exc):
176
160
def test_tmpdir_not_in_source_files(self):
245
230
# You should have received a copy of the GNU General Public License
246
231
# along with this program; if not, write to the Free Software
247
# 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
249
234
gpl_re = re.compile(re.escape(gpl_txt), re.MULTILINE)
251
for fname, text in self.get_source_file_contents(
252
extensions=('.py', '.pyx')):
236
for fname, text in self.get_source_file_contents():
253
237
if self.is_license_exception(fname):
255
239
if not gpl_re.search(text):
270
254
self.fail('\n'.join(help_text))
272
def _push_file(self, dict_, fname, line_no):
273
if fname not in dict_:
274
dict_[fname] = [line_no]
276
dict_[fname].append(line_no)
278
def _format_message(self, dict_, message):
279
files = ["%s: %s" % (f, ', '.join([str(i+1) for i in lines]))
280
for f, lines in dict_.items()]
282
return message + '\n\n %s' % ('\n '.join(files))
284
def test_coding_style(self):
285
"""Check if bazaar code conforms to some coding style conventions.
287
Currently we check for:
289
* trailing white space
291
* no newline at end of files
292
* lines longer than 79 chars
293
(only print how many files and lines are in violation)
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
problems.append(self._format_message(trailing_ws,
325
'Trailing white space was found in the following source files:'
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
raise KnownFailure("test_coding_style has failed")
342
self.fail('\n\n'.join(problems))
344
def test_no_asserts(self):
345
"""bzr shouldn't use the 'assert' statement."""
346
# assert causes too much variation between -O and not, and tends to
347
# give bad errors to the user
349
# scan down through x for assert statements, report any problems
350
# this is a bit cheesy; it may get some false positives?
351
if x[0] == symbol.assert_stmt:
353
elif x[0] == token.NAME:
354
# can't search further down
357
if sub and search(sub):
256
def test_no_tabs(self):
257
"""bzrlib source files should not contain any tab characters."""
361
260
for fname, text in self.get_source_file_contents():
362
if not self.is_our_code(fname):
261
if '/util/' in fname or '/plugins/' in fname:
364
ast = parser.ast2tuple(parser.suite(''.join(text)))
366
badfiles.append(fname)
369
"these files contain an assert statement and should not:\n%s"
370
% '\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)))