2019-06-08 20:43:50 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
""" Command line test driver. """
|
|
|
|
|
|
|
|
from __future__ import unicode_literals
|
2020-01-25 14:14:47 +00:00
|
|
|
from __future__ import print_function
|
2019-06-08 20:43:50 +00:00
|
|
|
|
|
|
|
import argparse
|
2020-03-15 23:43:06 +00:00
|
|
|
import datetime
|
2019-06-08 20:43:50 +00:00
|
|
|
import io
|
|
|
|
import re
|
|
|
|
import shlex
|
|
|
|
import subprocess
|
|
|
|
import sys
|
2020-11-22 13:39:48 +00:00
|
|
|
|
2020-11-22 10:24:41 +00:00
|
|
|
try:
|
|
|
|
from itertools import zip_longest
|
|
|
|
except ImportError:
|
|
|
|
from itertools import izip_longest as zip_longest
|
|
|
|
from difflib import SequenceMatcher
|
2019-06-08 20:43:50 +00:00
|
|
|
|
2020-09-26 09:52:00 +00:00
|
|
|
# Directives can occur at the beginning of a line, or anywhere in a line that does not start with #.
|
2020-11-22 13:39:48 +00:00
|
|
|
COMMENT_RE = r"^(?:[^#].*)?#\s*"
|
2020-09-26 09:52:00 +00:00
|
|
|
|
2019-06-08 20:43:50 +00:00
|
|
|
# A regex showing how to run the file.
|
2020-09-26 09:52:00 +00:00
|
|
|
RUN_RE = re.compile(COMMENT_RE + r"RUN:\s+(.*)\n")
|
2021-01-16 12:26:01 +00:00
|
|
|
REQUIRES_RE = re.compile(COMMENT_RE + r"REQUIRES:\s+(.*)\n")
|
2019-06-08 20:43:50 +00:00
|
|
|
|
|
|
|
# A regex capturing lines that should be checked against stdout.
|
2020-09-26 09:52:00 +00:00
|
|
|
CHECK_STDOUT_RE = re.compile(COMMENT_RE + r"CHECK:\s+(.*)\n")
|
2019-06-08 20:43:50 +00:00
|
|
|
|
|
|
|
# A regex capturing lines that should be checked against stderr.
|
2020-09-26 09:52:00 +00:00
|
|
|
CHECK_STDERR_RE = re.compile(COMMENT_RE + r"CHECKERR:\s+(.*)\n")
|
2019-06-08 20:43:50 +00:00
|
|
|
|
2021-01-16 12:26:01 +00:00
|
|
|
SKIP = object()
|
2019-06-08 20:43:50 +00:00
|
|
|
|
2021-02-21 09:54:05 +00:00
|
|
|
def find_command(program):
|
|
|
|
import os
|
|
|
|
|
|
|
|
path, name = os.path.split(program)
|
|
|
|
if path:
|
|
|
|
return os.path.isfile(program) and os.access(program, os.X_OK)
|
|
|
|
for path in os.environ["PATH"].split(os.pathsep):
|
|
|
|
exe = os.path.join(path, program)
|
|
|
|
if os.path.isfile(exe) and os.access(exe, os.X_OK):
|
|
|
|
return exe
|
|
|
|
|
|
|
|
return None
|
|
|
|
|
2019-06-08 20:43:50 +00:00
|
|
|
class Config(object):
|
|
|
|
def __init__(self):
|
|
|
|
# Whether to have verbose output.
|
|
|
|
self.verbose = False
|
|
|
|
# Whether output gets ANSI colorization.
|
|
|
|
self.colorize = False
|
2020-01-25 10:37:10 +00:00
|
|
|
# Whether to show which file was tested.
|
|
|
|
self.progress = False
|
2019-06-08 20:43:50 +00:00
|
|
|
|
|
|
|
def colors(self):
|
|
|
|
""" Return a dictionary mapping color names to ANSI escapes """
|
|
|
|
|
|
|
|
def ansic(n):
|
|
|
|
return "\033[%dm" % n if self.colorize else ""
|
|
|
|
|
|
|
|
return {
|
|
|
|
"RESET": ansic(0),
|
|
|
|
"BOLD": ansic(1),
|
|
|
|
"NORMAL": ansic(39),
|
|
|
|
"BLACK": ansic(30),
|
|
|
|
"RED": ansic(31),
|
|
|
|
"GREEN": ansic(32),
|
|
|
|
"YELLOW": ansic(33),
|
|
|
|
"BLUE": ansic(34),
|
|
|
|
"MAGENTA": ansic(35),
|
|
|
|
"CYAN": ansic(36),
|
|
|
|
"LIGHTGRAY": ansic(37),
|
|
|
|
"DARKGRAY": ansic(90),
|
|
|
|
"LIGHTRED": ansic(91),
|
|
|
|
"LIGHTGREEN": ansic(92),
|
|
|
|
"LIGHTYELLOW": ansic(93),
|
|
|
|
"LIGHTBLUE": ansic(94),
|
|
|
|
"LIGHTMAGENTA": ansic(95),
|
|
|
|
"LIGHTCYAN": ansic(96),
|
|
|
|
"WHITE": ansic(97),
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
def output(*args):
|
|
|
|
print("".join(args) + "\n")
|
|
|
|
|
|
|
|
|
2020-04-26 11:51:34 +00:00
|
|
|
import unicodedata
|
|
|
|
|
|
|
|
|
|
|
|
def esc(m):
|
|
|
|
map = {
|
|
|
|
"\n": "\\n",
|
|
|
|
"\\": "\\\\",
|
|
|
|
"'": "\\'",
|
|
|
|
'"': '\\"',
|
|
|
|
"\a": "\\a",
|
|
|
|
"\b": "\\b",
|
|
|
|
"\f": "\\f",
|
|
|
|
"\r": "\\r",
|
|
|
|
"\t": "\\t",
|
|
|
|
"\v": "\\v",
|
|
|
|
}
|
|
|
|
if m in map:
|
2020-05-16 13:15:48 +00:00
|
|
|
return map[m]
|
2020-04-26 11:51:34 +00:00
|
|
|
if unicodedata.category(m)[0] == "C":
|
|
|
|
return "\\x{:02x}".format(ord(m))
|
|
|
|
else:
|
|
|
|
return m
|
|
|
|
|
|
|
|
|
|
|
|
def escape_string(s):
|
|
|
|
return "".join(esc(ch) for ch in s)
|
|
|
|
|
|
|
|
|
2019-06-08 20:43:50 +00:00
|
|
|
class CheckerError(Exception):
|
|
|
|
"""Exception subclass for check line parsing.
|
|
|
|
|
|
|
|
Attributes:
|
|
|
|
line: the Line object on which the exception occurred.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, message, line=None):
|
|
|
|
super(CheckerError, self).__init__(message)
|
|
|
|
self.line = line
|
|
|
|
|
|
|
|
|
|
|
|
class Line(object):
|
|
|
|
""" A line that remembers where it came from. """
|
|
|
|
|
|
|
|
def __init__(self, text, number, file):
|
|
|
|
self.text = text
|
|
|
|
self.number = number
|
|
|
|
self.file = file
|
|
|
|
|
2020-11-25 16:23:29 +00:00
|
|
|
def __hash__(self):
|
|
|
|
# Chosen by fair diceroll
|
|
|
|
# No, just kidding.
|
|
|
|
# HACK: We pass this to the Sequencematcher, which puts the Checks into a dict.
|
|
|
|
# To force it to match the regexes, we return a hash collision intentionally,
|
|
|
|
# so it falls back on __eq__().
|
|
|
|
#
|
|
|
|
# CheckCmd has the same thing.
|
|
|
|
return 0
|
|
|
|
|
|
|
|
def __eq__(self, other):
|
2020-11-30 17:16:42 +00:00
|
|
|
if other is None:
|
|
|
|
return False
|
2020-11-25 16:23:29 +00:00
|
|
|
if isinstance(other, CheckCmd):
|
|
|
|
return other.regex.match(self.text)
|
|
|
|
if isinstance(other, Line):
|
2020-11-30 17:16:42 +00:00
|
|
|
# We only compare the text here so SequenceMatcher can reshuffle these
|
|
|
|
return self.text == other.text
|
2020-11-25 16:23:29 +00:00
|
|
|
raise NotImplementedError
|
|
|
|
|
2019-06-08 20:43:50 +00:00
|
|
|
def subline(self, text):
|
|
|
|
""" Return a substring of our line with the given text, preserving number and file. """
|
|
|
|
return Line(text, self.number, self.file)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def readfile(file, name):
|
|
|
|
return [Line(text, idx + 1, name) for idx, text in enumerate(file)]
|
|
|
|
|
|
|
|
def is_empty_space(self):
|
|
|
|
return not self.text or self.text.isspace()
|
|
|
|
|
2020-11-22 10:24:41 +00:00
|
|
|
def escaped_text(self, for_formatting=False):
|
|
|
|
ret = escape_string(self.text.rstrip("\n"))
|
|
|
|
if for_formatting:
|
|
|
|
ret = ret.replace("{", "{{").replace("}", "}}")
|
|
|
|
return ret
|
2019-06-08 20:43:50 +00:00
|
|
|
|
2020-11-22 13:39:48 +00:00
|
|
|
|
2019-06-08 20:43:50 +00:00
|
|
|
class RunCmd(object):
|
2020-11-22 13:39:48 +00:00
|
|
|
"""A command to run on a given Checker.
|
|
|
|
|
2019-06-08 20:43:50 +00:00
|
|
|
Attributes:
|
|
|
|
args: Unexpanded shell command as a string.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, args, line):
|
|
|
|
self.args = args
|
|
|
|
self.line = line
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def parse(line):
|
|
|
|
if not shlex.split(line.text):
|
|
|
|
raise CheckerError("Invalid RUN command", line)
|
|
|
|
return RunCmd(line.text, line)
|
|
|
|
|
|
|
|
|
|
|
|
class TestFailure(object):
|
2020-11-22 10:24:41 +00:00
|
|
|
def __init__(self, line, check, testrun, diff=None, lines=[], checks=[]):
|
2019-06-08 20:43:50 +00:00
|
|
|
self.line = line
|
|
|
|
self.check = check
|
|
|
|
self.testrun = testrun
|
2020-11-14 12:15:33 +00:00
|
|
|
self.error_annotation_lines = None
|
2020-11-22 10:24:41 +00:00
|
|
|
self.diff = diff
|
|
|
|
self.lines = lines
|
|
|
|
self.checks = checks
|
2022-08-05 16:29:29 +00:00
|
|
|
self.signal = None
|
2019-06-08 20:43:50 +00:00
|
|
|
|
|
|
|
def message(self):
|
|
|
|
fields = self.testrun.config.colors()
|
|
|
|
fields["name"] = self.testrun.name
|
|
|
|
fields["subbed_command"] = self.testrun.subbed_command
|
|
|
|
if self.line:
|
|
|
|
fields.update(
|
|
|
|
{
|
|
|
|
"output_file": self.line.file,
|
|
|
|
"output_lineno": self.line.number,
|
2020-11-22 10:24:41 +00:00
|
|
|
"output_line": self.line.escaped_text(),
|
2019-06-08 20:43:50 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
if self.check:
|
|
|
|
fields.update(
|
|
|
|
{
|
|
|
|
"input_file": self.check.line.file,
|
|
|
|
"input_lineno": self.check.line.number,
|
2020-11-22 10:24:41 +00:00
|
|
|
"input_line": self.check.line.escaped_text(),
|
2019-06-08 20:43:50 +00:00
|
|
|
"check_type": self.check.type,
|
|
|
|
}
|
|
|
|
)
|
2020-01-25 10:37:10 +00:00
|
|
|
filemsg = "" if self.testrun.config.progress else " in {name}"
|
|
|
|
fmtstrs = ["{RED}Failure{RESET}" + filemsg + ":", ""]
|
2022-08-05 16:29:29 +00:00
|
|
|
if self.signal:
|
|
|
|
fmtstrs += [
|
|
|
|
" Process was killed by signal {BOLD}" + self.signal + "{RESET}",
|
|
|
|
""
|
|
|
|
]
|
2019-06-08 20:43:50 +00:00
|
|
|
if self.line and self.check:
|
|
|
|
fmtstrs += [
|
|
|
|
" The {check_type} on line {input_lineno} wants:",
|
|
|
|
" {BOLD}{input_line}{RESET}",
|
|
|
|
"",
|
|
|
|
" which failed to match line {output_file}:{output_lineno}:",
|
|
|
|
" {BOLD}{output_line}{RESET}",
|
|
|
|
"",
|
|
|
|
]
|
|
|
|
|
|
|
|
elif self.check:
|
|
|
|
fmtstrs += [
|
|
|
|
" The {check_type} on line {input_lineno} wants:",
|
|
|
|
" {BOLD}{input_line}{RESET}",
|
|
|
|
"",
|
|
|
|
" but there was no remaining output to match.",
|
|
|
|
"",
|
|
|
|
]
|
|
|
|
else:
|
|
|
|
fmtstrs += [
|
|
|
|
" There were no remaining checks left to match {output_file}:{output_lineno}:",
|
|
|
|
" {BOLD}{output_line}{RESET}",
|
|
|
|
"",
|
|
|
|
]
|
2020-11-14 12:15:33 +00:00
|
|
|
if self.error_annotation_lines:
|
2020-11-22 13:39:48 +00:00
|
|
|
fields["error_annotation"] = " ".join(
|
|
|
|
[x.text for x in self.error_annotation_lines]
|
|
|
|
)
|
|
|
|
fields["error_annotation_lineno"] = str(
|
|
|
|
self.error_annotation_lines[0].number
|
|
|
|
)
|
2020-11-14 12:15:33 +00:00
|
|
|
if len(self.error_annotation_lines) > 1:
|
2020-11-22 13:39:48 +00:00
|
|
|
fields["error_annotation_lineno"] += ":" + str(
|
|
|
|
self.error_annotation_lines[-1].number
|
|
|
|
)
|
2019-06-08 20:43:50 +00:00
|
|
|
fmtstrs += [
|
|
|
|
" additional output on stderr:{error_annotation_lineno}:",
|
|
|
|
" {BOLD}{error_annotation}{RESET}",
|
|
|
|
]
|
2020-11-22 10:24:41 +00:00
|
|
|
if self.diff:
|
2020-11-14 12:15:33 +00:00
|
|
|
fmtstrs += [" Context:"]
|
2020-11-22 10:24:41 +00:00
|
|
|
lasthi = 0
|
|
|
|
lastcheckline = None
|
|
|
|
for d in self.diff.get_grouped_opcodes():
|
|
|
|
for op, alo, ahi, blo, bhi in d:
|
2020-11-22 13:39:48 +00:00
|
|
|
color = "{BOLD}"
|
|
|
|
if op == "replace" or op == "delete":
|
|
|
|
color = "{RED}"
|
2020-11-22 10:24:41 +00:00
|
|
|
# We got a new chunk, so we print a marker.
|
|
|
|
if alo > lasthi:
|
|
|
|
fmtstrs += [
|
2020-11-22 13:39:48 +00:00
|
|
|
" [...] from line "
|
|
|
|
+ str(self.checks[blo].line.number)
|
|
|
|
+ " ("
|
|
|
|
+ self.lines[alo].file
|
|
|
|
+ ":"
|
|
|
|
+ str(self.lines[alo].number)
|
|
|
|
+ "):"
|
2020-11-22 10:24:41 +00:00
|
|
|
]
|
|
|
|
lasthi = ahi
|
|
|
|
|
|
|
|
# We print one "no more checks" after the last check and then skip any markers
|
|
|
|
lastcheck = False
|
|
|
|
for a, b in zip_longest(self.lines[alo:ahi], self.checks[blo:bhi]):
|
|
|
|
# Clean up strings for use in a format string - double up the curlies.
|
2020-11-22 13:39:48 +00:00
|
|
|
astr = (
|
|
|
|
color + a.escaped_text(for_formatting=True) + "{RESET}"
|
|
|
|
if a
|
|
|
|
else ""
|
|
|
|
)
|
2020-11-22 10:24:41 +00:00
|
|
|
if b:
|
2020-11-22 13:39:48 +00:00
|
|
|
bstr = (
|
|
|
|
"'{BLUE}"
|
|
|
|
+ b.line.escaped_text(for_formatting=True)
|
|
|
|
+ "{RESET}'"
|
|
|
|
+ " on line "
|
|
|
|
+ str(b.line.number)
|
|
|
|
)
|
2020-11-22 10:24:41 +00:00
|
|
|
lastcheckline = b.line.number
|
|
|
|
|
2020-11-22 13:39:48 +00:00
|
|
|
if op == "equal":
|
2020-11-22 10:24:41 +00:00
|
|
|
fmtstrs += [" " + astr]
|
|
|
|
elif b and a:
|
2020-11-22 13:39:48 +00:00
|
|
|
fmtstrs += [
|
|
|
|
" "
|
|
|
|
+ astr
|
|
|
|
+ " <= does not match "
|
|
|
|
+ b.type
|
|
|
|
+ " "
|
|
|
|
+ bstr
|
|
|
|
]
|
2020-11-22 10:24:41 +00:00
|
|
|
elif b:
|
2020-11-22 13:39:48 +00:00
|
|
|
fmtstrs += [
|
|
|
|
" "
|
|
|
|
+ astr
|
|
|
|
+ " <= nothing to match "
|
|
|
|
+ b.type
|
|
|
|
+ " "
|
|
|
|
+ bstr
|
|
|
|
]
|
2020-11-22 10:24:41 +00:00
|
|
|
elif not b:
|
|
|
|
string = " " + astr
|
|
|
|
if bhi == len(self.checks):
|
|
|
|
if not lastcheck:
|
|
|
|
string += " <= no more checks"
|
|
|
|
lastcheck = True
|
|
|
|
elif lastcheckline is not None:
|
2020-11-22 13:39:48 +00:00
|
|
|
string += (
|
|
|
|
" <= no check matches this, previous check on line "
|
|
|
|
+ str(lastcheckline)
|
|
|
|
)
|
2020-11-22 10:24:41 +00:00
|
|
|
else:
|
|
|
|
string += " <= no check matches"
|
|
|
|
fmtstrs.append(string)
|
|
|
|
fmtstrs.append("")
|
2019-06-08 20:43:50 +00:00
|
|
|
fmtstrs += [" when running command:", " {subbed_command}"]
|
|
|
|
return "\n".join(fmtstrs).format(**fields)
|
|
|
|
|
|
|
|
def print_message(self):
|
|
|
|
""" Print our message to stdout. """
|
|
|
|
print(self.message())
|
|
|
|
|
|
|
|
|
|
|
|
def perform_substitution(input_str, subs):
|
2020-11-22 13:39:48 +00:00
|
|
|
"""Perform the substitutions described by subs to str
|
|
|
|
Return the substituted string.
|
2019-06-08 20:43:50 +00:00
|
|
|
"""
|
|
|
|
# Sort our substitutions into a list of tuples (key, value), descending by length.
|
|
|
|
# It needs to be descending because we need to try longer substitutions first.
|
|
|
|
subs_ordered = sorted(subs.items(), key=lambda s: len(s[0]), reverse=True)
|
|
|
|
|
|
|
|
def subber(m):
|
|
|
|
# We get the entire sequence of characters.
|
|
|
|
# Replace just the prefix and return it.
|
|
|
|
text = m.group(1)
|
|
|
|
for key, replacement in subs_ordered:
|
|
|
|
if text.startswith(key):
|
|
|
|
return replacement + text[len(key) :]
|
2020-01-25 10:37:10 +00:00
|
|
|
# No substitution found, so we default to running it as-is,
|
|
|
|
# which will end up running it via $PATH.
|
|
|
|
return text
|
2019-06-08 20:43:50 +00:00
|
|
|
|
|
|
|
return re.sub(r"%(%|[a-zA-Z0-9_-]+)", subber, input_str)
|
|
|
|
|
|
|
|
|
2021-01-16 12:26:01 +00:00
|
|
|
def runproc(cmd):
|
|
|
|
""" Wrapper around subprocess.Popen to save typing """
|
|
|
|
PIPE = subprocess.PIPE
|
|
|
|
proc = subprocess.Popen(
|
|
|
|
cmd,
|
|
|
|
stdin=PIPE,
|
|
|
|
stdout=PIPE,
|
|
|
|
stderr=PIPE,
|
|
|
|
shell=True,
|
|
|
|
close_fds=True, # For Python 2.6 as shipped on RHEL 6
|
|
|
|
)
|
|
|
|
return proc
|
|
|
|
|
|
|
|
|
2019-06-08 20:43:50 +00:00
|
|
|
class TestRun(object):
|
|
|
|
def __init__(self, name, runcmd, checker, subs, config):
|
|
|
|
self.name = name
|
|
|
|
self.runcmd = runcmd
|
|
|
|
self.subbed_command = perform_substitution(runcmd.args, subs)
|
|
|
|
self.checker = checker
|
|
|
|
self.subs = subs
|
|
|
|
self.config = config
|
|
|
|
|
|
|
|
def check(self, lines, checks):
|
|
|
|
# Reverse our lines and checks so we can pop off the end.
|
|
|
|
lineq = lines[::-1]
|
|
|
|
checkq = checks[::-1]
|
2020-11-22 10:24:41 +00:00
|
|
|
usedlines = []
|
|
|
|
usedchecks = []
|
|
|
|
mismatches = []
|
2019-06-08 20:43:50 +00:00
|
|
|
while lineq and checkq:
|
|
|
|
line = lineq[-1]
|
|
|
|
check = checkq[-1]
|
2020-11-25 16:23:29 +00:00
|
|
|
if check == line:
|
2019-06-08 20:43:50 +00:00
|
|
|
# This line matched this checker, continue on.
|
2020-11-22 10:24:41 +00:00
|
|
|
usedlines.append(line)
|
|
|
|
usedchecks.append(check)
|
2019-06-08 20:43:50 +00:00
|
|
|
lineq.pop()
|
|
|
|
checkq.pop()
|
|
|
|
elif line.is_empty_space():
|
|
|
|
# Skip all whitespace input lines.
|
|
|
|
lineq.pop()
|
|
|
|
else:
|
2020-11-22 10:24:41 +00:00
|
|
|
usedlines.append(line)
|
|
|
|
usedchecks.append(check)
|
|
|
|
mismatches.append((line, check))
|
2019-06-08 20:43:50 +00:00
|
|
|
# Failed to match.
|
2020-01-25 10:37:10 +00:00
|
|
|
lineq.pop()
|
2020-11-22 10:24:41 +00:00
|
|
|
checkq.pop()
|
|
|
|
|
|
|
|
# Drain empties
|
2019-06-08 20:43:50 +00:00
|
|
|
while lineq and lineq[-1].is_empty_space():
|
|
|
|
lineq.pop()
|
2020-11-22 10:24:41 +00:00
|
|
|
|
|
|
|
# Store the remaining lines for the diff
|
|
|
|
for i in lineq[::-1]:
|
|
|
|
if not i.is_empty_space():
|
|
|
|
usedlines.append(i)
|
|
|
|
# Store remaining checks for the diff
|
|
|
|
for i in checkq[::-1]:
|
|
|
|
usedchecks.append(i)
|
|
|
|
|
2022-08-05 16:29:29 +00:00
|
|
|
# If we have no more output, there's no reason to give
|
|
|
|
# SCREENFULS of text.
|
|
|
|
# So we truncate the check list.
|
|
|
|
if len(usedchecks) > len(usedlines):
|
|
|
|
usedchecks = usedchecks[:len(usedlines) + 5]
|
|
|
|
|
2020-11-22 10:24:41 +00:00
|
|
|
# Do a SequenceMatch! This gives us a diff-like thing.
|
2020-11-25 16:23:29 +00:00
|
|
|
diff = SequenceMatcher(a=usedlines, b=usedchecks, autojunk=False)
|
2020-11-22 10:24:41 +00:00
|
|
|
# If there's a mismatch or still lines or checkers, we have a failure.
|
2019-06-08 20:43:50 +00:00
|
|
|
# Otherwise it's success.
|
2020-11-22 10:24:41 +00:00
|
|
|
if mismatches:
|
2020-11-22 13:39:48 +00:00
|
|
|
return TestFailure(
|
|
|
|
mismatches[0][0],
|
|
|
|
mismatches[0][1],
|
|
|
|
self,
|
|
|
|
diff=diff,
|
|
|
|
lines=usedlines,
|
|
|
|
checks=usedchecks,
|
|
|
|
)
|
2020-11-22 10:24:41 +00:00
|
|
|
elif lineq:
|
2020-11-22 13:39:48 +00:00
|
|
|
return TestFailure(
|
|
|
|
lineq[-1], None, self, diff=diff, lines=usedlines, checks=usedchecks
|
|
|
|
)
|
2019-06-08 20:43:50 +00:00
|
|
|
elif checkq:
|
2020-11-22 13:39:48 +00:00
|
|
|
return TestFailure(
|
|
|
|
None, checkq[-1], self, diff=diff, lines=usedlines, checks=usedchecks
|
|
|
|
)
|
2019-06-08 20:43:50 +00:00
|
|
|
else:
|
2020-11-22 10:24:41 +00:00
|
|
|
# Success!
|
2019-06-08 20:43:50 +00:00
|
|
|
return None
|
|
|
|
|
|
|
|
def run(self):
|
|
|
|
""" Run the command. Return a TestFailure, or None. """
|
|
|
|
|
|
|
|
def split_by_newlines(s):
|
2020-11-22 13:39:48 +00:00
|
|
|
"""Decode a string and split it by newlines only,
|
|
|
|
retaining the newlines.
|
2019-06-08 20:43:50 +00:00
|
|
|
"""
|
|
|
|
return [s + "\n" for s in s.decode("utf-8").split("\n")]
|
|
|
|
|
|
|
|
if self.config.verbose:
|
|
|
|
print(self.subbed_command)
|
2021-01-16 12:26:01 +00:00
|
|
|
proc = runproc(self.subbed_command)
|
2019-06-08 20:43:50 +00:00
|
|
|
stdout, stderr = proc.communicate()
|
2020-01-25 10:37:10 +00:00
|
|
|
# HACK: This is quite cheesy: POSIX specifies that sh should return 127 for a missing command.
|
2021-02-21 09:54:05 +00:00
|
|
|
# It's also possible that it'll be returned in other situations,
|
|
|
|
# most likely when the last command in a shell script doesn't exist.
|
|
|
|
# So we check if the command *we execute* exists, and complain then.
|
2020-01-25 10:37:10 +00:00
|
|
|
status = proc.returncode
|
2021-02-21 09:54:05 +00:00
|
|
|
cmd = shlex.split(self.subbed_command)[0]
|
|
|
|
if status == 127 and not find_command(cmd):
|
|
|
|
raise CheckerError("Command could not be found: " + cmd)
|
|
|
|
if status == 126 and not find_command(cmd):
|
|
|
|
raise CheckerError("Command is not executable: " + cmd)
|
2020-01-25 10:37:10 +00:00
|
|
|
|
2019-06-08 20:43:50 +00:00
|
|
|
outlines = [
|
|
|
|
Line(text, idx + 1, "stdout")
|
|
|
|
for idx, text in enumerate(split_by_newlines(stdout))
|
|
|
|
]
|
|
|
|
errlines = [
|
|
|
|
Line(text, idx + 1, "stderr")
|
|
|
|
for idx, text in enumerate(split_by_newlines(stderr))
|
|
|
|
]
|
|
|
|
outfail = self.check(outlines, self.checker.outchecks)
|
|
|
|
errfail = self.check(errlines, self.checker.errchecks)
|
|
|
|
# It's possible that something going wrong on stdout resulted in new
|
|
|
|
# text being printed on stderr. If we have an outfailure, and either
|
|
|
|
# non-matching or unmatched stderr text, then annotate the outfail
|
|
|
|
# with it.
|
|
|
|
if outfail and errfail and errfail.line:
|
2020-11-22 13:39:48 +00:00
|
|
|
outfail.error_annotation_lines = errlines[errfail.line.number - 1 :]
|
2020-11-14 12:15:33 +00:00
|
|
|
# Trim a trailing newline
|
|
|
|
if outfail.error_annotation_lines[-1].text == "\n":
|
|
|
|
del outfail.error_annotation_lines[-1]
|
2022-08-05 16:29:29 +00:00
|
|
|
failure = outfail if outfail else errfail
|
|
|
|
|
|
|
|
if failure and status < 0:
|
|
|
|
# Process was killed by a signal and failed,
|
|
|
|
# add a message.
|
|
|
|
import signal
|
|
|
|
# Unfortunately strsignal only exists in python 3.8+,
|
|
|
|
# and signal.signals is 3.5+.
|
|
|
|
if hasattr(signal, "Signals"):
|
|
|
|
try:
|
|
|
|
sig = signal.Signals(-status)
|
|
|
|
failure.signal = sig.name + " (" + signal.strsignal(sig.value) + ")"
|
|
|
|
except ValueError:
|
|
|
|
failure.signal = str(-status)
|
|
|
|
else:
|
|
|
|
# No easy way to get the full list,
|
|
|
|
# make up a dict.
|
|
|
|
signals = {
|
|
|
|
signal.SIGABRT: "SIGABRT",
|
|
|
|
signal.SIGBUS: "SIGBUS",
|
|
|
|
signal.SIGFPE: "SIGFPE",
|
|
|
|
signal.SIGILL: "SIGILL",
|
|
|
|
signal.SIGSEGV: "SIGSEGV",
|
|
|
|
signal.SIGTERM: "SIGTERM",
|
|
|
|
}
|
|
|
|
failure.signal = signals.get(-status, str(-status))
|
|
|
|
|
|
|
|
return failure
|
2019-06-08 20:43:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
class CheckCmd(object):
|
|
|
|
def __init__(self, line, checktype, regex):
|
|
|
|
self.line = line
|
|
|
|
self.type = checktype
|
|
|
|
self.regex = regex
|
|
|
|
|
2020-11-25 16:23:29 +00:00
|
|
|
def __hash__(self):
|
|
|
|
# HACK: We pass this to the Sequencematcher, which puts the Checks into a dict.
|
|
|
|
# To force it to match the regexes, we return a hash collision intentionally,
|
|
|
|
# so it falls back on __eq__().
|
|
|
|
#
|
|
|
|
# Line has the same thing.
|
|
|
|
return 0
|
|
|
|
|
|
|
|
def __eq__(self, other):
|
|
|
|
# "Magical" comparison with lines and strings.
|
|
|
|
# Typically I wouldn't use this, but it allows us to check if a line matches any check in a dict or list via
|
|
|
|
# the `in` operator.
|
2020-11-30 17:16:42 +00:00
|
|
|
if other is None:
|
|
|
|
return False
|
2020-11-25 16:23:29 +00:00
|
|
|
if isinstance(other, CheckCmd):
|
2020-11-30 17:16:42 +00:00
|
|
|
return self.regex == other.regex
|
2020-11-25 16:23:29 +00:00
|
|
|
if isinstance(other, Line):
|
|
|
|
return self.regex.match(other.text)
|
|
|
|
if isinstance(other, str):
|
|
|
|
return self.regex.match(other)
|
|
|
|
raise NotImplementedError
|
|
|
|
|
2019-06-08 20:43:50 +00:00
|
|
|
@staticmethod
|
|
|
|
def parse(line, checktype):
|
|
|
|
# type: (Line) -> CheckCmd
|
|
|
|
# Everything inside {{}} is a regular expression.
|
|
|
|
# Everything outside of it is a literal string.
|
|
|
|
# Split around {{...}}. Then every odd index will be a regex, and
|
|
|
|
# evens will be literals.
|
|
|
|
# Note that if {{...}} appears first we will get an empty string in
|
|
|
|
# the split array, so the {{...}} matches are always at odd indexes.
|
|
|
|
bracket_re = re.compile(
|
|
|
|
r"""
|
|
|
|
\{\{ # Two open brackets
|
|
|
|
(.*?) # Nongreedy capture
|
|
|
|
\}\} # Two close brackets
|
|
|
|
""",
|
|
|
|
re.VERBOSE,
|
|
|
|
)
|
|
|
|
pieces = bracket_re.split(line.text)
|
|
|
|
even = True
|
|
|
|
re_strings = []
|
|
|
|
for piece in pieces:
|
|
|
|
if even:
|
|
|
|
# piece is a literal string.
|
|
|
|
re_strings.append(re.escape(piece))
|
|
|
|
else:
|
|
|
|
# piece is a regex (found inside {{...}}).
|
|
|
|
# Verify the regex can be compiled.
|
|
|
|
try:
|
|
|
|
re.compile(piece)
|
|
|
|
except re.error:
|
|
|
|
raise CheckerError("Invalid regular expression: '%s'" % piece, line)
|
|
|
|
re_strings.append(piece)
|
|
|
|
even = not even
|
|
|
|
# Enclose each piece in a non-capturing group.
|
|
|
|
# This ensures that lower-precedence operators don't trip up catenation.
|
|
|
|
# For example: {{b|c}}d would result in /b|cd/ which is different.
|
|
|
|
# Backreferences are assumed to match across the entire string.
|
|
|
|
re_strings = ["(?:%s)" % s for s in re_strings]
|
|
|
|
# Anchor at beginning and end (allowing arbitrary whitespace), and maybe
|
|
|
|
# a terminating newline.
|
|
|
|
# We need the anchors because Python's match() matches an arbitrary prefix,
|
|
|
|
# not the entire string.
|
|
|
|
re_strings = [r"^\s*"] + re_strings + [r"\s*\n?$"]
|
|
|
|
full_re = re.compile("".join(re_strings))
|
|
|
|
return CheckCmd(line, checktype, full_re)
|
|
|
|
|
|
|
|
|
|
|
|
class Checker(object):
|
|
|
|
def __init__(self, name, lines):
|
|
|
|
self.name = name
|
|
|
|
# Helper to yield subline containing group1 from all matching lines.
|
|
|
|
def group1s(regex):
|
|
|
|
for line in lines:
|
|
|
|
m = regex.match(line.text)
|
|
|
|
if m:
|
|
|
|
yield line.subline(m.group(1))
|
|
|
|
|
|
|
|
# Find run commands.
|
|
|
|
self.runcmds = [RunCmd.parse(sl) for sl in group1s(RUN_RE)]
|
2021-03-28 18:58:18 +00:00
|
|
|
self.shebang_cmd = None
|
2019-06-08 20:43:50 +00:00
|
|
|
if not self.runcmds:
|
2020-01-25 10:37:10 +00:00
|
|
|
# If no RUN command has been given, fall back to the shebang.
|
|
|
|
if lines[0].text.startswith("#!"):
|
|
|
|
# Remove the "#!" at the beginning, and the newline at the end.
|
2021-02-21 09:54:05 +00:00
|
|
|
cmd = lines[0].text[2:-1]
|
2021-03-28 18:58:18 +00:00
|
|
|
self.shebang_cmd = cmd
|
2021-02-21 09:54:05 +00:00
|
|
|
self.runcmds = [RunCmd(cmd + " %s", lines[0])]
|
2020-01-25 10:37:10 +00:00
|
|
|
else:
|
|
|
|
raise CheckerError("No runlines ('# RUN') found")
|
2019-06-08 20:43:50 +00:00
|
|
|
|
2021-01-16 12:26:01 +00:00
|
|
|
self.requirecmds = [RunCmd.parse(sl) for sl in group1s(REQUIRES_RE)]
|
|
|
|
|
2019-06-08 20:43:50 +00:00
|
|
|
# Find check cmds.
|
|
|
|
self.outchecks = [
|
|
|
|
CheckCmd.parse(sl, "CHECK") for sl in group1s(CHECK_STDOUT_RE)
|
|
|
|
]
|
|
|
|
self.errchecks = [
|
|
|
|
CheckCmd.parse(sl, "CHECKERR") for sl in group1s(CHECK_STDERR_RE)
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
def check_file(input_file, name, subs, config, failure_handler):
|
|
|
|
""" Check a single file. Return a True on success, False on error. """
|
|
|
|
success = True
|
|
|
|
lines = Line.readfile(input_file, name)
|
|
|
|
checker = Checker(name, lines)
|
2021-01-16 12:26:01 +00:00
|
|
|
|
|
|
|
# Run all the REQUIRES lines first,
|
|
|
|
# if any of them fail it's a SKIP
|
|
|
|
for reqcmd in checker.requirecmds:
|
|
|
|
proc = runproc(
|
|
|
|
perform_substitution(reqcmd.args, subs)
|
|
|
|
)
|
2021-03-28 18:58:18 +00:00
|
|
|
proc.communicate()
|
2021-01-16 12:26:01 +00:00
|
|
|
if proc.returncode > 0:
|
|
|
|
return SKIP
|
|
|
|
|
2021-03-28 18:58:18 +00:00
|
|
|
if checker.shebang_cmd is not None and not find_command(checker.shebang_cmd):
|
|
|
|
raise CheckerError("Command could not be found: " + checker.shebang_cmd)
|
|
|
|
|
2021-01-16 12:26:01 +00:00
|
|
|
# Only then run the RUN lines.
|
2019-06-08 20:43:50 +00:00
|
|
|
for runcmd in checker.runcmds:
|
|
|
|
failure = TestRun(name, runcmd, checker, subs, config).run()
|
|
|
|
if failure:
|
|
|
|
failure_handler(failure)
|
|
|
|
success = False
|
|
|
|
return success
|
|
|
|
|
|
|
|
|
|
|
|
def check_path(path, subs, config, failure_handler):
|
|
|
|
with io.open(path, encoding="utf-8") as fd:
|
|
|
|
return check_file(fd, path, subs, config, failure_handler)
|
|
|
|
|
|
|
|
|
|
|
|
def parse_subs(subs):
|
2020-11-22 13:39:48 +00:00
|
|
|
"""Given a list of input substitutions like 'foo=bar',
|
|
|
|
return a dictionary like {foo:bar}, or exit if invalid.
|
2019-06-08 20:43:50 +00:00
|
|
|
"""
|
|
|
|
result = {}
|
|
|
|
for sub in subs:
|
|
|
|
try:
|
|
|
|
key, val = sub.split("=", 1)
|
|
|
|
if not key:
|
|
|
|
print("Invalid substitution %s: empty key" % sub)
|
|
|
|
sys.exit(1)
|
|
|
|
if not val:
|
|
|
|
print("Invalid substitution %s: empty value" % sub)
|
|
|
|
sys.exit(1)
|
|
|
|
result[key] = val
|
|
|
|
except ValueError:
|
|
|
|
print("Invalid substitution %s: equal sign not found" % sub)
|
|
|
|
sys.exit(1)
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
|
|
def get_argparse():
|
|
|
|
""" Return a littlecheck argument parser. """
|
|
|
|
parser = argparse.ArgumentParser(
|
|
|
|
description="littlecheck: command line tool tester."
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"-s",
|
|
|
|
"--substitute",
|
|
|
|
type=str,
|
|
|
|
help="Add a new substitution for RUN lines. Example: bash=/bin/bash",
|
|
|
|
action="append",
|
|
|
|
default=[],
|
|
|
|
)
|
2020-01-25 10:37:10 +00:00
|
|
|
parser.add_argument(
|
|
|
|
"-p",
|
|
|
|
"--progress",
|
2020-02-17 13:12:27 +00:00
|
|
|
action="store_true",
|
|
|
|
dest="progress",
|
2020-01-25 10:37:10 +00:00
|
|
|
help="Show the files to be checked",
|
|
|
|
default=False,
|
|
|
|
)
|
2021-08-30 15:16:19 +00:00
|
|
|
parser.add_argument(
|
|
|
|
"--force-color",
|
|
|
|
action="store_true",
|
|
|
|
dest="force_color",
|
|
|
|
help="Force usage of color even if not connected to a terminal",
|
|
|
|
default=False,
|
|
|
|
)
|
2019-06-08 20:43:50 +00:00
|
|
|
parser.add_argument("file", nargs="+", help="File to check")
|
|
|
|
return parser
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
args = get_argparse().parse_args()
|
|
|
|
# Default substitution is %% -> %
|
|
|
|
def_subs = {"%": "%"}
|
|
|
|
def_subs.update(parse_subs(args.substitute))
|
|
|
|
|
2021-03-28 18:58:18 +00:00
|
|
|
tests_count = 0
|
|
|
|
failed = False
|
|
|
|
skip_count = 0
|
2019-06-08 20:43:50 +00:00
|
|
|
config = Config()
|
2021-08-30 15:16:19 +00:00
|
|
|
config.colorize = args.force_color or sys.stdout.isatty()
|
2020-01-25 10:37:10 +00:00
|
|
|
config.progress = args.progress
|
|
|
|
fields = config.colors()
|
2020-03-29 08:47:27 +00:00
|
|
|
|
2019-06-08 20:43:50 +00:00
|
|
|
for path in args.file:
|
2021-03-28 18:58:18 +00:00
|
|
|
tests_count += 1
|
2020-01-25 10:37:10 +00:00
|
|
|
fields["path"] = path
|
|
|
|
if config.progress:
|
2020-02-17 13:12:27 +00:00
|
|
|
print("Testing file {path} ... ".format(**fields), end="")
|
2020-03-15 23:43:06 +00:00
|
|
|
sys.stdout.flush()
|
2019-06-08 20:43:50 +00:00
|
|
|
subs = def_subs.copy()
|
|
|
|
subs["s"] = path
|
2020-03-15 23:43:06 +00:00
|
|
|
starttime = datetime.datetime.now()
|
2021-01-16 12:26:01 +00:00
|
|
|
ret = check_path(path, subs, config, TestFailure.print_message)
|
2022-08-05 16:29:29 +00:00
|
|
|
if ret is SKIP:
|
|
|
|
skip_count += 1
|
2021-01-16 12:26:01 +00:00
|
|
|
if not ret:
|
2021-03-28 18:58:18 +00:00
|
|
|
failed = True
|
2020-01-25 10:37:10 +00:00
|
|
|
elif config.progress:
|
2020-03-15 23:43:06 +00:00
|
|
|
endtime = datetime.datetime.now()
|
|
|
|
duration_ms = round((endtime - starttime).total_seconds() * 1000)
|
2021-01-16 12:26:01 +00:00
|
|
|
reason = "ok"
|
|
|
|
color = "{GREEN}"
|
|
|
|
if ret is SKIP:
|
|
|
|
reason = "SKIPPED"
|
|
|
|
color = "{BLUE}"
|
2020-03-15 23:43:06 +00:00
|
|
|
print(
|
2021-01-16 12:26:01 +00:00
|
|
|
(color + "{reason}{RESET} ({duration} ms)").format(
|
|
|
|
duration=duration_ms, reason=reason, **fields
|
2020-03-15 23:43:06 +00:00
|
|
|
)
|
|
|
|
)
|
2021-03-28 18:58:18 +00:00
|
|
|
|
|
|
|
# To facilitate integration with testing frameworks, use exit code 125 to indicate that all
|
|
|
|
# tests have been skipped (primarily for use when tests are run one at a time). Exit code 125 is
|
|
|
|
# used to indicate to automated `git bisect` runs that a revision has been skipped; we use it
|
|
|
|
# for the same reasons git does.
|
|
|
|
if skip_count > 0 and skip_count == tests_count:
|
|
|
|
sys.exit(125)
|
|
|
|
|
|
|
|
sys.exit(1 if failed else 0)
|
2019-06-08 20:43:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|