# Copyright 2023 (c) Anna Schumaker. """Tests the `xfstestsdb new` command.""" import io import unittest import unittest.mock import xfstestsdb.new class TestNew(unittest.TestCase): """Tests the `xfstestsdb new` command.""" def setUp(self): """Set up common variables.""" self.xfstestsdb = xfstestsdb.Command() self.new = self.xfstestsdb.commands["new"] def test_init(self): """Check that the new command was set up properly.""" self.assertIsInstance(self.new, xfstestsdb.commands.Command) self.assertIsInstance(self.new, xfstestsdb.new.Command) self.assertEqual(self.xfstestsdb.subparser.choices["new"], self.new.parser) @unittest.mock.patch("sys.stdout", new_callable=io.StringIO) def test_new(self, mock_stdout: io.StringIO): """Test running the `xfstestsdb new` command with correct input.""" self.xfstestsdb.run(["new", "/dev/vdb1"]) self.assertRegex(mock_stdout.getvalue(), r"created run #1 with test device '/dev/vdb1' " r"\[[\d\-\: ]+\]\n") @unittest.mock.patch("sys.stdout", new_callable=io.StringIO) def test_new_terse(self, mock_stdout: io.StringIO): """Test running `xfstestsdb new --terse`.""" self.xfstestsdb.run(["new", "--terse", "/dev/vdb1"]) self.assertEqual(mock_stdout.getvalue(), "1\n") @unittest.mock.patch("sys.stderr", new_callable=io.StringIO) def test_new_error(self, mock_stderr: io.StringIO): """Test running the `xfstestsdb new` command with invalid input.""" with self.assertRaises(SystemExit): self.xfstestsdb.run(["new"]) self.assertRegex(mock_stderr.getvalue(), "error: the following arguments are required: device")