xfstestsdb/xfstestsdb/testcase/list.py

98 lines
4.2 KiB
Python

# Copyright 2023 (c) Anna Schumaker.
"""The `xfstestsdb testcase list` command."""
import argparse
from .. import colors
from .. import commands
from .. import sqlite
from .. import table
class TestCaseTable(table.Table):
"""A custom Table for coloring rows based on status."""
def do_format_cell(self, rownum: int, text: str, color: str,
**kwargs) -> str:
"""Set row text color based on testcase status."""
color = f"fg-{self.rows[rownum][-2]}"
return super().do_format_cell(rownum, text, color, **kwargs)
class Command(commands.Command):
"""The `xfstestsdb testcase list` command."""
def __init__(self, subparser: argparse.Action,
sql: sqlite.Connection) -> None:
"""Set up the testcase list command."""
super().__init__(subparser, sql, "list",
help="list the xfstest testcase entries")
self.parser.add_argument("--color", metavar="color", nargs="?",
choices=["light", "dark", "none"],
const=colors.get_default_colors(),
default=colors.get_default_colors(),
help="list testcases with color output "
f"[default={colors.get_default_colors()}]")
self.parser.add_argument("--device", metavar="device", nargs=1,
help="show testcases with a device matching "
"the given pattern")
self.parser.add_argument("--failure", dest="status",
action="append_const", const="failure",
help="show failed testcases")
self.parser.add_argument("--passed", dest="status",
action="append_const", const="passed",
help="show passing testcases")
self.parser.add_argument("--runid", metavar="runid", nargs=1, type=int,
help="show testcases belonging to "
"a specific run")
self.parser.add_argument("--skipped", dest="status",
action="append_const", const="skipped",
help="show skipped testcases")
self.parser.add_argument("--testcase", metavar="testcase", nargs=1,
help="show testcases matching "
"the given pattern")
self.parser.add_argument("--xunit", metavar="xunit", nargs=1,
help="show testcases with an xunit "
"matching the given pattern")
def do_command(self, args: argparse.Namespace) -> None:
"""Print out the xfstestsdb xunits table."""
tbl = TestCaseTable(["run", "device", "xunit",
"testcase", "status", "time"],
["r", "l", "l", "l", "l", "r"], args.color)
sql_where = ""
sql_args = []
filter = []
if args.device:
filter.append("device GLOB ?")
sql_args.append(args.device[0])
if args.runid:
filter.append("runid=?")
sql_args.append(args.runid[0])
if args.status:
parameters = ["?"] * len(args.status)
filter.append(f"status IN ({','.join(parameters)})")
sql_args.extend(args.status)
if args.testcase:
filter.append("testcase GLOB ?")
sql_args.append(args.testcase[0])
if args.xunit:
filter.append("xunit GLOB ?")
sql_args.append(args.xunit[0])
if len(filter) > 0:
sql_where = " WHERE " + " AND ".join(filter) + " "
cur = self.sql("""SELECT runid, device, xunit, testcase, status, time
FROM testcases_view""" + sql_where + """
ORDER BY runid, xunit, testcase""", *sql_args)
for row in cur.fetchall():
tbl.add_row(row["runid"], row["device"], row["xunit"],
row["testcase"], row["status"], f"{row['time']} s")
if len(tbl.rows) > 0:
print(tbl)