command: Add a function to be called after the main function

The main function is run inside a sqlite transaction context, but there
are a few rare cases where we want to call a function after the main
function and outside of a transaction (such as using the sqlite 'VACUUM'
operation). This gives us a way to do that.

Signed-off-by: Anna Schumaker <anna@nowheycreamery.com>
This commit is contained in:
Anna Schumaker 2023-11-07 14:27:50 -05:00
parent 06d10cf883
commit dc6f5f54c3
4 changed files with 16 additions and 2 deletions

View File

@ -24,9 +24,15 @@ class TestCommand(unittest.TestCase):
self.assertEqual(self.command.parser.description, "description") self.assertEqual(self.command.parser.description, "description")
self.assertEqual(self.command.parser._defaults["function"], self.assertEqual(self.command.parser._defaults["function"],
self.command.do_command) self.command.do_command)
self.assertEqual(self.command.parser._defaults["done"],
self.command.do_done)
self.assertEqual(self.command.sql, self.sql) self.assertEqual(self.command.sql, self.sql)
def test_do_command(self): def test_do_command(self):
"""Test the do_command() function.""" """Test the do_command() function."""
with self.assertRaises(NotImplementedError): with self.assertRaises(NotImplementedError):
self.command.do_command(argparse.Namespace()) self.command.do_command(argparse.Namespace())
def test_do_done(self):
"""Test the do_done() function."""
self.command.do_done(argparse.Namespace())

View File

@ -37,16 +37,18 @@ class TestXfstestsdb(unittest.TestCase):
def test_run(self): def test_run(self):
"""Test running the xfstestsdb.""" """Test running the xfstestsdb."""
parser = self.xfstestsdb.subparser.add_parser("test-run", help="help") parser = self.xfstestsdb.subparser.add_parser("test-run", help="help")
test_done = unittest.mock.Mock()
test_passed = False test_passed = False
def test_func(args: argparse.Namespace) -> None: def test_func(args: argparse.Namespace) -> None:
nonlocal test_passed nonlocal test_passed
self.assertTrue(self.xfstestsdb.sql.sql.in_transaction) self.assertTrue(self.xfstestsdb.sql.sql.in_transaction)
test_passed = True test_passed = True
parser.set_defaults(function=test_func) parser.set_defaults(function=test_func, done=test_done)
self.xfstestsdb.run(["test-run"]) self.xfstestsdb.run(["test-run"])
self.assertTrue(test_passed) self.assertTrue(test_passed)
test_done.assert_called()
@unittest.mock.patch("sys.stdout", new_callable=io.StringIO) @unittest.mock.patch("sys.stdout", new_callable=io.StringIO)
def test_version(self, mock_stdout: io.StringIO): def test_version(self, mock_stdout: io.StringIO):

View File

@ -24,7 +24,8 @@ class Command:
def __init__(self) -> None: def __init__(self) -> None:
"""Initialize the xfstestsdb command.""" """Initialize the xfstestsdb command."""
self.parser = argparse.ArgumentParser() self.parser = argparse.ArgumentParser()
self.parser.set_defaults(function=lambda x: self.parser.print_usage()) self.parser.set_defaults(function=lambda x: self.parser.print_usage(),
done=lambda x: None)
self.parser.add_argument("--version", action="store_true", self.parser.add_argument("--version", action="store_true",
help="show version number and exit") help="show version number and exit")
self.subparser = self.parser.add_subparsers(title="commands") self.subparser = self.parser.add_subparsers(title="commands")
@ -55,3 +56,4 @@ class Command:
else: else:
with self.sql: with self.sql:
parsed.function(parsed) parsed.function(parsed)
parsed.done(parsed)

View File

@ -12,8 +12,12 @@ class Command:
"""Set up the Command.""" """Set up the Command."""
self.parser = subparser.add_parser(name, help=help, **kwargs) self.parser = subparser.add_parser(name, help=help, **kwargs)
self.parser.set_defaults(function=self.do_command) self.parser.set_defaults(function=self.do_command)
self.parser.set_defaults(done=self.do_done)
self.sql = sql self.sql = sql
def do_command(self, args: argparse.Namespace) -> None: def do_command(self, args: argparse.Namespace) -> None:
"""Do something.""" """Do something."""
raise NotImplementedError raise NotImplementedError
def do_done(self, args: argparse.Namespace) -> None:
"""Run after the main command, outside of a transaction."""