xref: /llvm-project/llvm/utils/lit/lit/formats/base.py (revision 49b209d5cca3750f71e8a6fda1c5870295ef3a9c)
1from __future__ import absolute_import
2import os
3
4import lit.Test
5import lit.util
6
7
8class TestFormat(object):
9    def getTestsForPath(self, testSuite, path_in_suite, litConfig, localConfig):
10        """
11        Given the path to a test in the test suite, generates the Lit tests associated
12        to that path. There can be zero, one or more tests. For example, some testing
13        formats allow expanding a single path in the test suite into multiple Lit tests
14        (e.g. they are generated on the fly).
15
16        Note that this method is only used when Lit needs to actually perform test
17        discovery, which is not the case for configs with standalone tests.
18        """
19        yield lit.Test.Test(testSuite, path_in_suite, localConfig)
20
21###
22
23
24class FileBasedTest(TestFormat):
25    def getTestsForPath(self, testSuite, path_in_suite, litConfig, localConfig):
26        """
27        Expand each path in a test suite to a Lit test using that path and assuming
28        it is a file containing the test. File extensions excluded by the configuration
29        or not contained in the allowed extensions are ignored.
30        """
31        filename = path_in_suite[-1]
32
33        # Ignore dot files and excluded tests.
34        if filename.startswith(".") or filename in localConfig.excludes:
35            return
36
37        base, ext = os.path.splitext(filename)
38        if ext in localConfig.suffixes:
39            yield lit.Test.Test(testSuite, path_in_suite, localConfig)
40
41    def getTestsInDirectory(self, testSuite, path_in_suite, litConfig, localConfig):
42        source_path = testSuite.getSourcePath(path_in_suite)
43        for filename in os.listdir(source_path):
44            filepath = os.path.join(source_path, filename)
45            if not os.path.isdir(filepath):
46                for t in self.getTestsForPath(testSuite, path_in_suite + (filename,), litConfig, localConfig):
47                    yield t
48
49
50###
51
52# Check exit code of a simple executable with no input
53class ExecutableTest(FileBasedTest):
54    def execute(self, test, litConfig):
55        if test.config.unsupported:
56            return lit.Test.UNSUPPORTED
57
58        out, err, exitCode = lit.util.executeCommand(test.getSourcePath())
59
60        if not exitCode:
61            return lit.Test.PASS, ""
62
63        return lit.Test.FAIL, out + err
64