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        if sys.version_info.major == 2:
78            # In Python 2, string objects can contain Unicode characters. Use
79            # the non-strict 'replace' decoding mode. We cannot use the strict
80            # mode right now because lldb's StringPrinter facility and the
81            # Python utf8 decoder have different interpretations of which
82            # characters are "printable". This leads to Python utf8 decoding
83            # exceptions even though lldb is behaving as expected.
84            out = out.decode('utf-8', 'replace')
85            err = err.decode('utf-8', 'replace')
86
87        output = """Script:\n--\n%s\n--\nExit Code: %d\n""" % (
88            ' '.join(cmd), exitCode)
89        if timeoutInfo is not None:
90            output += """Timeout: %s\n""" % (timeoutInfo,)
91        output += "\n"
92
93        if out:
94            output += """Command Output (stdout):\n--\n%s\n--\n""" % (out,)
95        if err:
96            output += """Command Output (stderr):\n--\n%s\n--\n""" % (err,)
97
98        if timeoutInfo:
99            return lit.Test.TIMEOUT, output
100
101        if exitCode:
102            if 'XPASS:' in out or 'XPASS:' in err:
103                return lit.Test.XPASS, output
104
105            # Otherwise this is just a failure.
106            return lit.Test.FAIL, output
107
108        has_unsupported_tests = 'UNSUPPORTED:' in out or 'UNSUPPORTED:' in err
109        has_passing_tests = 'PASS:' in out or 'PASS:' in err
110        if has_unsupported_tests and not has_passing_tests:
111            return lit.Test.UNSUPPORTED, output
112
113        passing_test_line = 'RESULT: PASSED'
114        if passing_test_line not in out and passing_test_line not in err:
115            return lit.Test.UNRESOLVED, output
116
117        return lit.Test.PASS, output
118