From c4ce95172667c91dde09b095945e5b0fc68cf2dd Mon Sep 17 00:00:00 2001 From: James Saryerwinnie Date: Fri, 21 Mar 2014 10:31:06 -0700 Subject: [PATCH] Raise exception when bad output format is given While argparse handles the case where it's specified as an arugment, we need to validate the case when output is specified as an env var and a config var. --- awscli/formatter.py | 2 +- tests/unit/output/test_json_output.py | 14 +++++++------- tests/unit/test_clidriver.py | 7 +++++++ 3 files changed, 15 insertions(+), 8 deletions(-) diff --git a/awscli/formatter.py b/awscli/formatter.py index 3c53e2e5f32a..a9e8c6b09c45 100644 --- a/awscli/formatter.py +++ b/awscli/formatter.py @@ -249,4 +249,4 @@ def get_formatter(format_type, args): return TextFormatter(args) elif format_type == 'table': return TableFormatter(args) - return None + raise ValueError("Unknown output type: %s" % format_type) diff --git a/tests/unit/output/test_json_output.py b/tests/unit/output/test_json_output.py index d064ca225034..fe21bad112a1 100644 --- a/tests/unit/output/test_json_output.py +++ b/tests/unit/output/test_json_output.py @@ -13,12 +13,6 @@ # language governing permissions and limitations under the License. from tests.unit import BaseAWSCommandParamsTest import json -import os -import sys -import re - -from six.moves import cStringIO -import mock class TestGetPasswordData(BaseAWSCommandParamsTest): @@ -26,7 +20,6 @@ class TestGetPasswordData(BaseAWSCommandParamsTest): prefix = 'iam add-user-to-group ' def test_empty_response_prints_nothing(self): - captured = cStringIO() # This is the default response, but we want to be explicit # that we're returning an empty dict. self.parsed_response = {} @@ -76,3 +69,10 @@ def test_jmespath_json_response(self): expected_rc=0)[0] parsed_output = json.loads(output) self.assertEqual(parsed_output, ['testuser-50', 'testuser-51']) + + def test_unknown_output_type_from_env_var(self): + # argparse already handles the case with a bad --output + # specified on the CLI, we need to verify that a bad + # output format from the env var still gives an error. + self.environ['AWS_DEFAULT_OUTPUT'] = 'bad-output-type' + self.run_cmd('iam list-users', expected_rc=255) diff --git a/tests/unit/test_clidriver.py b/tests/unit/test_clidriver.py index fcbd3749f6c2..1a3a7970b070 100644 --- a/tests/unit/test_clidriver.py +++ b/tests/unit/test_clidriver.py @@ -24,6 +24,7 @@ from awscli.clidriver import create_clidriver from awscli.clidriver import CustomArgument from awscli.clidriver import CLIOperationCaller +from awscli import formatter from botocore.hooks import HierarchicalEmitter from botocore.provider import Provider @@ -526,5 +527,11 @@ def test_verify_argument_is_none_by_default(self): self.assertIsNone(self.recorded_args.verify_ssl) +class TestFormatter(BaseAWSCommandParamsTest): + def test_bad_output(self): + with self.assertRaises(ValueError): + formatter.get_formatter('bad-type', None) + + if __name__ == '__main__': unittest.main()