/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

  • Committer: Robert Collins
  • Date: 2009-03-13 02:25:46 UTC
  • mfrom: (4133 +trunk)
  • mto: This revision was merged to the branch mainline in revision 4183.
  • Revision ID: robertc@robertcollins.net-20090313022546-e7de5zsdkbay5okf
MergeĀ .dev.

Show diffs side-by-side

added added

removed removed

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