Jonas Devlieghere | 1a928f3 | 2018-04-18 17:08:49 +0000 | [diff] [blame] | 1 | from __future__ import absolute_import |
| 2 | import os |
| 3 | |
| 4 | import subprocess |
| 5 | import sys |
| 6 | |
| 7 | import lit.Test |
| 8 | import lit.TestRunner |
| 9 | import lit.util |
| 10 | from lit.formats.base import TestFormat |
| 11 | |
Adrian Prantl | 748310b | 2018-08-23 17:19:08 +0000 | [diff] [blame] | 12 | def getBuildDir(cmd): |
| 13 | found = False |
| 14 | for arg in cmd: |
| 15 | if found: |
| 16 | return arg |
| 17 | if arg == '--build-dir': |
| 18 | found = True |
| 19 | return None |
Jonas Devlieghere | 1a928f3 | 2018-04-18 17:08:49 +0000 | [diff] [blame] | 20 | |
Adrian Prantl | 7e6ce43 | 2018-08-27 23:06:37 +0000 | [diff] [blame] | 21 | def mkdir_p(path): |
| 22 | import errno |
| 23 | try: |
| 24 | os.makedirs(path) |
| 25 | except OSError as e: |
| 26 | if e.errno != errno.EEXIST: |
| 27 | raise |
| 28 | if not os.path.isdir(path): |
| 29 | raise OSError(errno.ENOTDIR, "%s is not a directory"%path) |
| 30 | |
Jonas Devlieghere | 1a928f3 | 2018-04-18 17:08:49 +0000 | [diff] [blame] | 31 | class LLDBTest(TestFormat): |
| 32 | def __init__(self, dotest_cmd): |
| 33 | self.dotest_cmd = dotest_cmd |
| 34 | |
| 35 | def getTestsInDirectory(self, testSuite, path_in_suite, litConfig, |
| 36 | localConfig): |
| 37 | source_path = testSuite.getSourcePath(path_in_suite) |
| 38 | for filename in os.listdir(source_path): |
| 39 | # Ignore dot files and excluded tests. |
| 40 | if (filename.startswith('.') or filename in localConfig.excludes): |
| 41 | continue |
| 42 | |
| 43 | # Ignore files that don't start with 'Test'. |
| 44 | if not filename.startswith('Test'): |
| 45 | continue |
| 46 | |
| 47 | filepath = os.path.join(source_path, filename) |
| 48 | if not os.path.isdir(filepath): |
| 49 | base, ext = os.path.splitext(filename) |
| 50 | if ext in localConfig.suffixes: |
| 51 | yield lit.Test.Test(testSuite, path_in_suite + |
| 52 | (filename, ), localConfig) |
| 53 | |
| 54 | def execute(self, test, litConfig): |
| 55 | if litConfig.noExecute: |
| 56 | return lit.Test.PASS, '' |
| 57 | |
Michal Gorny | 6dde836 | 2018-06-06 09:44:14 +0000 | [diff] [blame] | 58 | if test.config.lldb_disable_python: |
| 59 | return (lit.Test.UNSUPPORTED, 'Python module disabled') |
| 60 | |
Jonas Devlieghere | 1a928f3 | 2018-04-18 17:08:49 +0000 | [diff] [blame] | 61 | if test.config.unsupported: |
| 62 | return (lit.Test.UNSUPPORTED, 'Test is unsupported') |
| 63 | |
| 64 | testPath, testFile = os.path.split(test.getSourcePath()) |
Adrian Prantl | 0e45df4 | 2018-08-20 22:00:32 +0000 | [diff] [blame] | 65 | # On Windows, the system does not always correctly interpret |
| 66 | # shebang lines. To make sure we can execute the tests, add |
| 67 | # python exe as the first parameter of the command. |
Aaron Smith | ba48c9b | 2018-04-24 17:08:05 +0000 | [diff] [blame] | 68 | cmd = [sys.executable] + self.dotest_cmd + [testPath, '-p', testFile] |
Jonas Devlieghere | 1a928f3 | 2018-04-18 17:08:49 +0000 | [diff] [blame] | 69 | |
Adrian Prantl | 748310b | 2018-08-23 17:19:08 +0000 | [diff] [blame] | 70 | # The macOS system integrity protection (SIP) doesn't allow injecting |
| 71 | # libraries into system binaries, but this can be worked around by |
| 72 | # copying the binary into a different location. |
Adrian Prantl | 6c7f588 | 2018-08-23 17:51:14 +0000 | [diff] [blame] | 73 | if 'DYLD_INSERT_LIBRARIES' in test.config.environment and \ |
Pavel Labath | d9e8c77 | 2018-08-31 06:01:02 +0000 | [diff] [blame] | 74 | (sys.executable.startswith('/System/') or \ |
| 75 | sys.executable.startswith('/usr/')): |
Adrian Prantl | 748310b | 2018-08-23 17:19:08 +0000 | [diff] [blame] | 76 | builddir = getBuildDir(cmd) |
Adrian Prantl | 7e6ce43 | 2018-08-27 23:06:37 +0000 | [diff] [blame] | 77 | mkdir_p(builddir) |
Adrian Prantl | 748310b | 2018-08-23 17:19:08 +0000 | [diff] [blame] | 78 | copied_python = os.path.join(builddir, 'copied-system-python') |
Adrian Prantl | dfa7755 | 2018-08-27 23:06:38 +0000 | [diff] [blame] | 79 | if not os.path.isfile(copied_python): |
| 80 | import shutil, subprocess |
| 81 | python = subprocess.check_output([ |
| 82 | '/usr/bin/python2.7', '-c', |
| 83 | 'import sys; print sys.executable']).strip() |
| 84 | shutil.copy(python, copied_python) |
Adrian Prantl | 748310b | 2018-08-23 17:19:08 +0000 | [diff] [blame] | 85 | cmd[0] = copied_python |
| 86 | |
Jonas Devlieghere | 1a928f3 | 2018-04-18 17:08:49 +0000 | [diff] [blame] | 87 | try: |
| 88 | out, err, exitCode = lit.util.executeCommand( |
| 89 | cmd, |
| 90 | env=test.config.environment, |
| 91 | timeout=litConfig.maxIndividualTestTime) |
| 92 | except lit.util.ExecuteCommandTimeoutException: |
| 93 | return (lit.Test.TIMEOUT, 'Reached timeout of {} seconds'.format( |
| 94 | litConfig.maxIndividualTestTime)) |
| 95 | |
| 96 | if exitCode: |
| 97 | return lit.Test.FAIL, out + err |
| 98 | |
| 99 | passing_test_line = 'RESULT: PASSED' |
| 100 | if passing_test_line not in out and passing_test_line not in err: |
| 101 | msg = ('Unable to find %r in dotest output:\n\n%s%s' % |
| 102 | (passing_test_line, out, err)) |
| 103 | return lit.Test.UNRESOLVED, msg |
| 104 | |
| 105 | return lit.Test.PASS, '' |