-
Notifications
You must be signed in to change notification settings - Fork 2
/
run_utils.py
143 lines (112 loc) · 4.15 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import glob
import importlib
import os
from contextlib import contextmanager
from types import ModuleType
from typing import Generator, List, Optional, Set
from utils.build_architectures import BuildArchitectures
from utils.test_modes import TestModes
def package_contents(package_path: str, filter_files: str) -> Set[str]:
"""
Finds all the files in a package.
:param package_path: the name of the package
:param filter_files: glob format expression to filter files by
:return: a set containing all the module names
"""
return set(
[
os.path.splitext(module)[0]
for module in glob.glob(filter_files, root_dir=package_path)
if module.endswith(".py") and not module.startswith("__init__")
]
)
@contextmanager
def modified_environment(**kwargs: str) -> Generator[None, None, None]:
"""
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: str) -> None:
self.__name = name
self.tests: Optional[List[str]] = None
self.__file = self.__get_file_reference()
self.__modes = self.__get_modes()
self.__architectures = self.__get_architectures()
@property
def name(self) -> str:
"""Returns the name of the module."""
return self.__name
@property
def modes(self) -> Set[TestModes]:
"""Returns the modes to run the tests in."""
return self.__modes
@property
def file(self) -> ModuleType:
"""Returns a reference to the module file."""
return self.__file
@property
def architectures(self) -> Set[BuildArchitectures]:
"""Returns the architectures the test can be run in."""
return self.__architectures
def __get_file_reference(self) -> ModuleType:
module = load_module("tests.{}".format(self.__name))
return module
def __get_modes(self) -> Set[TestModes]:
if not self.__file:
self.__get_file_reference()
return check_test_modes(self.__file)
def __get_architectures(self) -> Set[BuildArchitectures]:
if not self.__file:
self.__get_file_reference()
return check_build_architectures(self.__file)
def load_module(name: str) -> ModuleType:
"""
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: ModuleType) -> Set[TestModes]:
"""
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
def check_build_architectures(module: ModuleType) -> Set[BuildArchitectures]:
"""
Checks for which build architectures the test can run in.
If not specified, default to both 64 and 32 bit allowed.
:param module: Module to check which architectures the test can run in
:return: set: Architectures the test can be run in
"""
try:
architectures = set(module.BUILD_ARCHITECTURES)
except AttributeError:
architectures = set([BuildArchitectures._64BIT, BuildArchitectures._32BIT])
return architectures