Preserve all line breaks in literal blocks

Fixes #8512
This commit is contained in:
James Cammarata 2014-08-08 11:54:38 -05:00
parent 039e4571b9
commit 63bf2f6d9a
4 changed files with 35 additions and 12 deletions

View file

@ -101,15 +101,22 @@ def split_args(args):
# inside quotation characters # inside quotation characters
tokens = item.split('\n') tokens = item.split('\n')
line_continuation = False
for idx,token in enumerate(tokens): for idx,token in enumerate(tokens):
# if we're at the end of the enumeration, the character separator # if there was a newline split, we will re-assemble using
# used when reassembling quoted bits should be a space, otherwise # newlines, otherwise we re-assemble using spaces
# it will be a newline character
spacer = ' ' spacer = ' '
if idx > 0: if idx > 0:
spacer = '\n' spacer = '\n'
# if we hit a line continuation character, but
# we're not inside quotes, ignore it and continue
# on to the next token while setting a flag
if token == '\\' and not inside_quotes:
line_continuation = True
continue
# store the previous quoting state for checking later # store the previous quoting state for checking later
was_inside_quotes = inside_quotes was_inside_quotes = inside_quotes
quote_char = _get_quote_state(token, quote_char) quote_char = _get_quote_state(token, quote_char)
@ -155,7 +162,19 @@ def split_args(args):
# finally, if we're at zero depth for all blocks and not inside quotes, and have not # finally, if we're at zero depth for all blocks and not inside quotes, and have not
# yet appended anything to the list of params, we do so now # yet appended anything to the list of params, we do so now
if not (print_depth or block_depth or comment_depth) and not inside_quotes and not appended and token != '': if not (print_depth or block_depth or comment_depth) and not inside_quotes and not appended and token != '':
# if the spacer was a newline, prepend this param
# with it so that newlines are preserved, unless
# we previously hit a line continuation character
if spacer == '\n':
if line_continuation:
params.append(token) params.append(token)
else:
params.append('\n%s' % token)
else:
params.append(token)
# always clear the line continuation flag
line_continuation = False
# If we're done and things are not at zero depth or we're still inside quotes, # If we're done and things are not at zero depth or we're still inside quotes,
# raise an error to indicate that the args were unbalanced # raise an error to indicate that the args were unbalanced
@ -165,6 +184,7 @@ def split_args(args):
# finally, we decode each param back to the unicode it was in the arg string # finally, we decode each param back to the unicode it was in the arg string
if do_decode: if do_decode:
params = [x.decode('utf-8') for x in params] params = [x.decode('utf-8') for x in params]
return params return params
def unquote(data): def unquote(data):

View file

@ -704,7 +704,7 @@ def parse_kv(args):
for x in vargs: for x in vargs:
if "=" in x: if "=" in x:
k, v = x.split("=",1) k, v = x.split("=",1)
options[k] = unquote(v.strip()) options[k.strip()] = unquote(v.strip())
return options return options
def merge_hash(a, b): def merge_hash(a, b):

View file

@ -170,14 +170,17 @@
- "shell_result4.changed == False" - "shell_result4.changed == False"
- name: execute a shell command using a literal multiline block - name: execute a shell command using a literal multiline block
args:
executable: /bin/bash
shell: | shell: |
echo this is a echo this is a
"multiline echo" "multiline echo"
"with a new line "with a new line
in quotes" in quotes" \
| md5sum | md5sum \
| tr -s ' ' | tr -s ' ' \
| cut -f1 -d ' ' | cut -f1 -d ' '
echo "this is a second line"
register: shell_result5 register: shell_result5
- debug: var=shell_result5 - debug: var=shell_result5
@ -186,5 +189,5 @@
assert: assert:
that: that:
- "shell_result5.changed" - "shell_result5.changed"
- "shell_result5.stdout == '32f3cc201b69ed8afa3902b80f554ca8'" - "shell_result5.stdout == '32f3cc201b69ed8afa3902b80f554ca8\nthis is a second line'"

View file

@ -705,9 +705,9 @@ class TestUtils(unittest.TestCase):
# jinja2 loop blocks with lots of complexity # jinja2 loop blocks with lots of complexity
_test_combo( _test_combo(
# in memory of neighbors cat # in memory of neighbors cat
# we only preserve newlines inside of quotes # we preserve line breaks unless a line continuation character preceeds them
'a {% if x %} y {%else %} {{meow}} {% endif %} "cookie\nchip"\ndone', 'a {% if x %} y {%else %} {{meow}} {% endif %} "cookie\nchip" \\\ndone\nand done',
['a', '{% if x %}', 'y', '{%else %}', '{{meow}}', '{% endif %}', '"cookie\nchip"', 'done'] ['a', '{% if x %}', 'y', '{%else %}', '{{meow}}', '{% endif %}', '"cookie\nchip"', 'done', '\nand', 'done']
) )
# test space preservation within quotes # test space preservation within quotes