blob: ef9258b45c9a796a90a40f17d12af7e8397a9d92 [file] [log] [blame]
Johnny Chen9707bb62010-06-25 21:14:08 +00001#!/usr/bin/env python
2
3"""
4A simple testing framework for lldb using python's unit testing framework.
5
6Tests for lldb are written as python scripts which take advantage of the script
7bridging provided by LLDB.framework to interact with lldb core.
8
9A specific naming pattern is followed by the .py script to be recognized as
10a module which implements a test scenario, namely, Test*.py.
11
12To specify the directories where "Test*.py" python test scripts are located,
13you need to pass in a list of directory names. By default, the current
14working directory is searched if nothing is specified on the command line.
15"""
16
17import os
18import sys
Johnny Chen16b97472010-06-29 23:17:15 +000019import time
Johnny Chen75e28f92010-08-05 23:42:46 +000020import unittest2
Johnny Chen9707bb62010-06-25 21:14:08 +000021
22#
23# Global variables:
24#
25
26# The test suite.
Johnny Chen75e28f92010-08-05 23:42:46 +000027suite = unittest2.TestSuite()
Johnny Chen9707bb62010-06-25 21:14:08 +000028
29# Default verbosity is 0.
30verbose = 0
31
32# By default, search from the current working directory.
33testdirs = [ os.getcwd() ]
34
35
36def usage():
37 print """
38Usage: dotest.py [option] [args]
39where options:
40-h : print this help message and exit (also --help)
41-v : do verbose mode of unittest framework
42
43and:
44args : specify a list of directory names to search for python Test*.py scripts
45 if empty, search from the curret working directory, instead
Johnny Chen58f93922010-06-29 23:10:39 +000046
47Running of this script also sets up the LLDB_TEST environment variable so that
48individual test cases can locate their supporting files correctly.
Johnny Chen9707bb62010-06-25 21:14:08 +000049"""
50
51
52def setupSysPath():
53 """Add LLDB.framework/Resources/Python to the search paths for modules."""
54
55 # Get the directory containing the current script.
Johnny Chena1affab2010-07-03 03:41:59 +000056 scriptPath = sys.path[0]
57 if not scriptPath.endswith('test'):
Johnny Chen9707bb62010-06-25 21:14:08 +000058 print "This script expects to reside in lldb's test directory."
59 sys.exit(-1)
60
Johnny Chena1affab2010-07-03 03:41:59 +000061 os.environ["LLDB_TEST"] = scriptPath
Johnny Chen58f93922010-06-29 23:10:39 +000062
Johnny Chena1affab2010-07-03 03:41:59 +000063 base = os.path.abspath(os.path.join(scriptPath, os.pardir))
Johnny Chen9707bb62010-06-25 21:14:08 +000064 dbgPath = os.path.join(base, 'build', 'Debug', 'LLDB.framework',
65 'Resources', 'Python')
66 relPath = os.path.join(base, 'build', 'Release', 'LLDB.framework',
67 'Resources', 'Python')
68
69 lldbPath = None
70 if os.path.isfile(os.path.join(dbgPath, 'lldb.py')):
71 lldbPath = dbgPath
72 elif os.path.isfile(os.path.join(relPath, 'lldb.py')):
73 lldbPath = relPath
74
75 if not lldbPath:
76 print 'This script requires lldb.py to be in either ' + dbgPath,
77 print ' or' + relPath
78 sys.exit(-1)
79
80 sys.path.append(lldbPath)
Johnny Chena1affab2010-07-03 03:41:59 +000081 sys.path.append(scriptPath)
Johnny Chen9707bb62010-06-25 21:14:08 +000082
83
84def initTestdirs():
85 """Initialize the list of directories containing our unittest scripts.
86
87 '-h/--help as the first option prints out usage info and exit the program.
88 """
89
90 global verbose
91 global testdirs
92
93 if len(sys.argv) == 1:
94 pass
95 elif sys.argv[1].find('-h') != -1:
96 # Print usage info and exit.
97 usage()
98 sys.exit(0)
99 else:
100 # Process possible verbose flag.
101 index = 1
102 if sys.argv[1].find('-v') != -1:
103 verbose = 2
104 index += 1
105
106 # Gather all the dirs passed on the command line.
107 if len(sys.argv) > index:
108 testdirs = map(os.path.abspath, sys.argv[index:])
109
Johnny Chen9707bb62010-06-25 21:14:08 +0000110
111def visit(prefix, dir, names):
112 """Visitor function for os.path.walk(path, visit, arg)."""
113
114 global suite
115
116 for name in names:
117 if os.path.isdir(os.path.join(dir, name)):
118 continue
119
120 if '.py' == os.path.splitext(name)[1] and name.startswith(prefix):
121 # We found a pattern match for our test case. Add it to the suite.
Johnny Chena85d7ee2010-06-26 00:19:32 +0000122 if not sys.path.count(dir):
123 sys.path.append(dir)
Johnny Chen9707bb62010-06-25 21:14:08 +0000124 base = os.path.splitext(name)[0]
Johnny Chen75e28f92010-08-05 23:42:46 +0000125 suite.addTests(unittest2.defaultTestLoader.loadTestsFromName(base))
Johnny Chen9707bb62010-06-25 21:14:08 +0000126
127
128#
129# Start the actions by first setting up the module search path for lldb,
130# followed by initializing the test directories, and then walking the directory
131# trees, while collecting the tests into our test suite.
132#
133setupSysPath()
134initTestdirs()
135for testdir in testdirs:
136 os.path.walk(testdir, visit, 'Test')
137
138# Now that we have loaded all the test cases, run the whole test suite.
Johnny Chen1bfbd412010-06-29 19:44:16 +0000139
140# For the time being, let's bracket the test runner within the
141# lldb.SBDebugger.Initialize()/Terminate() pair.
142import lldb
143lldb.SBDebugger.Initialize()
144
Johnny Chen909e5a62010-07-01 22:52:57 +0000145# Create a singleton SBDebugger in the lldb namespace.
146lldb.DBG = lldb.SBDebugger.Create()
147
148# Turn on logging for debugging purposes if ${LLDB_LOG} environment variable is
149# is defined. Use ${LLDB_LOG} to specify the log file.
150ci = lldb.DBG.GetCommandInterpreter()
151res = lldb.SBCommandReturnObject()
152if ("LLDB_LOG" in os.environ):
153 ci.HandleCommand(
154 "log enable -f " + os.environ["LLDB_LOG"] + " lldb default", res)
Johnny Chen797674b2010-07-02 20:35:23 +0000155 if not res.Succeeded():
156 raise Exception('log enable failed (check your LLDB_LOG env variable...')
Johnny Chen909e5a62010-07-01 22:52:57 +0000157
Johnny Chen75e28f92010-08-05 23:42:46 +0000158unittest2.TextTestRunner(verbosity=verbose).run(suite)
Johnny Chen1bfbd412010-06-29 19:44:16 +0000159
Johnny Chen16b97472010-06-29 23:17:15 +0000160# Add some delay before calling SBDebugger.Terminate().
161time.sleep(1)
Johnny Chen1bfbd412010-06-29 19:44:16 +0000162lldb.SBDebugger.Terminate()