Exemplo n.º 1
0
def test_handle_file_plugins(plugin_target):
    """Test the FileChecker class handling different file plugin types."""
    # Mock an entry point returning the plugin target
    entry_point = mock.Mock(spec=['require', 'resolve', 'load'])
    entry_point.name = plugin_target.name
    entry_point.resolve.return_value = plugin_target

    # Load the checker plugins using the entry point mock
    with mock.patch('pkg_resources.iter_entry_points',
                    return_value=[entry_point]):
        checks = manager.Checkers()

    # Prevent it from reading lines from stdin or somewhere else
    with mock.patch('flake8.processor.FileProcessor.read_lines',
                    return_value=['Line 1']):
        file_checker = checker.FileChecker('-', checks.to_dictionary(),
                                           mock.MagicMock())

    # Do not actually build an AST
    file_checker.processor.build_ast = lambda: True

    # Forward reports to this mock
    report = mock.Mock()
    file_checker.report = report
    file_checker.run_ast_checks()
    report.assert_called_once_with(error_code=None,
                                   line_number=EXPECTED_REPORT[0],
                                   column=EXPECTED_REPORT[1],
                                   text=EXPECTED_REPORT[2])
Exemplo n.º 2
0
    def find_plugins(self):
        # type: () -> None
        """Find and load the plugins for this application.

        If :attr:`check_plugins`, or :attr:`formatting_plugins` are ``None``
        then this method will update them with the appropriate plugin manager
        instance. Given the expense of finding plugins (via :mod:`entrypoints`)
        we want this to be idempotent and so only update those attributes if
        they are ``None``.
        """
        if self.local_plugins is None:
            self.local_plugins = config.get_local_plugins(
                self.config_finder, self.prelim_opts.config,
                self.prelim_opts.isolated)

        sys.path.extend(self.local_plugins.paths)

        if self.check_plugins is None:
            self.check_plugins = plugin_manager.Checkers(
                self.local_plugins.extension)

        if self.formatting_plugins is None:
            self.formatting_plugins = plugin_manager.ReportFormatters(
                self.local_plugins.report)

        self.check_plugins.load_plugins()
        self.formatting_plugins.load_plugins()
Exemplo n.º 3
0
def mock_file_checker_with_plugin(plugin_target):
    """Get a mock FileChecker class with plugin_target registered.

    Useful as a starting point for mocking reports/results.
    """
    # Mock an entry point returning the plugin target
    entry_point = mock.Mock(spec=['load'])
    entry_point.name = plugin_target.name
    entry_point.load.return_value = plugin_target
    entry_point.value = 'mocked:value'

    # Load the checker plugins using the entry point mock
    with mock.patch.object(
            importlib_metadata,
            'entry_points',
            return_value={'flake8.extension': [entry_point]},
    ):
        checks = manager.Checkers()

    # Prevent it from reading lines from stdin or somewhere else
    with mock.patch('flake8.processor.FileProcessor.read_lines',
                    return_value=['Line 1']):
        file_checker = checker.FileChecker(
            '-',
            checks.to_dictionary(),
            mock.MagicMock()
        )
    return file_checker
Exemplo n.º 4
0
    def find_plugins(self, config_file, ignore_config_files):
        # type: (Optional[str], bool) -> None
        """Find and load the plugins for this application.

        If :attr:`check_plugins`, or :attr:`formatting_plugins` are ``None``
        then this method will update them with the appropriate plugin manager
        instance. Given the expense of finding plugins (via :mod:`entrypoints`)
        we want this to be idempotent and so only update those attributes if
        they are ``None``.

        :param str config_file:
            The optional configuraiton file to override all other configuration
            files (i.e., the --config option).
        :param bool ignore_config_files:
            Determine whether to parse configuration files or not. (i.e., the
            --isolated option).
        """
        if self.local_plugins is None:
            self.local_plugins = config.get_local_plugins(
                self.config_finder, config_file, ignore_config_files)

        sys.path.extend(self.local_plugins.paths)

        if self.check_plugins is None:
            self.check_plugins = plugin_manager.Checkers(
                self.local_plugins.extension)

        if self.formatting_plugins is None:
            self.formatting_plugins = plugin_manager.ReportFormatters(
                self.local_plugins.report)

        self.check_plugins.load_plugins()
        self.formatting_plugins.load_plugins()
Exemplo n.º 5
0
    def find_plugins(self, config_finder: config.ConfigFileFinder) -> None:
        """Find and load the plugins for this application.

        Set the :attr:`check_plugins` and :attr:`formatting_plugins` attributes
        based on the discovered plugins found.

        :param config.ConfigFileFinder config_finder:
            The finder for finding and reading configuration files.
        """
        local_plugins = config.get_local_plugins(config_finder)

        sys.path.extend(local_plugins.paths)

        self.check_plugins = plugin_manager.Checkers(local_plugins.extension)

        self.formatting_plugins = plugin_manager.ReportFormatters(
            local_plugins.report)

        self.check_plugins.load_plugins()
        self.formatting_plugins.load_plugins()
Exemplo n.º 6
0
    def find_plugins(self):
        # type: () -> NoneType
        """Find and load the plugins for this application.

        If :attr:`check_plugins`, :attr:`listening_plugins`, or
        :attr:`formatting_plugins` are ``None`` then this method will update
        them with the appropriate plugin manager instance. Given the expense
        of finding plugins (via :mod:`pkg_resources`) we want this to be
        idempotent and so only update those attributes if they are ``None``.
        """
        if self.check_plugins is None:
            self.check_plugins = plugin_manager.Checkers()

        if self.listening_plugins is None:
            self.listening_plugins = plugin_manager.Listeners()

        if self.formatting_plugins is None:
            self.formatting_plugins = plugin_manager.ReportFormatters()

        self.check_plugins.load_plugins()
        self.listening_plugins.load_plugins()
        self.formatting_plugins.load_plugins()