1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

Fix literal block multiline parsing

Fixes #8394
This commit is contained in:
James Cammarata 2014-08-04 09:31:30 -05:00
parent 07bb7e5a3b
commit af0d8cda7b
4 changed files with 102 additions and 46 deletions

View file

@ -76,7 +76,7 @@ def split_args(args):
do_decode = True
except UnicodeDecodeError:
do_decode = False
tokens = args.split(' ')
items = args.split(' ')
# iterate over the tokens, and reassemble any that may have been
# split on a space inside a jinja2 block.
@ -92,9 +92,23 @@ def split_args(args):
block_depth = 0 # used to count nested jinja2 {% %} blocks
comment_depth = 0 # used to count nested jinja2 {# #} blocks
# now we loop over each split token, coalescing tokens if the white space
# now we loop over each split chunk, coalescing tokens if the white space
# split occurred within quotes or a jinja2 block of some kind
for token in tokens:
for item in items:
# we split on spaces and newlines separately, so that we
# can tell which character we split on for reassembly
# inside quotation characters
tokens = item.split('\n')
for idx,token in enumerate(tokens):
# if we're at the end of the enumeration, the character separator
# used when reassembling quoted bits should be a space, otherwise
# it will be a newline character
spacer = ' '
if idx > 0:
spacer = '\n'
# store the previous quoting state for checking later
was_inside_quotes = inside_quotes
@ -115,7 +129,7 @@ def split_args(args):
params.append(token)
appended = True
elif print_depth or block_depth or comment_depth or inside_quotes or was_inside_quotes:
params[-1] = "%s %s" % (params[-1], token)
params[-1] = "%s%s%s" % (params[-1], spacer, token)
appended = True
# if the number of paired block tags is not the same, the depth has changed, so we calculate that here

View file

@ -168,3 +168,23 @@
assert:
that:
- "shell_result4.changed == False"
- name: execute a shell command using a literal multiline block
shell: |
echo this is a
"multiline echo"
"with a new line
in quotes"
| md5sum
| tr -s ' '
| cut -f1 -d ' '
register: shell_result5
- debug: var=shell_result5
- name: assert the multiline shell command ran as expected
assert:
that:
- "shell_result5.changed"
- "shell_result5.stdout == '32f3cc201b69ed8afa3902b80f554ca8'"

View file

@ -159,3 +159,24 @@
assert:
that:
- "not copy_result5|changed"
# issue 8394
- name: create a file with content and a literal multiline block
copy: |
content='this is the first line
this is the second line
this line is after an empty line
this line is the last line
'
dest={{output_dir}}/multiline.txt
register: copy_result6
- debug: var=copy_result6
- name: assert the multiline file was created correctly
assert:
that:
- "copy_result6.changed"
- "copy_result6.dest == '{{output_dir|expanduser}}/multiline.txt'"
- "copy_result6.md5sum == '1627d51e7e607c92cf1a502bf0c6cce3'"

View file

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