forked from ISISComputingGroup/EPICS-IOC_Test_Framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_utils.py
102 lines (76 loc) · 2.64 KB
/
run_utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import importlib
import os
from contextlib import contextmanager
def package_contents(package_path):
"""
Finds all the files in a package.
:param package_path: the name of the package
:return: a set containing all the module names
"""
return set([os.path.splitext(module)[0] for module in os.listdir(package_path)
if module.endswith('.py') and not module.startswith("__init__")])
@contextmanager
def modified_environment(**kwargs):
"""
Modifies the environment variables as required then returns them to their original state.
:param kwargs: the settings to apply
"""
# Copying old values
old_env = {name: os.environ.get(name, '') for name in kwargs.keys()}
# Apply new settings and then yield
os.environ.update(kwargs)
yield
# Restore old values
os.environ.update(old_env)
class ModuleTests(object):
"""
Object which contains information about tests in a module to be run.
Attributes:
name: Name of the module where the tests are.
tests: List of "dotted" test names. E.g. SampleTests.SampleTestCase.test_two runs
test_two in SampleTestCase class in the SampleTests module.
file: Reference to the module.
modes: Modes to run the tests in.
"""
def __init__(self, name):
self.__name = name
self.tests = None
self.__file = self.__get_file_reference()
self.__modes = self.__get_modes()
@property
def name(self):
""" Returns the name of the module."""
return self.__name
@property
def modes(self):
""" Returns the modes to run the tests in. """
return self.__modes
@property
def file(self):
""" Returns a reference to the module file. """
return self.__file
def __get_file_reference(self):
module = load_module("tests.{}".format(self.__name))
return module
def __get_modes(self):
if not self.__file:
self.__get_file_reference()
return check_test_modes(self.__file)
def load_module(name):
"""
Loads a module based on its name.
:param name: the name of the module
:return: a reference to the module
"""
return importlib.import_module(name, )
def check_test_modes(module):
"""
Checks for RECSIM and DEVSIM test modes.
:param module: Modules to check for RECSIM and DEVSIM test modes
:return: set: Modes that the tests can be run in.
"""
try:
modes = set(module.TEST_MODES)
except AttributeError:
raise ValueError("Expected test module {} to contain a TEST_MODES attribute".format(module.__name__))
return modes