test mode for file.line, fix #27906

This commit is contained in:
Christophe Drevet-Droguet 2015-12-22 13:21:42 +01:00
parent 62c9514108
commit 8b595b85c8

View File

@ -1609,7 +1609,7 @@ def line(path, content, match=None, mode=None, location=None,
changed = body_before != hashlib.sha256(salt.utils.to_bytes(body)).hexdigest() changed = body_before != hashlib.sha256(salt.utils.to_bytes(body)).hexdigest()
if backup and changed: if backup and changed and __opts__['test'] is False:
try: try:
temp_file = _mkstemp_copy(path=path, preserve_inode=True) temp_file = _mkstemp_copy(path=path, preserve_inode=True)
shutil.move(temp_file, '{0}.{1}'.format(path, time.strftime('%Y-%m-%d-%H-%M-%S', time.localtime()))) shutil.move(temp_file, '{0}.{1}'.format(path, time.strftime('%Y-%m-%d-%H-%M-%S', time.localtime())))
@ -1621,6 +1621,7 @@ def line(path, content, match=None, mode=None, location=None,
if changed: if changed:
if show_changes: if show_changes:
changes_diff = ''.join(difflib.unified_diff(salt.utils.fopen(path, 'r').readlines(), body.splitlines())) changes_diff = ''.join(difflib.unified_diff(salt.utils.fopen(path, 'r').readlines(), body.splitlines()))
if __opts__['test'] is False:
fh_ = None fh_ = None
try: try:
fh_ = salt.utils.atomicfile.atomic_open(path, 'w') fh_ = salt.utils.atomicfile.atomic_open(path, 'w')