2018-08-28 08:28:46 +00:00
|
|
|
#!/usr/bin/python3
|
|
|
|
# This can also be run with Python 2.
|
2014-03-04 07:20:53 +00:00
|
|
|
|
|
|
|
# Copyright (C) 2014 Red Hat, Inc.
|
|
|
|
#
|
|
|
|
# Cockpit is free software; you can redistribute it and/or modify it
|
|
|
|
# under the terms of the GNU Lesser General Public License as published by
|
|
|
|
# the Free Software Foundation; either version 2.1 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# Cockpit is distributed in the hope that it will be useful, but
|
|
|
|
# WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
# Lesser General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Lesser General Public License
|
|
|
|
# along with Cockpit; If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
#
|
|
|
|
# This is a test output compiler which produces TAP from GTest output
|
|
|
|
# if GTest output is detected.
|
|
|
|
#
|
|
|
|
# Versions of glib later than 2.38.x output TAP natively when tests are
|
|
|
|
# run with the --tap option. However we can't depend on such a recent
|
|
|
|
# version of glib for our purposes.
|
|
|
|
#
|
|
|
|
# This implements the Test Anything Protocol (ie: TAP)
|
|
|
|
# https://metacpan.org/pod/release/PETDANCE/Test-Harness-2.64/lib/Test/Harness/TAP.pod
|
|
|
|
#
|
|
|
|
|
|
|
|
import argparse
|
|
|
|
import os
|
|
|
|
import select
|
2018-08-28 08:28:46 +00:00
|
|
|
import signal
|
2014-03-04 07:20:53 +00:00
|
|
|
import subprocess
|
|
|
|
import sys
|
|
|
|
|
2018-08-28 08:28:46 +00:00
|
|
|
# Yes, it's dumb, but strsignal is not exposed in python
|
|
|
|
# In addition signal numbers varify heavily from arch to arch
|
|
|
|
def strsignal(sig):
|
|
|
|
for name in dir(signal):
|
|
|
|
if name.startswith("SIG") and sig == getattr(signal, name):
|
|
|
|
return name
|
|
|
|
return str(sig)
|
|
|
|
|
|
|
|
|
2014-03-04 07:20:53 +00:00
|
|
|
class NullCompiler:
|
|
|
|
def __init__(self, command):
|
|
|
|
self.command = command
|
|
|
|
|
|
|
|
def input(self, line):
|
|
|
|
sys.stdout.write(line)
|
|
|
|
|
|
|
|
def process(self, proc):
|
|
|
|
while True:
|
2018-08-28 08:28:46 +00:00
|
|
|
line = proc.stdout.readline()
|
2014-03-04 07:20:53 +00:00
|
|
|
if not line:
|
|
|
|
break
|
|
|
|
self.input(line)
|
|
|
|
proc.wait()
|
|
|
|
return proc.returncode
|
|
|
|
|
|
|
|
def run(self, proc, line=None):
|
|
|
|
if line:
|
|
|
|
self.input(line)
|
|
|
|
return self.process(proc)
|
|
|
|
|
|
|
|
|
|
|
|
class GTestCompiler(NullCompiler):
|
|
|
|
def __init__(self, filename):
|
|
|
|
NullCompiler.__init__(self, filename)
|
|
|
|
self.test_num = 0
|
|
|
|
self.test_name = None
|
|
|
|
self.test_remaining = []
|
|
|
|
|
|
|
|
def input(self, line):
|
|
|
|
line = line.strip()
|
|
|
|
if line.startswith("GTest: "):
|
|
|
|
(cmd, unused, data) = line[7:].partition(": ")
|
|
|
|
cmd = cmd.strip()
|
|
|
|
data = data.strip()
|
|
|
|
if cmd == "run":
|
|
|
|
self.test_name = data
|
|
|
|
assert self.test_name in self.test_remaining, "%s %s" % (self.test_name, repr(self.test_remaining))
|
|
|
|
self.test_remaining.remove(self.test_name)
|
|
|
|
self.test_num += 1
|
|
|
|
elif cmd == "result":
|
2018-08-28 08:28:46 +00:00
|
|
|
if self.test_name:
|
|
|
|
if data == "OK":
|
|
|
|
print("ok %d %s" % (self.test_num, self.test_name))
|
|
|
|
if data == "FAIL":
|
|
|
|
print("not ok %d %s" % (self.test_num, self.test_name))
|
|
|
|
if data == "SKIP":
|
|
|
|
print("ok %d %s # skip" % (self.test_num, self.test_name))
|
2014-03-04 07:20:53 +00:00
|
|
|
self.test_name = None
|
|
|
|
elif cmd == "skipping":
|
2014-03-05 10:39:42 +00:00
|
|
|
if "/subprocess" not in data:
|
2016-04-06 08:37:38 +00:00
|
|
|
print("ok %d # skip -- %s" % (self.test_num, data))
|
2014-03-04 07:20:53 +00:00
|
|
|
self.test_name = None
|
|
|
|
elif data:
|
2016-04-06 08:37:38 +00:00
|
|
|
print("# %s: %s" % (cmd, data))
|
2014-03-04 07:20:53 +00:00
|
|
|
else:
|
2016-04-06 08:37:38 +00:00
|
|
|
print("# %s" % cmd)
|
2014-03-04 07:20:53 +00:00
|
|
|
elif line.startswith("(MSG: "):
|
2016-04-06 08:37:38 +00:00
|
|
|
print("# %s" % line[6:-1])
|
2014-03-04 07:20:53 +00:00
|
|
|
elif line:
|
2016-04-06 08:37:38 +00:00
|
|
|
print("# %s" % line)
|
2014-03-04 07:20:53 +00:00
|
|
|
sys.stdout.flush()
|
|
|
|
|
|
|
|
def run(self, proc, output=""):
|
|
|
|
# Complete retrieval of the list of tests
|
2018-08-28 08:28:46 +00:00
|
|
|
output += proc.stdout.read()
|
2014-03-04 07:20:53 +00:00
|
|
|
proc.wait()
|
|
|
|
if proc.returncode:
|
2014-03-05 10:39:42 +00:00
|
|
|
sys.stderr.write("tap-gtester: listing GTest tests failed: %d\n" % proc.returncode)
|
|
|
|
return proc.returncode
|
2014-03-04 07:20:53 +00:00
|
|
|
self.test_remaining = []
|
|
|
|
for line in output.split("\n"):
|
|
|
|
if line.startswith("/"):
|
|
|
|
self.test_remaining.append(line.strip())
|
|
|
|
if not self.test_remaining:
|
2016-04-06 08:37:38 +00:00
|
|
|
print("Bail out! No tests found in GTest: %s" % self.command[0])
|
2014-03-04 07:20:53 +00:00
|
|
|
return 0
|
|
|
|
|
2016-04-06 08:37:38 +00:00
|
|
|
print("1..%d" % len(self.test_remaining))
|
2014-03-04 07:20:53 +00:00
|
|
|
|
|
|
|
# First try to run all the tests in a batch
|
2018-08-28 08:28:46 +00:00
|
|
|
proc = subprocess.Popen(self.command + ["--verbose" ], close_fds=True,
|
|
|
|
stdout=subprocess.PIPE, universal_newlines=True)
|
2014-03-04 07:20:53 +00:00
|
|
|
result = self.process(proc)
|
|
|
|
if result == 0:
|
|
|
|
return 0
|
|
|
|
|
2018-08-28 08:28:46 +00:00
|
|
|
if result < 0:
|
|
|
|
sys.stderr.write("%s terminated with %s\n" % (self.command[0], strsignal(-result)))
|
|
|
|
|
2014-03-04 07:20:53 +00:00
|
|
|
# Now pick up any stragglers due to failures
|
|
|
|
while True:
|
|
|
|
# Assume that the last test failed
|
|
|
|
if self.test_name:
|
2016-04-06 08:37:38 +00:00
|
|
|
print("not ok %d %s" % (self.test_num, self.test_name))
|
2014-03-04 07:20:53 +00:00
|
|
|
self.test_name = None
|
|
|
|
|
|
|
|
# Run any tests which didn't get run
|
|
|
|
if not self.test_remaining:
|
|
|
|
break
|
|
|
|
|
|
|
|
proc = subprocess.Popen(self.command + ["--verbose", "-p", self.test_remaining[0]],
|
2018-08-28 08:28:46 +00:00
|
|
|
close_fds=True, stdout=subprocess.PIPE,
|
|
|
|
universal_newlines=True)
|
2014-03-04 07:20:53 +00:00
|
|
|
result = self.process(proc)
|
|
|
|
|
|
|
|
# The various exit codes and signals we continue for
|
2014-03-05 10:39:42 +00:00
|
|
|
if result not in [ 0, 1, -4, -5, -6, -7, -8, -11, 33 ]:
|
2014-03-04 07:20:53 +00:00
|
|
|
break
|
|
|
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
def main(argv):
|
2018-08-28 08:28:46 +00:00
|
|
|
parser = argparse.ArgumentParser(description='Automake TAP compiler',
|
|
|
|
usage="tap-gtester [--format FORMAT] command ...")
|
2014-03-05 09:12:00 +00:00
|
|
|
parser.add_argument('--format', metavar='FORMAT', choices=[ "auto", "gtest", "tap" ],
|
2014-03-04 07:20:53 +00:00
|
|
|
default="auto", help='The input format to compile')
|
|
|
|
parser.add_argument('--verbose', action='store_true',
|
|
|
|
default=True, help='Verbose mode (ignored)')
|
2018-08-28 08:28:46 +00:00
|
|
|
parser.add_argument('command', nargs=argparse.REMAINDER, help="A test command to run")
|
2014-03-04 07:20:53 +00:00
|
|
|
args = parser.parse_args(argv[1:])
|
|
|
|
|
|
|
|
output = None
|
|
|
|
format = args.format
|
|
|
|
cmd = args.command
|
2018-08-28 08:28:46 +00:00
|
|
|
if not cmd:
|
|
|
|
sys.stderr.write("tap-gtester: specify a command to run\n")
|
|
|
|
return 2
|
|
|
|
if cmd[0] == '--':
|
|
|
|
cmd.pop(0)
|
|
|
|
|
2014-03-04 07:20:53 +00:00
|
|
|
proc = None
|
|
|
|
|
2014-03-06 11:50:46 +00:00
|
|
|
os.environ['HARNESS_ACTIVE'] = '1'
|
|
|
|
|
2014-03-05 09:12:00 +00:00
|
|
|
if format in ["auto", "gtest"]:
|
2014-03-04 07:20:53 +00:00
|
|
|
list_cmd = cmd + ["-l", "--verbose"]
|
2018-08-28 08:28:46 +00:00
|
|
|
proc = subprocess.Popen(list_cmd, close_fds=True, stdout=subprocess.PIPE,
|
|
|
|
universal_newlines=True)
|
|
|
|
output = proc.stdout.readline()
|
2014-03-04 07:20:53 +00:00
|
|
|
# Smell whether we're dealing with GTest list output from first line
|
|
|
|
if "random seed" in output or "GTest" in output or output.startswith("/"):
|
2014-03-05 09:12:00 +00:00
|
|
|
format = "gtest"
|
2014-03-04 07:20:53 +00:00
|
|
|
else:
|
2014-03-05 09:12:00 +00:00
|
|
|
format = "tap"
|
2014-03-04 07:20:53 +00:00
|
|
|
else:
|
2018-08-28 08:28:46 +00:00
|
|
|
proc = subprocess.Popen(cmd, close_fds=True, stdout=subprocess.PIPE,
|
|
|
|
universal_newlines=True)
|
2014-03-04 07:20:53 +00:00
|
|
|
|
2014-03-05 09:12:00 +00:00
|
|
|
if format == "gtest":
|
2014-03-04 07:20:53 +00:00
|
|
|
compiler = GTestCompiler(cmd)
|
2014-03-05 09:12:00 +00:00
|
|
|
elif format == "tap":
|
2014-03-04 07:20:53 +00:00
|
|
|
compiler = NullCompiler(cmd)
|
|
|
|
else:
|
|
|
|
assert False, "not reached"
|
|
|
|
|
|
|
|
return compiler.run(proc, output)
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
sys.exit(main(sys.argv))
|