mirror of
https://github.com/saltstack/salt.git
synced 2025-04-17 10:10:20 +00:00
Make integration green. Added test for mode ensure insert before first line
This commit is contained in:
parent
9b7df671a5
commit
d2af81e9c7
3 changed files with 65 additions and 21 deletions
|
@ -1776,6 +1776,22 @@ def _set_line_eol(src, line):
|
|||
return line.rstrip() + line_ending
|
||||
|
||||
|
||||
def _insert_line_before(idx, body, content, indent):
|
||||
if not idx or (idx and _starts_till(body[idx - 1], content) < 0):
|
||||
cnd = _set_line_indent(body[idx], content, indent)
|
||||
body.insert(idx, cnd)
|
||||
return body
|
||||
|
||||
|
||||
def _insert_line_after(idx, body, content, indent):
|
||||
# No duplicates or append, if "after" is the last line
|
||||
next_line = idx + 1 < len(body) and body[idx + 1] or None
|
||||
if next_line is None or _starts_till(next_line, content) < 0:
|
||||
cnd = _set_line_indent(body[idx], content, indent)
|
||||
body.insert(idx + 1, cnd)
|
||||
return body
|
||||
|
||||
|
||||
def line(path, content=None, match=None, mode=None, location=None,
|
||||
before=None, after=None, show_changes=True, backup=False,
|
||||
quiet=False, indent=True):
|
||||
|
@ -1906,8 +1922,8 @@ def line(path, content=None, match=None, mode=None, location=None,
|
|||
with salt.utils.files.fopen(path, mode='r') as fp_:
|
||||
body = [salt.utils.stringutils.to_unicode(line) for line in fp_.readlines()]
|
||||
body_before = hashlib.sha256(salt.utils.stringutils.to_bytes(''.join(body))).hexdigest()
|
||||
if body and body[-1] and not _get_eol(body[-1]):
|
||||
body[-1] += (len(body) > 1) and _get_eol(body[-2]) or os.linesep
|
||||
if body and _get_eol(body[-1]):
|
||||
body.append('')
|
||||
|
||||
after = _regex_to_static(body, after)
|
||||
before = _regex_to_static(body, before)
|
||||
|
@ -1937,7 +1953,8 @@ def line(path, content=None, match=None, mode=None, location=None,
|
|||
if line == after[0]:
|
||||
in_range = True
|
||||
elif line == before[0] and in_range:
|
||||
out.append(_set_line_indent(line, _set_line_eol(line, content), indent))
|
||||
cnd = _set_line_indent(line, content, indent)
|
||||
out.append(cnd)
|
||||
out.append(line)
|
||||
body = out
|
||||
|
||||
|
@ -1945,20 +1962,13 @@ def line(path, content=None, match=None, mode=None, location=None,
|
|||
_assert_occurrence(before, 'before')
|
||||
|
||||
idx = body.index(before[0])
|
||||
if not idx or (idx and _starts_till(body[idx - 1], content) < 0): # Job for replace instead
|
||||
cnd = _set_line_indent(body[idx], content, indent)
|
||||
cnd = _set_line_eol(body[idx], cnd)
|
||||
body.insert(idx, cnd)
|
||||
body = _insert_line_before(idx, body, content, indent)
|
||||
|
||||
elif after and not before:
|
||||
_assert_occurrence(after, 'after')
|
||||
|
||||
idx = body.index(after[0])
|
||||
# No duplicates or append, if "after" is the last line
|
||||
if body[((idx + 1) < len(body)) and idx + 1 or idx].strip() != content or idx + 1 == len(body):
|
||||
cnd = _set_line_indent(body[idx], content, indent)
|
||||
cnd = _set_line_eol(body[idx], cnd)
|
||||
body.insert(idx + 1, cnd)
|
||||
body = _insert_line_after(idx, body, content, indent)
|
||||
|
||||
else:
|
||||
if location == 'start':
|
||||
|
@ -1979,7 +1989,8 @@ def line(path, content=None, match=None, mode=None, location=None,
|
|||
if not is_there:
|
||||
idx = body.index(after[0])
|
||||
if idx < (len(body) - 1) and body[idx + 1] == before[0]:
|
||||
body.insert(idx + 1, _set_line_indent(body[idx], _set_line_eol(body[idx], content), indent))
|
||||
cnd = _set_line_indent(body[idx], content, indent)
|
||||
body.insert(idx + 1, cnd)
|
||||
else:
|
||||
raise CommandExecutionError('Found more than one line between '
|
||||
'boundaries "before" and "after".')
|
||||
|
@ -1988,16 +1999,13 @@ def line(path, content=None, match=None, mode=None, location=None,
|
|||
_assert_occurrence(before, 'before')
|
||||
|
||||
idx = body.index(before[0])
|
||||
if body[idx-1].find(content) < 0:
|
||||
body.insert(idx, _set_line_indent(body[idx], _set_line_eol(body[idx], content), indent))
|
||||
body = _insert_line_before(idx, body, content, indent)
|
||||
|
||||
elif not before and after:
|
||||
_assert_occurrence(after, 'after')
|
||||
|
||||
idx = body.index(after[0])
|
||||
next_line = idx + 1 < len(body) and body[idx + 1] or None
|
||||
if next_line is None or _starts_till(next_line, content) < 0:
|
||||
body.insert(idx+1, _set_line_indent(body[idx], _set_line_eol(body[idx], content), indent))
|
||||
body = _insert_line_after(idx, body, content, indent)
|
||||
|
||||
else:
|
||||
raise CommandExecutionError("Wrong conditions? "
|
||||
|
@ -2005,7 +2013,12 @@ def line(path, content=None, match=None, mode=None, location=None,
|
|||
"where to put it before and/or after.")
|
||||
|
||||
if body:
|
||||
body.append(body.pop().rstrip('\r\n'))
|
||||
for idx, line in enumerate(body):
|
||||
if not _get_eol(line) and idx+1 < len(body):
|
||||
prev = idx and idx-1 or 1
|
||||
body[idx] = _set_line_eol(body[prev], line)
|
||||
if '' == body[-1]:
|
||||
body.pop()
|
||||
|
||||
changed = body_before != hashlib.sha256(salt.utils.stringutils.to_bytes(''.join(body))).hexdigest()
|
||||
|
||||
|
|
|
@ -59,7 +59,8 @@ from salt.modules.file import (check_hash, # pylint: disable=W0611
|
|||
lstat, path_exists_glob, write, pardir, join, HASHES, HASHES_REVMAP,
|
||||
comment, uncomment, _add_flags, comment_line, _regex_to_static,
|
||||
_set_line_indent, apply_template_on_contents, dirname, basename,
|
||||
list_backups_dir, _assert_occurrence, _starts_till, _set_line_eol, _get_eol)
|
||||
list_backups_dir, _assert_occurrence, _starts_till, _set_line_eol, _get_eol,
|
||||
_insert_line_after, _insert_line_before)
|
||||
from salt.modules.file import normpath as normpath_
|
||||
|
||||
from salt.utils.functools import namespaced_function as _namespaced_function
|
||||
|
@ -116,8 +117,9 @@ def __virtual__():
|
|||
global blockreplace, prepend, seek_read, seek_write, rename, lstat
|
||||
global write, pardir, join, _add_flags, apply_template_on_contents
|
||||
global path_exists_glob, comment, uncomment, _mkstemp_copy
|
||||
global _regex_to_static, _get_line_indent, dirname, basename
|
||||
global _regex_to_static, _set_line_indent, dirname, basename
|
||||
global list_backups_dir, normpath_, _assert_occurrence, _starts_till
|
||||
global _insert_line_before, _insert_line_after, _set_line_eol, _get_eol
|
||||
|
||||
replace = _namespaced_function(replace, globals())
|
||||
search = _namespaced_function(search, globals())
|
||||
|
@ -176,6 +178,8 @@ def __virtual__():
|
|||
_set_line_indent = _namespaced_function(_set_line_indent, globals())
|
||||
_set_line_eol = _namespaced_function(_set_line_eol, globals())
|
||||
_get_eol = _namespaced_function(_get_eol, globals())
|
||||
_insert_line_after = _namespaced_function(_insert_line_after, globals())
|
||||
_insert_line_before = _namespaced_function(_insert_line_before, globals())
|
||||
_mkstemp_copy = _namespaced_function(_mkstemp_copy, globals())
|
||||
_add_flags = _namespaced_function(_add_flags, globals())
|
||||
apply_template_on_contents = _namespaced_function(apply_template_on_contents, globals())
|
||||
|
|
|
@ -1382,6 +1382,33 @@ class FilemodLineTests(TestCase, LoaderModuleMockMixin):
|
|||
write_content = handles[0].write.call_args_list[0][0][0]
|
||||
assert write_content == file_modified, write_content
|
||||
|
||||
@patch('os.path.realpath', MagicMock())
|
||||
@patch('os.path.isfile', MagicMock(return_value=True))
|
||||
@patch('os.stat', MagicMock())
|
||||
def test_line_insert_ensure_before_first_line(self):
|
||||
'''
|
||||
Test for file.line for insertion ensuring the line is before first line
|
||||
:return:
|
||||
'''
|
||||
cfg_content = '#!/bin/bash'
|
||||
file_content = os.linesep.join([
|
||||
'/etc/init.d/someservice restart',
|
||||
'exit 0'
|
||||
])
|
||||
file_modified = os.linesep.join([
|
||||
cfg_content,
|
||||
'/etc/init.d/someservice restart',
|
||||
'exit 0'
|
||||
])
|
||||
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):
|
||||
filemod.line('foo', content=cfg_content, before='/etc/init.d/someservice restart', mode='ensure')
|
||||
self.assertEqual(len(atomic_opener().write.call_args_list), 1)
|
||||
self.assertEqual(atomic_opener().write.call_args_list[0][0][0],
|
||||
file_modified)
|
||||
|
||||
@with_tempfile()
|
||||
def test_line_insert_ensure_beforeafter_twolines(self, name):
|
||||
'''
|
||||
|
|
Loading…
Add table
Reference in a new issue