2102.3.1
by mbp at sourcefrog
test_source should avoid walking into tempdirs |
1 |
# Copyright (C) 2005, 2006 Canonical Ltd
|
1523
by Robert Collins
Test for the number of uses of self.working_tree() in branch.py |
2 |
# Authors: Robert Collins <robert.collins@canonical.com>
|
3 |
#
|
|
4 |
# This program is free software; you can redistribute it and/or modify
|
|
5 |
# it under the terms of the GNU General Public License as published by
|
|
6 |
# the Free Software Foundation; either version 2 of the License, or
|
|
7 |
# (at your option) any later version.
|
|
8 |
#
|
|
9 |
# This program is distributed in the hope that it will be useful,
|
|
10 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
11 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
12 |
# GNU General Public License for more details.
|
|
13 |
#
|
|
14 |
# You should have received a copy of the GNU General Public License
|
|
15 |
# along with this program; if not, write to the Free Software
|
|
16 |
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
17 |
||
18 |
"""These tests are tests about the source code of bzrlib itself.
|
|
19 |
||
20 |
They are useful for testing code quality, checking coverage metric etc.
|
|
21 |
"""
|
|
22 |
||
23 |
# import system imports here
|
|
24 |
import os |
|
2052.3.1
by John Arbash Meinel
Add tests to cleanup the copyright of all source files |
25 |
import re |
1523
by Robert Collins
Test for the number of uses of self.working_tree() in branch.py |
26 |
import sys |
27 |
||
28 |
#import bzrlib specific imports here
|
|
2052.3.1
by John Arbash Meinel
Add tests to cleanup the copyright of all source files |
29 |
from bzrlib import ( |
30 |
osutils, |
|
31 |
)
|
|
1523
by Robert Collins
Test for the number of uses of self.working_tree() in branch.py |
32 |
import bzrlib.branch |
2052.3.1
by John Arbash Meinel
Add tests to cleanup the copyright of all source files |
33 |
from bzrlib.tests import TestCase, TestSkipped |
34 |
||
35 |
||
2052.3.8
by John Arbash Meinel
Better documentation about the exception variables |
36 |
# Files which are listed here will be skipped when testing for Copyright (or
|
37 |
# GPL) statements.
|
|
2052.3.5
by John Arbash Meinel
Guide people to how to add files to the list of exceptions |
38 |
COPYRIGHT_EXCEPTIONS = ['bzrlib/lsprof.py'] |
39 |
||
40 |
LICENSE_EXCEPTIONS = ['bzrlib/lsprof.py'] |
|
2052.3.8
by John Arbash Meinel
Better documentation about the exception variables |
41 |
# Technically, 'bzrlib/lsprof.py' should be 'bzrlib/util/lsprof.py',
|
42 |
# (we do not check bzrlib/util/, since that is code bundled from elsewhere)
|
|
43 |
# but for compatibility with previous releases, we don't want to move it.
|
|
2052.3.5
by John Arbash Meinel
Guide people to how to add files to the list of exceptions |
44 |
|
45 |
||
2052.3.1
by John Arbash Meinel
Add tests to cleanup the copyright of all source files |
46 |
class TestSourceHelper(TestCase): |
47 |
||
48 |
def source_file_name(self, package): |
|
49 |
"""Return the path of the .py file for package."""
|
|
50 |
path = package.__file__ |
|
51 |
if path[-1] in 'co': |
|
52 |
return path[:-1] |
|
53 |
else: |
|
54 |
return path |
|
55 |
||
56 |
||
57 |
class TestApiUsage(TestSourceHelper): |
|
1523
by Robert Collins
Test for the number of uses of self.working_tree() in branch.py |
58 |
|
1524
by Robert Collins
Test the uses of WorkingTree from branch.py |
59 |
def find_occurences(self, rule, filename): |
60 |
"""Find the number of occurences of rule in a file."""
|
|
1523
by Robert Collins
Test for the number of uses of self.working_tree() in branch.py |
61 |
occurences = 0 |
1524
by Robert Collins
Test the uses of WorkingTree from branch.py |
62 |
source = file(filename, 'r') |
1523
by Robert Collins
Test for the number of uses of self.working_tree() in branch.py |
63 |
for line in source: |
1524
by Robert Collins
Test the uses of WorkingTree from branch.py |
64 |
if line.find(rule) > -1: |
1523
by Robert Collins
Test for the number of uses of self.working_tree() in branch.py |
65 |
occurences += 1 |
1524
by Robert Collins
Test the uses of WorkingTree from branch.py |
66 |
return occurences |
67 |
||
68 |
def test_branch_working_tree(self): |
|
69 |
"""Test that the number of uses of working_tree in branch is stable."""
|
|
70 |
occurences = self.find_occurences('self.working_tree()', |
|
1526
by Robert Collins
Bugfix to source testing logic to get the right path when .py is returned by __file__ |
71 |
self.source_file_name(bzrlib.branch)) |
1523
by Robert Collins
Test for the number of uses of self.working_tree() in branch.py |
72 |
# do not even think of increasing this number. If you think you need to
|
73 |
# increase it, then you almost certainly are doing something wrong as
|
|
74 |
# the relationship from working_tree to branch is one way.
|
|
1534.4.35
by Robert Collins
Give branch its own basis tree and last_revision methods; deprecated branch.working_tree() |
75 |
# Note that this is an exact equality so that when the number drops,
|
76 |
#it is not given a buffer but rather has this test updated immediately.
|
|
77 |
self.assertEqual(0, occurences) |
|
1524
by Robert Collins
Test the uses of WorkingTree from branch.py |
78 |
|
79 |
def test_branch_WorkingTree(self): |
|
80 |
"""Test that the number of uses of working_tree in branch is stable."""
|
|
81 |
occurences = self.find_occurences('WorkingTree', |
|
1526
by Robert Collins
Bugfix to source testing logic to get the right path when .py is returned by __file__ |
82 |
self.source_file_name(bzrlib.branch)) |
1524
by Robert Collins
Test the uses of WorkingTree from branch.py |
83 |
# do not even think of increasing this number. If you think you need to
|
84 |
# increase it, then you almost certainly are doing something wrong as
|
|
85 |
# the relationship from working_tree to branch is one way.
|
|
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
|
|
91 |
# immediately.
|
|
1773.4.3
by Martin Pool
[merge] bzr.dev |
92 |
self.assertEqual(2, occurences) |
2052.3.1
by John Arbash Meinel
Add tests to cleanup the copyright of all source files |
93 |
|
94 |
||
95 |
class TestSource(TestSourceHelper): |
|
96 |
||
97 |
def get_bzrlib_dir(self): |
|
98 |
"""Get the path to the root of bzrlib"""
|
|
99 |
source = self.source_file_name(bzrlib) |
|
100 |
source_dir = os.path.dirname(source) |
|
101 |
||
102 |
# Avoid the case when bzrlib is packaged in a zip file
|
|
103 |
if not os.path.isdir(source_dir): |
|
104 |
raise TestSkipped('Cannot find bzrlib source directory. Expected %s' |
|
105 |
% source_dir) |
|
106 |
return source_dir |
|
107 |
||
108 |
def get_source_files(self): |
|
109 |
"""yield all source files for bzr and bzrlib"""
|
|
110 |
bzrlib_dir = self.get_bzrlib_dir() |
|
111 |
||
112 |
# This is the front-end 'bzr' script
|
|
113 |
bzr_path = self.get_bzr_path() |
|
114 |
yield bzr_path |
|
115 |
||
116 |
for root, dirs, files in os.walk(bzrlib_dir): |
|
2102.3.1
by mbp at sourcefrog
test_source should avoid walking into tempdirs |
117 |
for d in dirs: |
118 |
if d.endswith('.tmp'): |
|
119 |
dirs.remove(d) |
|
2052.3.1
by John Arbash Meinel
Add tests to cleanup the copyright of all source files |
120 |
for f in files: |
121 |
if not f.endswith('.py'): |
|
122 |
continue
|
|
123 |
yield osutils.pathjoin(root, f) |
|
124 |
||
125 |
def get_source_file_contents(self): |
|
126 |
for fname in self.get_source_files(): |
|
127 |
f = open(fname, 'rb') |
|
128 |
try: |
|
129 |
text = f.read() |
|
130 |
finally: |
|
131 |
f.close() |
|
132 |
yield fname, text |
|
133 |
||
2052.3.5
by John Arbash Meinel
Guide people to how to add files to the list of exceptions |
134 |
def is_copyright_exception(self, fname): |
135 |
"""Certain files are allowed to be different"""
|
|
2084.1.5
by John Arbash Meinel
Don't check plugins for copyright or license |
136 |
if '/util/' in fname or '/plugins/' in fname: |
137 |
# We don't ask that external utilities or plugins be
|
|
138 |
# (C) Canonical Ltd
|
|
2052.3.5
by John Arbash Meinel
Guide people to how to add files to the list of exceptions |
139 |
return True |
140 |
||
141 |
for exc in COPYRIGHT_EXCEPTIONS: |
|
142 |
if fname.endswith(exc): |
|
143 |
return True |
|
144 |
||
145 |
return False |
|
146 |
||
147 |
def is_license_exception(self, fname): |
|
148 |
"""Certain files are allowed to be different"""
|
|
2084.1.5
by John Arbash Meinel
Don't check plugins for copyright or license |
149 |
if '/util/' in fname or '/plugins/' in fname: |
150 |
# We don't ask that external utilities or plugins be
|
|
151 |
# (C) Canonical Ltd
|
|
2052.3.5
by John Arbash Meinel
Guide people to how to add files to the list of exceptions |
152 |
return True |
153 |
||
154 |
for exc in LICENSE_EXCEPTIONS: |
|
2052.3.1
by John Arbash Meinel
Add tests to cleanup the copyright of all source files |
155 |
if fname.endswith(exc): |
156 |
return True |
|
157 |
||
158 |
return False |
|
159 |
||
2102.3.1
by mbp at sourcefrog
test_source should avoid walking into tempdirs |
160 |
def test_tmpdir_not_in_source_files(self): |
161 |
"""When scanning for source files, we don't descend test tempdirs"""
|
|
162 |
for filename in self.get_source_files(): |
|
163 |
if re.search(r'test....\.tmp', filename): |
|
164 |
self.fail("get_source_file() returned filename %r " |
|
165 |
"from within a temporary directory"
|
|
166 |
% filename) |
|
167 |
||
2052.3.1
by John Arbash Meinel
Add tests to cleanup the copyright of all source files |
168 |
def test_copyright(self): |
169 |
"""Test that all .py files have a valid copyright statement"""
|
|
170 |
# These are files which contain a different copyright statement
|
|
171 |
# and that is okay.
|
|
172 |
incorrect = [] |
|
173 |
||
2052.3.7
by John Arbash Meinel
Use positive lookahead to avoid extra newlines |
174 |
copyright_re = re.compile('#\\s*copyright.*(?=\n)', re.I) |
2052.3.1
by John Arbash Meinel
Add tests to cleanup the copyright of all source files |
175 |
copyright_canonical_re = re.compile( |
176 |
r'# Copyright \(C\) ' # Opening "# Copyright (C)" |
|
177 |
r'(\d+)(, \d+)*' # Followed by a series of dates |
|
178 |
r'.*Canonical Ltd' # And containing 'Canonical Ltd' |
|
179 |
)
|
|
180 |
||
181 |
for fname, text in self.get_source_file_contents(): |
|
2052.3.5
by John Arbash Meinel
Guide people to how to add files to the list of exceptions |
182 |
if self.is_copyright_exception(fname): |
2052.3.1
by John Arbash Meinel
Add tests to cleanup the copyright of all source files |
183 |
continue
|
2052.3.2
by John Arbash Meinel
Change Copyright .. by Canonical to Copyright ... Canonical |
184 |
match = copyright_canonical_re.search(text) |
185 |
if not match: |
|
186 |
match = copyright_re.search(text) |
|
187 |
if match: |
|
188 |
incorrect.append((fname, 'found: %s' % (match.group(),))) |
|
2052.3.1
by John Arbash Meinel
Add tests to cleanup the copyright of all source files |
189 |
else: |
190 |
incorrect.append((fname, 'no copyright line found\n')) |
|
2052.3.2
by John Arbash Meinel
Change Copyright .. by Canonical to Copyright ... Canonical |
191 |
else: |
192 |
if 'by Canonical' in match.group(): |
|
193 |
incorrect.append((fname, |
|
194 |
'should not have: "by Canonical": %s' |
|
195 |
% (match.group(),))) |
|
2052.3.1
by John Arbash Meinel
Add tests to cleanup the copyright of all source files |
196 |
|
197 |
if incorrect: |
|
198 |
help_text = ["Some files have missing or incorrect copyright" |
|
199 |
" statements.", |
|
2052.3.5
by John Arbash Meinel
Guide people to how to add files to the list of exceptions |
200 |
"", |
201 |
"Please either add them to the list of"
|
|
202 |
" COPYRIGHT_EXCEPTIONS in"
|
|
203 |
" bzrlib/tests/test_source.py", |
|
204 |
# this is broken to prevent a false match
|
|
205 |
"or add '# Copyright (C)"
|
|
206 |
" 2006 Canonical Ltd' to these files:", |
|
2052.3.1
by John Arbash Meinel
Add tests to cleanup the copyright of all source files |
207 |
"", |
208 |
]
|
|
209 |
for fname, comment in incorrect: |
|
210 |
help_text.append(fname) |
|
211 |
help_text.append((' '*4) + comment) |
|
212 |
||
213 |
self.fail('\n'.join(help_text)) |
|
214 |
||
215 |
def test_gpl(self): |
|
216 |
"""Test that all .py files have a GPL disclaimer"""
|
|
217 |
incorrect = [] |
|
218 |
||
219 |
gpl_txt = """ |
|
220 |
# This program is free software; you can redistribute it and/or modify
|
|
221 |
# it under the terms of the GNU General Public License as published by
|
|
222 |
# the Free Software Foundation; either version 2 of the License, or
|
|
223 |
# (at your option) any later version.
|
|
224 |
#
|
|
225 |
# This program is distributed in the hope that it will be useful,
|
|
226 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
227 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
228 |
# GNU General Public License for more details.
|
|
229 |
#
|
|
230 |
# You should have received a copy of the GNU General Public License
|
|
231 |
# along with this program; if not, write to the Free Software
|
|
232 |
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
233 |
"""
|
|
234 |
gpl_re = re.compile(re.escape(gpl_txt), re.MULTILINE) |
|
235 |
||
236 |
for fname, text in self.get_source_file_contents(): |
|
2052.3.5
by John Arbash Meinel
Guide people to how to add files to the list of exceptions |
237 |
if self.is_license_exception(fname): |
2052.3.1
by John Arbash Meinel
Add tests to cleanup the copyright of all source files |
238 |
continue
|
239 |
if not gpl_re.search(text): |
|
240 |
incorrect.append(fname) |
|
241 |
||
242 |
if incorrect: |
|
243 |
help_text = ['Some files have missing or incomplete GPL statement', |
|
2052.3.5
by John Arbash Meinel
Guide people to how to add files to the list of exceptions |
244 |
"", |
245 |
"Please either add them to the list of"
|
|
246 |
" LICENSE_EXCEPTIONS in"
|
|
247 |
" bzrlib/tests/test_source.py", |
|
248 |
"Or add the following text to the beginning:", |
|
2052.3.1
by John Arbash Meinel
Add tests to cleanup the copyright of all source files |
249 |
gpl_txt
|
250 |
]
|
|
251 |
for fname in incorrect: |
|
252 |
help_text.append((' '*4) + fname) |
|
253 |
||
254 |
self.fail('\n'.join(help_text)) |
|
2120.2.1
by John Arbash Meinel
Remove tabs from source files, and add a test to keep it that way. |
255 |
|
256 |
def test_no_tabs(self): |
|
257 |
"""bzrlib source files should not contain any tab characters."""
|
|
258 |
incorrect = [] |
|
259 |
||
260 |
for fname, text in self.get_source_file_contents(): |
|
261 |
if '\t' in text: |
|
262 |
incorrect.append(fname) |
|
263 |
||
264 |
if incorrect: |
|
265 |
self.fail('Tab characters were found in the following source files.' |
|
266 |
'\nThey should either be replaced by "\\t" or by spaces:' |
|
267 |
'\n\n %s' |
|
268 |
% ('\n '.join(incorrect))) |