Add unit test for file.line ensuring line between two lines but content already exists

This commit is contained in:
Bo Maryniuk 2017-10-31 12:44:08 +01:00
parent c7a320ae8e
commit c2d9d8f8ea

View File

@ -1056,6 +1056,27 @@ class FilemodLineTests(TestCase, LoaderModuleMockMixin):
assert 1 == len(atomic_opener().write.call_args_list)
assert file_modified == atomic_opener().write.call_args_list[0][0][0]
@patch('os.path.realpath', MagicMock())
@patch('os.path.isfile', MagicMock(return_value=True))
@patch('os.stat', MagicMock())
def test_line_insert_ensure_beforeafter_twolines_exists(self):
'''
Test for file.line for insertion ensuring the line is between two lines where content already exists
:return:
'''
cfg_content = 'EXTRA_GROUPS="dialout"'
file_content = 'NAME_REGEX="^[a-z][-a-z0-9_]*\$"\nEXTRA_GROUPS="dialout"' \
'\nSKEL_IGNORE_REGEX="dpkg-(old|new|dist|save)"'
after, before = file_content.split(os.linesep)[0], file_content.split(os.linesep)[2]
for (_after, _before) in [(after, before), ('NAME_.*', 'SKEL_.*')]:
files_fopen = mock_open(read_data=file_content)
with patch('salt.utils.files.fopen', files_fopen):
atomic_opener = mock_open()
with patch('salt.utils.atomicfile.atomic_open', atomic_opener):
result = filemod.line('foo', content=cfg_content, after=_after, before=_before, mode='ensure')
assert 0 == len(atomic_opener().write.call_args_list)
assert not result
@patch('os.path.realpath', MagicMock())
@patch('os.path.isfile', MagicMock(return_value=True))
@patch('os.stat', MagicMock())