mirror of
https://github.com/saltstack/salt.git
synced 2025-04-17 10:10:20 +00:00
Merge pull request #38467 from gtmanfred/2016.3
file.line fail with mode=delete
This commit is contained in:
commit
3d0c752acd
2 changed files with 33 additions and 9 deletions
|
@ -1534,17 +1534,15 @@ def line(path, content, match=None, mode=None, location=None,
|
|||
before = _regex_to_static(body, before)
|
||||
match = _regex_to_static(body, match)
|
||||
|
||||
if mode == 'delete':
|
||||
if os.stat(path).st_size == 0 and mode in ('delete', 'replace'):
|
||||
log.warning('Cannot find text to {0}. File \'{1}\' is empty.'.format(mode, path))
|
||||
body = ''
|
||||
elif mode == 'delete':
|
||||
body = os.linesep.join([line for line in body.split(os.linesep) if line.find(match) < 0])
|
||||
|
||||
elif mode == 'replace':
|
||||
if os.stat(path).st_size == 0:
|
||||
log.warning('Cannot find text to replace. File \'{0}\' is empty.'.format(path))
|
||||
body = ''
|
||||
else:
|
||||
body = os.linesep.join([(_get_line_indent(file_line, content, indent)
|
||||
if (file_line.find(match) > -1 and not file_line == content) else file_line)
|
||||
for file_line in body.split(os.linesep)])
|
||||
body = os.linesep.join([(_get_line_indent(file_line, content, indent)
|
||||
if (file_line.find(match) > -1 and not file_line == content) else file_line)
|
||||
for file_line in body.split(os.linesep)])
|
||||
elif mode == 'insert':
|
||||
if not location and not before and not after:
|
||||
raise CommandExecutionError('On insert must be defined either "location" or "before/after" conditions.')
|
||||
|
|
|
@ -669,6 +669,32 @@ class FileModuleTestCase(TestCase):
|
|||
empty_file.close()
|
||||
os.remove(empty_file.name)
|
||||
|
||||
def test_delete_line_in_empty_file(self):
|
||||
'''
|
||||
Tests that when calling file.line with ``mode=delete``,
|
||||
the function doesn't stack trace if the file is empty.
|
||||
Should return ``False``.
|
||||
|
||||
See Issue #38438.
|
||||
'''
|
||||
# Create an empty temporary named file
|
||||
empty_file = tempfile.NamedTemporaryFile(delete=False,
|
||||
mode='w+')
|
||||
|
||||
# Assert that the file was created and is empty
|
||||
self.assertEqual(os.stat(empty_file.name).st_size, 0)
|
||||
|
||||
# Now call the function on the empty file and assert
|
||||
# the return is False instead of stack-tracing
|
||||
self.assertFalse(filemod.line(empty_file.name,
|
||||
content='foo',
|
||||
match='bar',
|
||||
mode='delete'))
|
||||
|
||||
# Close and remove the file
|
||||
empty_file.close()
|
||||
os.remove(empty_file.name)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
from integration import run_tests
|
||||
|
|
Loading…
Add table
Reference in a new issue