diff options
author | Clint Byrum <clint@fewbar.com> | 2014-01-16 12:10:55 -0800 |
---|---|---|
committer | Clint Byrum <clint@fewbar.com> | 2014-02-03 13:45:38 -0800 |
commit | 97c14e034fa79dfe55d12841878b77c644e28bfe (patch) | |
tree | 5b46a7fd7b694228944bf931bcf3d1ddc4abd4bf | |
parent | e447666860730039eb1a3fa040f79d6b8a852fd5 (diff) | |
download | cliff-97c14e034fa79dfe55d12841878b77c644e28bfe.tar.gz |
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
-rw-r--r-- | cliff/formatters/shell.py | 3 | ||||
-rw-r--r-- | 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() |