From 00292d6b0dbfd93b09c518597caf04fc824ae5f1 Mon Sep 17 00:00:00 2001 From: Shane Lee Date: Fri, 26 Jan 2024 13:36:23 -0700 Subject: [PATCH] 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 --- salt/modules/file.py | 52 ++++++++++++------- .../functional/states/file/test_managed.py | 7 ++- 2 files changed, 39 insertions(+), 20 deletions(-) diff --git a/salt/modules/file.py b/salt/modules/file.py index 4c75fa67605..f64df3bc76e 100644 --- a/salt/modules/file.py +++ b/salt/modules/file.py @@ -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( diff --git a/tests/pytests/functional/states/file/test_managed.py b/tests/pytests/functional/states/file/test_managed.py index 1600f51dc0a..7831a5269e5 100644 --- a/tests/pytests/functional/states/file/test_managed.py +++ b/tests/pytests/functional/states/file/test_managed.py @@ -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()