diff --git a/tests/unit/modules/test_file.py b/tests/unit/modules/test_file.py index a6531ea525..37f4920eb4 100644 --- a/tests/unit/modules/test_file.py +++ b/tests/unit/modules/test_file.py @@ -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())