xfstestsdb/tests/testcase/test_show.py

171 lines
6.1 KiB
Python

# Copyright 2023 (c) Anna Schumaker.
"""Tests the `xfstestsdb testcase show` command."""
import errno
import io
import unittest
import unittest.mock
import xfstestsdb.testcase.show
import tests.xunit
class TestTestCaseShow(unittest.TestCase):
"""Tests the `xfstestsdb testcase show` command."""
def setUp(self):
"""Set up common variables."""
self.xfstestsdb = xfstestsdb.Command()
self.testcase = self.xfstestsdb.commands["testcase"]
self.show = self.testcase.commands["show"]
def setup_run(self, mock_stdout: io.StringIO):
"""Set up runs in the database and clear stdout."""
self.xfstestsdb.run(["new", "/dev/vda1"])
self.xfstestsdb.run(["xunit", "read", "1", str(tests.xunit.XUNIT_1)])
self.xfstestsdb.sql("UPDATE messages SET message=? WHERE rowid=6",
"\n".join([f"stderr line {i}" for i in range(10)]))
mock_stdout.seek(0)
mock_stdout.truncate(0)
def test_init(self):
"""Check that the testcase show command was set up properly."""
self.assertIsInstance(self.show, xfstestsdb.commands.Command)
self.assertIsInstance(self.show, xfstestsdb.testcase.show.Command)
self.assertEqual(self.testcase.subparser.choices["show"],
self.show.parser)
@unittest.mock.patch("sys.stdout", new_callable=io.StringIO)
def test_show_passing(self, mock_stdout: io.StringIO):
"""Test showing a passing test case."""
self.setup_run(mock_stdout)
self.xfstestsdb.run(["testcase", "show", "1", "test-1", "test/01",
"--color", "none"])
self.assertEqual(mock_stdout.getvalue(),
"test/01: passed, 1 second\n")
@unittest.mock.patch("sys.stdout", new_callable=io.StringIO)
def test_show_skipped(self, mock_stdout: io.StringIO):
"""Test showing a skipped test case."""
self.setup_run(mock_stdout)
print()
self.xfstestsdb.run(["testcase", "show", "1", "test-1", "test/02",
"--color", "none"])
self.assertEqual(mock_stdout.getvalue(),
"""
test/02: skipped, 0 seconds:
skipped on $TEST_DEV
""")
@unittest.mock.patch("sys.stdout", new_callable=io.StringIO)
def test_show_failure(self, mock_stdout: io.StringIO):
"""Test showing a failing test case."""
self.setup_run(mock_stdout)
print()
self.xfstestsdb.run(["testcase", "show", "1", "test-1", "test/09",
"--color", "none"])
self.assertEqual(mock_stdout.getvalue(),
"""
test/09: failure, 9 seconds:
output mismatch (see somefile)
text printed to system-out:
there was a problem with '$SCRATCH_DEV'
text printed to system-err:
stderr line 0
stderr line 1
stderr line 2
stderr line 3
stderr line 4
stderr line 5
stderr line 6
stderr line 7
stderr line 8
stderr line 9
""")
@unittest.mock.patch("sys.stdout", new_callable=io.StringIO)
def test_show_maxlines(self, mock_stdout: io.StringIO):
"""Test showing a failing test case, with a line limit."""
self.setup_run(mock_stdout)
print()
self.xfstestsdb.run(["testcase", "show", "1", "test-1", "test/09",
"--maxlines", "5", "--color", "none"])
self.assertEqual(mock_stdout.getvalue(),
"""
test/09: failure, 9 seconds:
output mismatch (see somefile)
text printed to system-out:
there was a problem with '$SCRATCH_DEV'
text printed to system-err:
stderr line 0
stderr line 1
stderr line 2
stderr line 3
stderr line 4
...
[5 additional lines, see `--maxlines=`]
""")
@unittest.mock.patch("sys.stdout", new_callable=io.StringIO)
def test_show_negative_maxlines(self, mock_stdout: io.StringIO):
"""Test showing a failing test case, with a negative line limit."""
self.setup_run(mock_stdout)
print()
self.xfstestsdb.run(["testcase", "show", "1", "test-1", "test/09",
"--maxlines", "-1", "--color", "none"])
self.assertEqual(mock_stdout.getvalue(),
"""
test/09: failure, 9 seconds:
output mismatch (see somefile)
text printed to system-out:
there was a problem with '$SCRATCH_DEV'
text printed to system-err:
stderr line 0
stderr line 1
stderr line 2
stderr line 3
stderr line 4
stderr line 5
stderr line 6
stderr line 7
stderr line 8
stderr line 9
""")
@unittest.mock.patch("sys.stderr", new_callable=io.StringIO)
def test_show_error(self, mock_stderr: io.StringIO):
"""Test the `xfstestsdb testcase show` command with invalid input."""
with self.assertRaises(SystemExit):
self.xfstestsdb.run(["testcase", "show"])
self.assertRegex(mock_stderr.getvalue(),
"error: the following arguments are required: runid")
with self.assertRaises(SystemExit):
self.xfstestsdb.run(["testcase", "show", "1"])
self.assertRegex(mock_stderr.getvalue(),
"error: the following arguments are required: xunit")
with self.assertRaises(SystemExit):
self.xfstestsdb.run(["testcase", "show", "1", "xunit-1"])
self.assertRegex(mock_stderr.getvalue(),
"the following arguments are required: testcase")
@unittest.mock.patch("sys.stderr", new_callable=io.StringIO)
@unittest.mock.patch("sys.stdout", new_callable=io.StringIO)
def test_show_enoent(self, mock_stdout: io.StringIO,
mock_stderr: io.StringIO):
"""Test `xfstestsdb testcase show` with an invalid runid."""
self.setup_run(mock_stderr)
with self.assertRaises(SystemExit) as sys_exit:
self.xfstestsdb.run(["testcase", "show", "2", "test-1", "test/01"])
self.assertEqual(sys_exit.exception.code, errno.ENOENT)
self.assertEqual(mock_stderr.getvalue(),
"error: either run #2, xunit 'test-1', "
"or testcase 'test/01' do not exist\n")