~jelmer/ubuntu/maverick/bzr/2.2.5

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_source.py

ImportĀ upstreamĀ versionĀ 1.13~rc1

Show diffs side-by-side

added added

removed removed

Lines of Context:
35
35
    )
36
36
import bzrlib.branch
37
37
from bzrlib.tests import (
38
 
    KnownFailure,
39
38
    TestCase,
40
39
    TestSkipped,
41
40
    )
55
54
 
56
55
    def source_file_name(self, package):
57
56
        """Return the path of the .py file for package."""
 
57
        if getattr(sys, "frozen", None) is not None:
 
58
            raise TestSkipped("can't test sources in frozen distributions.")
58
59
        path = package.__file__
59
60
        if path[-1] in 'co':
60
61
            return path[:-1]
80
81
        # do not even think of increasing this number. If you think you need to
81
82
        # increase it, then you almost certainly are doing something wrong as
82
83
        # the relationship from working_tree to branch is one way.
83
 
        # 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,
84
85
        #it is not given a buffer but rather has this test updated immediately.
85
86
        self.assertEqual(0, occurences)
86
87
 
110
111
 
111
112
    def get_source_files(self):
112
113
        """Yield all source files for bzr and bzrlib
113
 
        
 
114
 
114
115
        :param our_files_only: If true, exclude files from included libraries
115
116
            or plugins.
116
117
        """
261
262
 
262
263
            self.fail('\n'.join(help_text))
263
264
 
264
 
    def test_no_tabs(self):
265
 
        """bzrlib source files should not contain any tab characters."""
266
 
        incorrect = []
267
 
 
 
265
    def _push_file(self, dict_, fname, line_no):
 
266
        if fname not in dict_:
 
267
            dict_[fname] = [line_no]
 
268
        else:
 
269
            dict_[fname].append(line_no)
 
270
 
 
271
    def _format_message(self, dict_, message):
 
272
        files = ["%s: %s" % (f, ', '.join([str(i+1) for i in lines]))
 
273
                for f, lines in dict_.items()]
 
274
        files.sort()
 
275
        return message + '\n\n    %s' % ('\n    '.join(files))
 
276
 
 
277
    def test_coding_style(self):
 
278
        """Check if bazaar code conforms to some coding style conventions.
 
279
 
 
280
        Currently we check for:
 
281
         * any tab characters
 
282
         * trailing white space
 
283
         * non-unix newlines
 
284
         * no newline at end of files
 
285
         * lines longer than 79 chars
 
286
           (only print how many files and lines are in violation)
 
287
        """
 
288
        tabs = {}
 
289
        trailing_ws = {}
 
290
        illegal_newlines = {}
 
291
        long_lines = {}
 
292
        no_newline_at_eof = []
268
293
        for fname, text in self.get_source_file_contents():
269
294
            if not self.is_our_code(fname):
270
295
                continue
271
 
            if '\t' in text:
272
 
                incorrect.append(fname)
273
 
 
274
 
        if incorrect:
275
 
            self.fail('Tab characters were found in the following source files.'
276
 
              '\nThey should either be replaced by "\\t" or by spaces:'
277
 
              '\n\n    %s'
278
 
              % ('\n    '.join(incorrect)))
 
296
            lines = text.splitlines(True)
 
297
            last_line_no = len(lines) - 1
 
298
            for line_no, line in enumerate(lines):
 
299
                if '\t' in line:
 
300
                    self._push_file(tabs, fname, line_no)
 
301
                if not line.endswith('\n') or line.endswith('\r\n'):
 
302
                    if line_no != last_line_no: # not no_newline_at_eof
 
303
                        self._push_file(illegal_newlines, fname, line_no)
 
304
                if line.endswith(' \n'):
 
305
                    self._push_file(trailing_ws, fname, line_no)
 
306
                if len(line) > 80:
 
307
                    self._push_file(long_lines, fname, line_no)
 
308
            if not lines[-1].endswith('\n'):
 
309
                no_newline_at_eof.append(fname)
 
310
        problems = []
 
311
        if tabs:
 
312
            problems.append(self._format_message(tabs,
 
313
                'Tab characters were found in the following source files.'
 
314
                '\nThey should either be replaced by "\\t" or by spaces:'))
 
315
        if trailing_ws:
 
316
            problems.append(self._format_message(trailing_ws,
 
317
                'Trailing white space was found in the following source files:'
 
318
                ))
 
319
        if illegal_newlines:
 
320
            problems.append(self._format_message(illegal_newlines,
 
321
                'Non-unix newlines were found in the following source files:'))
 
322
        if long_lines:
 
323
            print ("There are %i lines longer than 79 characters in %i files."
 
324
                % (sum([len(lines) for f, lines in long_lines.items()]),
 
325
                    len(long_lines)))
 
326
        if no_newline_at_eof:
 
327
            no_newline_at_eof.sort()
 
328
            problems.append("The following source files doesn't have a "
 
329
                "newline at the end:"
 
330
               '\n\n    %s'
 
331
               % ('\n    '.join(no_newline_at_eof)))
 
332
        if problems:
 
333
            self.fail('\n\n'.join(problems))
279
334
 
280
335
    def test_no_asserts(self):
281
336
        """bzr shouldn't use the 'assert' statement."""