xref: /aosp_15_r20/external/llvm/utils/lit/tests/unit/TestRunner.py (revision 9880d6810fe72a1726cb53787c6711e909410d58)
1*9880d681SAndroid Build Coastguard Worker# RUN: %{python} %s
2*9880d681SAndroid Build Coastguard Worker#
3*9880d681SAndroid Build Coastguard Worker# END.
4*9880d681SAndroid Build Coastguard Worker
5*9880d681SAndroid Build Coastguard Worker
6*9880d681SAndroid Build Coastguard Workerimport unittest
7*9880d681SAndroid Build Coastguard Workerimport platform
8*9880d681SAndroid Build Coastguard Workerimport os.path
9*9880d681SAndroid Build Coastguard Workerimport tempfile
10*9880d681SAndroid Build Coastguard Worker
11*9880d681SAndroid Build Coastguard Workerimport lit
12*9880d681SAndroid Build Coastguard Workerfrom lit.TestRunner import ParserKind, IntegratedTestKeywordParser, \
13*9880d681SAndroid Build Coastguard Worker                           parseIntegratedTestScript
14*9880d681SAndroid Build Coastguard Worker
15*9880d681SAndroid Build Coastguard Worker
16*9880d681SAndroid Build Coastguard Workerclass TestIntegratedTestKeywordParser(unittest.TestCase):
17*9880d681SAndroid Build Coastguard Worker    inputTestCase = None
18*9880d681SAndroid Build Coastguard Worker
19*9880d681SAndroid Build Coastguard Worker    @staticmethod
20*9880d681SAndroid Build Coastguard Worker    def load_keyword_parser_lit_tests():
21*9880d681SAndroid Build Coastguard Worker        """
22*9880d681SAndroid Build Coastguard Worker        Create and load the LIT test suite and test objects used by
23*9880d681SAndroid Build Coastguard Worker        TestIntegratedTestKeywordParser
24*9880d681SAndroid Build Coastguard Worker        """
25*9880d681SAndroid Build Coastguard Worker        # Create the global config object.
26*9880d681SAndroid Build Coastguard Worker        lit_config = lit.LitConfig.LitConfig(progname='lit',
27*9880d681SAndroid Build Coastguard Worker                                             path=[],
28*9880d681SAndroid Build Coastguard Worker                                             quiet=False,
29*9880d681SAndroid Build Coastguard Worker                                             useValgrind=False,
30*9880d681SAndroid Build Coastguard Worker                                             valgrindLeakCheck=False,
31*9880d681SAndroid Build Coastguard Worker                                             valgrindArgs=[],
32*9880d681SAndroid Build Coastguard Worker                                             noExecute=False,
33*9880d681SAndroid Build Coastguard Worker                                             debug=False,
34*9880d681SAndroid Build Coastguard Worker                                             isWindows=(
35*9880d681SAndroid Build Coastguard Worker                                               platform.system() == 'Windows'),
36*9880d681SAndroid Build Coastguard Worker                                             params={})
37*9880d681SAndroid Build Coastguard Worker        TestIntegratedTestKeywordParser.litConfig = lit_config
38*9880d681SAndroid Build Coastguard Worker        # Perform test discovery.
39*9880d681SAndroid Build Coastguard Worker        test_path = os.path.dirname(os.path.dirname(__file__))
40*9880d681SAndroid Build Coastguard Worker        inputs = [os.path.join(test_path, 'Inputs/testrunner-custom-parsers/')]
41*9880d681SAndroid Build Coastguard Worker        assert os.path.isdir(inputs[0])
42*9880d681SAndroid Build Coastguard Worker        run = lit.run.Run(lit_config,
43*9880d681SAndroid Build Coastguard Worker                          lit.discovery.find_tests_for_inputs(lit_config, inputs))
44*9880d681SAndroid Build Coastguard Worker        assert len(run.tests) == 1 and "there should only be one test"
45*9880d681SAndroid Build Coastguard Worker        TestIntegratedTestKeywordParser.inputTestCase = run.tests[0]
46*9880d681SAndroid Build Coastguard Worker
47*9880d681SAndroid Build Coastguard Worker    @staticmethod
48*9880d681SAndroid Build Coastguard Worker    def make_parsers():
49*9880d681SAndroid Build Coastguard Worker        def custom_parse(line_number, line, output):
50*9880d681SAndroid Build Coastguard Worker            if output is None:
51*9880d681SAndroid Build Coastguard Worker                output = []
52*9880d681SAndroid Build Coastguard Worker            output += [part for part in line.split(' ') if part.strip()]
53*9880d681SAndroid Build Coastguard Worker            return output
54*9880d681SAndroid Build Coastguard Worker
55*9880d681SAndroid Build Coastguard Worker        return [
56*9880d681SAndroid Build Coastguard Worker            IntegratedTestKeywordParser("MY_TAG.", ParserKind.TAG),
57*9880d681SAndroid Build Coastguard Worker            IntegratedTestKeywordParser("MY_DNE_TAG.", ParserKind.TAG),
58*9880d681SAndroid Build Coastguard Worker            IntegratedTestKeywordParser("MY_LIST:", ParserKind.LIST),
59*9880d681SAndroid Build Coastguard Worker            IntegratedTestKeywordParser("MY_RUN:", ParserKind.COMMAND),
60*9880d681SAndroid Build Coastguard Worker            IntegratedTestKeywordParser("MY_CUSTOM:", ParserKind.CUSTOM,
61*9880d681SAndroid Build Coastguard Worker                                        custom_parse)
62*9880d681SAndroid Build Coastguard Worker        ]
63*9880d681SAndroid Build Coastguard Worker
64*9880d681SAndroid Build Coastguard Worker    @staticmethod
65*9880d681SAndroid Build Coastguard Worker    def get_parser(parser_list, keyword):
66*9880d681SAndroid Build Coastguard Worker        for p in parser_list:
67*9880d681SAndroid Build Coastguard Worker            if p.keyword == keyword:
68*9880d681SAndroid Build Coastguard Worker                return p
69*9880d681SAndroid Build Coastguard Worker        assert False and "parser not found"
70*9880d681SAndroid Build Coastguard Worker
71*9880d681SAndroid Build Coastguard Worker    @staticmethod
72*9880d681SAndroid Build Coastguard Worker    def parse_test(parser_list):
73*9880d681SAndroid Build Coastguard Worker        script = parseIntegratedTestScript(
74*9880d681SAndroid Build Coastguard Worker            TestIntegratedTestKeywordParser.inputTestCase,
75*9880d681SAndroid Build Coastguard Worker            additional_parsers=parser_list, require_script=False)
76*9880d681SAndroid Build Coastguard Worker        assert not isinstance(script, lit.Test.Result)
77*9880d681SAndroid Build Coastguard Worker        assert isinstance(script, list)
78*9880d681SAndroid Build Coastguard Worker        assert len(script) == 0
79*9880d681SAndroid Build Coastguard Worker
80*9880d681SAndroid Build Coastguard Worker    def test_tags(self):
81*9880d681SAndroid Build Coastguard Worker        parsers = self.make_parsers()
82*9880d681SAndroid Build Coastguard Worker        self.parse_test(parsers)
83*9880d681SAndroid Build Coastguard Worker        tag_parser = self.get_parser(parsers, 'MY_TAG.')
84*9880d681SAndroid Build Coastguard Worker        dne_tag_parser = self.get_parser(parsers, 'MY_DNE_TAG.')
85*9880d681SAndroid Build Coastguard Worker        self.assertTrue(tag_parser.getValue())
86*9880d681SAndroid Build Coastguard Worker        self.assertFalse(dne_tag_parser.getValue())
87*9880d681SAndroid Build Coastguard Worker
88*9880d681SAndroid Build Coastguard Worker    def test_lists(self):
89*9880d681SAndroid Build Coastguard Worker        parsers = self.make_parsers()
90*9880d681SAndroid Build Coastguard Worker        self.parse_test(parsers)
91*9880d681SAndroid Build Coastguard Worker        list_parser = self.get_parser(parsers, 'MY_LIST:')
92*9880d681SAndroid Build Coastguard Worker        self.assertItemsEqual(list_parser.getValue(),
93*9880d681SAndroid Build Coastguard Worker                              ['one', 'two', 'three', 'four'])
94*9880d681SAndroid Build Coastguard Worker
95*9880d681SAndroid Build Coastguard Worker    def test_commands(self):
96*9880d681SAndroid Build Coastguard Worker        parsers = self.make_parsers()
97*9880d681SAndroid Build Coastguard Worker        self.parse_test(parsers)
98*9880d681SAndroid Build Coastguard Worker        cmd_parser = self.get_parser(parsers, 'MY_RUN:')
99*9880d681SAndroid Build Coastguard Worker        value = cmd_parser.getValue()
100*9880d681SAndroid Build Coastguard Worker        self.assertEqual(len(value), 2)  # there are only two run lines
101*9880d681SAndroid Build Coastguard Worker        self.assertEqual(value[0].strip(), 'baz')
102*9880d681SAndroid Build Coastguard Worker        self.assertEqual(value[1].strip(), 'foo  bar')
103*9880d681SAndroid Build Coastguard Worker
104*9880d681SAndroid Build Coastguard Worker    def test_custom(self):
105*9880d681SAndroid Build Coastguard Worker        parsers = self.make_parsers()
106*9880d681SAndroid Build Coastguard Worker        self.parse_test(parsers)
107*9880d681SAndroid Build Coastguard Worker        custom_parser = self.get_parser(parsers, 'MY_CUSTOM:')
108*9880d681SAndroid Build Coastguard Worker        value = custom_parser.getValue()
109*9880d681SAndroid Build Coastguard Worker        self.assertItemsEqual(value, ['a', 'b', 'c'])
110*9880d681SAndroid Build Coastguard Worker
111*9880d681SAndroid Build Coastguard Worker
112*9880d681SAndroid Build Coastguard Workerif __name__ == '__main__':
113*9880d681SAndroid Build Coastguard Worker    TestIntegratedTestKeywordParser.load_keyword_parser_lit_tests()
114*9880d681SAndroid Build Coastguard Worker    unittest.main(verbosity=2)
115