summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRyan Petrello <lists@ryanpetrello.com>2014-04-03 13:00:08 -0400
committerRyan Petrello <lists@ryanpetrello.com>2014-04-03 18:00:22 -0400
commite9973ad72861ffbbacb0d4471ea7afc80ce31fe8 (patch)
tree6db1b09895e57fd45d6a1c49a5f57e99dbbb40fa
parenta52c09fa34da16aea1a235f66267d07ac0ae4ed6 (diff)
downloadcliff-e9973ad72861ffbbacb0d4471ea7afc80ce31fe8.tar.gz
Fix a bug in ShellFormatter's escaping of double quotes in strings.
Fixes bug: 1302066 Change-Id: I7fe066165ce11cfe977a18c0e49a55ff84ba4187
-rw-r--r--cliff/formatters/shell.py7
-rw-r--r--cliff/tests/test_formatters_shell.py16
2 files changed, 20 insertions, 3 deletions
diff --git a/cliff/formatters/shell.py b/cliff/formatters/shell.py
index 385d48c..fd4f29e 100644
--- a/cliff/formatters/shell.py
+++ b/cliff/formatters/shell.py
@@ -3,6 +3,8 @@
from .base import SingleFormatter
+import six
+
class ShellFormatter(SingleFormatter):
@@ -34,6 +36,7 @@ class ShellFormatter(SingleFormatter):
desired_columns = parsed_args.variables
for name, value in zip(variable_names, data):
if name in desired_columns or not desired_columns:
- stdout.write('%s%s="%s"\n' % (parsed_args.prefix, name,
- value.replace('"', '\\"')))
+ if isinstance(value, six.string_types):
+ value = value.replace('"', '\\"')
+ stdout.write('%s%s="%s"\n' % (parsed_args.prefix, name, value))
return
diff --git a/cliff/tests/test_formatters_shell.py b/cliff/tests/test_formatters_shell.py
index 93452ba..e6813e5 100644
--- a/cliff/tests/test_formatters_shell.py
+++ b/cliff/tests/test_formatters_shell.py
@@ -1,5 +1,5 @@
#!/usr/bin/env python
-from six import StringIO
+from six import StringIO, text_type
from cliff.formatters import shell
@@ -18,3 +18,17 @@ def test_shell_formatter():
sf.emit_one(c, d, output, args)
actual = output.getvalue()
assert expected == actual
+
+
+def test_shell_formatter_with_non_string_values():
+ sf = shell.ShellFormatter()
+ c = ('a', 'b', 'c', 'd', 'e')
+ d = (True, False, 100, '"esc"', text_type('"esc"'))
+ expected = 'a="True"\nb="False"\nc="100"\nd="\\"esc\\""\ne="\\"esc\\""\n'
+ output = StringIO()
+ args = mock.Mock()
+ args.variables = ['a', 'b', 'c', 'd', 'e']
+ args.prefix = ''
+ sf.emit_one(c, d, output, args)
+ actual = output.getvalue()
+ assert expected == actual