Skip to content

Commit

Permalink
Return error if patch file passed to state file.patch is malformed
Browse files Browse the repository at this point in the history
If patch file provided for file.patch state is malformed then state
returns `Patch was already applied` but patch is not applied.

          ID: patch_example
    Function: file.patch
        Name: /tmp/example
      Result: True
     Comment: Patch was already applied
     Started: 12:20:50.953163
    Duration: 61.558 ms
     Changes:

It is better to return error in such case.

          ID: patch_example
    Function: file.patch
        Name: /tmp/example
      Result: False
     Comment: /usr/bin/patch: **** malformed patch at line 7:
     Started: 12:33:44.915605
    Duration: 59.202 ms
     Changes:
  • Loading branch information
msciciel authored and dwoz committed Dec 10, 2023
1 parent 0b423e4 commit 3069063
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 1 deletion.
1 change: 1 addition & 0 deletions changelog/59806.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Return error if patch file passed to state file.patch is malformed.
4 changes: 4 additions & 0 deletions salt/states/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -7263,6 +7263,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
Expand Down
3 changes: 2 additions & 1 deletion tests/pytests/integration/states/test_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand All @@ -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.
Expand Down

0 comments on commit 3069063

Please sign in to comment.