Merge branch 'tap-python3' into 'master'

build: update tap scripts

See merge request GNOME/libsecret!4
This commit is contained in:
Daiki Ueno 2018-08-28 09:43:30 +00:00
commit 5ce2540785
2 changed files with 139 additions and 43 deletions

View File

@ -1,4 +1,5 @@
#!/usr/bin/env python3 #!/usr/bin/python3
# This can also be run with Python 2.
# Copyright (C) 2013 Red Hat, Inc. # Copyright (C) 2013 Red Hat, Inc.
# #
@ -29,21 +30,59 @@
# #
import argparse import argparse
import fcntl
import os import os
import select import select
import struct
import subprocess import subprocess
import sys import sys
import termios
import errno
_PY3 = sys.version[0] >= '3'
_str = _PY3 and str or unicode
def out(data, stream=None, flush=False):
if not isinstance(data, bytes):
data = data.encode("UTF-8")
if not stream:
stream = _PY3 and sys.stdout.buffer or sys.stdout
while True:
try:
if data:
stream.write(data)
data = None
if flush:
stream.flush()
flush = False
break
except IOError as e:
if e.errno == errno.EAGAIN:
continue
raise
def terminal_width():
try:
h, w, hp, wp = struct.unpack('HHHH',
fcntl.ioctl(1, termios.TIOCGWINSZ,
struct.pack('HHHH', 0, 0, 0, 0)))
return w
except IOError as e:
if e.errno != errno.ENOTTY:
sys.stderr.write("%i %s %s\n" % (e.errno, e.strerror, sys.exc_info()))
return sys.maxsize
class Driver: class Driver:
def __init__(self, args): def __init__(self, args):
self.argv = args.command self.argv = args.command
self.test_name = args.test_name self.test_name = args.test_name
self.log = open(args.log_file, "w") self.log = open(args.log_file, "wb", 0)
self.log.write("# %s\n" % " ".join(sys.argv)) self.log.write(("# %s\n" % " ".join(sys.argv)).encode("UTF-8"))
self.trs = open(args.trs_file, "w") self.trs = open(args.trs_file, "w", 1)
self.color_tests = args.color_tests self.color_tests = args.color_tests
self.expect_failure = args.expect_failure self.expect_failure = args.expect_failure
self.enable_hard_errors = args.enable_hard_errors self.enable_hard_errors = args.enable_hard_errors
self.width = terminal_width() - 9
def report(self, code, *args): def report(self, code, *args):
CODES = { CODES = {
@ -58,17 +97,18 @@ class Driver:
# Print out to console # Print out to console
if self.color_tests: if self.color_tests:
if code in CODES: if code in CODES:
sys.stdout.write(CODES[code]) out(CODES[code])
sys.stdout.write(code) out(code)
if self.color_tests: if self.color_tests:
sys.stdout.write('\x1b[m') out('\x1b[m')
sys.stdout.write(": ") out(": ")
sys.stdout.write(self.test_name) msg = "".join([ self.test_name + " " ] + list(map(_str, args)))
sys.stdout.write(" ") if code == "PASS" and len(msg) > self.width:
for arg in args: out(msg[:self.width])
sys.stdout.write(str(arg)) out("...")
sys.stdout.write("\n") else:
sys.stdout.flush() out(msg)
out("\n", flush=True)
# Book keeping # Book keeping
if code in CODES: if code in CODES:
@ -104,31 +144,40 @@ class Driver:
def execute(self): def execute(self):
try: try:
proc = subprocess.Popen(self.argv, close_fds=True, proc = subprocess.Popen(self.argv, close_fds=True,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.PIPE) stderr=subprocess.PIPE)
except OSError as ex: except OSError as ex:
self.report_error("Couldn't run %s: %s" % (self.argv[0], str(ex))) self.report_error("Couldn't run %s: %s" % (self.argv[0], str(ex)))
return return
proc.stdin.close()
outf = proc.stdout.fileno() outf = proc.stdout.fileno()
errf = proc.stderr.fileno() errf = proc.stderr.fileno()
rset = [outf, errf] rset = [outf, errf]
while len(rset) > 0: while len(rset) > 0:
ret = select.select(rset, [], [], 10) ret = select.select(rset, [], [], 10)
if outf in ret[0]: if outf in ret[0]:
data = os.read(outf, 1024).decode("utf-8") data = os.read(outf, 1024)
if data == "": if data == b"":
rset.remove(outf) rset.remove(outf)
self.log.write(data) self.log.write(data)
self.process(data) self.process(data)
if errf in ret[0]: if errf in ret[0]:
data = os.read(errf, 1024).decode("utf-8") data = os.read(errf, 1024)
if data == "": if data == b"":
rset.remove(errf) rset.remove(errf)
self.log.write(data) self.log.write(data)
sys.stderr.write(data) stream = _PY3 and sys.stderr.buffer or sys.stderr
out(data, stream=stream, flush=True)
proc.wait() proc.wait()
# Make sure the test didn't change blocking output
assert fcntl.fcntl(0, fcntl.F_GETFL) & os.O_NONBLOCK == 0
assert fcntl.fcntl(1, fcntl.F_GETFL) & os.O_NONBLOCK == 0
assert fcntl.fcntl(2, fcntl.F_GETFL) & os.O_NONBLOCK == 0
return proc.returncode return proc.returncode
@ -141,6 +190,7 @@ class TapDriver(Driver):
self.late_plan = False self.late_plan = False
self.errored = False self.errored = False
self.bail_out = False self.bail_out = False
self.skip_all_reason = None
def report(self, code, num, *args): def report(self, code, num, *args):
if num: if num:
@ -166,21 +216,30 @@ class TapDriver(Driver):
return return
description = description.lstrip() description = description.lstrip()
# Special case if description starts with this, then skip # Parse out a directive from description, if any
if description.lower().startswith("# skip"): (description, unused, directive) = description.partition("#")
# Special case if directive starts with this, then skip
if directive.lstrip().lower().startswith("skip"):
self.result_skip(num, description) self.result_skip(num, description)
elif ok: elif ok:
self.result_pass(num, description) self.result_pass(num, description)
else: else:
self.result_fail(num, description) self.result_fail(num, description)
def consume_test_plan(self, first, last): def consume_test_plan(self, line):
# Only one test plan is supported # Only one test plan is supported
if self.test_plan: if self.test_plan:
self.report_error("Get a second TAP test plan") self.report_error("Get a second TAP test plan")
return return
if line.lower().startswith('1..0 # skip'):
self.skip_all_reason = line[5:].strip()
self.bail_out = True
return
try: try:
(first, unused, last) = line.partition("..")
first = int(first) first = int(first)
last = int(last) last = int(last)
except ValueError: except ValueError:
@ -196,7 +255,7 @@ class TapDriver(Driver):
def process(self, output): def process(self, output):
if output: if output:
self.output += output self.output += output.decode("UTF-8")
elif self.output: elif self.output:
self.output += "\n" self.output += "\n"
(ready, unused, self.output) = self.output.rpartition("\n") (ready, unused, self.output) = self.output.rpartition("\n")
@ -206,8 +265,7 @@ class TapDriver(Driver):
elif line.startswith("not ok "): elif line.startswith("not ok "):
self.consume_test_line(False, line[7:]) self.consume_test_line(False, line[7:])
elif line and line[0].isdigit() and ".." in line: elif line and line[0].isdigit() and ".." in line:
(first, unused, last) = line.partition("..") self.consume_test_plan(line)
self.consume_test_plan(first, last)
elif line.lower().startswith("bail out!"): elif line.lower().startswith("bail out!"):
self.consume_bail_out(line) self.consume_bail_out(line)
@ -217,6 +275,13 @@ class TapDriver(Driver):
failed = False failed = False
skipped = True skipped = True
if self.skip_all_reason is not None:
self.result_skip("skipping:", self.skip_all_reason)
self.trs.write(":global-test-result: SKIP\n")
self.trs.write(":test-global-result: SKIP\n")
self.trs.write(":recheck: no\n")
return 0
# Basic collation of results # Basic collation of results
for (num, code) in self.reported.items(): for (num, code) in self.reported.items():
if code == "ERROR": if code == "ERROR":
@ -226,7 +291,10 @@ class TapDriver(Driver):
if code != "SKIP": if code != "SKIP":
skipped = False skipped = False
if not self.errored and returncode: if not self.errored:
if returncode == 77:
skipped = True
elif returncode:
self.report_error("process failed: %d" % returncode) self.report_error("process failed: %d" % returncode)
self.errored = True self.errored = True
@ -328,7 +396,7 @@ class YesNoAction(argparse.Action):
def main(argv): def main(argv):
parser = argparse.ArgumentParser(description='Automake TAP driver') parser = argparse.ArgumentParser(description='Automake TAP driver')
parser.add_argument('--format', metavar='FORMAT', choices=[ "simple", "tap" ], parser.add_argument('--format', metavar='FORMAT', choices=[ "simple", "tap" ],
default="simple", help='The type of test to drive') default="tap", help='The type of test to drive')
parser.add_argument('--missing', metavar="TOOL", nargs='?', parser.add_argument('--missing', metavar="TOOL", nargs='?',
help="Force the test to skip due to missing tool") help="Force the test to skip due to missing tool")
parser.add_argument('--test-name', metavar='NAME', parser.add_argument('--test-name', metavar='NAME',

View File

@ -1,4 +1,5 @@
#!/usr/bin/env python3 #!/usr/bin/python3
# This can also be run with Python 2.
# Copyright (C) 2014 Red Hat, Inc. # Copyright (C) 2014 Red Hat, Inc.
# #
@ -30,9 +31,19 @@
import argparse import argparse
import os import os
import select import select
import signal
import subprocess import subprocess
import sys import sys
# 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)
class NullCompiler: class NullCompiler:
def __init__(self, command): def __init__(self, command):
self.command = command self.command = command
@ -42,7 +53,7 @@ class NullCompiler:
def process(self, proc): def process(self, proc):
while True: while True:
line = proc.stdout.readline().decode("utf-8") line = proc.stdout.readline()
if not line: if not line:
break break
self.input(line) self.input(line)
@ -74,10 +85,13 @@ class GTestCompiler(NullCompiler):
self.test_remaining.remove(self.test_name) self.test_remaining.remove(self.test_name)
self.test_num += 1 self.test_num += 1
elif cmd == "result": elif cmd == "result":
if self.test_name:
if data == "OK": if data == "OK":
print("ok %d %s" % (self.test_num, self.test_name)) print("ok %d %s" % (self.test_num, self.test_name))
if data == "FAIL": if data == "FAIL":
print("not ok %d %s", (self.test_num, self.test_name)) 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))
self.test_name = None self.test_name = None
elif cmd == "skipping": elif cmd == "skipping":
if "/subprocess" not in data: if "/subprocess" not in data:
@ -95,7 +109,7 @@ class GTestCompiler(NullCompiler):
def run(self, proc, output=""): def run(self, proc, output=""):
# Complete retrieval of the list of tests # Complete retrieval of the list of tests
output += proc.stdout.read().decode("utf-8") output += proc.stdout.read()
proc.wait() proc.wait()
if proc.returncode: if proc.returncode:
sys.stderr.write("tap-gtester: listing GTest tests failed: %d\n" % proc.returncode) sys.stderr.write("tap-gtester: listing GTest tests failed: %d\n" % proc.returncode)
@ -111,11 +125,15 @@ class GTestCompiler(NullCompiler):
print("1..%d" % len(self.test_remaining)) print("1..%d" % len(self.test_remaining))
# First try to run all the tests in a batch # First try to run all the tests in a batch
proc = subprocess.Popen(self.command + ["--verbose" ], close_fds=True, stdout=subprocess.PIPE) proc = subprocess.Popen(self.command + ["--verbose" ], close_fds=True,
stdout=subprocess.PIPE, universal_newlines=True)
result = self.process(proc) result = self.process(proc)
if result == 0: if result == 0:
return 0 return 0
if result < 0:
sys.stderr.write("%s terminated with %s\n" % (self.command[0], strsignal(-result)))
# Now pick up any stragglers due to failures # Now pick up any stragglers due to failures
while True: while True:
# Assume that the last test failed # Assume that the last test failed
@ -128,7 +146,8 @@ class GTestCompiler(NullCompiler):
break break
proc = subprocess.Popen(self.command + ["--verbose", "-p", self.test_remaining[0]], proc = subprocess.Popen(self.command + ["--verbose", "-p", self.test_remaining[0]],
close_fds=True, stdout=subprocess.PIPE) close_fds=True, stdout=subprocess.PIPE,
universal_newlines=True)
result = self.process(proc) result = self.process(proc)
# The various exit codes and signals we continue for # The various exit codes and signals we continue for
@ -138,32 +157,41 @@ class GTestCompiler(NullCompiler):
return result return result
def main(argv): def main(argv):
parser = argparse.ArgumentParser(description='Automake TAP compiler') parser = argparse.ArgumentParser(description='Automake TAP compiler',
usage="tap-gtester [--format FORMAT] command ...")
parser.add_argument('--format', metavar='FORMAT', choices=[ "auto", "gtest", "tap" ], parser.add_argument('--format', metavar='FORMAT', choices=[ "auto", "gtest", "tap" ],
default="auto", help='The input format to compile') default="auto", help='The input format to compile')
parser.add_argument('--verbose', action='store_true', parser.add_argument('--verbose', action='store_true',
default=True, help='Verbose mode (ignored)') default=True, help='Verbose mode (ignored)')
parser.add_argument('command', nargs='+', help="A test command to run") parser.add_argument('command', nargs=argparse.REMAINDER, help="A test command to run")
args = parser.parse_args(argv[1:]) args = parser.parse_args(argv[1:])
output = None output = None
format = args.format format = args.format
cmd = args.command cmd = args.command
if not cmd:
sys.stderr.write("tap-gtester: specify a command to run\n")
return 2
if cmd[0] == '--':
cmd.pop(0)
proc = None proc = None
os.environ['HARNESS_ACTIVE'] = '1' os.environ['HARNESS_ACTIVE'] = '1'
if format in ["auto", "gtest"]: if format in ["auto", "gtest"]:
list_cmd = cmd + ["-l", "--verbose"] list_cmd = cmd + ["-l", "--verbose"]
proc = subprocess.Popen(list_cmd, close_fds=True, stdout=subprocess.PIPE) proc = subprocess.Popen(list_cmd, close_fds=True, stdout=subprocess.PIPE,
output = proc.stdout.readline().decode("utf-8") universal_newlines=True)
output = proc.stdout.readline()
# Smell whether we're dealing with GTest list output from first line # Smell whether we're dealing with GTest list output from first line
if "random seed" in output or "GTest" in output or output.startswith("/"): if "random seed" in output or "GTest" in output or output.startswith("/"):
format = "gtest" format = "gtest"
else: else:
format = "tap" format = "tap"
else: else:
proc = subprocess.Popen(cmd, close_fds=True, stdout=subprocess.PIPE) proc = subprocess.Popen(cmd, close_fds=True, stdout=subprocess.PIPE,
universal_newlines=True)
if format == "gtest": if format == "gtest":
compiler = GTestCompiler(cmd) compiler = GTestCompiler(cmd)