/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_source.py

merge bzr.dev r4042

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
    )
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
 
112
111
 
113
112
    def get_source_files(self):
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
        """
263
262
 
264
263
            self.fail('\n'.join(help_text))
265
264
 
266
 
    def test_no_tabs(self):
267
 
        """bzrlib source files should not contain any tab characters."""
268
 
        incorrect = []
269
 
 
 
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 = []
270
293
        for fname, text in self.get_source_file_contents():
271
294
            if not self.is_our_code(fname):
272
295
                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)))
 
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))
281
334
 
282
335
    def test_no_asserts(self):
283
336
        """bzr shouldn't use the 'assert' statement."""