mirror of
https://github.com/ansible-collections/community.general.git
synced 2026-05-06 13:22:48 +00:00
Added additional lineinfile documentation.
A little more unit testing.
This commit is contained in:
@@ -456,7 +456,7 @@ class TestRunner(unittest.TestCase):
|
||||
# The order of the test cases is important
|
||||
|
||||
# The regexp doesn't match, so the line will not be added anywhere.
|
||||
testline = '\\1: Line added by default at the end of the file.'
|
||||
testline = r'\\1: Line added by default at the end of the file.'
|
||||
testcase = ('lineinfile', [
|
||||
"dest=%s" % sample,
|
||||
"regexp='^(First): '",
|
||||
@@ -471,7 +471,7 @@ class TestRunner(unittest.TestCase):
|
||||
|
||||
# insertafter with EOF
|
||||
# The regexp doesn't match, so the line will not be added anywhere.
|
||||
testline = '\\1: Line added with insertafter=EOF'
|
||||
testline = r'\\1: Line added with insertafter=EOF'
|
||||
testcase = ('lineinfile', [
|
||||
"dest=%s" % sample,
|
||||
"insertafter=EOF",
|
||||
@@ -487,7 +487,7 @@ class TestRunner(unittest.TestCase):
|
||||
|
||||
# with invalid insertafter regex
|
||||
# The regexp doesn't match, so do nothing.
|
||||
testline = '\\1: Line added with an invalid insertafter regex'
|
||||
testline = r'\\1: Line added with an invalid insertafter regex'
|
||||
testcase = ('lineinfile', [
|
||||
"dest=%s" % sample,
|
||||
"insertafter='^abcdefgh'",
|
||||
@@ -501,7 +501,7 @@ class TestRunner(unittest.TestCase):
|
||||
|
||||
# with an insertafter regex
|
||||
# The regexp doesn't match, so do nothing.
|
||||
testline = '\\1: Line added with a valid insertafter regex'
|
||||
testline = r'\\1: Line added with a valid insertafter regex'
|
||||
testcase = ('lineinfile', [
|
||||
"dest=%s" % sample,
|
||||
"insertafter='^receive messages to '",
|
||||
@@ -520,8 +520,8 @@ class TestRunner(unittest.TestCase):
|
||||
target_line = 'combination of microphone, speaker, keyboard and display. It can send and'
|
||||
idx = artifact.index(target_line)
|
||||
|
||||
testline = '\\\\1 of microphone'
|
||||
testline_after = 'combination of microphone'
|
||||
testline = r'\\1 of megaphone'
|
||||
testline_after = 'combination of megaphone'
|
||||
testcase = ('lineinfile', [
|
||||
"dest=%s" % sample,
|
||||
"regexp='(combination) of microphone'",
|
||||
@@ -536,6 +536,39 @@ class TestRunner(unittest.TestCase):
|
||||
assert artifact.index(testline_after) == idx
|
||||
assert target_line not in artifact
|
||||
|
||||
# Go again, should be unchanged now.
|
||||
testline = r'\\1 of megaphone'
|
||||
testline_after = 'combination of megaphone'
|
||||
testcase = ('lineinfile', [
|
||||
"dest=%s" % sample,
|
||||
"regexp='(combination) of megaphone'",
|
||||
"line='%s'" % testline,
|
||||
"backrefs=yes",
|
||||
])
|
||||
result = self._run(*testcase)
|
||||
assert not result['changed']
|
||||
assert result['msg'] == ''
|
||||
|
||||
# Try a numeric, named capture group example.
|
||||
f = open(sample, 'a+')
|
||||
f.write("1 + 1 = 3" + os.linesep)
|
||||
f.close()
|
||||
testline = r"2 + \\g<num> = 3"
|
||||
testline_after = "2 + 1 = 3"
|
||||
testcase = ('lineinfile', [
|
||||
"dest=%s" % sample,
|
||||
r"regexp='1 \\+ (?P<num>\\d) = 3'",
|
||||
"line='%s'" % testline,
|
||||
"backrefs=yes",
|
||||
])
|
||||
result = self._run(*testcase)
|
||||
artifact = [x.strip() for x in open(sample)]
|
||||
assert result['changed']
|
||||
assert result['msg'] == 'line replaced'
|
||||
artifact = [x.strip() for x in open(sample)]
|
||||
assert '1 + 1 = 3' not in artifact
|
||||
assert testline_after == artifact[-1]
|
||||
|
||||
# with both insertafter and insertbefore (should fail)
|
||||
testline = 'Seventh: this line should not be there'
|
||||
testcase = ('lineinfile', [
|
||||
|
||||
Reference in New Issue
Block a user