108db735a54cee4bb3eaeb577619087dbd661194
[scons.git] / runtest.py
1 #!/usr/bin/env python
2 #
3 # runtests.py - wrapper script for running SCons tests
4 #
5 # This script mainly exists to set PYTHONPATH to the right list of
6 # directories to test the SCons modules.
7 #
8 # By default, it directly uses the modules in the local tree:
9 # ./src/ (source files we ship) and ./etc/ (other modules we don't)
10 #
11 # When "-b aegis" is specified, it assumes it's in a directory
12 # in which an Aegis build (aeb) has been performed, and sets
13 # PYTHONPATH so that it *only* references the modules that have
14 # unpacked from the built packages, to test whether the packages
15 # are good.
16 #
17 # Options:
18 #
19 #       -1              Use the test configuration in build/test1
20 #                       (installed from the scons package)
21 #
22 #       -2              Use the test configuration in build/test2
23 #                       (installed from the python-scons and scons-script
24 #                       packages)
25 #
26 #       -a              Run all tests; does a virtual 'find' for
27 #                       all SCons tests under the current directory.
28 #
29 #       -b system       Assume you're in the specified built system.
30 #                       'aegis' is the only one currently defined.
31 #
32 #       -d              Debug.  Runs the script under the Python
33 #                       debugger (pdb.py) so you don't have to
34 #                       muck with PYTHONPATH yourself.
35 #
36 #       -q              Quiet.  By default, runtest.py prints the
37 #                       command line it will execute before
38 #                       executing it.  This suppresses that print.
39 #
40 #       -v              Version.  Specifies the version number to
41 #                       be used for Aegis interaction.
42 #
43
44 import getopt
45 import os
46 import os.path
47 import re
48 import string
49 import sys
50
51 all = 0
52 build = None
53 debug = ''
54 tests = []
55 printcmd = 1
56 version = None
57 testver = 1
58
59 opts, tests = getopt.getopt(sys.argv[1:], "12ab:dqv:",
60                             ['all','build=','debug','quiet','version='])
61
62 for o, a in opts:
63     if o == '-1': testver = 1
64     elif o == '-2': testver = 2
65     elif o == '-a' or o == '--all': all = 1
66     elif o == '-b' or o == '--build': build = a
67     elif o == '-d' or o == '--debug': debug = os.path.join(
68                                         sys.exec_prefix,
69                                         "lib",
70                                         "python" + sys.version[0:3],
71                                         "pdb.py")
72     elif o == '-q' or o == '--quiet': printcmd = 0
73     elif o == '-v' or o == '--version': version = a
74
75 cwd = os.getcwd()
76
77 if tests:
78     map(os.path.abspath, tests)
79 elif all:
80     def find_Test_py(arg, dirname, names):
81         global tests
82         n = filter(lambda n: n[-8:] == "Tests.py", names)
83         n = map(lambda x,d=dirname: os.path.join(d, x), n)
84         tests = tests + n
85     os.path.walk('src', find_Test_py, 0)
86
87     def find_py(arg, dirname, names):
88         global tests
89         n = filter(lambda n: n[-3:] == ".py", names)
90         n = map(lambda x,d=dirname: os.path.join(d, x), n)
91         tests = tests + n
92     os.path.walk('test', find_py, 0)
93
94 if build == 'aegis':
95     if not version:
96         version = os.popen("aesub '$version'").read()[:-1]
97
98     match = re.compile(r'^[CD]0*')
99
100     def aegis_to_version(aever):
101         arr = string.split(aever, '.')
102         end = max(len(arr) - 1, 2)
103         arr = map(lambda e: match.sub('', e), arr[:end])
104         def rep(e):
105             if len(e) == 1:
106                 e = '0' + e
107             return e
108         arr[1:] = map(rep, arr[1:])
109         return string.join(arr, '.')
110
111     version = aegis_to_version(version)
112
113     scons_dir = os.path.join(cwd, 'build', 'test' + str(testver), 'bin')
114
115     if testver == 1:
116         test_dir = os.path.join('test1', 'lib', 'scons-' + str(version))
117     elif testver == 2:
118         test_dir = os.path.join('test2', 'lib', 'python' + sys.version[0:3],
119                                 'site-packages')
120
121     os.environ['PYTHONPATH'] = os.path.join(cwd, 'build', test_dir)
122
123 else:
124
125     scons_dir = os.path.join(cwd, 'src', 'script')
126
127     os.environ['PYTHONPATH'] = string.join([os.path.join(cwd, 'src', 'engine'),
128                                             os.path.join(cwd, 'etc')],
129                                            os.pathsep)
130
131 os.chdir(scons_dir)
132
133 fail = []
134
135 for path in tests:
136     if os.path.isabs(path):
137         abs = path
138     else:
139         abs = os.path.join(cwd, path)
140     cmd = string.join(["python", debug, abs], " ")
141     if printcmd:
142         print cmd
143     if os.system(cmd):
144         fail.append(path)
145
146 sys.exit(len(fail))