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')
161 sp = os.popen("aesub '$sp' 2>/dev/null", "r").read()[:-1]
162 sp = string.split(sp, os.pathsep)
163 spe = os.popen("aesub '$spe' 2>/dev/null", "r").read()[:-1]
164 spe = string.split(spe, os.pathsep)
170 def __init__(self, path, spe=None):
172 self.abspath = os.path.abspath(path)
175 f = os.path.join(dir, path)
176 if os.path.isfile(f):
185 for g in glob.glob(a):
186 tests.append(Test(g))
189 x = os.path.join(dir, a)
193 tests.append(Test(g))
197 for g in glob.glob(a):
198 tests.append(Test(g))
202 def find_Test_py(arg, dirname, names, tdict=tdict):
203 for n in filter(lambda n: n[-8:] == "Tests.py", names):
204 t = os.path.join(dirname, n)
205 if not tdict.has_key(t):
207 os.path.walk('src', find_Test_py, 0)
209 def find_py(arg, dirname, names, tdict=tdict):
210 for n in filter(lambda n: n[-3:] == ".py", names):
211 t = os.path.join(dirname, n)
212 if not tdict.has_key(t):
214 os.path.walk('test', find_py, 0)
217 cmd = "aegis -list -unf pf 2>/dev/null"
218 for line in os.popen(cmd, "r").readlines():
219 a = string.split(line)
220 if a[0] == "test" and not tdict.has_key(a[-1]):
221 tdict[a[-1]] = Test(a[-1], spe)
222 cmd = "aegis -list -unf cf 2>/dev/null"
223 for line in os.popen(cmd, "r").readlines():
224 a = string.split(line)
228 elif not tdict.has_key(a[-1]):
229 tdict[a[-1]] = Test(a[-1], spe)
233 tests = map(tdict.get, keys)
239 'local-tar-gz' : None,
248 # The hard-coded "python2.1" here is the library directory
249 # name on Debian systems, not an executable, so it's all right.
251 'deb' : os.path.join('python2.1', 'site-packages')
254 if not dir.has_key(package):
255 sys.stderr.write("Unknown package '%s'\n" % package)
258 test_dir = os.path.join(cwd, 'build', 'test-%s' % package)
260 if dir[package] is None:
261 scons_script_dir = test_dir
262 globs = glob.glob(os.path.join(test_dir, 'scons-local-*'))
264 sys.stderr.write("No `scons-local-*' dir in `%s'\n" % test_dir)
267 pythonpath_dir = globs[len(globs)-1]
268 elif sys.platform == 'win32':
269 scons_script_dir = os.path.join(test_dir, dir[package], 'Scripts')
270 scons_lib_dir = os.path.join(test_dir, dir[package])
271 pythonpath_dir = scons_lib_dir
273 scons_script_dir = os.path.join(test_dir, dir[package], 'bin')
274 l = lib.get(package, 'scons')
275 scons_lib_dir = os.path.join(test_dir, dir[package], 'lib', l)
276 pythonpath_dir = scons_lib_dir
282 # XXX: Logic like the following will be necessary once
283 # we fix runtest.py to run tests within an Aegis change
284 # without symlinks back to the baseline(s).
289 # d = os.path.join(dir, 'src', 'script')
290 # f = os.path.join(d, 'scons.py')
291 # if os.path.isfile(f):
294 # spe = map(lambda x: os.path.join(x, 'src', 'engine'), spe)
295 # ld = string.join(spe, os.pathsep)
297 scons_script_dir = sd or os.path.join(cwd, 'src', 'script')
299 scons_lib_dir = ld or os.path.join(cwd, 'src', 'engine')
301 pythonpath_dir = scons_lib_dir
304 # Let the version of SCons that the -x option pointed to find
306 os.environ['SCONS'] = scons
308 # Because SCons is really aggressive about finding its modules,
309 # it sometimes finds SCons modules elsewhere on the system.
310 # This forces SCons to use the modules that are being tested.
311 os.environ['SCONS_LIB_DIR'] = scons_lib_dir
314 os.environ['SCONS_EXEC'] = '1'
316 os.environ['SCONS_SCRIPT_DIR'] = scons_script_dir
317 os.environ['SCONS_CWD'] = cwd
319 os.environ['SCONS_VERSION'] = version
321 old_pythonpath = os.environ.get('PYTHONPATH')
323 pythonpaths = [ pythonpath_dir ]
325 pythonpaths.append(os.path.join(p, 'build', 'etc'))
326 pythonpaths.append(os.path.join(p, 'etc'))
327 os.environ['PYTHONPATH'] = string.join(pythonpaths, os.pathsep)
330 os.environ['PYTHONPATH'] = os.environ['PYTHONPATH'] + \
334 os.chdir(scons_script_dir)
337 def __init__(self, file):
339 def write(self, arg):
342 def __getattr__(self, attr):
343 return getattr(self.file, attr)
345 sys.stdout = Unbuffered(sys.stdout)
348 cmd = string.join([python, debug, t.abspath], " ")
350 sys.stdout.write(cmd + "\n")
356 sys.stdout.write("Unexpected exit status %d\n" % s)
358 fail = filter(lambda t: t.status == 1, tests)
359 no_result = filter(lambda t: t.status == 2, tests)
364 sys.stdout.write("\nFailed the following test:\n")
366 sys.stdout.write("\nFailed the following %d tests:\n" % len(fail))
367 paths = map(lambda x: x.path, fail)
368 sys.stdout.write("\t" + string.join(paths, "\n\t") + "\n")
370 if len(no_result) == 1:
371 sys.stdout.write("\nNO RESULT from the following test:\n")
373 sys.stdout.write("\nNO RESULT from the following %d tests:\n" % len(no_result))
374 paths = map(lambda x: x.path, no_result)
375 sys.stdout.write("\t" + string.join(paths, "\n\t") + "\n")
378 f = open(output, 'w')
379 f.write("test_result = [\n")
381 f.write(' { file_name = "%s";\n' % t.path)
382 f.write(' exit_status = %d; },\n' % t.status)