59ca6943e7648c97e039fc8c214b41a9e9a6bf62
[oota-llvm.git] / utils / lit / lit / Test.py
1 import os
2
3 # Test results.
4
5 class ResultCode(object):
6     """Test result codes."""
7
8     def __init__(self, name, isFailure):
9         self.name = name
10         self.isFailure = isFailure
11
12     def __repr__(self):
13         return '%s%r' % (self.__class__.__name__,
14                          (self.name, self.isFailure))
15
16 PASS        = ResultCode('PASS', False)
17 XFAIL       = ResultCode('XFAIL', False)
18 FAIL        = ResultCode('FAIL', True)
19 XPASS       = ResultCode('XPASS', True)
20 UNRESOLVED  = ResultCode('UNRESOLVED', True)
21 UNSUPPORTED = ResultCode('UNSUPPORTED', False)
22
23 class Result(object):
24     """Wrapper for the results of executing an individual test."""
25
26     def __init__(self, code, output='', elapsed=None):
27         # The result code.
28         self.code = code
29         # The test output.
30         self.output = output
31         # The wall timing to execute the test, if timing.
32         self.elapsed = elapsed
33
34 # Test classes.
35
36 class TestSuite:
37     """TestSuite - Information on a group of tests.
38
39     A test suite groups together a set of logically related tests.
40     """
41
42     def __init__(self, name, source_root, exec_root, config):
43         self.name = name
44         self.source_root = source_root
45         self.exec_root = exec_root
46         # The test suite configuration.
47         self.config = config
48
49     def getSourcePath(self, components):
50         return os.path.join(self.source_root, *components)
51
52     def getExecPath(self, components):
53         return os.path.join(self.exec_root, *components)
54
55 class Test:
56     """Test - Information on a single test instance."""
57
58     def __init__(self, suite, path_in_suite, config):
59         self.suite = suite
60         self.path_in_suite = path_in_suite
61         self.config = config
62         # A list of conditions under which this test is expected to fail. These
63         # can optionally be provided by test format handlers, and will be
64         # honored when the test result is supplied.
65         self.xfails = []
66         # The test result, once complete.
67         self.result = None
68
69     def setResult(self, result):
70         if self.result is not None:
71             raise ArgumentError("test result already set")
72         if not isinstance(result, Result):
73             raise ArgumentError("unexpected result type")
74
75         self.result = result
76
77         # Apply the XFAIL handling to resolve the result exit code.
78         if self.isExpectedToFail():
79             if self.result.code == PASS:
80                 self.result.code = XPASS
81             elif self.result.code == FAIL:
82                 self.result.code = XFAIL
83         
84     def getFullName(self):
85         return self.suite.config.name + ' :: ' + '/'.join(self.path_in_suite)
86
87     def getSourcePath(self):
88         return self.suite.getSourcePath(self.path_in_suite)
89
90     def getExecPath(self):
91         return self.suite.getExecPath(self.path_in_suite)
92
93     def isExpectedToFail(self):
94         """
95         isExpectedToFail() -> bool
96
97         Check whether this test is expected to fail in the current
98         configuration. This check relies on the test xfails property which by
99         some test formats may not be computed until the test has first been
100         executed.
101         """
102
103         # Check if any of the xfails match an available feature or the target.
104         for item in self.xfails:
105             # If this is the wildcard, it always fails.
106             if item == '*':
107                 return True
108
109             # If this is an exact match for one of the features, it fails.
110             if item in self.config.available_features:
111                 return True
112
113             # If this is a part of the target triple, it fails.
114             if item in self.suite.config.target_triple:
115                 return True
116
117         return False