ext: Remove dead code from loader.py
[gem5.git] / ext / testlib / wrappers.py
1 # Copyright (c) 2019 ARM Limited
2 # All rights reserved
3 #
4 # The license below extends only to copyright in the software and shall
5 # not be construed as granting a license to any other intellectual
6 # property including but not limited to intellectual property relating
7 # to a hardware implementation of the functionality of the software
8 # licensed hereunder. You may use the software subject to the license
9 # terms below provided that you ensure that this notice is replicated
10 # unmodified and in its entirety in all distributions of the software,
11 # modified or unmodified, in source code or in binary form.
12 #
13 # Copyright (c) 2017 Mark D. Hill and David A. Wood
14 # All rights reserved.
15 #
16 # Redistribution and use in source and binary forms, with or without
17 # modification, are permitted provided that the following conditions are
18 # met: redistributions of source code must retain the above copyright
19 # notice, this list of conditions and the following disclaimer;
20 # redistributions in binary form must reproduce the above copyright
21 # notice, this list of conditions and the following disclaimer in the
22 # documentation and/or other materials provided with the distribution;
23 # neither the name of the copyright holders nor the names of its
24 # contributors may be used to endorse or promote products derived from
25 # this software without specific prior written permission.
26 #
27 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
30 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
32 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
33 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
34 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
35 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
37 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 #
39 # Authors: Sean Wilson
40
41 '''
42 Module contains wrappers for test items that have been
43 loaded by the testlib :class:`testlib.loader.Loader`.
44 '''
45 import itertools
46
47 import testlib.uid as uid
48 from testlib.state import Status, Result
49
50 class TestCaseMetadata():
51 def __init__(self, name, uid, path, result, status, suite_uid):
52 self.name = name
53 self.uid = uid
54 self.path = path
55 self.status = status
56 self.result = result
57 self.suite_uid = suite_uid
58
59
60 class TestSuiteMetadata():
61 def __init__(self, name, uid, tags, path, status, result):
62 self.name = name
63 self.uid = uid
64 self.tags = tags
65 self.path = path
66 self.status = status
67 self.result = result
68
69
70 class LibraryMetadata():
71 def __init__(self, name, result, status):
72 self.name = name
73 self.result = result
74 self.status = status
75
76
77 class LoadedTestable(object):
78 '''
79 Base class for loaded test items.
80
81 :property:`result` and :property:`status` setters
82 notify testlib via the :func:`log_result` and :func:`log_status`
83 of the updated status.
84 '''
85 def __init__(self, obj):
86 self.obj = obj
87 self.metadata = self._generate_metadata()
88
89 @property
90 def status(self):
91 return self.metadata.status
92
93 @status.setter
94 def status(self, status):
95 self.log_status(status)
96 self.metadata.status = status
97
98 @property
99 def result(self):
100 return self.metadata.result
101
102 @result.setter
103 def result(self, result):
104 self.log_result(result)
105 self.metadata.result = result
106
107 @property
108 def uid(self):
109 return self.metadata.uid
110
111 @property
112 def name(self):
113 return self.metadata.name
114
115 @property
116 def fixtures(self):
117 return self.obj.fixtures
118
119 @fixtures.setter
120 def fixtures(self, fixtures):
121 self.obj.fixtures = fixtures
122
123 @property
124 def runner(self):
125 return self.obj.runner
126
127 # TODO Change log to provide status_update, result_update for all types.
128 def log_status(self, status):
129 import testlib.log as log
130 log.test_log.status_update(self, status)
131
132 def log_result(self, result):
133 import testlib.log as log
134 log.test_log.result_update(self, result)
135
136 def __iter__(self):
137 return iter(())
138
139
140 class LoadedTest(LoadedTestable):
141 def __init__(self, test_obj, loaded_suite, path):
142 self.parent_suite = loaded_suite
143 self._path = path
144 LoadedTestable.__init__(self, test_obj)
145
146 def test(self, *args, **kwargs):
147 self.obj.test(*args, **kwargs)
148
149 def _generate_metadata(self):
150 return TestCaseMetadata( **{
151 'name':self.obj.name,
152 'path': self._path,
153 'uid': uid.TestUID(self._path,
154 self.obj.name,
155 self.parent_suite.name),
156 'status': Status.Unscheduled,
157 'result': Result(Result.NotRun),
158 'suite_uid': self.parent_suite.metadata.uid
159 })
160
161
162 class LoadedSuite(LoadedTestable):
163 def __init__(self, suite_obj, path):
164 self._path = path
165 LoadedTestable.__init__(self, suite_obj)
166 self.tests = self._wrap_children(suite_obj)
167
168 def _wrap_children(self, suite_obj):
169 return [LoadedTest(test, self, self.metadata.path)
170 for test in suite_obj]
171
172 def _generate_metadata(self):
173 return TestSuiteMetadata( **{
174 'name': self.obj.name,
175 'tags':self.obj.tags,
176 'path': self._path,
177 'uid': uid.SuiteUID(self._path, self.obj.name),
178 'status': Status.Unscheduled,
179 'result': Result(Result.NotRun)
180 })
181
182 def __iter__(self):
183 return iter(self.tests)
184
185 @property
186 def tags(self):
187 return self.metadata.tags
188
189
190 class LoadedLibrary(LoadedTestable):
191 '''
192 Wraps a collection of all loaded test suites and
193 provides utility functions for accessing fixtures.
194 '''
195 def __init__(self, suites):
196 LoadedTestable.__init__(self, suites)
197
198 def _generate_metadata(self):
199 return LibraryMetadata( **{
200 'name': 'Test Library',
201 'status': Status.Unscheduled,
202 'result': Result(Result.NotRun)
203 })
204
205 def __iter__(self):
206 '''
207 :returns: an iterator over contained :class:`TestSuite` objects.
208 '''
209 return iter(self.obj)
210
211 def all_fixtures(self):
212 '''
213 :returns: an interator overall all global, suite,
214 and test fixtures
215 '''
216 return itertools.chain(itertools.chain(
217 *(suite.fixtures for suite in self.obj)),
218 *(self.test_fixtures(suite) for suite in self.obj)
219 )
220
221 def test_fixtures(self, suite):
222 '''
223 :returns: an interator over all fixtures of each
224 test contained in the given suite
225 '''
226 return itertools.chain(*(test.fixtures for test in suite))
227
228 @property
229 def fixtures(self):
230 global_fixtures = []
231 for fixture in self.all_fixtures():
232 if fixture.is_global():
233 global_fixtures.append(fixture)
234 return global_fixtures
235
236 @property
237 def uid(self):
238 return self.name
239
240 @property
241 def suites(self):
242 return self.obj
243
244 @suites.setter
245 def suites(self, suites):
246 self.obj = suites