Skip to content

Commit

Permalink
Merge branch 'formatter-false-values' into develop
Browse files Browse the repository at this point in the history
* formatter-false-values:
  Remove unused imports
  Explicitly check for an empty dict
  Don't print response only if an empty dict/list/str
  • Loading branch information
jamesls committed Sep 16, 2015
2 parents 6c4cb6c + d8820ac commit 1cbd32e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 11 deletions.
5 changes: 2 additions & 3 deletions awscli/formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.
import logging
import sys
from botocore.compat import json

from botocore.utils import set_value_from_jmespath
Expand Down Expand Up @@ -91,9 +90,9 @@ def _format_response(self, command_name, response, stream):
# the response will be an empty string. We don't want to print
# that out to the user but other "falsey" values like an empty
# dictionary should be printed.
if response:
if response != {}:
json.dump(response, stream, indent=4, default=json_encoder,
ensure_ascii=False)
ensure_ascii=False)
stream.write('\n')


Expand Down
34 changes: 26 additions & 8 deletions tests/unit/output/test_json_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,29 @@

class TestGetPasswordData(BaseAWSCommandParamsTest):

prefix = 'iam add-user-to-group '
COMMAND = 'iam add-user-to-group --group-name foo --user-name bar'

def test_empty_response_prints_nothing(self):
def setUp(self):
super(TestGetPasswordData, self).setUp()
self.parsed_response = {}

def test_empty_dict_response_prints_nothing(self):
# This is the default response, but we want to be explicit
# that we're returning an empty dict.
self.parsed_response = {}
args = ' --group-name foo --user-name bar'
cmdline = self.prefix + args
result = {'GroupName': 'foo', 'UserName': 'bar'}
stdout = self.assert_params_for_cmd(cmdline, result, expected_rc=0)[0]
# We should have printed nothing because the parsed response
# is an empty dict: {}.
stdout = self.run_cmd(self.COMMAND, expected_rc=0)[0]
self.assertEqual(stdout, '')

def test_empty_list_prints_list(self):
self.parsed_response = []
stdout = self.run_cmd(self.COMMAND, expected_rc=0)[0]
self.assertEqual(stdout, '[]\n')

def test_empty_string_prints_nothing(self):
self.parsed_response = ''
stdout = self.run_cmd(self.COMMAND, expected_rc=0)[0]
self.assertEqual(stdout, '""\n')


class TestListUsers(BaseAWSCommandParamsTest):

Expand Down Expand Up @@ -76,6 +85,15 @@ def test_jmespath_json_response(self):
parsed_output = json.loads(output)
self.assertEqual(parsed_output, ['testuser-50', 'testuser-51'])

def test_zero_value_is_printed(self):
# Even though the integer 0 is false-like, we still
# should be printing it to stdout if a jmespath query
# evalutes to 0.
jmespath_query = '`0`'
output = self.run_cmd('iam list-users --query %s' % jmespath_query,
expected_rc=0)[0]
self.assertEqual(output, '0\n')

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
Expand Down

0 comments on commit 1cbd32e

Please sign in to comment.