1from __future__ import absolute_import
2import os
3import tempfile
4import subprocess
5import sys
6import platform
7
8import lit.Test
9import lit.TestRunner
10import lit.util
11from lit.formats.base import TestFormat
12
13
14class LLDBTest(TestFormat):
15    def __init__(self, dotest_cmd):
16        self.dotest_cmd = dotest_cmd
17
18    def getTestsInDirectory(self, testSuite, path_in_suite, litConfig,
19                            localConfig):
20        source_path = testSuite.getSourcePath(path_in_suite)
21        for filename in os.listdir(source_path):
22            # Ignore dot files and excluded tests.
23            if (filename.startswith('.') or filename in localConfig.excludes):
24                continue
25
26            # Ignore files that don't start with 'Test'.
27            if not filename.startswith('Test'):
28                continue
29
30            filepath = os.path.join(source_path, filename)
31            if not os.path.isdir(filepath):
32                base, ext = os.path.splitext(filename)
33                if ext in localConfig.suffixes:
34                    yield lit.Test.Test(testSuite, path_in_suite +
35                                        (filename, ), localConfig)
36
37    def execute(self, test, litConfig):
38        if litConfig.noExecute:
39            return lit.Test.PASS, ''
40
41        if not getattr(test.config, 'lldb_enable_python', False):
42            return (lit.Test.UNSUPPORTED, 'Python module disabled')
43
44        if test.config.unsupported:
45            return (lit.Test.UNSUPPORTED, 'Test is unsupported')
46
47        testPath, testFile = os.path.split(test.getSourcePath())
48
49        # The Python used to run lit can be different from the Python LLDB was
50        # build with.
51        executable = test.config.python_executable
52
53        isLuaTest = testFile == test.config.lua_test_entry
54
55        # On Windows, the system does not always correctly interpret
56        # shebang lines.  To make sure we can execute the tests, add
57        # python exe as the first parameter of the command.
58        cmd = [executable] + self.dotest_cmd + [testPath, '-p', testFile]
59
60        if isLuaTest:
61            luaExecutable = test.config.lua_executable
62            cmd.extend(['--env', 'LUA_EXECUTABLE=%s' % luaExecutable])
63
64        timeoutInfo = None
65        try:
66            out, err, exitCode = lit.util.executeCommand(
67                cmd,
68                env=test.config.environment,
69                timeout=litConfig.maxIndividualTestTime)
70        except lit.util.ExecuteCommandTimeoutException as e:
71            out = e.out
72            err = e.err
73            exitCode = e.exitCode
74            timeoutInfo = 'Reached timeout of {} seconds'.format(
75                litConfig.maxIndividualTestTime)
76
77        output = """Script:\n--\n%s\n--\nExit Code: %d\n""" % (
78            ' '.join(cmd), exitCode)
79        if timeoutInfo is not None:
80            output += """Timeout: %s\n""" % (timeoutInfo,)
81        output += "\n"
82
83        if out:
84            output += """Command Output (stdout):\n--\n%s\n--\n""" % (out,)
85        if err:
86            output += """Command Output (stderr):\n--\n%s\n--\n""" % (err,)
87
88        if timeoutInfo:
89            return lit.Test.TIMEOUT, output
90
91        if exitCode:
92            if 'XPASS:' in out or 'XPASS:' in err:
93                return lit.Test.XPASS, output
94
95            # Otherwise this is just a failure.
96            return lit.Test.FAIL, output
97
98        has_unsupported_tests = 'UNSUPPORTED:' in out or 'UNSUPPORTED:' in err
99        has_passing_tests = 'PASS:' in out or 'PASS:' in err
100        if has_unsupported_tests and not has_passing_tests:
101            return lit.Test.UNSUPPORTED, output
102
103        passing_test_line = 'RESULT: PASSED'
104        if passing_test_line not in out and passing_test_line not in err:
105            return lit.Test.UNRESOLVED, output
106
107        return lit.Test.PASS, output
108