From 97c14e034fa79dfe55d12841878b77c644e28bfe Mon Sep 17 00:00:00 2001 From: Clint Byrum Date: Thu, 16 Jan 2014 12:10:55 -0800 Subject: Escape double quotes in shell formatter Before this change the shell formatter could not be trusted to output anything with double quotes. With this change we can pass the output directly to the shell for evaluation. Change-Id: I8f2bf23a1523f6f02e5ce8563038b20e079b52da Closes-Bug: #1269908 --- cliff/formatters/shell.py | 3 ++- cliff/tests/test_formatters_shell.py | 8 ++++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/cliff/formatters/shell.py b/cliff/formatters/shell.py index d1c392b..385d48c 100644 --- a/cliff/formatters/shell.py +++ b/cliff/formatters/shell.py @@ -34,5 +34,6 @@ 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)) + stdout.write('%s%s="%s"\n' % (parsed_args.prefix, name, + value.replace('"', '\\"'))) return diff --git a/cliff/tests/test_formatters_shell.py b/cliff/tests/test_formatters_shell.py index 5d49e65..93452ba 100644 --- a/cliff/tests/test_formatters_shell.py +++ b/cliff/tests/test_formatters_shell.py @@ -8,12 +8,12 @@ import mock def test_shell_formatter(): sf = shell.ShellFormatter() - c = ('a', 'b', 'c') - d = ('A', 'B', 'C') - expected = 'a="A"\nb="B"\n' + c = ('a', 'b', 'c', 'd') + d = ('A', 'B', 'C', '"escape me"') + expected = 'a="A"\nb="B"\nd="\\"escape me\\""\n' output = StringIO() args = mock.Mock() - args.variables = ['a', 'b'] + args.variables = ['a', 'b', 'd'] args.prefix = '' sf.emit_one(c, d, output, args) actual = output.getvalue() -- cgit v1.2.1