xref: /llvm-project/llvm/utils/lit/lit/LitTestCase.py (revision 49b209d5cca3750f71e8a6fda1c5870295ef3a9c)
1import unittest
2
3import lit.discovery
4import lit.LitConfig
5import lit.worker
6
7"""
8TestCase adaptor for providing a Python 'unittest' compatible interface to 'lit'
9tests.
10"""
11
12
13class UnresolvedError(RuntimeError):
14    pass
15
16
17class LitTestCase(unittest.TestCase):
18    def __init__(self, test, lit_config):
19        unittest.TestCase.__init__(self)
20        self._test = test
21        self._lit_config = lit_config
22
23    def id(self):
24        return self._test.getFullName()
25
26    def shortDescription(self):
27        return self._test.getFullName()
28
29    def runTest(self):
30        # Run the test.
31        result = lit.worker._execute(self._test, self._lit_config)
32
33        # Adapt the result to unittest.
34        if result.code is lit.Test.UNRESOLVED:
35            raise UnresolvedError(result.output)
36        elif result.code.isFailure:
37            self.fail(result.output)
38
39
40def load_test_suite(inputs):
41    import platform
42
43    windows = platform.system() == "Windows"
44
45    # Create the global config object.
46    lit_config = lit.LitConfig.LitConfig(
47        progname="lit",
48        path=[],
49        quiet=False,
50        useValgrind=False,
51        valgrindLeakCheck=False,
52        valgrindArgs=[],
53        noExecute=False,
54        debug=False,
55        isWindows=windows,
56        order="smart",
57        params={},
58    )
59
60    # Perform test discovery.
61    tests = lit.discovery.find_tests_for_inputs(lit_config, inputs)
62    test_adaptors = [LitTestCase(t, lit_config) for t in tests]
63
64    # Return a unittest test suite which just runs the tests in order.
65    return unittest.TestSuite(test_adaptors)
66