Exemplo n.º 1
0
    def test_register_module_with_plugin_class(self, plugin_manager_mock):
        """Test that both the module itself and the class (instance) are
        registered."""
        expected_calls = [call(javac), call(ANY)]

        plugin.register_plugins([javac])

        plugin_manager_mock.register.assert_has_calls(expected_calls)
Exemplo n.º 2
0
    def test_register_modules_and_classes(self, plugin_manager_mock):
        """Test that both module level hooks and class hooks are registered
        properly at the same time.

        Among other things, checks the reverse order of registration.
        """
        modules = [javac, pylint]
        # pylint should be registered before javac because of FIFO order
        expected_calls = [call(pylint), call(javac), call(ANY)]
        plugin.register_plugins(modules)

        plugin_manager_mock.register.assert_has_calls(expected_calls)
Exemplo n.º 3
0
def test_register():
    """Just test that there is no crash"""
    plugin.register_plugins([timestamp])
Exemplo n.º 4
0
 def test_register_module(self, plugin_manager_mock):
     """Test registering a plugin module with module level hooks."""
     plugin.register_plugins([pylint])
     plugin_manager_mock.register.assert_called_once_with(pylint)
Exemplo n.º 5
0
def test_register():
    """Just test that there is no crash"""
    plugin.register_plugins([csvgrades])
Exemplo n.º 6
0
def test_register():
    """Just test that there is no crash"""
    plugin.register_plugins([feedback])
Exemplo n.º 7
0
def test_register():
    """Just test that there is no crash"""
    plugin.register_plugins([encode])
Exemplo n.º 8
0
def run(
    cmd: List[str],
    config_file: Union[str, pathlib.Path] = "",
    plugins: Optional[List[Union[ModuleType, plug.Plugin]]] = None,
    workdir: Union[str, pathlib.Path] = ".",
) -> Mapping[str, List[plug.Result]]:
    """Run RepoBee with the provided options. This function is mostly intended
    to be used for testing plugins.

    .. important::

        This function will always unregister all plugins after execution,
        including anly plugins that may have been registered prior to running
        this function.

    Running this function is almost equivalent to running RepoBee from the CLI,
    with the following exceptions:

    1. Preparser options must be passed as arguments to this function (i.e.
       cannot be given as part of ``cmd``).
    2. There is no error handling at the top level, so exceptions are raised
       instead of just logged.

    As an example, the following CLI call:

    .. code-block:: bash

        $ repobee --plug ext.py --config-file config.ini config show

    Can be executed as follows:

    .. code-block:: python

        import ext
        from repobee import run

        run(["config", "show"], config_file="config.ini", plugins=[ext])

    Args:
        cmd: The command to run.
        config_file: Path to the configuration file.
        plugins: A list of plugin modules and/or plugin classes.
        workdir: The working directory to run RepoBee in.
    Returns:
        A mapping (plugin_name -> plugin_results).
    """
    config_file = pathlib.Path(config_file)
    cur_workdir = pathlib.Path(".").absolute()
    requested_workdir = pathlib.Path(str(workdir)).resolve(strict=True)

    @contextlib.contextmanager
    def _in_requested_workdir():
        try:
            os.chdir(requested_workdir)
            yield
        finally:
            os.chdir(cur_workdir)

    def _ensure_is_module(p: Union[ModuleType, plug.Plugin]):
        if isinstance(p, type) and issubclass(p, plug.Plugin):
            mod = ModuleType(p.__name__.lower())
            mod.__package__ = f"__{p.__name__}"
            setattr(mod, p.__name__, p)
            return mod
        elif isinstance(p, ModuleType):
            return p
        else:
            raise TypeError(f"not plugin or module: {p}")

    wrapped_plugins = list(map(_ensure_is_module, plugins or []))

    with _in_requested_workdir():
        try:
            _repobee.cli.parsing.setup_logging()
            # FIXME calling _initialize_plugins like this is ugly, should be
            # refactored
            _initialize_plugins(argparse.Namespace(no_plugins=False, plug=[]))
            plugin.register_plugins(wrapped_plugins)
            parsed_args, api = _parse_args(cmd, config_file)

            with _set_output_verbosity(getattr(parsed_args, "quiet", 0)):
                return _repobee.cli.dispatch.dispatch_command(
                    parsed_args, api, config_file)
        finally:
            plugin.unregister_all_plugins()
Exemplo n.º 9
0
 def test_register(self):
     register_plugins([pairwise])
Exemplo n.º 10
0
def _initialize_logging_and_plugins_for_run(plugins: List[Any]):
    wrapped_plugins = list(map(_wrap_in_plugin_module, plugins or []))
    _repobee.cli.parsing.setup_logging()
    _initialize_mandatory_plugins()
    plugin.register_plugins(wrapped_plugins)