xunit: Create the xfstestsdb xunit delete command

This command is used to delete individual xunit entries attached to a
specific xfstests run.

Implements: #5 (`xfstestsdb xunit delete`)
Signed-off-by: Anna Schumaker <anna@nowheycreamery.com>
This commit is contained in:
Anna Schumaker 2023-02-04 08:56:04 -05:00
parent 42ad3ad0fb
commit 1b95487d06
3 changed files with 95 additions and 1 deletions

View File

@ -0,0 +1,60 @@
# Copyright 2023 (c) Anna Schumaker.
"""Tests the `xfstestsdb xunit delete` command."""
import errno
import io
import unittest
import unittest.mock
import xfstestsdb.delete
import tests.xunit
class TestXunitDelete(unittest.TestCase):
"""Tests the `xfstestsdb xunit delete` command."""
def setUp(self):
"""Set up common variables."""
self.xfstestsdb = xfstestsdb.Command()
self.xunit = self.xfstestsdb.commands["xunit"]
self.delete = self.xunit.commands["delete"]
def test_init(self):
"""Check that the xunit delete command was set up properly."""
self.assertIsInstance(self.delete, xfstestsdb.commands.Command)
self.assertIsInstance(self.delete, xfstestsdb.xunit.delete.Command)
self.assertEqual(self.xunit.subparser.choices["delete"],
self.delete.parser)
@unittest.mock.patch("sys.stdout", new_callable=io.StringIO)
def test_delete(self, mock_stdout: io.StringIO):
"""Test the `xfstestsdb xunit delete` command with valid input."""
self.xfstestsdb.run(["new", "/dev/test"])
self.xfstestsdb.run(["xunit", "read", "1", str(tests.xunit.XUNIT_1)])
self.xfstestsdb.run(["xunit", "delete", "1", tests.xunit.XUNIT_1.stem])
self.assertRegex(mock_stdout.getvalue(),
"run #1 xunit 'test-1' has been deleted")
cur = self.xfstestsdb.sql("SELECT COUNT(*) FROM xunits")
self.assertEqual(cur.fetchone()["COUNT(*)"], 0)
@unittest.mock.patch("sys.stderr", new_callable=io.StringIO)
def test_delete_error(self, mock_stderr: io.StringIO):
"""Test the `xfstestsdb xunit delete` command with invalid input."""
with self.assertRaises(SystemExit):
self.xfstestsdb.run(["xunit", "delete"])
self.assertRegex(mock_stderr.getvalue(),
"error: the following arguments are required: runid")
with self.assertRaises(SystemExit):
self.xfstestsdb.run(["xunit", "delete", "1"])
self.assertRegex(mock_stderr.getvalue(),
"error: the following arguments are required: name")
@unittest.mock.patch("sys.stderr", new_callable=io.StringIO)
def test_delete_enoent(self, mock_stderr: io.StringIO):
"""Test the `xfstestsdb delete` command with an invalid runid."""
with self.assertRaises(SystemExit) as sys_exit:
self.xfstestsdb.run(["xunit", "delete", "2", "test-1"])
self.assertEqual(sys_exit.exception.code, errno.ENOENT)
self.assertRegex(mock_stderr.getvalue(),
"error: either run #2 or xunit 'test-1' do not exist")

View File

@ -3,6 +3,7 @@
import argparse
from .. import commands
from .. import sqlite
from . import delete
from . import read
from . import rename
@ -16,7 +17,8 @@ class Command(commands.Command):
super().__init__(subparser, sql, "xunit",
help="xfstestsdb xunit commands")
self.subparser = self.parser.add_subparsers(title="xunit commands")
self.commands = {"read": read.Command(self.subparser, sql),
self.commands = {"delete": delete.Command(self.subparser, sql),
"read": read.Command(self.subparser, sql),
"rename": rename.Command(self.subparser, sql)}
def do_command(self, args: argparse.Namespace) -> None:

View File

@ -0,0 +1,32 @@
# Copyright 2023 (c) Anna Schumaker.
"""The `xfstestsdb xunit delete` command."""
import argparse
import errno
import sys
from . import commands
from . import sqlite
class Command(commands.Command):
"""The `xfstestsdb delete` command."""
def __init__(self, subparser: argparse.Action,
sql: sqlite.Connection) -> None:
"""Set up the delete command."""
super().__init__(subparser, sql, "delete",
help="delete an xfstests xunit entry")
self.parser.add_argument("runid", metavar="runid", nargs=1, type=int,
help="runid of the xfstests xunit to delete")
self.parser.add_argument("name", metavar="name", nargs=1, type=str,
help="name of the xunit entry to delete")
def do_command(self, args: argparse.Namespace) -> None:
"""Delete a row from the xunits table."""
cur = self.sql("DELETE FROM xunits WHERE rowid=? and name=?",
args.runid[0], args.name[0])
if cur.rowcount == 0:
print(f"error: either run #{args.runid[0]} or "
f"xunit '{args.name[0]}' do not exist", file=sys.stderr)
sys.exit(errno.ENOENT)
print(f"run #{args.runid[0]} xunit '{args.name[0]}' has been deleted")