1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
|
# Copyright (C) 2005, 2006, 2008 Canonical Ltd
# Authors: Robert Collins <robert.collins@canonical.com>
# and others
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
"""These tests are tests about the source code of bzrlib itself.
They are useful for testing code quality, checking coverage metric etc.
"""
# import system imports here
import os
import parser
import re
import symbol
import sys
import token
#import bzrlib specific imports here
from bzrlib import (
osutils,
)
import bzrlib.branch
from bzrlib.tests import (
KnownFailure,
TestCase,
TestSkipped,
)
# Files which are listed here will be skipped when testing for Copyright (or
# GPL) statements.
COPYRIGHT_EXCEPTIONS = ['bzrlib/lsprof.py']
LICENSE_EXCEPTIONS = ['bzrlib/lsprof.py']
# Technically, 'bzrlib/lsprof.py' should be 'bzrlib/util/lsprof.py',
# (we do not check bzrlib/util/, since that is code bundled from elsewhere)
# but for compatibility with previous releases, we don't want to move it.
class TestSourceHelper(TestCase):
def source_file_name(self, package):
"""Return the path of the .py file for package."""
if getattr(sys, "frozen", None) is not None:
raise TestSkipped("can't test sources in frozen distributions.")
path = package.__file__
if path[-1] in 'co':
return path[:-1]
else:
return path
class TestApiUsage(TestSourceHelper):
def find_occurences(self, rule, filename):
"""Find the number of occurences of rule in a file."""
occurences = 0
source = file(filename, 'r')
for line in source:
if line.find(rule) > -1:
occurences += 1
return occurences
def test_branch_working_tree(self):
"""Test that the number of uses of working_tree in branch is stable."""
occurences = self.find_occurences('self.working_tree()',
self.source_file_name(bzrlib.branch))
# do not even think of increasing this number. If you think you need to
# increase it, then you almost certainly are doing something wrong as
# the relationship from working_tree to branch is one way.
# Note that this is an exact equality so that when the number drops,
#it is not given a buffer but rather has this test updated immediately.
self.assertEqual(0, occurences)
def test_branch_WorkingTree(self):
"""Test that the number of uses of working_tree in branch is stable."""
occurences = self.find_occurences('WorkingTree',
self.source_file_name(bzrlib.branch))
# Do not even think of increasing this number. If you think you need to
# increase it, then you almost certainly are doing something wrong as
# the relationship from working_tree to branch is one way.
# As of 20070809, there are no longer any mentions at all.
self.assertEqual(0, occurences)
class TestSource(TestSourceHelper):
def get_bzrlib_dir(self):
"""Get the path to the root of bzrlib"""
source = self.source_file_name(bzrlib)
source_dir = os.path.dirname(source)
# Avoid the case when bzrlib is packaged in a zip file
if not os.path.isdir(source_dir):
raise TestSkipped('Cannot find bzrlib source directory. Expected %s'
% source_dir)
return source_dir
def get_source_files(self):
"""Yield all source files for bzr and bzrlib
:param our_files_only: If true, exclude files from included libraries
or plugins.
"""
bzrlib_dir = self.get_bzrlib_dir()
# This is the front-end 'bzr' script
bzr_path = self.get_bzr_path()
yield bzr_path
for root, dirs, files in os.walk(bzrlib_dir):
for d in dirs:
if d.endswith('.tmp'):
dirs.remove(d)
for f in files:
if not f.endswith('.py'):
continue
yield osutils.pathjoin(root, f)
def get_source_file_contents(self):
for fname in self.get_source_files():
f = open(fname, 'rb')
try:
text = f.read()
finally:
f.close()
yield fname, text
def is_our_code(self, fname):
"""Return true if it's a "real" part of bzrlib rather than external code"""
if '/util/' in fname or '/plugins/' in fname:
return False
else:
return True
def is_copyright_exception(self, fname):
"""Certain files are allowed to be different"""
if not self.is_our_code(fname):
# We don't ask that external utilities or plugins be
# (C) Canonical Ltd
return True
for exc in COPYRIGHT_EXCEPTIONS:
if fname.endswith(exc):
return True
return False
def is_license_exception(self, fname):
"""Certain files are allowed to be different"""
if not self.is_our_code(fname):
return True
for exc in LICENSE_EXCEPTIONS:
if fname.endswith(exc):
return True
return False
def test_tmpdir_not_in_source_files(self):
"""When scanning for source files, we don't descend test tempdirs"""
for filename in self.get_source_files():
if re.search(r'test....\.tmp', filename):
self.fail("get_source_file() returned filename %r "
"from within a temporary directory"
% filename)
def test_copyright(self):
"""Test that all .py files have a valid copyright statement"""
# These are files which contain a different copyright statement
# and that is okay.
incorrect = []
copyright_re = re.compile('#\\s*copyright.*(?=\n)', re.I)
copyright_canonical_re = re.compile(
r'# Copyright \(C\) ' # Opening "# Copyright (C)"
r'(\d+)(, \d+)*' # Followed by a series of dates
r'.*Canonical Ltd' # And containing 'Canonical Ltd'
)
for fname, text in self.get_source_file_contents():
if self.is_copyright_exception(fname):
continue
match = copyright_canonical_re.search(text)
if not match:
match = copyright_re.search(text)
if match:
incorrect.append((fname, 'found: %s' % (match.group(),)))
else:
incorrect.append((fname, 'no copyright line found\n'))
else:
if 'by Canonical' in match.group():
incorrect.append((fname,
'should not have: "by Canonical": %s'
% (match.group(),)))
if incorrect:
help_text = ["Some files have missing or incorrect copyright"
" statements.",
"",
"Please either add them to the list of"
" COPYRIGHT_EXCEPTIONS in"
" bzrlib/tests/test_source.py",
# this is broken to prevent a false match
"or add '# Copyright (C)"
" 2007 Canonical Ltd' to these files:",
"",
]
for fname, comment in incorrect:
help_text.append(fname)
help_text.append((' '*4) + comment)
self.fail('\n'.join(help_text))
def test_gpl(self):
"""Test that all .py files have a GPL disclaimer"""
incorrect = []
gpl_txt = """
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
"""
gpl_re = re.compile(re.escape(gpl_txt), re.MULTILINE)
for fname, text in self.get_source_file_contents():
if self.is_license_exception(fname):
continue
if not gpl_re.search(text):
incorrect.append(fname)
if incorrect:
help_text = ['Some files have missing or incomplete GPL statement',
"",
"Please either add them to the list of"
" LICENSE_EXCEPTIONS in"
" bzrlib/tests/test_source.py",
"Or add the following text to the beginning:",
gpl_txt
]
for fname in incorrect:
help_text.append((' '*4) + fname)
self.fail('\n'.join(help_text))
def test_no_tabs(self):
"""bzrlib source files should not contain any tab characters."""
incorrect = []
for fname, text in self.get_source_file_contents():
if not self.is_our_code(fname):
continue
if '\t' in text:
incorrect.append(fname)
if incorrect:
self.fail('Tab characters were found in the following source files.'
'\nThey should either be replaced by "\\t" or by spaces:'
'\n\n %s'
% ('\n '.join(incorrect)))
def test_no_asserts(self):
"""bzr shouldn't use the 'assert' statement."""
# assert causes too much variation between -O and not, and tends to
# give bad errors to the user
def search(x):
# scan down through x for assert statements, report any problems
# this is a bit cheesy; it may get some false positives?
if x[0] == symbol.assert_stmt:
return True
elif x[0] == token.NAME:
# can't search further down
return False
for sub in x[1:]:
if sub and search(sub):
return True
return False
badfiles = []
for fname, text in self.get_source_file_contents():
if not self.is_our_code(fname):
continue
ast = parser.ast2tuple(parser.suite(''.join(text)))
if search(ast):
badfiles.append(fname)
if badfiles:
self.fail(
"these files contain an assert statement and should not:\n%s"
% '\n'.join(badfiles))
|