Return error if patch file passed to state file.patch is malformed

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:
This commit is contained in:
Krzysztof Pawłowski 2023-04-14 17:35:38 +02:00 committed by Daniel Wozniak
parent 0b423e443f
commit 30690637b5
3 changed files with 7 additions and 1 deletions

1
changelog/59806.fixed.md Normal file
View file

@ -0,0 +1 @@
Return error if patch file passed to state file.patch is malformed.

View file

@ -7263,6 +7263,10 @@ def patch(
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

View file

@ -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.