50
57
# (we do not check bzrlib/util/, since that is code bundled from elsewhere)
51
58
# but for compatibility with previous releases, we don't want to move it.
60
def check_coding_style(old_filename, oldlines, new_filename, newlines, to_file,
61
allow_binary=False, sequence_matcher=None,
62
path_encoding='utf8'):
63
"""text_differ to be passed to diff.DiffText, which checks code style """
64
# Special workaround for Python2.3, where difflib fails if
65
# both sequences are empty.
66
if not oldlines and not newlines:
69
if allow_binary is False:
70
textfile.check_text_lines(oldlines)
71
textfile.check_text_lines(newlines)
73
if sequence_matcher is None:
74
sequence_matcher = patiencediff.PatienceSequenceMatcher
76
started = [False] #trick to access parent scoped variable
77
def start_if_needed():
79
to_file.write('+++ %s\n' % new_filename)
82
def check_newlines(j1, j2):
83
for i, line in enumerate(newlines[j1:j2]):
84
bad_ws_match = re.match(r'^(([\t]*)(.*?)([\t ]*))(\r?\n)?$', line)
86
line_content = bad_ws_match.group(1)
87
has_leading_tabs = bool(bad_ws_match.group(2))
88
has_trailing_whitespace = bool(bad_ws_match.group(4))
91
to_file.write('line %i has leading tabs: "%s"\n'% (
92
i+1+j1, line_content))
93
if has_trailing_whitespace:
95
to_file.write('line %i has trailing whitespace: "%s"\n'% (
96
i+1+j1, line_content))
97
if len(line_content) > 79:
99
'\nFile %s\nline %i is longer than 79 characters:'
100
' "%s"\n'% (new_filename, i+1+j1, line_content))
102
for group in sequence_matcher(None, oldlines, newlines
103
).get_grouped_opcodes(0):
104
for tag, i1, i2, j1, j2 in group:
105
if tag == 'replace' or tag == 'insert':
106
check_newlines(j1, j2)
108
if len(newlines) == j2 and not newlines[j2-1].endswith('\n'):
110
to_file.write("\\ No newline at end of file\n")
54
113
class TestSourceHelper(TestCase):
280
339
% ('\n '.join(incorrect)))
341
def test_coding_style(self):
342
""" Check if bazaar code conforms to some coding style conventions.
344
Currently we check all .py files for:
345
* new trailing white space
347
* new long lines (give warning only)
348
* no newline at end of files
350
bzr_dir = osutils.dirname(osutils.realpath(sys.argv[0]))
351
wt = WorkingTree.open(bzr_dir)
352
diff_output = StringIO()
357
old_tree = new_tree.basis_tree()
362
iterator = new_tree.iter_changes(old_tree, specific_files=None,
363
extra_trees=None, require_versioned=False)
364
for (file_id, paths, changed_content, versioned, parent,
365
name, kind, executable) in iterator:
366
if (changed_content and kind[1] == 'file'
367
and paths[1].endswith('.py')):
368
diff_text = diff.DiffText(old_tree, new_tree,
370
text_differ=check_coding_style)
371
diff_text.diff(file_id, paths[0], paths[1],
378
if len(diff_output.getvalue()) > 0:
379
self.fail("Unacceptable coding style:\n" + diff_output.getvalue())
282
381
def test_no_asserts(self):
283
382
"""bzr shouldn't use the 'assert' statement."""
284
383
# assert causes too much variation between -O and not, and tends to