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 # --aegis Print test results to an output file (specified
29 # by the -o option) in the format expected by
30 # aetest(5). This is intended for use in the
31 # batch_test_command field in the Aegis project
34 # -d Debug. Runs the script under the Python
35 # debugger (pdb.py) so you don't have to
36 # muck with PYTHONPATH yourself.
38 # -f file Only execute the tests listed in the specified
41 # -h Print the help and exit.
43 # -o file Print test results to the specified file.
44 # The --aegis and --xml options specify the
47 # -P Python Use the specified Python interpreter.
49 # -p package Test against the specified package.
51 # --passed In the final summary, also report which tests
52 # passed. The default is to only report tests
53 # which failed or returned NO RESULT.
55 # -q Quiet. By default, runtest.py prints the
56 # command line it will execute before
57 # executing it. This suppresses that print.
59 # -X The scons "script" is an executable; don't
62 # -x scons The scons script to use for tests.
64 # --xml Print test results to an output file (specified
65 # by the -o option) in an SCons-specific XML format.
66 # This is (will be) used for reporting results back
67 # to a central SCons test monitoring infrastructure.
69 # (Note: There used to be a -v option that specified the SCons
70 # version to be tested, when we were installing in a version-specific
71 # library directory. If we ever resurrect that as the default, then
72 # you can find the appropriate code in the 0.04 version of this script,
73 # rather than reinventing that wheel.)
92 print_passed_summary = None
100 python = os.path.join(sys.prefix, 'jython')
102 python = sys.executable
106 if sys.platform == 'win32' or os.name == 'java':
107 lib_dir = os.path.join(sys.exec_prefix, "Lib")
109 # The hard-coded "python" here is the directory name,
110 # not an executable, so it's all right.
111 lib_dir = os.path.join(sys.exec_prefix, "lib", "python" + sys.version[0:3])
114 Usage: runtest.py [OPTIONS] [TEST ...]
116 -a, --all Run all tests.
117 --aegis Print results in Aegis format.
118 -d, --debug Run test scripts under the Python debugger.
119 -f FILE, --file FILE Run tests in specified FILE.
120 -h, --help Print this message and exit.
121 -o FILE, --output FILE Print test results to FILE.
122 -P Python Use the specified Python interpreter.
123 -p PACKAGE, --package PACKAGE
124 Test against the specified PACKAGE:
126 local-tar-gz .tar.gz standalone package
127 local-zip .zip standalone package
129 src-tar-gz .tar.gz source package
130 src-zip .zip source package
131 tar-gz .tar.gz distribution
132 zip .zip distribution
133 --passed Summarize which tests passed.
134 -q, --quiet Don't print the test being executed.
135 -v version Specify the SCons version.
136 -X Test script is executable, don't feed to Python.
137 -x SCRIPT, --exec SCRIPT Test SCRIPT.
138 --xml Print results in SCons XML format.
141 opts, args = getopt.getopt(sys.argv[1:], "adf:ho:P:p:qv:Xx:",
143 'debug', 'file=', 'help', 'output=',
144 'package=', 'passed', 'python=', 'quiet',
149 if o == '-a' or o == '--all':
151 elif o == '-d' or o == '--debug':
152 debug = os.path.join(lib_dir, "pdb.py")
153 elif o == '-f' or o == '--file':
154 if not os.path.isabs(a):
155 a = os.path.join(cwd, a)
157 elif o == '-h' or o == '--help':
160 elif o == '-o' or o == '--output':
161 if a != '-' and not os.path.isabs(a):
162 a = os.path.join(cwd, a)
164 elif o == '-p' or o == '--package':
166 elif o == '--passed':
167 print_passed_summary = 1
168 elif o == '-P' or o == '--python':
170 elif o == '-q' or o == '--quiet':
172 elif o == '-v' or o == '--version':
176 elif o == '-x' or o == '--exec':
178 elif o in ['--aegis', '--xml']:
182 for dir in string.split(os.environ['PATH'], os.pathsep):
183 f = os.path.join(dir, file)
184 if os.path.isfile(f):
189 if stat.S_IMODE(st[stat.ST_MODE]) & 0111:
193 aegis = whereis('aegis')
198 paths = os.popen("aesub '$sp' 2>/dev/null", "r").read()[:-1]
199 sp.extend(string.split(paths, os.pathsep))
200 spe = os.popen("aesub '$spe' 2>/dev/null", "r").read()[:-1]
201 spe = string.split(spe, os.pathsep)
208 def __init__(self, path, spe=None):
210 self.abspath = os.path.abspath(path)
213 f = os.path.join(dir, path)
214 if os.path.isfile(f):
219 class SystemExecutor(Base):
221 s = os.system(self.command)
226 sys.stdout.write("Unexpected exit status %d\n" % s)
230 except AttributeError:
231 class PopenExecutor(Base):
233 (tochild, fromchild, childerr) = os.popen3(self.command)
235 self.stdout = fromchild.read()
236 self.stderr = childerr.read()
238 self.status = childerr.close()
242 class PopenExecutor(Base):
244 p = popen2.Popen3(self.command, 1)
246 self.stdout = p.fromchild.read()
247 self.stderr = p.childerr.read()
248 self.status = p.wait()
250 class Aegis(SystemExecutor):
252 f.write('test_result = [\n')
254 f.write(' { file_name = "%s";\n' % self.path)
255 f.write(' exit_status = %d; },\n' % self.status)
259 class XML(PopenExecutor):
261 f.write(' <results>\n')
264 f.write(' <file_name>%s</file_name>\n' % self.path)
265 f.write(' <command_line>%s</command_line>\n' % self.command)
266 f.write(' <exit_status>%s</exit_status>\n' % self.status)
267 f.write(' <stdout>%s</stdout>\n' % self.stdout)
268 f.write(' <stderr>%s</stderr>\n' % self.stderr)
269 f.write(' </test>\n')
271 f.write(' </results>\n')
274 None : SystemExecutor,
278 Test = format_class[format]
284 for g in glob.glob(a):
285 tests.append(Test(g))
288 x = os.path.join(dir, a)
292 tests.append(Test(g))
296 for g in glob.glob(a):
297 tests.append(Test(g))
301 def find_Test_py(arg, dirname, names, tdict=tdict):
302 for n in filter(lambda n: n[-8:] == "Tests.py", names):
303 t = os.path.join(dirname, n)
304 if not tdict.has_key(t):
306 os.path.walk('src', find_Test_py, 0)
308 def find_py(arg, dirname, names, tdict=tdict):
309 for n in filter(lambda n: n[-3:] == ".py", names):
310 t = os.path.join(dirname, n)
311 if not tdict.has_key(t):
313 os.path.walk('test', find_py, 0)
316 cmd = "aegis -list -unf pf 2>/dev/null"
317 for line in os.popen(cmd, "r").readlines():
318 a = string.split(line)
319 if a[0] == "test" and not tdict.has_key(a[-1]):
320 tdict[a[-1]] = Test(a[-1], spe)
321 cmd = "aegis -list -unf cf 2>/dev/null"
322 for line in os.popen(cmd, "r").readlines():
323 a = string.split(line)
327 elif not tdict.has_key(a[-1]):
328 tdict[a[-1]] = Test(a[-1], spe)
332 tests = map(tdict.get, keys)
334 tests = map(Test, map(lambda x: x[:-1], open(testlistfile, 'r').readlines()))
336 sys.stderr.write("""\
337 runtest.py: No tests were specified on the command line.
338 List one or more tests, or use the -a option
339 to find and run all tests.
347 'local-tar-gz' : None,
356 # The hard-coded "python2.1" here is the library directory
357 # name on Debian systems, not an executable, so it's all right.
359 'deb' : os.path.join('python2.1', 'site-packages')
362 if not dir.has_key(package):
363 sys.stderr.write("Unknown package '%s'\n" % package)
366 test_dir = os.path.join(cwd, 'build', 'test-%s' % package)
368 if dir[package] is None:
369 scons_script_dir = test_dir
370 globs = glob.glob(os.path.join(test_dir, 'scons-local-*'))
372 sys.stderr.write("No `scons-local-*' dir in `%s'\n" % test_dir)
375 pythonpath_dir = globs[len(globs)-1]
376 elif sys.platform == 'win32':
377 scons_script_dir = os.path.join(test_dir, dir[package], 'Scripts')
378 scons_lib_dir = os.path.join(test_dir, dir[package])
379 pythonpath_dir = scons_lib_dir
381 scons_script_dir = os.path.join(test_dir, dir[package], 'bin')
382 l = lib.get(package, 'scons')
383 scons_lib_dir = os.path.join(test_dir, dir[package], 'lib', l)
384 pythonpath_dir = scons_lib_dir
390 # XXX: Logic like the following will be necessary once
391 # we fix runtest.py to run tests within an Aegis change
392 # without symlinks back to the baseline(s).
397 # d = os.path.join(dir, 'src', 'script')
398 # f = os.path.join(d, 'scons.py')
399 # if os.path.isfile(f):
402 # spe = map(lambda x: os.path.join(x, 'src', 'engine'), spe)
403 # ld = string.join(spe, os.pathsep)
405 scons_script_dir = sd or os.path.join(cwd, 'src', 'script')
407 scons_lib_dir = ld or os.path.join(cwd, 'src', 'engine')
409 pythonpath_dir = scons_lib_dir
412 # Let the version of SCons that the -x option pointed to find
414 os.environ['SCONS'] = scons
416 # Because SCons is really aggressive about finding its modules,
417 # it sometimes finds SCons modules elsewhere on the system.
418 # This forces SCons to use the modules that are being tested.
419 os.environ['SCONS_LIB_DIR'] = scons_lib_dir
422 os.environ['SCONS_EXEC'] = '1'
424 os.environ['SCONS_SCRIPT_DIR'] = scons_script_dir
425 os.environ['SCONS_CWD'] = cwd
427 os.environ['SCONS_VERSION'] = version
429 old_pythonpath = os.environ.get('PYTHONPATH')
431 pythonpaths = [ pythonpath_dir ]
433 pythonpaths.append(os.path.join(p, 'build', 'etc'))
434 pythonpaths.append(os.path.join(p, 'etc'))
435 os.environ['PYTHONPATH'] = string.join(pythonpaths, os.pathsep)
438 os.environ['PYTHONPATH'] = os.environ['PYTHONPATH'] + \
442 os.chdir(scons_script_dir)
445 def __init__(self, file):
447 def write(self, arg):
450 def __getattr__(self, attr):
451 return getattr(self.file, attr)
453 sys.stdout = Unbuffered(sys.stdout)
455 _ws = re.compile('\s')
463 t.command = string.join(map(escape, [python, debug, t.abspath]), " ")
465 sys.stdout.write(t.command + "\n")
468 passed = filter(lambda t: t.status == 0, tests)
469 fail = filter(lambda t: t.status == 1, tests)
470 no_result = filter(lambda t: t.status == 2, tests)
473 if passed and print_passed_summary:
475 sys.stdout.write("\nPassed the following test:\n")
477 sys.stdout.write("\nPassed the following %d tests:\n" % len(passed))
478 paths = map(lambda x: x.path, passed)
479 sys.stdout.write("\t" + string.join(paths, "\n\t") + "\n")
482 sys.stdout.write("\nFailed the following test:\n")
484 sys.stdout.write("\nFailed the following %d tests:\n" % len(fail))
485 paths = map(lambda x: x.path, fail)
486 sys.stdout.write("\t" + string.join(paths, "\n\t") + "\n")
488 if len(no_result) == 1:
489 sys.stdout.write("\nNO RESULT from the following test:\n")
491 sys.stdout.write("\nNO RESULT from the following %d tests:\n" % len(no_result))
492 paths = map(lambda x: x.path, no_result)
493 sys.stdout.write("\t" + string.join(paths, "\n\t") + "\n")
496 if outputfile == '-':
499 f = open(outputfile, 'w')
501 #f.write("test_result = [\n")
506 if outputfile != '-':
509 if format == '--aegis':