From 66a97f2b403252d2b6f8aaa38f17deccbd753b4c Mon Sep 17 00:00:00 2001 From: Georgina Shippey Date: Thu, 14 Feb 2019 11:50:50 +0000 Subject: [PATCH] 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 --- action/config_template.py | 4 ++-- tests/templates/test_with_comments.ini | 1 + tests/test-common-tasks.yml | 2 ++ 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/action/config_template.py b/action/config_template.py index d9c97e2..fbf6508 100644 --- a/action/config_template.py +++ b/action/config_template.py @@ -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": diff --git a/tests/templates/test_with_comments.ini b/tests/templates/test_with_comments.ini index 79d3644..85a351e 100644 --- a/tests/templates/test_with_comments.ini +++ b/tests/templates/test_with_comments.ini @@ -1,3 +1,4 @@ + # This comment tests bug 1755821 # A default section comment # broken into multiple lines [DEFAULT] diff --git a/tests/test-common-tasks.yml b/tests/test-common-tasks.yml index 0d94a00..3d07e47 100644 --- a/tests/test-common-tasks.yml +++ b/tests/test-common-tasks.yml @@ -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\\]')}}"