diff --git a/changelog/59806.fixed.md b/changelog/59806.fixed.md new file mode 100644 index 000000000000..2cca505c2bfc --- /dev/null +++ b/changelog/59806.fixed.md @@ -0,0 +1 @@ +Return error if patch file passed to state file.patch is malformed. diff --git a/salt/states/file.py b/salt/states/file.py index d41895e15158..fcf0a8ad822f 100644 --- a/salt/states/file.py +++ b/salt/states/file.py @@ -7265,6 +7265,10 @@ def _patch(patch_file, options=None, dry_run=False): pre_check = _patch(patch_file, patch_opts) if pre_check["retcode"] != 0: + if not os.path.exists(patch_rejects) or os.path.getsize(patch_rejects) == 0: + ret["comment"] = pre_check["stderr"] + ret["result"] = False + return ret # Try to reverse-apply hunks from rejects file using a dry-run. # If this returns a retcode of 0, we know that the patch was # already applied. Rejects are written from the base of the diff --git a/tests/pytests/integration/states/test_file.py b/tests/pytests/integration/states/test_file.py index a84d2f4797e5..cd64fc918b65 100644 --- a/tests/pytests/integration/states/test_file.py +++ b/tests/pytests/integration/states/test_file.py @@ -929,6 +929,7 @@ def test_patch_directory_template( - source: {all_patch_template} - template: "jinja" - context: {context} + - strip: 1 """.format( base_dir=tmp_path, all_patch_template=all_patch_template, context=context ) @@ -945,7 +946,7 @@ def test_patch_directory_template( # Check to make sure the patch was applied okay state_run = next(iter(ret.data.values())) assert state_run["result"] is True - assert state_run["comment"] == "Patch was already applied" + assert state_run["comment"] == "Patch successfully applied" # Re-run the state, should succeed and there should be a message about # a partially-applied hunk.