Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion SoftLayer/CLI/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
3: logging.DEBUG
}

VALID_FORMATS = ['table', 'raw', 'json']
VALID_FORMATS = ['table', 'raw', 'json', 'jsonraw']
DEFAULT_FORMAT = 'raw'
if sys.stdout.isatty():
DEFAULT_FORMAT = 'table'
Expand Down
5 changes: 4 additions & 1 deletion SoftLayer/CLI/formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def format_output(data, fmt='table'): # pylint: disable=R0911,R0912
:param string fmt (optional): One of: table, raw, json, python
"""
if isinstance(data, utils.string_types):
if fmt == 'json':
if fmt in ('json', 'jsonraw'):
return json.dumps(data)
return data

Expand All @@ -46,6 +46,9 @@ def format_output(data, fmt='table'): # pylint: disable=R0911,R0912
format_output(data, fmt='python'),
indent=4,
cls=CLIJSONEncoder)
elif fmt == 'jsonraw':
return json.dumps(format_output(data, fmt='python'),
CLIJSONEncoder)
elif fmt == 'python':
return data.to_python()

Expand Down
40 changes: 40 additions & 0 deletions tests/CLI/helper_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,21 @@ def test_format_output_json(self):
ret = formatting.format_output('test', 'json')
self.assertEqual('"test"', ret)

def test_format_output_jsonraw(self):
t = formatting.Table(['nothing'])
t.align['nothing'] = 'c'
t.add_row(['testdata'])
t.add_row([formatting.blank()])
t.sortby = 'nothing'
ret = formatting.format_output(t, 'jsonraw')
# This uses json.dumps due to slight changes in the output between
# py3.3 and py3.4
expected = json.dumps([{'nothing': 'testdata'}, {'nothing': None}])
self.assertEqual(expected, ret)

ret = formatting.format_output('test', 'json')
self.assertEqual('"test"', ret)

def test_format_output_json_keyvaluetable(self):
t = formatting.KeyValueTable(['key', 'value'])
t.add_row(['nothing', formatting.blank()])
Expand All @@ -315,6 +330,21 @@ def test_format_output_json_keyvaluetable(self):
"nothing": null
}''', ret)

def test_format_output_jsonraw_keyvaluetable(self):
t = formatting.KeyValueTable(['key', 'value'])
t.add_row(['nothing', formatting.blank()])
t.sortby = 'nothing'
ret = formatting.format_output(t, 'jsonraw')
self.assertEqual('''{"nothing": null}''', ret)

def test_format_output_json_string(self):
ret = formatting.format_output("test", 'json')
self.assertEqual('"test"', ret)

def test_format_output_jsonraw_string(self):
ret = formatting.format_output("test", 'jsonraw')
self.assertEqual('"test"', ret)

def test_format_output_formatted_item(self):
item = formatting.FormattedItem('test', 'test_formatted')
ret = formatting.format_output(item, 'table')
Expand Down Expand Up @@ -380,6 +410,16 @@ def test_format_output_unicode(self):
t = formatting.format_output(item, 'raw')
self.assertEqual('raw ☃', t)

def test_format_output_table_invalid_sort(self):
t = formatting.Table(['nothing'])
t.align['nothing'] = 'c'
t.add_row(['testdata'])
t.sortby = 'DOES NOT EXIST'
self.assertRaises(
exceptions.CLIHalt,
formatting.format_output, t, 'table',
)


class TestTemplateArgs(testing.TestCase):

Expand Down