From 46a89155420940059f85450cdbe4104453e4df68 Mon Sep 17 00:00:00 2001
From: Jana Cupova <jcupova@redhat.com>
Date: Mar 18 2021 11:41:09 +0000
Subject: cli: buildinfo returns error for non exist build


Fixes: https://pagure.io/koji/issue/2762

---

diff --git a/cli/koji_cli/commands.py b/cli/koji_cli/commands.py
index 9b4e151..85057b3 100644
--- a/cli/koji_cli/commands.py
+++ b/cli/koji_cli/commands.py
@@ -3227,12 +3227,14 @@ def anon_handle_buildinfo(goptions, session, args):
     if len(args) < 1:
         parser.error(_("Please specify a build"))
     ensure_connection(session, goptions)
+    error_hit = False
     for build in args:
         if build.isdigit():
             build = int(build)
         info = session.getBuild(build)
         if info is None:
             warn("No such build: %s\n" % build)
+            error_hit = True
             continue
         task = None
         if info['task_id']:
@@ -3315,6 +3317,8 @@ def anon_handle_buildinfo(goptions, session, args):
             if changelog:
                 print("Changelog:")
                 print(koji.util.formatChangelog(changelog))
+    if error_hit:
+        error()
 
 
 def anon_handle_hostinfo(goptions, session, args):
diff --git a/tests/test_cli/test_buildinfo.py b/tests/test_cli/test_buildinfo.py
new file mode 100644
index 0000000..14fb830
--- /dev/null
+++ b/tests/test_cli/test_buildinfo.py
@@ -0,0 +1,119 @@
+from __future__ import absolute_import
+import koji
+import mock
+import os
+import time
+import copy
+import locale
+from six.moves import StringIO
+
+from koji_cli.commands import anon_handle_buildinfo
+from . import utils
+
+
+class TestBuildinfo(utils.CliTestCase):
+    def setUp(self):
+        # force locale to compare 'expect' value
+        locale.setlocale(locale.LC_ALL, ('en_US', 'UTF-8'))
+        self.maxDiff = None
+        self.options = mock.MagicMock()
+        self.options.quiet = True
+        self.options.debug = False
+        self.session = mock.MagicMock()
+        self.session.getAPIVersion.return_value = koji.API_VERSION
+        self.original_timezone = os.environ.get('TZ')
+        os.environ['TZ'] = 'UTC'
+        time.tzset()
+        self.taskinfo = {'arch': 'noarch',
+                         'id': 8,
+                         'method': 'build'}
+        self.buildinfo = {'build_id': 1,
+                          'id': 1,
+                          'name': 'test-build',
+                          'release': '1',
+                          'task_id': 8,
+                          'version': '1',
+                          'state': 1,
+                          'completion_ts': 1614869140.368759,
+                          'owner_name': 'kojiadmin',
+                          'volume_name': 'DEFAULT'}
+
+    def tearDown(self):
+        locale.resetlocale()
+        if self.original_timezone is None:
+            del os.environ['TZ']
+        else:
+            os.environ['TZ'] = self.original_timezone
+        time.tzset()
+
+    @mock.patch('sys.stdout', new_callable=StringIO)
+    def test_buildinfo_valid(self, stdout):
+        build = 'test-build-1-1'
+        self.session.getBuild.return_value = self.buildinfo
+        self.session.getTaskInfo.return_value = self.taskinfo
+        self.session.listTags.return_value = []
+        self.session.getMavenBuild.return_value = None
+        self.session.getWinBuild.return_value = None
+        self.session.listArchives.return_value = []
+        self.session.listRPMs.return_value = []
+        expected_stdout = """BUILD: test-build-1-1 [1]
+State: COMPLETE
+Built by: kojiadmin
+Volume: DEFAULT
+Task: 8 build (noarch)
+Finished: Thu, 04 Mar 2021 14:45:40 UTC
+Tags: 
+"""
+        anon_handle_buildinfo(self.options, self.session, [build])
+        self.assert_console_message(stdout, expected_stdout)
+        self.session.listTags.assert_called_once_with(build)
+        self.session.getBuild.assert_called_once_with(build)
+        self.session.getTaskInfo.assert_called_once_with(self.buildinfo['task_id'], request=True)
+        self.session.getMavenBuild.assert_called_once_with(self.buildinfo['id'])
+        self.session.getWinBuild.assert_called_once_with(self.buildinfo['id'])
+        self.session.listRPMs.assert_called_once_with(buildID=self.buildinfo['id'])
+        self.assertEqual(self.session.listArchives.call_count, 4)
+
+    @mock.patch('sys.stderr', new_callable=StringIO)
+    @mock.patch('sys.stdout', new_callable=StringIO)
+    def test_buildinfo_more_build_with_non_exist_build(self, stdout, stderr):
+        build = 'test-build-1-1'
+        non_exist_build = 'test-build-11-12'
+        buildinfo = copy.deepcopy(self.buildinfo)
+        buildinfo['task_id'] = None
+        self.session.getBuild.side_effect = [None, buildinfo]
+        self.session.listTags.return_value = []
+        self.session.getMavenBuild.return_value = None
+        self.session.getWinBuild.return_value = None
+        self.session.listArchives.return_value = []
+        self.session.listRPMs.return_value = []
+        expected_stdout = """BUILD: test-build-1-1 [1]
+State: COMPLETE
+Built by: kojiadmin
+Volume: DEFAULT
+Task: none
+Finished: Thu, 04 Mar 2021 14:45:40 UTC
+Tags: 
+"""
+        expected_error = "No such build: %s\n\n" % non_exist_build
+        with self.assertRaises(SystemExit) as ex:
+            anon_handle_buildinfo(self.options, self.session, [non_exist_build, build])
+        self.assertExitCode(ex, 1)
+        self.assert_console_message(stderr, expected_error)
+        self.assert_console_message(stdout, expected_stdout)
+        self.session.listTags.assert_called_once_with(build)
+        self.session.getMavenBuild.assert_called_once_with(self.buildinfo['id'])
+        self.session.getWinBuild.assert_called_once_with(self.buildinfo['id'])
+        self.session.listRPMs.assert_called_once_with(buildID=self.buildinfo['id'])
+        self.assertEqual(self.session.getBuild.call_count, 2)
+        self.assertEqual(self.session.listArchives.call_count, 4)
+
+    @mock.patch('sys.stderr', new_callable=StringIO)
+    def test_buildinfo_non_exist_build(self, stderr):
+        non_exist_build = 'test-build-11-12'
+        self.session.getBuild.return_value = None
+        expected = "No such build: %s\n\n" % non_exist_build
+        with self.assertRaises(SystemExit) as ex:
+            anon_handle_buildinfo(self.options, self.session, [non_exist_build])
+        self.assertExitCode(ex, 1)
+        self.assert_console_message(stderr, expected)

