Johnny Chen | 9707bb6 | 2010-06-25 21:14:08 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | """ |
| 4 | A simple testing framework for lldb using python's unit testing framework. |
| 5 | |
| 6 | Tests for lldb are written as python scripts which take advantage of the script |
| 7 | bridging provided by LLDB.framework to interact with lldb core. |
| 8 | |
| 9 | A specific naming pattern is followed by the .py script to be recognized as |
| 10 | a module which implements a test scenario, namely, Test*.py. |
| 11 | |
| 12 | To specify the directories where "Test*.py" python test scripts are located, |
| 13 | you need to pass in a list of directory names. By default, the current |
| 14 | working directory is searched if nothing is specified on the command line. |
| 15 | """ |
| 16 | |
| 17 | import os |
| 18 | import sys |
| 19 | import unittest |
| 20 | |
| 21 | # |
| 22 | # Global variables: |
| 23 | # |
| 24 | |
| 25 | # The test suite. |
| 26 | suite = unittest.TestSuite() |
| 27 | |
| 28 | # Default verbosity is 0. |
| 29 | verbose = 0 |
| 30 | |
| 31 | # By default, search from the current working directory. |
| 32 | testdirs = [ os.getcwd() ] |
| 33 | |
| 34 | |
| 35 | def usage(): |
| 36 | print """ |
| 37 | Usage: dotest.py [option] [args] |
| 38 | where options: |
| 39 | -h : print this help message and exit (also --help) |
| 40 | -v : do verbose mode of unittest framework |
| 41 | |
| 42 | and: |
| 43 | args : 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 | |
| 48 | def 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) |
Johnny Chen | 9707bb6 | 2010-06-25 21:14:08 +0000 | [diff] [blame] | 75 | |
| 76 | |
| 77 | def initTestdirs(): |
| 78 | """Initialize the list of directories containing our unittest scripts. |
| 79 | |
| 80 | '-h/--help as the first option prints out usage info and exit the program. |
| 81 | """ |
| 82 | |
| 83 | global verbose |
| 84 | global testdirs |
| 85 | |
| 86 | if len(sys.argv) == 1: |
| 87 | pass |
| 88 | elif sys.argv[1].find('-h') != -1: |
| 89 | # Print usage info and exit. |
| 90 | usage() |
| 91 | sys.exit(0) |
| 92 | else: |
| 93 | # Process possible verbose flag. |
| 94 | index = 1 |
| 95 | if sys.argv[1].find('-v') != -1: |
| 96 | verbose = 2 |
| 97 | index += 1 |
| 98 | |
| 99 | # Gather all the dirs passed on the command line. |
| 100 | if len(sys.argv) > index: |
| 101 | testdirs = map(os.path.abspath, sys.argv[index:]) |
| 102 | |
Johnny Chen | 9707bb6 | 2010-06-25 21:14:08 +0000 | [diff] [blame] | 103 | |
| 104 | def visit(prefix, dir, names): |
| 105 | """Visitor function for os.path.walk(path, visit, arg).""" |
| 106 | |
| 107 | global suite |
| 108 | |
| 109 | for name in names: |
| 110 | if os.path.isdir(os.path.join(dir, name)): |
| 111 | continue |
| 112 | |
| 113 | if '.py' == os.path.splitext(name)[1] and name.startswith(prefix): |
| 114 | # We found a pattern match for our test case. Add it to the suite. |
Johnny Chen | a85d7ee | 2010-06-26 00:19:32 +0000 | [diff] [blame^] | 115 | if not sys.path.count(dir): |
| 116 | sys.path.append(dir) |
Johnny Chen | 9707bb6 | 2010-06-25 21:14:08 +0000 | [diff] [blame] | 117 | base = os.path.splitext(name)[0] |
| 118 | suite.addTests(unittest.defaultTestLoader.loadTestsFromName(base)) |
| 119 | |
| 120 | |
| 121 | # |
| 122 | # Start the actions by first setting up the module search path for lldb, |
| 123 | # followed by initializing the test directories, and then walking the directory |
| 124 | # trees, while collecting the tests into our test suite. |
| 125 | # |
| 126 | setupSysPath() |
| 127 | initTestdirs() |
| 128 | for testdir in testdirs: |
| 129 | os.path.walk(testdir, visit, 'Test') |
| 130 | |
| 131 | # Now that we have loaded all the test cases, run the whole test suite. |
Johnny Chen | 9707bb6 | 2010-06-25 21:14:08 +0000 | [diff] [blame] | 132 | unittest.TextTestRunner(verbosity=verbose).run(suite) |