# Copyright 2023 (c) Anna Schumaker. """Tests the `xfstestsdb untag` command.""" import errno import io import unittest import unittest.mock import xfstestsdb.untag class TestUntag(unittest.TestCase): """Tests the `xfstestsdb untag` command.""" def setUp(self): """Set up common variables.""" self.xfstestsdb = xfstestsdb.Command() self.untag = self.xfstestsdb.commands["untag"] def test_init(self): """Check that the untag command was set up properly.""" self.assertIsInstance(self.untag, xfstestsdb.commands.Command) self.assertIsInstance(self.untag, xfstestsdb.untag.Command) self.assertEqual(self.xfstestsdb.subparser.choices["untag"], self.untag.parser) @unittest.mock.patch("sys.stdout", new_callable=io.StringIO) def test_untag(self, mock_stdout: io.StringIO): """Test the `xfstestsdb untag` command with valid input.""" self.xfstestsdb.run(["new", "/dev/test"]) self.xfstestsdb.run(["tag", "1", "mytag"]) self.xfstestsdb.run(["untag", "1", "mytag"]) self.assertRegex(mock_stdout.getvalue(), "tag 'mytag' removed from run #1") cur = self.xfstestsdb.sql("SELECT COUNT(*) FROM tags") self.assertEqual(cur.fetchone()["COUNT(*)"], 0) @unittest.mock.patch("sys.stdout", new_callable=io.StringIO) def test_untag_nonexistant(self, mock_stdout: io.StringIO): """Test the `xfstestsdb untag` command with a nonexistant tag.""" self.xfstestsdb.run(["new", "/dev/test"]) self.xfstestsdb.run(["untag", "1", "mytag"]) self.assertNotRegex(mock_stdout.getvalue(), "tag 'mytag' removed from run #1") @unittest.mock.patch("sys.stderr", new_callable=io.StringIO) def test_untag_error(self, mock_stderr: io.StringIO): """Test the `xfstestsdb untag` command with invalid input.""" with self.assertRaises(SystemExit): self.xfstestsdb.run(["untag"]) self.assertRegex(mock_stderr.getvalue(), "error: the following arguments are required: runid") with self.assertRaises(SystemExit): self.xfstestsdb.run(["untag", "3"]) self.assertRegex(mock_stderr.getvalue(), "error: the following arguments are required: tag") @unittest.mock.patch("sys.stderr", new_callable=io.StringIO) def test_untag_enoent(self, mock_stderr: io.StringIO): """Test the `xfstestsdb untag` command with invalid runid and tag.""" with self.assertRaises(SystemExit) as sys_exit: self.xfstestsdb.run(["untag", "2", "mytag"]) self.assertEqual(sys_exit.exception.code, errno.ENOENT) self.assertRegex(mock_stderr.getvalue(), "error: run #2 does not exist")