6
0
mirror of https://github.com/ChristianLight/tutor.git synced 2024-12-12 22:27:47 +00:00
tutor/tests/commands/base.py
Régis Behmo 16e6131f96 feat: pluggable local/dev/k8s do <job> commands
We introduce a new filter to implement custom commands in arbitrary containers.
It becomes easy to write convenient ad-hoc commands that users will
then be able to run either on Kubernetes or locally using a documented CLI.

Pluggable jobs are declared as Click commands and are responsible for
parsing their own arguments. See the new CLI_DO_COMMANDS filter.

Close https://github.com/overhangio/2u-tutor-adoption/issues/75
2022-11-15 09:46:08 +01:00

40 lines
1.1 KiB
Python

import typing as t
import click.testing
from tests.helpers import TestContext, temporary_root
from tutor.commands.cli import cli
class TestCommandMixin:
"""
Run CLI tests in an isolated test root.
"""
@staticmethod
def invoke(args: t.List[str]) -> click.testing.Result:
with temporary_root() as root:
return TestCommandMixin.invoke_in_root(root, args)
@staticmethod
def invoke_in_root(
root: str, args: t.List[str], catch_exceptions: bool = True
) -> click.testing.Result:
"""
Use this method for commands that all need to run in the same root:
with temporary_root() as root:
result1 = self.invoke_in_root(root, ...)
result2 = self.invoke_in_root(root, ...)
"""
runner = click.testing.CliRunner(
env={
"TUTOR_ROOT": root,
"TUTOR_IGNORE_ENTRYPOINT_PLUGINS": "1",
"TUTOR_IGNORE_DICT_PLUGINS": "1",
}
)
return runner.invoke(
cli, args, obj=TestContext(root), catch_exceptions=catch_exceptions
)