blob: d622cc00d2ea9209fa75bd183ae30c78315a978b [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
45"""
46
47
48def setupSysPath():
49 """Add LLDB.framework/Resources/Python to the search paths for modules."""
50
51 # Get the directory containing the current script.
52 testPath = sys.path[0]
53 if not testPath.endswith('test'):
54 print "This script expects to reside in lldb's test directory."
55 sys.exit(-1)
56
57 base = os.path.abspath(os.path.join(testPath, os.pardir))
58 dbgPath = os.path.join(base, 'build', 'Debug', 'LLDB.framework',
59 'Resources', 'Python')
60 relPath = os.path.join(base, 'build', 'Release', 'LLDB.framework',
61 'Resources', 'Python')
62
63 lldbPath = None
64 if os.path.isfile(os.path.join(dbgPath, 'lldb.py')):
65 lldbPath = dbgPath
66 elif os.path.isfile(os.path.join(relPath, 'lldb.py')):
67 lldbPath = relPath
68
69 if not lldbPath:
70 print 'This script requires lldb.py to be in either ' + dbgPath,
71 print ' or' + relPath
72 sys.exit(-1)
73
74 sys.path.append(lldbPath)
75 #print 'sys.path =', sys.path
76
77
78def initTestdirs():
79 """Initialize the list of directories containing our unittest scripts.
80
81 '-h/--help as the first option prints out usage info and exit the program.
82 """
83
84 global verbose
85 global testdirs
86
87 if len(sys.argv) == 1:
88 pass
89 elif sys.argv[1].find('-h') != -1:
90 # Print usage info and exit.
91 usage()
92 sys.exit(0)
93 else:
94 # Process possible verbose flag.
95 index = 1
96 if sys.argv[1].find('-v') != -1:
97 verbose = 2
98 index += 1
99
100 # Gather all the dirs passed on the command line.
101 if len(sys.argv) > index:
102 testdirs = map(os.path.abspath, sys.argv[index:])
103
104 #print "testdirs =", testdirs
105
106
107def visit(prefix, dir, names):
108 """Visitor function for os.path.walk(path, visit, arg)."""
109
110 global suite
111
112 for name in names:
113 if os.path.isdir(os.path.join(dir, name)):
114 continue
115
116 if '.py' == os.path.splitext(name)[1] and name.startswith(prefix):
117 # We found a pattern match for our test case. Add it to the suite.
118 sys.path.append(dir)
119 base = os.path.splitext(name)[0]
120 suite.addTests(unittest.defaultTestLoader.loadTestsFromName(base))
121
122
123#
124# Start the actions by first setting up the module search path for lldb,
125# followed by initializing the test directories, and then walking the directory
126# trees, while collecting the tests into our test suite.
127#
128setupSysPath()
129initTestdirs()
130for testdir in testdirs:
131 os.path.walk(testdir, visit, 'Test')
132
133# Now that we have loaded all the test cases, run the whole test suite.
134#print "test suite =", suite
135#print "verbose =", verbose
136unittest.TextTestRunner(verbosity=verbose).run(suite)