92 lines
4.2 KiB
Python
92 lines
4.2 KiB
Python
# Copyright 2023 (c) Anna Schumaker.
|
||
"""The `xfstestsdb testcase show` command."""
|
||
import argparse
|
||
import errno
|
||
import sys
|
||
from .. import colors
|
||
from .. import commands
|
||
from .. import sqlite
|
||
|
||
|
||
class Command(commands.Command):
|
||
"""The `xfstestsdb testcase show` command."""
|
||
|
||
def __init__(self, subparser: argparse.Action,
|
||
sql: sqlite.Connection) -> None:
|
||
"""Set up the testcase show command."""
|
||
super().__init__(subparser, sql, "show",
|
||
help="show detailed information"
|
||
"about a single testcase")
|
||
self.parser.add_argument("--color", metavar="color", nargs="?",
|
||
choices=["light", "dark", "none"],
|
||
const=colors.get_default_colors(),
|
||
default=colors.get_default_colors(),
|
||
help="show testcases with color output "
|
||
f"[default={colors.get_default_colors()}]")
|
||
self.parser.add_argument("--maxlines", metavar="n", type=int,
|
||
default=20,
|
||
help="show n lines of system-out "
|
||
"and system-err logs")
|
||
self.parser.add_argument("runid", metavar="runid", nargs=1,
|
||
help="runid of the testcase to show")
|
||
self.parser.add_argument("xunit", metavar="xunit", nargs=1,
|
||
help="xunit of the testcase to show")
|
||
self.parser.add_argument("testcase", metavar="testcase", nargs=1,
|
||
help="the testcase to show")
|
||
|
||
def __print_output(self, colors: colors.ColorScheme, system_log: str,
|
||
output: str, maxlines: int, diff: bool = False) -> None:
|
||
print()
|
||
print(colors.format(f"text printed to {system_log}:", bold=True))
|
||
|
||
lines = output.split("\n")
|
||
maxlines = maxlines if maxlines > 0 else len(lines)
|
||
|
||
for line in lines[:maxlines]:
|
||
color = f"diff-{line[0]}" if diff is True else None
|
||
print(" ", colors.format(line, color=color, dark="fg-dark"))
|
||
|
||
if (remaining := len(lines) - maxlines) > 0:
|
||
ellipsis = " ..." if colors.name == "none" else " ․․․"
|
||
print(colors.format(ellipsis, color="fg-skipped",
|
||
bold=True, dark="fg-dark"))
|
||
print(colors.format(f" [{remaining} additional lines, "
|
||
"see `--maxlines=`]", color="fg-header",
|
||
bold=True, dark="fg-dark"))
|
||
|
||
def do_command(self, args: argparse.Namespace) -> None:
|
||
"""Show details about a specific testcase."""
|
||
scheme = colors.get_colors(args.color)
|
||
cur = self.sql("""SELECT * FROM testcases_view
|
||
WHERE runid=? AND xunit=? AND testcase=?""",
|
||
args.runid[0], args.xunit[0], args.testcase[0])
|
||
if (row := cur.fetchone()) is None:
|
||
print(f"error: either run #{args.runid[0]}, "
|
||
f"xunit '{args.xunit[0]}', or testcase '{args.testcase[0]}' "
|
||
"do not exist", file=sys.stderr)
|
||
sys.exit(errno.ENOENT)
|
||
|
||
time = row['time']
|
||
s_time = "" if time == 1 else "s"
|
||
status_color = f"fg-{row['status']}"
|
||
|
||
print(scheme.format(f"{args.testcase[0]}:", bold=True), end=" ")
|
||
print(scheme.format(row["status"], color=status_color,
|
||
bold=True, dark="fg-dark"), end="")
|
||
print(scheme.format(f", {row['time']} second{s_time}", bold=True),
|
||
end="")
|
||
print(scheme.format(":" if row["message"] else "", bold=True))
|
||
|
||
if row["message"]:
|
||
print(" ", scheme.format(row["message"].lstrip("- "),
|
||
color=status_color,
|
||
bold=True, dark="fg-dark"))
|
||
|
||
if row["stdout"]:
|
||
self.__print_output(scheme, "system-out", row["stdout"],
|
||
args.maxlines)
|
||
|
||
if row["stderr"]:
|
||
self.__print_output(scheme, "system-err", row["stderr"],
|
||
args.maxlines, diff=True)
|