xfstestsdb: Create the `xfstestsdb rename` command
This command renames the device in an xfstests_runs database row. Implements: #8 (`xfstestsdb rename`) Signed-off-by: Anna Schumaker <anna@nowheycreamery.com>
This commit is contained in:
parent
6a6fe5dda8
commit
56774dbb85
|
@ -0,0 +1,67 @@
|
|||
# Copyright 2023 (c) Anna Schumaker.
|
||||
"""Tests the `xfstestsdb rename` command."""
|
||||
import errno
|
||||
import io
|
||||
import unittest
|
||||
import unittest.mock
|
||||
import xfstestsdb.rename
|
||||
|
||||
|
||||
class TestRename(unittest.TestCase):
|
||||
"""Tests the `xfstestsdb rename` command."""
|
||||
|
||||
def setUp(self):
|
||||
"""Set up common variables."""
|
||||
self.xfstestsdb = xfstestsdb.Command()
|
||||
self.rename = self.xfstestsdb.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.rename.Command)
|
||||
self.assertEqual(self.xfstestsdb.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 rename` command with valid input."""
|
||||
self.xfstestsdb.run(["new", "/dev/test"])
|
||||
self.xfstestsdb.run(["rename", "1", "/dev/new/name"])
|
||||
self.assertRegex(mock_stdout.getvalue(),
|
||||
"renamed run #1 device '/dev/test' "
|
||||
"to '/dev/new/name'")
|
||||
|
||||
cur = self.xfstestsdb.sql("SELECT device FROM xfstests_runs "
|
||||
"WHERE rowid=1")
|
||||
self.assertEqual(cur.fetchone()["device"], "/dev/new/name")
|
||||
|
||||
@unittest.mock.patch("sys.stdout", new_callable=io.StringIO)
|
||||
def test_rename_same(self, mock_stdout: io.StringIO):
|
||||
"""Test the `xfstestsdb rename` command with the same name."""
|
||||
self.xfstestsdb.run(["new", "/dev/test"])
|
||||
self.xfstestsdb.run(["rename", "1", "/dev/test"])
|
||||
self.assertNotRegex(mock_stdout.getvalue(),
|
||||
"renamed run #1 device '/dev/test' to '/dev/test'")
|
||||
|
||||
@unittest.mock.patch("sys.stderr", new_callable=io.StringIO)
|
||||
def test_rename_error(self, mock_stderr: io.StringIO):
|
||||
"""Test the `xfstestsdb rename` command with invalid input."""
|
||||
with self.assertRaises(SystemExit):
|
||||
self.xfstestsdb.run(["rename"])
|
||||
self.assertRegex(mock_stderr.getvalue(),
|
||||
"error: the following arguments are required: runid")
|
||||
|
||||
with self.assertRaises(SystemExit):
|
||||
self.xfstestsdb.run(["rename", "3"])
|
||||
self.assertRegex(mock_stderr.getvalue(),
|
||||
"error: the following arguments are required: name")
|
||||
|
||||
@unittest.mock.patch("sys.stderr", new_callable=io.StringIO)
|
||||
def test_rename_enoent(self, mock_stderr: io.StringIO):
|
||||
"""Test the `xfstestsdb rename` command with an invalid runid."""
|
||||
with self.assertRaises(SystemExit) as sys_exit:
|
||||
self.xfstestsdb.run(["rename", "2", "/dev/new/name"])
|
||||
|
||||
self.assertEqual(sys_exit.exception.code, errno.ENOENT)
|
||||
self.assertRegex(mock_stderr.getvalue(),
|
||||
"error: run #2 does not exist")
|
|
@ -3,6 +3,7 @@
|
|||
import argparse
|
||||
from . import sqlite
|
||||
from . import new
|
||||
from . import rename
|
||||
|
||||
MAJOR = 1
|
||||
MINOR = 0
|
||||
|
@ -19,7 +20,8 @@ class Command:
|
|||
help="show version number and exit")
|
||||
self.subparser = self.parser.add_subparsers(title="commands")
|
||||
self.sql = sqlite.Connection()
|
||||
self.commands = {"new": new.Command(self.subparser, self.sql)}
|
||||
self.commands = {"new": new.Command(self.subparser, self.sql),
|
||||
"rename": rename.Command(self.subparser, self.sql)}
|
||||
|
||||
def __del__(self) -> None:
|
||||
"""Clean up."""
|
||||
|
|
|
@ -0,0 +1,39 @@
|
|||
# Copyright 2023 (c) Anna Schumaker.
|
||||
"""The `xfstestsdb rename` command."""
|
||||
import argparse
|
||||
import errno
|
||||
import sys
|
||||
from . import commands
|
||||
from . import sqlite
|
||||
|
||||
|
||||
class Command(commands.Command):
|
||||
"""The `xfstestsdb rename` command."""
|
||||
|
||||
def __init__(self, subparser: argparse.Action,
|
||||
sql: sqlite.Connection) -> None:
|
||||
"""Set up the rename command."""
|
||||
super().__init__(subparser, sql, "rename",
|
||||
help="change the device name for an xfstests run")
|
||||
self.parser.add_argument("runid", metavar="runid", nargs=1, type=int,
|
||||
help="runid of the xfstests run to change")
|
||||
self.parser.add_argument("name", metavar="name", nargs=1,
|
||||
help="new device name for the xfstests run")
|
||||
|
||||
def do_command(self, args: argparse.Namespace) -> None:
|
||||
"""Rename a device in the xfstests_runs table."""
|
||||
row = self.sql("SELECT device FROM xfstests_runs WHERE rowid=?",
|
||||
args.runid[0]).fetchone()
|
||||
if row is None:
|
||||
print(f"error: run #{args.runid[0]} does not exist",
|
||||
file=sys.stderr)
|
||||
sys.exit(errno.ENOENT)
|
||||
|
||||
old_name = row["device"]
|
||||
if args.name[0] == old_name:
|
||||
return
|
||||
|
||||
self.sql("UPDATE xfstests_runs SET device=? WHERE rowid=?",
|
||||
args.name[0], args.runid[0])
|
||||
print(f"renamed run #{args.runid[0]} device '{old_name}'", end=" ")
|
||||
print(f"to '{args.name[0]}'")
|
Loading…
Reference in New Issue