gtk: Give the gtk command a runid argument

This argument is passed to the Application using the 'command-line'
signal, so a running Application can switch to a new runid when
requested.

Signed-off-by: Anna Schumaker <anna@nowheycreamery.com>
This commit is contained in:
Anna Schumaker 2023-08-01 10:18:47 -04:00
parent 62bf603ca4
commit d5413700f0
2 changed files with 93 additions and 5 deletions

View File

@ -1,8 +1,11 @@
# Copyright 2023 (c) Anna Schumaker.
"""Tests the `xfstestsdb gtk` command."""
import errno
import io
import unittest
import unittest.mock
import xfstestsdb.gtk
from gi.repository import Gio
from gi.repository import Adw
@ -18,6 +21,34 @@ class TestApplication(unittest.TestCase):
self.assertIsInstance(self.application, Adw.Application)
self.assertEqual(self.application.get_application_id(),
xfstestsdb.gtk.gsetup.APPLICATION_ID)
self.assertEqual(self.application.get_flags(),
Gio.ApplicationFlags.HANDLES_COMMAND_LINE)
self.assertEqual(self.application.runid, 0)
@unittest.mock.patch("gi.repository.Adw.Application.activate")
@unittest.mock.patch("gi.repository.Adw.Application.do_command_line")
def test_command_line(self, mock_command_line: unittest.mock.Mock,
mock_activate: unittest.mock.Mock):
"""Check that we handle the command-line signal."""
mock_cmd = unittest.mock.Mock()
mock_cmd.get_arguments.return_value = []
self.application.do_command_line(mock_cmd)
mock_command_line.assert_called_with(self.application, mock_cmd)
mock_cmd.get_arguments.assert_called()
mock_activate.assert_called()
self.assertEqual(self.application.runid, 0)
mock_command_line.reset_mock()
mock_activate.reset_mock()
mock_cmd.reset_mock()
mock_cmd.get_arguments.return_value = ["runid=42"]
self.application.do_command_line(mock_cmd)
mock_command_line.assert_called_with(self.application, mock_cmd)
mock_cmd.get_arguments.assert_called()
mock_activate.assert_called()
self.assertEqual(self.application.runid, 42)
@unittest.mock.patch("gi.repository.Adw.Application.do_startup")
def test_startup(self, mock_startup: unittest.mock.Mock):
@ -53,9 +84,36 @@ class TestGtk(unittest.TestCase):
self.assertEqual(self.xfstestsdb.subparser.choices["gtk"],
self.gtk.parser)
@unittest.mock.patch("sys.stderr", new_callable=io.StringIO)
@unittest.mock.patch("xfstestsdb.gtk.Application")
def test_command(self, mock_app: unittest.mock.Mock):
"""Check that running `xfstestsdb gtk` creates an Application."""
self.xfstestsdb.run(["gtk"])
def test_gtk_empty(self, mock_app: unittest.mock.Mock,
mock_stderr: unittest.mock.Mock):
"""Check that running `xfstestsdb gtk` without a runid."""
with self.assertRaises(SystemExit):
self.xfstestsdb.run(["gtk"])
self.assertRegex(mock_stderr.getvalue(),
"error: the following arguments are required: runid")
@unittest.mock.patch("sys.stdout", new_callable=io.StringIO)
@unittest.mock.patch("xfstestsdb.gtk.Application")
def test_gtk_runid(self, mock_app: unittest.mock.Mock,
mock_stdout: io.StringIO):
"""Check running `xfstestsdb gtk` with the --runid option."""
self.xfstestsdb.run(["new", "/dev/vda1"])
self.xfstestsdb.run(["gtk", "1"])
mock_app.assert_called()
mock_app.return_value.run.assert_called_with([])
mock_app.return_value.run.assert_called_with(["runid=1"])
@unittest.mock.patch("sys.stderr", new_callable=io.StringIO)
@unittest.mock.patch("xfstestsdb.gtk.Application")
def test_gtk_error(self, mock_app: unittest.mock.Mock,
mock_stderr: io.StringIO):
"""Check running the gtk command with an invalid runid."""
with self.assertRaises(SystemExit) as sys_exit:
self.xfstestsdb.run(["gtk", "2"])
mock_app.assert_not_called()
self.assertEqual(sys_exit.exception.code, errno.ENOENT)
self.assertEqual(mock_stderr.getvalue(),
"error: run #2 does not exist\n")

View File

@ -1,7 +1,11 @@
# Copyright 2023 (c) Anna Schumaker.
"""The `xfstestsdb gtk` command."""
import argparse
import errno
import sys
from . import gsetup
from gi.repository import GObject
from gi.repository import Gio
from gi.repository import Adw
from .. import commands
from .. import sqlite
@ -10,9 +14,24 @@ from .. import sqlite
class Application(Adw.Application):
"""Our Adw.Application for displaying xfstests results."""
runid = GObject.Property(type=int)
def __init__(self):
"""Initialize the application."""
super().__init__(application_id=gsetup.APPLICATION_ID)
super().__init__(application_id=gsetup.APPLICATION_ID,
flags=Gio.ApplicationFlags.HANDLES_COMMAND_LINE)
def do_command_line(self, cmd_line: Gio.ApplicationCommandLine) -> int:
"""Handle the Adw.Application::command-line signal."""
Adw.Application.do_command_line(self, cmd_line)
for arg in cmd_line.get_arguments():
split = arg.split("=")
match split[0]:
case "runid": self.runid = int(split[1])
self.activate()
return 0
def do_startup(self) -> None:
"""Handle the Adw.Application::startup signal."""
@ -34,8 +53,19 @@ class Command(commands.Command):
sql: sqlite.Connection) -> None:
"""Set up the gtk command."""
super().__init__(subparser, sql, "gtk", help="show a gtk-based ui")
self.parser.add_argument("runid", metavar="runid", nargs=1, type=int,
help="show a specific xfstests run")
def do_command(self, args: argparse.Namespace) -> None:
"""Run the Gtk UI."""
app_args = []
if self.sql("SELECT 1 FROM xfstests_runs WHERE runid=?",
args.runid[0]).fetchone():
app_args.append(f"runid={args.runid[0]}")
else:
print(f"error: run #{args.runid[0]} does not exist",
file=sys.stderr)
sys.exit(errno.ENOENT)
Application().run(app_args)