Beispiel #1
0
    def test_process__checker_dispatched(self):
        """Test the process() method for a path with a dispatched checker."""
        file_path = 'foo.txt'
        lines = ['line1', 'line2']
        line_numbers = [100]

        expected_error_handler = DefaultStyleErrorHandler(
            configuration=self._configuration,
            file_path=file_path,
            increment_error_count=self._do_nothing,
            line_numbers=line_numbers)

        self._processor.process(
            lines=lines, file_path=file_path, line_numbers=line_numbers)

        # Check that the carriage-return checker was instantiated correctly
        # and was passed lines correctly.
        carriage_checker = self.carriage_checker
        self.assertEqual(carriage_checker.style_error_handler,
                         expected_error_handler)
        self.assertEqual(carriage_checker.lines, ['line1', 'line2'])

        # Check that the style checker was dispatched correctly and was
        # passed lines correctly.
        checker = self._mock_dispatcher.dispatched_checker
        self.assertEqual(checker.file_path, 'foo.txt')
        self.assertEqual(checker.min_confidence, 3)
        self.assertEqual(checker.style_error_handler, expected_error_handler)

        self.assertEqual(checker.lines, ['line1', 'line2'])
Beispiel #2
0
 def dispatch(self, file_path):
     """Call dispatch() with the given file path."""
     dispatcher = CheckerDispatcher()
     self.mock_handle_style_error = DefaultStyleErrorHandler(
         '', None, None, [])
     checker = dispatcher.dispatch(
         file_path, self.mock_handle_style_error, min_confidence=3)
     return checker
Beispiel #3
0
 def make_handler(configuration=self._style_checker_configuration(),
                  file_path='foo.txt',
                  increment_error_count=lambda: True,
                  line_numbers=None):
     line_numbers = line_numbers or [100]
     return DefaultStyleErrorHandler(
         configuration=configuration,
         file_path=file_path,
         increment_error_count=increment_error_count,
         line_numbers=line_numbers)
Beispiel #4
0
    def process(self, lines, file_path, line_numbers=None):
        """Check the given lines for style.

        Arguments:
          lines: A list of all lines in the file to check.
          file_path: The path of the file to process.  If possible, the path
                     should be relative to the source root.  Otherwise,
                     path-specific logic may not behave as expected.
          line_numbers: A list of line numbers of the lines for which
                        style errors should be reported, or None if errors
                        for all lines should be reported.  When not None, this
                        list normally contains the line numbers corresponding
                        to the modified lines of a patch.
        """
        _log.debug('Checking style: ' + file_path)

        style_error_handler = DefaultStyleErrorHandler(
            configuration=self._configuration,
            file_path=file_path,
            increment_error_count=self._increment_error_count,
            line_numbers=line_numbers)

        carriage_checker = self._carriage_checker_class(style_error_handler)

        # Check for and remove trailing carriage returns ("\r").
        if self._dispatcher.should_check_and_strip_carriage_returns(file_path):
            lines = carriage_checker.check(lines)

        min_confidence = self._configuration.min_confidence
        checker = self._dispatcher.dispatch(file_path, style_error_handler,
                                            min_confidence)

        if checker is None:
            raise AssertionError("File should not be checked: '%s'" %
                                 file_path)

        _log.debug('Using class: ' + checker.__class__.__name__)

        checker.check(lines)
Beispiel #5
0
 def _error_handler(self, configuration, line_numbers=None):
     return DefaultStyleErrorHandler(
         configuration=configuration,
         file_path=self._file_path,
         increment_error_count=self._mock_increment_error_count,
         line_numbers=line_numbers)