~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_source.py

  • Committer: John Ferlito
  • Date: 2009-09-02 04:31:45 UTC
  • mto: (4665.7.1 serve-init)
  • mto: This revision was merged to the branch mainline in revision 4913.
  • Revision ID: johnf@inodes.org-20090902043145-gxdsfw03ilcwbyn5
Add a debian init script for bzr --serve

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
#
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
18
18
 
19
19
"""These tests are tests about the source code of bzrlib itself.
20
20
 
35
35
    )
36
36
import bzrlib.branch
37
37
from bzrlib.tests import (
38
 
    KnownFailure,
39
38
    TestCase,
40
39
    TestSkipped,
41
40
    )
43
42
 
44
43
# Files which are listed here will be skipped when testing for Copyright (or
45
44
# GPL) statements.
46
 
COPYRIGHT_EXCEPTIONS = ['bzrlib/lsprof.py']
 
45
COPYRIGHT_EXCEPTIONS = ['bzrlib/lsprof.py', 'bzrlib/_bencode_py.py']
47
46
 
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)
88
87
 
110
109
                              % source_dir)
111
110
        return source_dir
112
111
 
113
 
    def get_source_files(self):
 
112
    def get_source_files(self, extensions=None):
114
113
        """Yield all source files for bzr and bzrlib
115
 
        
 
114
 
116
115
        :param our_files_only: If true, exclude files from included libraries
117
116
            or plugins.
118
117
        """
119
118
        bzrlib_dir = self.get_bzrlib_dir()
 
119
        if extensions is None:
 
120
            extensions = ('.py',)
120
121
 
121
122
        # This is the front-end 'bzr' script
122
123
        bzr_path = self.get_bzr_path()
127
128
                if d.endswith('.tmp'):
128
129
                    dirs.remove(d)
129
130
            for f in files:
130
 
                if not f.endswith('.py'):
 
131
                for extension in extensions:
 
132
                    if f.endswith(extension):
 
133
                        break
 
134
                else:
 
135
                    # Did not match the accepted extensions
131
136
                    continue
132
137
                yield osutils.pathjoin(root, f)
133
138
 
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')
137
142
            try:
138
143
                text = f.read()
176
181
                          % filename)
177
182
 
178
183
    def test_copyright(self):
179
 
        """Test that all .py files have a valid copyright statement"""
180
 
        # These are files which contain a different copyright statement
181
 
        # and that is okay.
 
184
        """Test that all .py and .pyx files have a valid copyright statement"""
182
185
        incorrect = []
183
186
 
184
187
        copyright_re = re.compile('#\\s*copyright.*(?=\n)', re.I)
188
191
            r'.*Canonical Ltd' # And containing 'Canonical Ltd'
189
192
            )
190
193
 
191
 
        for fname, text in self.get_source_file_contents():
 
194
        for fname, text in self.get_source_file_contents(
 
195
                extensions=('.py', '.pyx')):
192
196
            if self.is_copyright_exception(fname):
193
197
                continue
194
198
            match = copyright_canonical_re.search(text)
223
227
            self.fail('\n'.join(help_text))
224
228
 
225
229
    def test_gpl(self):
226
 
        """Test that all .py files have a GPL disclaimer"""
 
230
        """Test that all .py and .pyx files have a GPL disclaimer."""
227
231
        incorrect = []
228
232
 
229
233
        gpl_txt = """
239
243
#
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
243
247
"""
244
248
        gpl_re = re.compile(re.escape(gpl_txt), re.MULTILINE)
245
249
 
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):
248
253
                continue
249
254
            if not gpl_re.search(text):
263
268
 
264
269
            self.fail('\n'.join(help_text))
265
270
 
266
 
    def test_no_tabs(self):
267
 
        """bzrlib source files should not contain any tab characters."""
268
 
        incorrect = []
269
 
 
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]
 
274
        else:
 
275
            dict_[fname].append(line_no)
 
276
 
 
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()]
 
280
        files.sort()
 
281
        return message + '\n\n    %s' % ('\n    '.join(files))
 
282
 
 
283
    def test_coding_style(self):
 
284
        """Check if bazaar code conforms to some coding style conventions.
 
285
 
 
286
        Currently we assert that the following is not present:
 
287
         * any tab characters
 
288
         * non-unix newlines
 
289
         * no newline at end of files
 
290
 
 
291
        Print how many files have
 
292
         * trailing white space
 
293
         * lines longer than 79 chars
 
294
        """
 
295
        tabs = {}
 
296
        trailing_ws = {}
 
297
        illegal_newlines = {}
 
298
        long_lines = {}
 
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):
272
303
                continue
273
 
            if '\t' in text:
274
 
                incorrect.append(fname)
275
 
 
276
 
        if incorrect:
277
 
            self.fail('Tab characters were found in the following source files.'
278
 
              '\nThey should either be replaced by "\\t" or by spaces:'
279
 
              '\n\n    %s'
280
 
              % ('\n    '.join(incorrect)))
 
304
            lines = text.splitlines(True)
 
305
            last_line_no = len(lines) - 1
 
306
            for line_no, line in enumerate(lines):
 
307
                if '\t' in line:
 
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)
 
314
                if len(line) > 80:
 
315
                    self._push_file(long_lines, fname, line_no)
 
316
            if not lines[-1].endswith('\n'):
 
317
                no_newline_at_eof.append(fname)
 
318
        problems = []
 
319
        if tabs:
 
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:'))
 
323
        if trailing_ws:
 
324
            print ("There are %i lines with trailing white space in %i files."
 
325
                % (sum([len(lines) for f, lines in trailing_ws.items()]),
 
326
                    len(trailing_ws)))
 
327
        if illegal_newlines:
 
328
            problems.append(self._format_message(illegal_newlines,
 
329
                'Non-unix newlines were found in the following source files:'))
 
330
        if long_lines:
 
331
            print ("There are %i lines longer than 79 characters in %i files."
 
332
                % (sum([len(lines) for f, lines in long_lines.items()]),
 
333
                    len(long_lines)))
 
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:"
 
338
               '\n\n    %s'
 
339
               % ('\n    '.join(no_newline_at_eof)))
 
340
        if problems:
 
341
            self.fail('\n\n'.join(problems))
281
342
 
282
343
    def test_no_asserts(self):
283
344
        """bzr shouldn't use the 'assert' statement."""