Remove whitespace before comments

Python 2.7 ConfigParser does not accept whitespace before comments.
To keep config files produced by ansible-config_temple parsable
we will strip out the whitespace preceding comments.

Change-Id: I39c88578d0a9e42fded7410d7eb4ec8bd415550c
Related-Bug: #1755821
This commit is contained in:
Georgina Shippey 2019-02-14 11:50:50 +00:00
parent 78bcedc36e
commit 66a97f2b40
3 changed files with 5 additions and 2 deletions

View File

@ -216,8 +216,8 @@ class ConfigTemplateParser(ConfigParser.RawConfigParser):
comments.append('')
continue
if line[0] in '#;':
comments.append(line)
if line.lstrip()[0] in '#;':
comments.append(line.lstrip())
continue
if line.split(None, 1)[0].lower() == 'rem' and line[0] in "rR":

View File

@ -1,3 +1,4 @@
# This comment tests bug 1755821
# A default section comment
# broken into multiple lines
[DEFAULT]

View File

@ -203,6 +203,8 @@
that:
- "(lookup('ini', 'new_key section=DEFAULT file=/tmp/test_with_comments.ini')) == 'new_value'"
- "(lookup('ini', 'baz section=foo file=/tmp/test_with_comments.ini')) == 'bar'"
- "{{ ini_file.content | b64decode | search('# This comment tests bug 1755821')}}"
- "{{ not(ini_file.content | b64decode | search(' # This comment tests bug 1755821'))}}"
- "{{ ini_file.content | b64decode | search('#This is a comment')}}"
- "{{ ini_file.content | b64decode | search('# A default section comment\n# broken into multiple lines\n\\[DEFAULT\\]')}}"