diff --git a/src/composer/cli/utilities.py b/src/composer/cli/utilities.py index 5170c636..97f348e0 100644 --- a/src/composer/cli/utilities.py +++ b/src/composer/cli/utilities.py @@ -58,17 +58,28 @@ def handle_api_result(result, show_json=False): :param result: JSON result from the http query :type result: dict + :rtype: tuple + :returns: (rc, errors) + + Return the correct rc for the program (0 or 1), and whether or + not to continue processing the results. """ if show_json: print(json.dumps(result, indent=4)) - - for err in result.get("errors", []): - log.error(err) - - if result["status"] == True: - return 0 else: - return 1 + for err in result.get("errors", []): + log.error(err) + + # What's the rc? If status is present, use that + # If not, use length of errors + if "status" in result: + rc = bool(not result["status"]) + else: + rc = bool(len(result.get("errors", [])) > 0) + + # Caller should return if showing json, or status was present and False + exit_now = show_json or ("status" in result and rc) + return (rc, exit_now) def packageNEVRA(pkg): """Return the package info as a NEVRA diff --git a/tests/composer/test_utilities.py b/tests/composer/test_utilities.py index 9005e1ad..d8e5d58e 100644 --- a/tests/composer/test_utilities.py +++ b/tests/composer/test_utilities.py @@ -17,6 +17,7 @@ import unittest from composer.cli.utilities import argify, toml_filename, frozen_toml_filename, packageNEVRA +from composer.cli.utilities import handle_api_result class CliUtilitiesTest(unittest.TestCase): def test_argify(self): @@ -45,3 +46,48 @@ class CliUtilitiesTest(unittest.TestCase): "version": "10.0"} self.assertEqual(packageNEVRA(epoch_0), "basesystem-10.0-7.el7.noarch") self.assertEqual(packageNEVRA(epoch_3), "basesystem-3:10.0-7.el7.noarch") + + def test_api_result_1(self): + """Test a result with no status and no error fields""" + result = {"foo": "bar"} + self.assertEqual(handle_api_result(result, show_json=False), (0, False)) + + def test_api_result_2(self): + """Test a result with errors=["some error"], and no status field""" + result = {"foo": "bar", "errors": ["some error"]} + self.assertEqual(handle_api_result(result, show_json=False), (1, False)) + + def test_api_result_3(self): + """Test a result with status=True, and errors=[]""" + result = {"status": True, "errors": []} + self.assertEqual(handle_api_result(result, show_json=False), (0, False)) + + def test_api_result_4(self): + """Test a result with status=False, and errors=[]""" + result = {"status": False, "errors": []} + self.assertEqual(handle_api_result(result, show_json=False), (1, True)) + + def test_api_result_5(self): + """Test a result with status=False, and errors=["some error"]""" + result = {"status": False, "errors": ["some error"]} + self.assertEqual(handle_api_result(result, show_json=False), (1, True)) + + def test_api_result_6(self): + """Test a result with show_json=True, and no status or errors fields""" + result = {"foo": "bar"} + self.assertEqual(handle_api_result(result, show_json=True), (0, True)) + + def test_api_result_7(self): + """Test a result with show_json=True, status=False, and errors=["some error"]""" + result = {"status": False, "errors": ["some error"]} + self.assertEqual(handle_api_result(result, show_json=True), (1, True)) + + def test_api_result_8(self): + """Test a result with show_json=True, errors=["some error"], and no status field""" + result = {"foo": "bar", "errors": ["some error"]} + self.assertEqual(handle_api_result(result, show_json=True), (1, True)) + + def test_api_result_9(self): + """Test a result with show_json=True, errors=[], and no status field""" + result = {"foo": "bar", "errors": []} + self.assertEqual(handle_api_result(result, show_json=True), (0, True))