2010-10-18 06:50:27 -07:00
|
|
|
#!/usr/bin/env python
|
2009-03-19 07:19:38 -07:00
|
|
|
"""
|
|
|
|
Run the test(s) listed on the command line. If a directory is listed, the script will recursively
|
|
|
|
walk the directory for files named .mk and run each.
|
|
|
|
|
|
|
|
For each test, we run gmake -f test.mk. By default, make must exit with an exit code of 0, and must print 'TEST-PASS'.
|
|
|
|
|
|
|
|
Each test is run in an empty directory.
|
|
|
|
|
|
|
|
The test file may contain lines at the beginning to alter the default behavior. These are all evaluated as python:
|
|
|
|
|
|
|
|
#T commandline: ['extra', 'params', 'here']
|
|
|
|
#T returncode: 2
|
|
|
|
#T returncode-on: {'win32': 2}
|
2009-03-26 13:24:51 -07:00
|
|
|
#T environment: {'VAR': 'VALUE}
|
2009-03-31 14:10:23 -07:00
|
|
|
#T grep-for: "text"
|
2009-03-19 07:19:38 -07:00
|
|
|
"""
|
|
|
|
|
|
|
|
from subprocess import Popen, PIPE, STDOUT
|
|
|
|
from optparse import OptionParser
|
2009-10-23 08:16:27 -07:00
|
|
|
import os, re, sys, shutil, glob
|
|
|
|
|
|
|
|
class ParentDict(dict):
|
|
|
|
def __init__(self, parent, **kwargs):
|
|
|
|
self.d = dict(kwargs)
|
|
|
|
self.parent = parent
|
|
|
|
|
|
|
|
def __setitem__(self, k, v):
|
|
|
|
self.d[k] = v
|
|
|
|
|
|
|
|
def __getitem__(self, k):
|
|
|
|
if k in self.d:
|
|
|
|
return self.d[k]
|
|
|
|
|
|
|
|
return self.parent[k]
|
2009-03-19 07:19:38 -07:00
|
|
|
|
|
|
|
thisdir = os.path.dirname(os.path.abspath(__file__))
|
|
|
|
|
2009-10-23 08:16:27 -07:00
|
|
|
pymake = [sys.executable, os.path.join(os.path.dirname(thisdir), 'make.py')]
|
|
|
|
manifest = os.path.join(thisdir, 'tests.manifest')
|
|
|
|
|
2009-03-19 07:19:38 -07:00
|
|
|
o = OptionParser()
|
2009-10-23 08:16:27 -07:00
|
|
|
o.add_option('-g', '--gmake',
|
|
|
|
dest="gmake", default="gmake")
|
2009-03-19 07:19:38 -07:00
|
|
|
o.add_option('-d', '--tempdir',
|
|
|
|
dest="tempdir", default="_mktests")
|
|
|
|
opts, args = o.parse_args()
|
|
|
|
|
|
|
|
if len(args) == 0:
|
2009-10-23 08:16:27 -07:00
|
|
|
args = [thisdir]
|
2009-03-19 07:19:38 -07:00
|
|
|
|
|
|
|
makefiles = []
|
|
|
|
for a in args:
|
|
|
|
if os.path.isfile(a):
|
|
|
|
makefiles.append(a)
|
|
|
|
elif os.path.isdir(a):
|
2012-08-21 16:03:50 -07:00
|
|
|
makefiles.extend(sorted(glob.glob(os.path.join(a, '*.mk'))))
|
2009-03-19 07:19:38 -07:00
|
|
|
|
2009-10-23 08:16:27 -07:00
|
|
|
def runTest(makefile, make, logfile, options):
|
|
|
|
"""
|
|
|
|
Given a makefile path, test it with a given `make` and return
|
|
|
|
(pass, message).
|
|
|
|
"""
|
2009-03-19 07:19:38 -07:00
|
|
|
|
|
|
|
if os.path.exists(opts.tempdir): shutil.rmtree(opts.tempdir)
|
|
|
|
os.mkdir(opts.tempdir, 0755)
|
|
|
|
|
2009-10-23 08:16:27 -07:00
|
|
|
logfd = open(logfile, 'w')
|
|
|
|
p = Popen(make + options['commandline'], stdout=logfd, stderr=STDOUT, env=options['env'])
|
|
|
|
logfd.close()
|
|
|
|
retcode = p.wait()
|
|
|
|
|
|
|
|
if retcode != options['returncode']:
|
|
|
|
return False, "FAIL (returncode=%i)" % retcode
|
|
|
|
|
|
|
|
logfd = open(logfile)
|
|
|
|
stdout = logfd.read()
|
|
|
|
logfd.close()
|
|
|
|
|
|
|
|
if stdout.find('TEST-FAIL') != -1:
|
2011-07-21 11:48:23 -07:00
|
|
|
print stdout
|
2009-10-23 08:16:27 -07:00
|
|
|
return False, "FAIL (TEST-FAIL printed)"
|
|
|
|
|
|
|
|
if options['grepfor'] and stdout.find(options['grepfor']) == -1:
|
2011-07-21 11:48:23 -07:00
|
|
|
print stdout
|
|
|
|
return False, "FAIL (%s not in output)" % options['grepfor']
|
2009-10-23 08:16:27 -07:00
|
|
|
|
|
|
|
if options['returncode'] == 0 and stdout.find('TEST-PASS') == -1:
|
2011-07-21 11:48:23 -07:00
|
|
|
print stdout
|
2009-10-23 08:16:27 -07:00
|
|
|
return False, 'FAIL (No TEST-PASS printed)'
|
|
|
|
|
|
|
|
if options['returncode'] != 0:
|
|
|
|
return True, 'PASS (retcode=%s)' % retcode
|
|
|
|
|
|
|
|
return True, 'PASS'
|
|
|
|
|
|
|
|
print "%-30s%-28s%-28s" % ("Test:", "gmake:", "pymake:")
|
|
|
|
|
|
|
|
gmakefails = 0
|
|
|
|
pymakefails = 0
|
|
|
|
|
|
|
|
tre = re.compile('^#T (gmake |pymake )?([a-z-]+)(?:: (.*))?$')
|
|
|
|
|
|
|
|
for makefile in makefiles:
|
2009-03-19 07:19:38 -07:00
|
|
|
# For some reason, MAKEFILE_LIST uses native paths in GNU make on Windows
|
|
|
|
# (even in MSYS!) so we pass both TESTPATH and NATIVE_TESTPATH
|
2009-10-23 08:16:27 -07:00
|
|
|
cline = ['-C', opts.tempdir, '-f', os.path.abspath(makefile), 'TESTPATH=%s' % thisdir.replace('\\','/'), 'NATIVE_TESTPATH=%s' % thisdir]
|
2009-03-19 07:19:38 -07:00
|
|
|
if sys.platform == 'win32':
|
|
|
|
#XXX: hack so we can specialize the separator character on windows.
|
|
|
|
# we really shouldn't need this, but y'know
|
|
|
|
cline += ['__WIN32__=1']
|
|
|
|
|
2009-10-23 08:16:27 -07:00
|
|
|
options = {
|
|
|
|
'returncode': 0,
|
|
|
|
'grepfor': None,
|
|
|
|
'env': dict(os.environ),
|
|
|
|
'commandline': cline,
|
|
|
|
'pass': True,
|
|
|
|
'skip': False,
|
|
|
|
}
|
|
|
|
|
|
|
|
gmakeoptions = ParentDict(options)
|
|
|
|
pymakeoptions = ParentDict(options)
|
|
|
|
|
|
|
|
dmap = {None: options, 'gmake ': gmakeoptions, 'pymake ': pymakeoptions}
|
2009-03-26 13:24:51 -07:00
|
|
|
|
2009-03-19 07:19:38 -07:00
|
|
|
mdata = open(makefile)
|
|
|
|
for line in mdata:
|
2011-07-21 11:48:23 -07:00
|
|
|
line = line.strip()
|
2009-03-19 07:19:38 -07:00
|
|
|
m = tre.search(line)
|
|
|
|
if m is None:
|
|
|
|
break
|
2009-10-23 08:16:27 -07:00
|
|
|
|
|
|
|
make, key, data = m.group(1, 2, 3)
|
|
|
|
d = dmap[make]
|
|
|
|
if data is not None:
|
|
|
|
data = eval(data)
|
2009-03-19 07:19:38 -07:00
|
|
|
if key == 'commandline':
|
2009-10-23 08:16:27 -07:00
|
|
|
assert make is None
|
|
|
|
d['commandline'].extend(data)
|
2009-03-19 07:19:38 -07:00
|
|
|
elif key == 'returncode':
|
2009-10-23 08:16:27 -07:00
|
|
|
d['returncode'] = data
|
2009-03-19 07:19:38 -07:00
|
|
|
elif key == 'returncode-on':
|
|
|
|
if sys.platform in data:
|
2009-10-23 08:16:27 -07:00
|
|
|
d['returncode'] = data[sys.platform]
|
2009-03-26 13:24:51 -07:00
|
|
|
elif key == 'environment':
|
|
|
|
for k, v in data.iteritems():
|
2009-10-23 08:16:27 -07:00
|
|
|
d['env'][k] = v
|
2009-03-31 14:10:23 -07:00
|
|
|
elif key == 'grep-for':
|
2011-10-31 10:16:21 -07:00
|
|
|
d['grepfor'] = data
|
2009-10-23 08:16:27 -07:00
|
|
|
elif key == 'fail':
|
|
|
|
d['pass'] = False
|
|
|
|
elif key == 'skip':
|
|
|
|
d['skip'] = True
|
2009-03-19 07:19:38 -07:00
|
|
|
else:
|
2009-10-23 08:16:27 -07:00
|
|
|
print >>sys.stderr, "%s: Unexpected #T key: %s" % (makefile, key)
|
2009-03-19 07:19:38 -07:00
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
mdata.close()
|
|
|
|
|
2009-10-23 08:16:27 -07:00
|
|
|
if gmakeoptions['skip']:
|
|
|
|
gmakepass, gmakemsg = True, ''
|
|
|
|
else:
|
|
|
|
gmakepass, gmakemsg = runTest(makefile, [opts.gmake],
|
|
|
|
makefile + '.gmakelog', gmakeoptions)
|
|
|
|
|
|
|
|
if gmakeoptions['pass']:
|
|
|
|
if not gmakepass:
|
|
|
|
gmakefails += 1
|
|
|
|
else:
|
|
|
|
if gmakepass:
|
|
|
|
gmakefails += 1
|
|
|
|
gmakemsg = "UNEXPECTED PASS"
|
2009-03-31 14:10:23 -07:00
|
|
|
else:
|
2009-10-23 08:16:27 -07:00
|
|
|
gmakemsg = "KNOWN FAIL"
|
|
|
|
|
|
|
|
if pymakeoptions['skip']:
|
|
|
|
pymakepass, pymakemsg = True, ''
|
2009-03-19 07:19:38 -07:00
|
|
|
else:
|
2009-10-23 08:16:27 -07:00
|
|
|
pymakepass, pymakemsg = runTest(makefile, pymake,
|
|
|
|
makefile + '.pymakelog', pymakeoptions)
|
|
|
|
|
|
|
|
if pymakeoptions['pass']:
|
|
|
|
if not pymakepass:
|
|
|
|
pymakefails += 1
|
|
|
|
else:
|
|
|
|
if pymakepass:
|
|
|
|
pymakefails += 1
|
|
|
|
pymakemsg = "UNEXPECTED PASS"
|
|
|
|
else:
|
|
|
|
pymakemsg = "OK (known fail)"
|
|
|
|
|
|
|
|
print "%-30.30s%-28.28s%-28.28s" % (os.path.basename(makefile),
|
|
|
|
gmakemsg, pymakemsg)
|
|
|
|
|
|
|
|
print
|
|
|
|
print "Summary:"
|
|
|
|
print "%-30s%-28s%-28s" % ("", "gmake:", "pymake:")
|
|
|
|
|
|
|
|
if gmakefails == 0:
|
|
|
|
gmakemsg = 'PASS'
|
|
|
|
else:
|
|
|
|
gmakemsg = 'FAIL (%i failures)' % gmakefails
|
|
|
|
|
|
|
|
if pymakefails == 0:
|
|
|
|
pymakemsg = 'PASS'
|
|
|
|
else:
|
|
|
|
pymakemsg = 'FAIL (%i failures)' % pymakefails
|
|
|
|
|
|
|
|
print "%-30.30s%-28.28s%-28.28s" % ('', gmakemsg, pymakemsg)
|
2009-03-19 07:19:38 -07:00
|
|
|
|
|
|
|
shutil.rmtree(opts.tempdir)
|
2009-10-23 08:16:27 -07:00
|
|
|
|
|
|
|
if gmakefails or pymakefails:
|
|
|
|
sys.exit(1)
|