blob: 3ab116bd0a2ad884f74c82b417531b15e488fefa [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
19import unittest
20
21#
22# Global variables:
23#
24
25# The test suite.
26suite = unittest.TestSuite()
27
28# Default verbosity is 0.
29verbose = 0
30
31# By default, search from the current working directory.
32testdirs = [ os.getcwd() ]
33
34
35def usage():
36 print """
37Usage: dotest.py [option] [args]
38where options:
39-h : print this help message and exit (also --help)
40-v : do verbose mode of unittest framework
41
42and:
43args : specify a list of directory names to search for python Test*.py scripts
44 if empty, search from the curret working directory, instead
Johnny Chen58f93922010-06-29 23:10:39 +000045
46Running of this script also sets up the LLDB_TEST environment variable so that
47individual test cases can locate their supporting files correctly.
Johnny Chen9707bb62010-06-25 21:14:08 +000048"""
49
50
51def setupSysPath():
52 """Add LLDB.framework/Resources/Python to the search paths for modules."""
53
54 # Get the directory containing the current script.
55 testPath = sys.path[0]
56 if not testPath.endswith('test'):
57 print "This script expects to reside in lldb's test directory."
58 sys.exit(-1)
59
Johnny Chen58f93922010-06-29 23:10:39 +000060 os.environ["LLDB_TEST"] = testPath
61
Johnny Chen9707bb62010-06-25 21:14:08 +000062 base = os.path.abspath(os.path.join(testPath, os.pardir))
63 dbgPath = os.path.join(base, 'build', 'Debug', 'LLDB.framework',
64 'Resources', 'Python')
65 relPath = os.path.join(base, 'build', 'Release', 'LLDB.framework',
66 'Resources', 'Python')
67
68 lldbPath = None
69 if os.path.isfile(os.path.join(dbgPath, 'lldb.py')):
70 lldbPath = dbgPath
71 elif os.path.isfile(os.path.join(relPath, 'lldb.py')):
72 lldbPath = relPath
73
74 if not lldbPath:
75 print 'This script requires lldb.py to be in either ' + dbgPath,
76 print ' or' + relPath
77 sys.exit(-1)
78
79 sys.path.append(lldbPath)
Johnny Chen9707bb62010-06-25 21:14:08 +000080
81
82def initTestdirs():
83 """Initialize the list of directories containing our unittest scripts.
84
85 '-h/--help as the first option prints out usage info and exit the program.
86 """
87
88 global verbose
89 global testdirs
90
91 if len(sys.argv) == 1:
92 pass
93 elif sys.argv[1].find('-h') != -1:
94 # Print usage info and exit.
95 usage()
96 sys.exit(0)
97 else:
98 # Process possible verbose flag.
99 index = 1
100 if sys.argv[1].find('-v') != -1:
101 verbose = 2
102 index += 1
103
104 # Gather all the dirs passed on the command line.
105 if len(sys.argv) > index:
106 testdirs = map(os.path.abspath, sys.argv[index:])
107
Johnny Chen9707bb62010-06-25 21:14:08 +0000108
109def visit(prefix, dir, names):
110 """Visitor function for os.path.walk(path, visit, arg)."""
111
112 global suite
113
114 for name in names:
115 if os.path.isdir(os.path.join(dir, name)):
116 continue
117
118 if '.py' == os.path.splitext(name)[1] and name.startswith(prefix):
119 # We found a pattern match for our test case. Add it to the suite.
Johnny Chena85d7ee2010-06-26 00:19:32 +0000120 if not sys.path.count(dir):
121 sys.path.append(dir)
Johnny Chen9707bb62010-06-25 21:14:08 +0000122 base = os.path.splitext(name)[0]
123 suite.addTests(unittest.defaultTestLoader.loadTestsFromName(base))
124
125
126#
127# Start the actions by first setting up the module search path for lldb,
128# followed by initializing the test directories, and then walking the directory
129# trees, while collecting the tests into our test suite.
130#
131setupSysPath()
132initTestdirs()
133for testdir in testdirs:
134 os.path.walk(testdir, visit, 'Test')
135
136# Now that we have loaded all the test cases, run the whole test suite.
Johnny Chen1bfbd412010-06-29 19:44:16 +0000137
138# For the time being, let's bracket the test runner within the
139# lldb.SBDebugger.Initialize()/Terminate() pair.
140import lldb
141lldb.SBDebugger.Initialize()
142
Johnny Chen9707bb62010-06-25 21:14:08 +0000143unittest.TextTestRunner(verbosity=verbose).run(suite)
Johnny Chen1bfbd412010-06-29 19:44:16 +0000144
145lldb.SBDebugger.Terminate()