mirror of
https://github.com/ChristianLight/tutor.git
synced 2024-11-16 18:15:09 +00:00
15b219e235
This is a very large refactoring which aims at making Tutor both more extendable and more generic. Historically, the Tutor plugin system was designed as an ad-hoc solution to allow developers to modify their own Open edX platforms without having to fork Tutor. The plugin API was simple, but limited, because of its ad-hoc nature. As a consequence, there were many things that plugin developers could not do, such as extending different parts of the CLI or adding custom template filters. Here, we refactor the whole codebase to make use of a generic plugin system. This system was inspired by the Wordpress plugin API and the Open edX "hooks and filters" API. The various components are added to a small core thanks to a set of actions and filters. Actions are callback functions that can be triggered at different points of the application lifecycle. Filters are functions that modify some data. Both actions and filters are collectively named as "hooks". Hooks can optionally be created within a certain context, which makes it easier to keep track of which application created which callback. This new hooks system allows us to provide a Python API that developers can use to extend their applications. The API reference is added to the documentation, along with a new plugin development tutorial. The plugin v0 API remains supported for backward compatibility of existing plugins. Done: - Do not load commands from plugins which are not enabled. - Load enabled plugins once on start. - Implement contexts for actions and filters, which allow us to keep track of the source of every hook. - Migrate patches - Migrate commands - Migrate plugin detection - Migrate templates_root - Migrate config - Migrate template environment globals and filters - Migrate hooks to tasks - Generate hook documentation - Generate patch reference documentation - Add the concept of action priority Close #499.
79 lines
2.2 KiB
Python
79 lines
2.2 KiB
Python
import os
|
|
import tempfile
|
|
import typing as t
|
|
import unittest
|
|
import unittest.result
|
|
|
|
from tutor import hooks
|
|
from tutor.commands.context import BaseJobContext
|
|
from tutor.jobs import BaseJobRunner
|
|
from tutor.types import Config
|
|
|
|
|
|
class TestJobRunner(BaseJobRunner):
|
|
"""
|
|
Mock job runner for unit testing.
|
|
|
|
This runner does nothing except print the service name and command,
|
|
separated by dashes.
|
|
"""
|
|
|
|
def run_job(self, service: str, command: str) -> int:
|
|
print(os.linesep.join([f"Service: {service}", "-----", command, "----- "]))
|
|
return 0
|
|
|
|
|
|
def temporary_root() -> "tempfile.TemporaryDirectory[str]":
|
|
"""
|
|
Context manager to handle temporary test root.
|
|
|
|
This function can be used as follows:
|
|
|
|
with temporary_root() as root:
|
|
config = tutor_config.load_full(root)
|
|
...
|
|
"""
|
|
return tempfile.TemporaryDirectory(prefix="tutor-test-root-")
|
|
|
|
|
|
class TestContext(BaseJobContext):
|
|
"""
|
|
Click context that will use only test job runners.
|
|
"""
|
|
|
|
def job_runner(self, config: Config) -> TestJobRunner:
|
|
return TestJobRunner(self.root, config)
|
|
|
|
|
|
class PluginsTestCase(unittest.TestCase):
|
|
"""
|
|
This test case class clears the hooks created during tests. It also makes sure that
|
|
we don't accidentally load entrypoint/dict plugins from the user.
|
|
"""
|
|
|
|
def setUp(self) -> None:
|
|
self.clean()
|
|
self.addCleanup(self.clean)
|
|
super().setUp()
|
|
|
|
def clean(self) -> None:
|
|
# We clear hooks created in some contexts, such that user plugins are never loaded.
|
|
for context in [
|
|
hooks.Contexts.PLUGINS.name,
|
|
hooks.Contexts.PLUGINS_V0_ENTRYPOINT.name,
|
|
hooks.Contexts.PLUGINS_V0_YAML.name,
|
|
"unittests",
|
|
]:
|
|
hooks.filters.clear_all(context=context)
|
|
hooks.actions.clear_all(context=context)
|
|
|
|
def run(
|
|
self, result: t.Optional[unittest.result.TestResult] = None
|
|
) -> t.Optional[unittest.result.TestResult]:
|
|
"""
|
|
Run all actions and filters with a test context, such that they can be cleared
|
|
from one run to the next.
|
|
"""
|
|
with hooks.contexts.enter("unittests"):
|
|
return super().run(result=result)
|