xunit: Create the `xfstestsdb xunit rename` command
This command renames an xunit file associated with an xfstests run. Implement: #4 (`xfstestsdb xunit rename`) Signed-off-by: Anna Schumaker <anna@nowheycreamery.com>
This commit is contained in:
parent
ad48147a48
commit
42ad3ad0fb
|
@ -0,0 +1,94 @@
|
|||
# Copyright 2023 (c) Anna Schumaker.
|
||||
"""Tests the `xfstestsdb xunit rename` command."""
|
||||
import errno
|
||||
import io
|
||||
import unittest
|
||||
import unittest.mock
|
||||
import xfstestsdb.rename
|
||||
import tests.xunit
|
||||
|
||||
|
||||
class TestXunitRename(unittest.TestCase):
|
||||
"""Tests the `xfstestsdb xunit rename` command."""
|
||||
|
||||
def setUp(self):
|
||||
"""Set up common variables."""
|
||||
self.xfstestsdb = xfstestsdb.Command()
|
||||
self.xunit = self.xfstestsdb.commands["xunit"]
|
||||
self.rename = self.xunit.commands["rename"]
|
||||
|
||||
def test_init(self):
|
||||
"""Check that the rename command was set up properly."""
|
||||
self.assertIsInstance(self.rename, xfstestsdb.commands.Command)
|
||||
self.assertIsInstance(self.rename, xfstestsdb.xunit.rename.Command)
|
||||
self.assertEqual(self.xunit.subparser.choices["rename"],
|
||||
self.rename.parser)
|
||||
|
||||
@unittest.mock.patch("sys.stdout", new_callable=io.StringIO)
|
||||
def test_rename(self, mock_stdout: io.StringIO):
|
||||
"""Test the `xfstestsdb xunit rename` 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", "rename", "1", "test-1", "test-new"])
|
||||
self.assertRegex(mock_stdout.getvalue(),
|
||||
"renamed run #1 xunit from 'test-1' to 'test-new'")
|
||||
|
||||
cur = self.xfstestsdb.sql("SELECT name FROM xunits WHERE runid=1")
|
||||
self.assertEqual(cur.fetchone()["name"], "test-new")
|
||||
|
||||
@unittest.mock.patch("sys.stdout", new_callable=io.StringIO)
|
||||
def test_rename_same(self, mock_stdout: io.StringIO):
|
||||
"""Test the `xfstestsdb xunit rename` command with the same name."""
|
||||
self.xfstestsdb.run(["new", "/dev/test"])
|
||||
self.xfstestsdb.run(["xunit", "read", "1", str(tests.xunit.XUNIT_1)])
|
||||
self.xfstestsdb.run(["xunit", "rename", "1", "test-1", "test-1"])
|
||||
self.assertNotRegex(mock_stdout.getvalue(),
|
||||
"renamed run #1 xunit from 'test-1' to 'test-1'")
|
||||
|
||||
@unittest.mock.patch("sys.stderr", new_callable=io.StringIO)
|
||||
def test_rename_error(self, mock_stderr: io.StringIO):
|
||||
"""Test the `xfstestsdb xunit rename` command with invalid input."""
|
||||
with self.assertRaises(SystemExit):
|
||||
self.xfstestsdb.run(["xunit", "rename"])
|
||||
self.assertRegex(mock_stderr.getvalue(),
|
||||
"error: the following arguments are required: runid")
|
||||
|
||||
with self.assertRaises(SystemExit):
|
||||
self.xfstestsdb.run(["xunit", "rename", "3"])
|
||||
self.assertRegex(mock_stderr.getvalue(),
|
||||
"error: the following arguments are required: old")
|
||||
|
||||
with self.assertRaises(SystemExit):
|
||||
self.xfstestsdb.run(["xunit", "rename", "3", "old"])
|
||||
self.assertRegex(mock_stderr.getvalue(),
|
||||
"error: the following arguments are required: new")
|
||||
|
||||
@unittest.mock.patch("sys.stdout", new_callable=io.StringIO)
|
||||
@unittest.mock.patch("sys.stderr", new_callable=io.StringIO)
|
||||
def test_rename_runid_enoent(self, mock_stderr: io.StringIO,
|
||||
mock_stdout: io.StringIO):
|
||||
"""Test the `xfstestsdb xunit rename` command with an invalid runid."""
|
||||
self.xfstestsdb.run(["new", "/dev/test"])
|
||||
with self.assertRaises(SystemExit) as sys_exit:
|
||||
self.xfstestsdb.run(["xunit", "rename", "2", "old", "new"])
|
||||
|
||||
self.assertEqual(sys_exit.exception.code, errno.ENOENT)
|
||||
self.assertRegex(mock_stderr.getvalue(),
|
||||
"error: either run #2 or xunit 'old' do not exist")
|
||||
|
||||
@unittest.mock.patch("sys.stdout", new_callable=io.StringIO)
|
||||
@unittest.mock.patch("sys.stderr", new_callable=io.StringIO)
|
||||
def test_rename_runid_eexist(self, mock_stderr: io.StringIO,
|
||||
mock_stdout: io.StringIO):
|
||||
"""Test the `xfstestsdb xunit rename` command with an existing name."""
|
||||
self.xfstestsdb.run(["new", "/dev/test"])
|
||||
self.xfstestsdb.run(["xunit", "read", "1", str(tests.xunit.XUNIT_1)])
|
||||
self.xfstestsdb.run(["xunit", "read", "1", str(tests.xunit.XUNIT_1),
|
||||
"--name", "test-name"])
|
||||
with self.assertRaises(SystemExit) as sys_exit:
|
||||
self.xfstestsdb.run(["xunit", "rename", "1",
|
||||
"test-1", "test-name"])
|
||||
|
||||
self.assertEqual(sys_exit.exception.code, errno.EEXIST)
|
||||
self.assertRegex(mock_stderr.getvalue(),
|
||||
"error: run #1 xunit 'test-name' already exists")
|
|
@ -4,6 +4,7 @@ import argparse
|
|||
from .. import commands
|
||||
from .. import sqlite
|
||||
from . import read
|
||||
from . import rename
|
||||
|
||||
|
||||
class Command(commands.Command):
|
||||
|
@ -15,7 +16,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 = {"read": read.Command(self.subparser, sql),
|
||||
"rename": rename.Command(self.subparser, sql)}
|
||||
|
||||
def do_command(self, args: argparse.Namespace) -> None:
|
||||
"""Print help text for the xunit subcommand."""
|
||||
|
|
|
@ -0,0 +1,43 @@
|
|||
# Copyright 2023 (c) Anna Schumaker.
|
||||
"""The `xfstestsdb xunit rename` command."""
|
||||
import argparse
|
||||
import errno
|
||||
import sys
|
||||
from . import commands
|
||||
from . import sqlite
|
||||
|
||||
|
||||
class Command(commands.Command):
|
||||
"""The `xfstestsdb xunit rename` command."""
|
||||
|
||||
def __init__(self, subparser: argparse.Action,
|
||||
sql: sqlite.Connection) -> None:
|
||||
"""Set up the xunit rename command."""
|
||||
super().__init__(subparser, sql, "rename",
|
||||
help="change the name for xfstests xunit file")
|
||||
self.parser.add_argument("runid", metavar="runid", nargs=1, type=int,
|
||||
help="runid of the xfstests run to change")
|
||||
self.parser.add_argument("old", metavar="old", nargs=1,
|
||||
help="old xunit name")
|
||||
self.parser.add_argument("new", metavar="new", nargs=1,
|
||||
help="new xunit name")
|
||||
|
||||
def do_command(self, args: argparse.Namespace) -> None:
|
||||
"""Rename an xunit entry in the xunits table."""
|
||||
if self.sql("SELECT name FROM xunits WHERE runid=? AND name=?",
|
||||
args.runid[0], args.old[0]).fetchone() is None:
|
||||
print(f"error: either run #{args.runid[0]} or "
|
||||
f"xunit '{args.old[0]}' do not exist", file=sys.stderr)
|
||||
sys.exit(errno.ENOENT)
|
||||
|
||||
if args.new[0] == args.old[0]:
|
||||
return
|
||||
|
||||
if self.sql("UPDATE xunits SET name=? WHERE runid=? AND name=?",
|
||||
args.new[0], args.runid[0], args.old[0]) is None:
|
||||
print(f"error: run #{args.runid[0]} xunit "
|
||||
f"'{args.new[0]}' already exists", file=sys.stderr)
|
||||
sys.exit(errno.EEXIST)
|
||||
|
||||
print(f"renamed run #{args.runid[0]} xunit from "
|
||||
f"'{args.old[0]}' to '{args.new[0]}'")
|
Loading…
Reference in New Issue