From 436465d66b85725a098f34ffb651dc9e54cafba8 Mon Sep 17 00:00:00 2001 From: Yuming Zhu Date: Apr 18 2018 22:37:11 +0000 Subject: hub: throw GenericError in `get_archive_file()` relates: #719 --- diff --git a/hub/kojihub.py b/hub/kojihub.py index e70a860..de02382 100644 --- a/hub/kojihub.py +++ b/hub/kojihub.py @@ -4337,11 +4337,13 @@ def get_archive_file(archive_id, filename): size: uncompressed size of the file (integer) """ files = list_archive_files(archive_id) + if not files: + raise koji.GenericError('Archive#%s doesn\'t contain any files' % archive_id) for file_info in files: if file_info['name'] == filename: return file_info - #otherwise - return None + else: + raise koji.GenericError('No such file: %s in archive#%s' % (filename, archive_id)) def list_task_output(taskID, stat=False, all_volumes=False): """List the files generated by the task with the given ID. This diff --git a/tests/test_hub/test_get_archive_file.py b/tests/test_hub/test_get_archive_file.py new file mode 100644 index 0000000..4470c2f --- /dev/null +++ b/tests/test_hub/test_get_archive_file.py @@ -0,0 +1,43 @@ +import unittest +import mock + +import koji +import kojihub + +FILES = [{'archive_id': 1, + 'name': 'archive1.zip', + 'size': 1024}, + {'archive_id': 1, + 'name': 'archive2.jar', + 'size': 4096}] + +EMPTY_FILES = [] + + +class TestGetArchiveFile(unittest.TestCase): + + @mock.patch('kojihub.list_archive_files') + def test_simple(self, list_archive_files): + list_archive_files.return_value = FILES + + rv = kojihub.get_archive_file(1, 'archive1.zip') + list_archive_files.assert_called_with(1) + self.assertEqual(rv, FILES[0]) + + @mock.patch('kojihub.list_archive_files') + def test_empty_files(self, list_archive_files): + list_archive_files.return_value = EMPTY_FILES + + with self.assertRaises(koji.GenericError) as cm: + kojihub.get_archive_file(1, 'archive1.zip') + list_archive_files.assert_called_with(1) + self.assertEqual(cm.exception.args[0], 'Archive#1 doesn\'t contain any files') + + @mock.patch('kojihub.list_archive_files') + def test_non_existing_file(self, list_archive_files): + list_archive_files.return_value = FILES + + with self.assertRaises(koji.GenericError) as cm: + kojihub.get_archive_file(1, 'archive3.xml') + list_archive_files.assert_called_with(1) + self.assertEqual(cm.exception.args[0], 'No such file: archive3.xml in archive#1')