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,13 +1621,14 @@ 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()))
fh_ = None if __opts__['test'] is False:
try: fh_ = None
fh_ = salt.utils.atomicfile.atomic_open(path, 'w') try:
fh_.write(body) fh_ = salt.utils.atomicfile.atomic_open(path, 'w')
finally: fh_.write(body)
if fh_: finally:
fh_.close() if fh_:
fh_.close()
return show_changes and changes_diff or changed return show_changes and changes_diff or changed