Fix the module and test for Windows

Use salt.utils.files.mkstemp isntead of tempfile.NamedTemporaryFile when
creating the temporary file for get_diff
Fix the test to check for the linesep of the os at the end
On windows there is also a newfile entry in the change dict
This commit is contained in:
Shane Lee 2024-01-26 13:36:23 -07:00 committed by Daniel Wozniak
parent e142205060
commit 00292d6b0d
2 changed files with 39 additions and 20 deletions

View file

@ -6026,11 +6026,15 @@ def check_file_meta(
name, sfn, template=True, show_filenames=False
)
else:
with tempfile.NamedTemporaryFile() as _mt:
_mt.write(b"")
changes["diff"] = get_diff(
_mt.name, sfn, template=True, show_filenames=False
)
# Since the target file doesn't exist, create an empty one to
# compare against
tmp_empty = salt.utils.files.mkstemp(
prefix=salt.utils.files.TEMPFILE_PREFIX, text=False
)
with salt.utils.files.fopen(tmp_empty, "wb") as tmp_:
tmp_.write(b"")
changes["diff"] = get_diff(tmp_empty, sfn, show_filenames=False)
except CommandExecutionError as exc:
changes["diff"] = exc.strerror
else:
@ -6068,9 +6072,14 @@ def check_file_meta(
elif lstats:
differences = get_diff(name, tmp, show_filenames=False)
else:
with tempfile.NamedTemporaryFile() as _mt:
_mt.write(b"")
differences = get_diff(_mt.name, tmp, show_filenames=False)
# Since the target file doesn't exist, create an empty one to
# compare against
tmp_empty = salt.utils.files.mkstemp(
prefix=salt.utils.files.TEMPFILE_PREFIX, text=False
)
with salt.utils.files.fopen(tmp_empty, "wb") as tmp_:
tmp_.write(b"")
differences = get_diff(tmp_empty, tmp, show_filenames=False)
except CommandExecutionError as exc:
log.error("Failed to diff files: %s", exc)
differences = exc.strerror
@ -6952,11 +6961,15 @@ def manage_file(
# It is a new file, set the diff accordingly
ret["changes"]["diff"] = "New file"
if new_file_diff:
with tempfile.NamedTemporaryFile() as _mt:
_mt.write(b"")
ret["changes"]["diff"] = get_diff(
_mt.name, sfn, show_filenames=False
)
# Since the target file doesn't exist, create an empty one to
# compare against
tmp_empty = salt.utils.files.mkstemp(
prefix=salt.utils.files.TEMPFILE_PREFIX, text=False
)
with salt.utils.files.fopen(tmp_empty, "wb") as tmp_:
tmp_.write(b"")
ret["changes"]["diff"] = get_diff(tmp_empty, sfn, show_filenames=False)
if not os.path.isdir(contain_dir):
if makedirs:
@ -7013,11 +7026,14 @@ def manage_file(
tmp_.write(salt.utils.stringutils.to_bytes(contents))
if new_file_diff and ret["changes"]["diff"] == "New file":
with tempfile.NamedTemporaryFile() as _mt:
_mt.write(b"")
ret["changes"]["diff"] = get_diff(
_mt.name, tmp, show_filenames=False
)
# Since the target file doesn't exist, create an empty one to
# compare against
tmp_empty = salt.utils.files.mkstemp(
prefix=salt.utils.files.TEMPFILE_PREFIX, text=False
)
with salt.utils.files.fopen(tmp_empty, "wb") as tmp_:
tmp_.write(b"")
ret["changes"]["diff"] = get_diff(tmp_empty, tmp, show_filenames=False)
# Copy into place
salt.utils.files.copyfile(

View file

@ -1048,8 +1048,11 @@ def test_issue_60203(
def test_file_managed_new_file_diff(file, tmp_path):
name = tmp_path / "new_file_diff.txt"
ret = file.managed(str(name), contents="EITR", new_file_diff=True, test=True)
assert ret.changes == {"diff": "--- \n+++ \n@@ -0,0 +1 @@\n+EITR\n"}
assert ret.changes == {
"diff": f"--- \n+++ \n@@ -0,0 +1 @@\n+EITR{os.linesep}",
"newfile": str(name),
}
assert not name.exists()
ret = file.managed(str(name), contents="EITR", new_file_diff=True)
assert ret.changes == {"diff": "--- \n+++ \n@@ -0,0 +1 @@\n+EITR\n"}
assert ret.changes == {"diff": f"--- \n+++ \n@@ -0,0 +1 @@\n+EITR{os.linesep}"}
assert name.exists()