from __future__ import absolute_import import unittest import mock import six from koji_cli.commands import anon_handle_latest_build from . import utils class TestLatestBuild(utils.CliTestCase): def setUp(self): self.maxDiff = None self.options = mock.MagicMock() self.session = mock.MagicMock() self.tag_name = 'test-tag' self.pkg_name = 'test-pkg' self.expected_part_help = """Usage: %s latest-build [options] [ ...] The first option should be the name of a tag, not the name of a build target. If you want to know the latest build in buildroots for a given build target, then you should use the name of the build tag for that target. You can find this value by running '%s list-targets --name=' More information on tags and build targets can be found in the documentation. https://docs.pagure.org/koji/HOWTO/#package-organization (Specify the --help global option for a list of other help options) """ \ % (self.progname, self.progname) def tearDown(self): mock.patch.stopall() @mock.patch('sys.stderr', new_callable=six.StringIO) @mock.patch('koji_cli.commands.ensure_connection') @mock.patch('koji_cli.commands.activate_session') def test_handle_latest_build_without_args(self, activate_session_mock, ensure_connection, stderr): with self.assertRaises(SystemExit) as ex: anon_handle_latest_build(self.options, self.session, []) self.assertExitCode(ex, 2) actual = stderr.getvalue() expected_stderr = \ self.expected_part_help + "%s: error: A tag name must be specified\n" % self.progname self.assertMultiLineEqual(actual, expected_stderr) activate_session_mock.assert_not_called() ensure_connection.assert_not_called() @mock.patch('sys.stderr', new_callable=six.StringIO) @mock.patch('koji_cli.commands.ensure_connection') @mock.patch('koji_cli.commands.activate_session') def test_handle_latest_build_more_args(self, activate_session_mock, ensure_connection, stderr): with self.assertRaises(SystemExit) as ex: anon_handle_latest_build(self.options, self.session, [self.tag_name]) self.assertExitCode(ex, 2) actual = stderr.getvalue() expected_stderr = \ self.expected_part_help + "%s: error: A tag name and package name must " \ "be specified\n" % self.progname self.assertMultiLineEqual(actual, expected_stderr) activate_session_mock.assert_not_called() ensure_connection.called_once() @mock.patch('sys.stderr', new_callable=six.StringIO) @mock.patch('koji_cli.commands.ensure_connection') @mock.patch('koji_cli.commands.activate_session') def test_handle_latest_build_all_and_pkg(self, activate_session_mock, ensure_connection, stderr): with self.assertRaises(SystemExit) as ex: anon_handle_latest_build(self.options, self.session, ['--all', self.tag_name, self.pkg_name]) self.assertExitCode(ex, 2) actual = stderr.getvalue() expected_stderr = \ self.expected_part_help + "%s: error: A package name may not be combined " \ "with --all\n" % self.progname self.assertMultiLineEqual(actual, expected_stderr) activate_session_mock.assert_not_called() ensure_connection.called_once() def test_handle_latest_build_help(self): self.assert_help( anon_handle_latest_build, self.expected_part_help + """Options: -h, --help show this help message and exit --arch=ARCH List all of the latest packages for this arch --all List all of the latest packages for this tag --quiet Do not print the header information --paths Show the file paths --type=TYPE Show builds of the given type only. Currently supported types: maven, win, image, or any custom content generator btypes """) if __name__ == '__main__': unittest.main()