From 012e3ae206d3b0039f5f8c3ca7a8aef45af396f6 Mon Sep 17 00:00:00 2001 From: Jakub Jirutka Date: Sun, 19 Jan 2014 02:24:26 +0100 Subject: [PATCH] Fixes #5679: lineinfile ignores newline in line argument --- library/files/lineinfile | 6 +++++- test/TestRunner.py | 17 +++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/library/files/lineinfile b/library/files/lineinfile index 82658cae654..e05652a1f9b 100644 --- a/library/files/lineinfile +++ b/library/files/lineinfile @@ -348,7 +348,11 @@ def main(): if ins_bef is None and ins_aft is None: ins_aft = 'EOF' - present(module, dest, params['regexp'], params['line'], + # Replace the newline character with an actual newline. Don't replace + # escaped \\n, hence sub and not str.replace. + line = re.sub(r'\n', os.linesep, params['line']) + + present(module, dest, params['regexp'], line, ins_aft, ins_bef, create, backup, backrefs) else: if params['regexp'] is None and params.get('line', None) is None: diff --git a/test/TestRunner.py b/test/TestRunner.py index 67e658cc7d9..3eb8be40b14 100644 --- a/test/TestRunner.py +++ b/test/TestRunner.py @@ -512,6 +512,23 @@ class TestRunner(unittest.TestCase): result = self._run(*testcase) assert result['failed'] + # insert multiline at the end of the file + testline1 = '#12: The \\n character replaced with' + testline2 = 'an actual newline.' + testcase = ('lineinfile', [ + "dest=%s" % sample, + "regexp='^#12: '", + "line='%s\n%s'" % (testline1, testline2) + ]) + result = self._run(*testcase) + assert result['changed'] + assert result['msg'] == 'line added' + artifact = [x.strip() for x in open(sample)] + assert artifact[-2] == testline1 + assert artifact[-1] == testline2 + assert artifact.count(testline1) == 1 + assert artifact.count(testline2) == 1 + # cleanup os.unlink(sample)