import subprocess
import types
from domin8.git_ops import apply_diff
class DummyFail:
def __init__(self, returncode=1, stdout='', stderr='patch failed'):
self.returncode = returncode
self.stdout = stdout
self.stderr = stderr
def test_apply_diff_patch_failure(monkeypatch):
original = 'line1\nline2\n'
# Construct a diff that deletes everything (and thus would result in empty output)
diff = """--- a/file.txt
+++ b/file.txt
@@ -1,2 +0,0 @@
-line1
-line2
"""
# Simulate `patch` failing
monkeypatch.setattr('subprocess.run', lambda *a, **k: DummyFail(returncode=2))
try:
apply_diff(original, diff)
assert False, "apply_diff should have raised ValueError on invalid patch"
except ValueError as e:
assert 'Patch' in str(e) or 'empty output' in str(e)