3 # runtest.py - wrapper script for running SCons tests
5 # This script mainly exists to set PYTHONPATH to the right list of
6 # directories to test the SCons modules.
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).
11 # HOWEVER, now that SCons has Repository support, we don't have
12 # Aegis copy all of the files into the local tree. So if you're
13 # using Aegis and want to run tests by hand using this script, you
14 # must "aecp ." the entire source tree into your local directory
15 # structure. When you're done with your change, you can then
16 # "aecpu -unch ." to un-copy any files that you haven't changed.
18 # When any -p option is specified, this script assumes it's in a
19 # directory in which a build has been performed, and sets PYTHONPATH
20 # so that it *only* references the modules that have unpacked from
21 # the specified built package, to test whether the packages are good.
25 # -a Run all tests; does a virtual 'find' for
26 # all SCons tests under the current directory.
28 # -d Debug. Runs the script under the Python
29 # debugger (pdb.py) so you don't have to
30 # muck with PYTHONPATH yourself.
32 # -h Print the help and exit.
34 # -o file Print test results to the specified file
35 # in the format expected by aetest(5). This
36 # is intended for use in the batch_test_command
37 # field in the Aegis project config file.
39 # -P Python Use the specified Python interpreter.
41 # -p package Test against the specified package.
43 # -q Quiet. By default, runtest.py prints the
44 # command line it will execute before
45 # executing it. This suppresses that print.
47 # -X The scons "script" is an executable; don't
50 # -x scons The scons script to use for tests.
52 # (Note: There used to be a -v option that specified the SCons
53 # version to be tested, when we were installing in a version-specific
54 # library directory. If we ever resurrect that as the default, then
55 # you can find the appropriate code in the 0.04 version of this script,
56 # rather than reinventing that wheel.)
79 python = os.path.join(sys.prefix, 'jython')
81 python = sys.executable
85 if sys.platform == 'win32' or os.name == 'java':
86 lib_dir = os.path.join(sys.exec_prefix, "Lib")
88 # The hard-coded "python" here is the directory name,
89 # not an executable, so it's all right.
90 lib_dir = os.path.join(sys.exec_prefix, "lib", "python" + sys.version[0:3])
93 Usage: runtest.py [OPTIONS] [TEST ...]
95 -a, --all Run all tests.
96 -d, --debug Run test scripts under the Python debugger.
97 -h, --help Print this message and exit.
98 -o FILE, --output FILE Print test results to FILE (Aegis format).
99 -P Python Use the specified Python interpreter.
100 -p PACKAGE, --package PACKAGE
101 Test against the specified PACKAGE:
103 local-tar-gz .tar.gz standalone package
104 local-zip .zip standalone package
106 src-tar-gz .tar.gz source package
107 src-zip .zip source package
108 tar-gz .tar.gz distribution
109 zip .zip distribution
110 -q, --quiet Don't print the test being executed.
111 -v version Specify the SCons version.
112 -X Test script is executable, don't feed to Python.
113 -x SCRIPT, --exec SCRIPT Test SCRIPT.
116 opts, args = getopt.getopt(sys.argv[1:], "adho:P:p:qv:Xx:",
117 ['all', 'debug', 'help', 'output=',
118 'package=', 'python=', 'quiet',
119 'version=', 'exec='])
122 if o == '-a' or o == '--all':
124 elif o == '-d' or o == '--debug':
125 debug = os.path.join(lib_dir, "pdb.py")
126 elif o == '-h' or o == '--help':
129 elif o == '-o' or o == '--output':
130 if not os.path.isabs(a):
131 a = os.path.join(cwd, a)
133 elif o == '-P' or o == '--python':
135 elif o == '-p' or o == '--package':
137 elif o == '-q' or o == '--quiet':
139 elif o == '-v' or o == '--version':
143 elif o == '-x' or o == '--exec':
147 for dir in string.split(os.environ['PATH'], os.pathsep):
148 f = os.path.join(dir, file)
149 if os.path.isfile(f):
154 if stat.S_IMODE(st[stat.ST_MODE]) & 0111:
158 aegis = whereis('aegis')
162 spe = os.popen("aesub '$spe' 2>/dev/null", "r").read()[:-1]
163 spe = string.split(spe, os.pathsep)
166 def __init__(self, path, spe=None):
168 self.abspath = os.path.abspath(path)
171 f = os.path.join(dir, path)
172 if os.path.isfile(f):
181 for g in glob.glob(a):
182 tests.append(Test(g))
185 x = os.path.join(dir, a)
189 tests.append(Test(g))
193 for g in glob.glob(a):
194 tests.append(Test(g))
198 def find_Test_py(arg, dirname, names, tdict=tdict):
199 for n in filter(lambda n: n[-8:] == "Tests.py", names):
200 t = os.path.join(dirname, n)
201 if not tdict.has_key(t):
203 os.path.walk('src', find_Test_py, 0)
205 def find_py(arg, dirname, names, tdict=tdict):
206 for n in filter(lambda n: n[-3:] == ".py", names):
207 t = os.path.join(dirname, n)
208 if not tdict.has_key(t):
210 os.path.walk('test', find_py, 0)
213 cmd = "aegis -list -unf pf 2>/dev/null"
214 for line in os.popen(cmd, "r").readlines():
215 a = string.split(line)
216 if a[0] == "test" and not tdict.has_key(a[-1]):
217 tdict[a[-1]] = Test(a[-1], spe)
218 cmd = "aegis -list -unf cf 2>/dev/null"
219 for line in os.popen(cmd, "r").readlines():
220 a = string.split(line)
224 elif not tdict.has_key(a[-1]):
225 tdict[a[-1]] = Test(a[-1], spe)
229 tests = map(tdict.get, keys)
235 'local-tar-gz' : None,
244 # The hard-coded "python2.1" here is the library directory
245 # name on Debian systems, not an executable, so it's all right.
247 'deb' : os.path.join('python2.1', 'site-packages')
250 if not dir.has_key(package):
251 sys.stderr.write("Unknown package '%s'\n" % package)
254 test_dir = os.path.join(cwd, 'build', 'test-%s' % package)
256 if dir[package] is None:
257 scons_script_dir = test_dir
258 globs = glob.glob(os.path.join(test_dir, 'scons-local-*'))
260 sys.stderr.write("No `scons-local-*' dir in `%s'\n" % test_dir)
263 pythonpath_dir = globs[len(globs)-1]
264 elif sys.platform == 'win32':
265 scons_script_dir = os.path.join(test_dir, dir[package], 'Scripts')
266 scons_lib_dir = os.path.join(test_dir, dir[package])
267 pythonpath_dir = scons_lib_dir
269 scons_script_dir = os.path.join(test_dir, dir[package], 'bin')
270 l = lib.get(package, 'scons')
271 scons_lib_dir = os.path.join(test_dir, dir[package], 'lib', l)
272 pythonpath_dir = scons_lib_dir
278 # XXX: Logic like the following will be necessary once
279 # we fix runtest.py to run tests within an Aegis change
280 # without symlinks back to the baseline(s).
285 # d = os.path.join(dir, 'src', 'script')
286 # f = os.path.join(d, 'scons.py')
287 # if os.path.isfile(f):
290 # spe = map(lambda x: os.path.join(x, 'src', 'engine'), spe)
291 # ld = string.join(spe, os.pathsep)
293 scons_script_dir = sd or os.path.join(cwd, 'src', 'script')
295 scons_lib_dir = ld or os.path.join(cwd, 'src', 'engine')
297 pythonpath_dir = scons_lib_dir
300 # Let the version of SCons that the -x option pointed to find
302 os.environ['SCONS'] = scons
304 # Because SCons is really aggressive about finding its modules,
305 # it sometimes finds SCons modules elsewhere on the system.
306 # This forces SCons to use the modules that are being tested.
307 os.environ['SCONS_LIB_DIR'] = scons_lib_dir
310 os.environ['SCONS_EXEC'] = '1'
312 os.environ['SCONS_SCRIPT_DIR'] = scons_script_dir
313 os.environ['SCONS_CWD'] = cwd
315 os.environ['SCONS_VERSION'] = version
317 old_pythonpath = os.environ.get('PYTHONPATH')
318 os.environ['PYTHONPATH'] = pythonpath_dir + \
320 os.path.join(cwd, 'build', 'etc') + \
322 os.path.join(cwd, 'etc')
324 os.environ['PYTHONPATH'] = os.environ['PYTHONPATH'] + \
328 os.chdir(scons_script_dir)
331 def __init__(self, file):
333 def write(self, arg):
336 def __getattr__(self, attr):
337 return getattr(self.file, attr)
339 sys.stdout = Unbuffered(sys.stdout)
342 cmd = string.join([python, debug, t.abspath], " ")
344 sys.stdout.write(cmd + "\n")
350 sys.stdout.write("Unexpected exit status %d\n" % s)
352 fail = filter(lambda t: t.status == 1, tests)
353 no_result = filter(lambda t: t.status == 2, tests)
358 sys.stdout.write("\nFailed the following test:\n")
360 sys.stdout.write("\nFailed the following %d tests:\n" % len(fail))
361 paths = map(lambda x: x.path, fail)
362 sys.stdout.write("\t" + string.join(paths, "\n\t") + "\n")
364 if len(no_result) == 1:
365 sys.stdout.write("\nNO RESULT from the following test:\n")
367 sys.stdout.write("\nNO RESULT from the following %d tests:\n" % len(no_result))
368 paths = map(lambda x: x.path, no_result)
369 sys.stdout.write("\t" + string.join(paths, "\n\t") + "\n")
372 f = open(output, 'w')
373 f.write("test_result = [\n")
375 f.write(' { file_name = "%s";\n' % t.path)
376 f.write(' exit_status = %d; },\n' % t.status)