blob: 08c143a69777a252f5c90fcbaa8ad05accda4358 [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 Chen9707bb62010-06-25 21:14:08 +000020import unittest
21
22#
23# Global variables:
24#
25
26# The test suite.
27suite = unittest.TestSuite()
28
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.
56 testPath = sys.path[0]
57 if not testPath.endswith('test'):
58 print "This script expects to reside in lldb's test directory."
59 sys.exit(-1)
60
Johnny Chen58f93922010-06-29 23:10:39 +000061 os.environ["LLDB_TEST"] = testPath
62
Johnny Chen9707bb62010-06-25 21:14:08 +000063 base = os.path.abspath(os.path.join(testPath, os.pardir))
64 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 Chen9707bb62010-06-25 21:14:08 +000081
82
83def initTestdirs():
84 """Initialize the list of directories containing our unittest scripts.
85
86 '-h/--help as the first option prints out usage info and exit the program.
87 """
88
89 global verbose
90 global testdirs
91
92 if len(sys.argv) == 1:
93 pass
94 elif sys.argv[1].find('-h') != -1:
95 # Print usage info and exit.
96 usage()
97 sys.exit(0)
98 else:
99 # Process possible verbose flag.
100 index = 1
101 if sys.argv[1].find('-v') != -1:
102 verbose = 2
103 index += 1
104
105 # Gather all the dirs passed on the command line.
106 if len(sys.argv) > index:
107 testdirs = map(os.path.abspath, sys.argv[index:])
108
Johnny Chen9707bb62010-06-25 21:14:08 +0000109
110def visit(prefix, dir, names):
111 """Visitor function for os.path.walk(path, visit, arg)."""
112
113 global suite
114
115 for name in names:
116 if os.path.isdir(os.path.join(dir, name)):
117 continue
118
119 if '.py' == os.path.splitext(name)[1] and name.startswith(prefix):
120 # We found a pattern match for our test case. Add it to the suite.
Johnny Chena85d7ee2010-06-26 00:19:32 +0000121 if not sys.path.count(dir):
122 sys.path.append(dir)
Johnny Chen9707bb62010-06-25 21:14:08 +0000123 base = os.path.splitext(name)[0]
124 suite.addTests(unittest.defaultTestLoader.loadTestsFromName(base))
125
126
127#
128# Start the actions by first setting up the module search path for lldb,
129# followed by initializing the test directories, and then walking the directory
130# trees, while collecting the tests into our test suite.
131#
132setupSysPath()
133initTestdirs()
134for testdir in testdirs:
135 os.path.walk(testdir, visit, 'Test')
136
137# Now that we have loaded all the test cases, run the whole test suite.
Johnny Chen1bfbd412010-06-29 19:44:16 +0000138
139# For the time being, let's bracket the test runner within the
140# lldb.SBDebugger.Initialize()/Terminate() pair.
141import lldb
142lldb.SBDebugger.Initialize()
143
Johnny Chen9707bb62010-06-25 21:14:08 +0000144unittest.TextTestRunner(verbosity=verbose).run(suite)
Johnny Chen1bfbd412010-06-29 19:44:16 +0000145
Johnny Chen16b97472010-06-29 23:17:15 +0000146# Add some delay before calling SBDebugger.Terminate().
147time.sleep(1)
Johnny Chen1bfbd412010-06-29 19:44:16 +0000148lldb.SBDebugger.Terminate()