source: mod_gnutls/test/mgstest/hooks.py @ 0909c92

asyncioproxy-ticket
Last change on this file since 0909c92 was 0909c92, checked in by Fiona Klute <fiona.klute@…>, 22 months ago

Add hooks system to the test runner

With this all tests can use runtest.py to set up their
environment. Note that at the moment only the run_connection hook is
implemented.

  • Property mode set to 100644
File size: 1.3 KB
Line 
1"""Test case hooks for mod_gnutls tests
2
3Test cases can implement hooks that (depending on the hook) override
4or supplement the default test run behavior."""
5
6import importlib.util
7import inspect
8import os.path
9
10hooks = [
11    'prepare_env',
12    'run_connection',
13    'post_check'
14]
15
16class Plugin:
17    def __init__(self, module=None):
18        self.module = module
19        for hook in hooks:
20            if module:
21                func = getattr(module, hook, None)
22                if func and not inspect.isfunction(func):
23                    raise TypeError(f'{hook} in plugin module must be '
24                                    'a function!')
25                setattr(self, hook, func)
26            else:
27                setattr(self, hook, None)
28
29def load_module_file(file_path, module_name='mgstest.plugin'):
30    spec = importlib.util.spec_from_file_location(module_name, file_path)
31    module = importlib.util.module_from_spec(spec)
32    spec.loader.exec_module(module)
33    return module
34
35def load_hooks_plugin(file_path, module_name='mgstest.plugin'):
36    """Load a hooks plugin module from the given path, if it
37    exists. Returns a Plugin instance without any hooks if the module
38    file does not exist.
39    """
40    if os.path.exists(file_path):
41        return Plugin(module=load_module_file(file_path, module_name))
42    else:
43        return Plugin()
Note: See TracBrowser for help on using the repository browser.