refactory CLI unittests including:
- introduce new CliTestCase class - implement useful assert functions in CliTestCase
This commit is contained in:
parent
f79cab8c0d
commit
d331fdfa11
6 changed files with 402 additions and 292 deletions
|
|
@ -1,20 +1,19 @@
|
|||
from __future__ import absolute_import
|
||||
from __future__ import print_function
|
||||
import mock
|
||||
import os
|
||||
import six
|
||||
import sys
|
||||
import unittest
|
||||
|
||||
from koji_cli.commands import handle_resubmit
|
||||
from . import utils
|
||||
|
||||
|
||||
class TestResubmit(unittest.TestCase):
|
||||
class TestResubmit(utils.CliTestCase):
|
||||
|
||||
# Show long diffs in error output...
|
||||
maxDiff = None
|
||||
|
||||
def setUp(self):
|
||||
self.progname = os.path.basename(sys.argv[0]) or 'koji'
|
||||
self.task_id = 101
|
||||
self.taskinfo = """Task: 101
|
||||
Type: createrepo
|
||||
|
|
@ -29,28 +28,11 @@ Log Files:
|
|||
/mnt/koji/work/tasks/2/2/mergerepos.log
|
||||
"""
|
||||
|
||||
def format_error_message(self, message):
|
||||
return """Usage: %s resubmit [options] taskID
|
||||
self.error_format = """Usage: %s resubmit [options] taskID
|
||||
(Specify the --help global option for a list of other help options)
|
||||
|
||||
%s: error: %s
|
||||
""" % (self.progname, self.progname, message)
|
||||
|
||||
def assert_console_output(self, device, expected, wipe=True, regex=False):
|
||||
if not isinstance(device, six.StringIO):
|
||||
raise TypeError('Not a StringIO object')
|
||||
|
||||
output = device.getvalue()
|
||||
if not regex:
|
||||
self.assertMultiLineEqual(output, expected)
|
||||
else:
|
||||
six.assertRegex(self, output, expected)
|
||||
if wipe:
|
||||
device.truncate(0)
|
||||
device.seek(0)
|
||||
|
||||
def print_taskinfo(self, *args, **kwargs):
|
||||
print(self.taskinfo)
|
||||
%s: error: {message}
|
||||
""" % (self.progname, self.progname)
|
||||
|
||||
@mock.patch('sys.stdout', new_callable=six.StringIO)
|
||||
@mock.patch('sys.stderr', new_callable=six.StringIO)
|
||||
|
|
@ -62,7 +44,7 @@ Log Files:
|
|||
watch_tasks_mock,
|
||||
stderr,
|
||||
stdout):
|
||||
"""Test handle_maven_chain function"""
|
||||
"""Test handle_resubmit function"""
|
||||
arguments = [str(self.task_id)]
|
||||
options = mock.MagicMock(quiet=False)
|
||||
new_task_id = self.task_id + 100
|
||||
|
|
@ -75,14 +57,14 @@ Log Files:
|
|||
|
||||
# Generate task info and nowait tests
|
||||
with mock.patch('koji_cli.commands._printTaskInfo') as p_mock:
|
||||
p_mock.side_effect = self.print_taskinfo
|
||||
p_mock.side_effect = lambda *args, **kwargs: print(self.taskinfo)
|
||||
handle_resubmit(options, session, arguments + ['--nowait'])
|
||||
activate_session_mock.assert_called_with(session, options)
|
||||
expected = "Resubmitting the following task:" + "\n"
|
||||
expected += self.taskinfo + "\n"
|
||||
expected += "Resubmitted task %s as new task %s" % \
|
||||
(self.task_id, new_task_id) + "\n"
|
||||
self.assert_console_output(stdout, expected)
|
||||
self.assert_console_message(stdout, expected)
|
||||
|
||||
session.logout.reset_mock()
|
||||
session.resubmitTask.reset_mock()
|
||||
|
|
@ -102,14 +84,14 @@ Log Files:
|
|||
session.logout.assert_called_once()
|
||||
session.resubmitTask.assert_called_with(self.task_id)
|
||||
session.resubmitTask.assert_called_once()
|
||||
self.assert_console_output(stdout, '')
|
||||
self.assert_console_message(stdout, '')
|
||||
|
||||
@mock.patch('sys.stdout', new_callable=six.StringIO)
|
||||
@mock.patch('sys.stderr', new_callable=six.StringIO)
|
||||
@mock.patch('koji_cli.commands.activate_session')
|
||||
def test_handle_resubmit_help_compat(
|
||||
def test_handle_resubmit_argument_error(
|
||||
self, activate_session_mock, stderr, stdout):
|
||||
"""Test handle_resubmit help message compact output"""
|
||||
"""Test handle_resubmit argument error"""
|
||||
arguments = []
|
||||
options = mock.MagicMock()
|
||||
|
||||
|
|
@ -117,33 +99,28 @@ Log Files:
|
|||
session = mock.MagicMock()
|
||||
|
||||
# Run it and check immediate output
|
||||
with self.assertRaises(SystemExit) as cm:
|
||||
handle_resubmit(options, session, arguments)
|
||||
expected_stderr = self.format_error_message(
|
||||
expected = self.format_error_message(
|
||||
"Please specify a single task ID")
|
||||
self.assert_console_output(stdout, '')
|
||||
self.assert_console_output(stderr, expected_stderr)
|
||||
|
||||
self.assert_system_exit(
|
||||
handle_resubmit,
|
||||
options,
|
||||
session,
|
||||
arguments,
|
||||
stderr=expected,
|
||||
activate_session=None)
|
||||
|
||||
# Check there is no message on stdout
|
||||
self.assert_console_message(stdout, '')
|
||||
|
||||
# Finally, assert that things were called as we expected.
|
||||
activate_session_mock.assert_not_called()
|
||||
self.assertEqual(cm.exception.code, 2)
|
||||
|
||||
@mock.patch('sys.stdout', new_callable=six.StringIO)
|
||||
@mock.patch('sys.stderr', new_callable=six.StringIO)
|
||||
@mock.patch('koji_cli.commands.activate_session')
|
||||
def test_handle_resubmit_help_full(
|
||||
self, activate_session_mock, stderr, stdout):
|
||||
"""Test handle_resubmit help message full output"""
|
||||
arguments = ['--help']
|
||||
options = mock.MagicMock()
|
||||
|
||||
# Mock out the xmlrpc server
|
||||
session = mock.MagicMock()
|
||||
|
||||
# Run it and check immediate output
|
||||
with self.assertRaises(SystemExit) as cm:
|
||||
handle_resubmit(options, session, arguments)
|
||||
expected_stdout = """Usage: %s resubmit [options] taskID
|
||||
def test_handle_resubmit_help(self):
|
||||
"""Test handle_resubmit help message output"""
|
||||
self.assert_help(
|
||||
handle_resubmit,
|
||||
"""Usage: %s resubmit [options] taskID
|
||||
(Specify the --help global option for a list of other help options)
|
||||
|
||||
Options:
|
||||
|
|
@ -151,13 +128,7 @@ Options:
|
|||
--nowait Don't wait on task
|
||||
--nowatch An alias for --nowait
|
||||
--quiet Do not print the task information
|
||||
""" % (self.progname)
|
||||
self.assert_console_output(stdout, expected_stdout)
|
||||
self.assert_console_output(stderr, '')
|
||||
|
||||
# Finally, assert that things were called as we expected.
|
||||
activate_session_mock.assert_not_called()
|
||||
self.assertEqual(cm.exception.code, 0)
|
||||
""" % self.progname)
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue