import types
import subprocess
import pytest
import sys
from pathlib import Path
from domin8 import git_ops
def test_stage_and_commit_with_list(monkeypatch, tmp_path):
calls = []
def fake_run(cmd, **kwargs):
calls.append(cmd)
return types.SimpleNamespace(returncode=0, stdout='', stderr='')
monkeypatch.setattr('subprocess.run', fake_run)
p1 = tmp_path / 'a.txt'
p2 = tmp_path / 'b.txt'
p1.write_text('1\n')
p2.write_text('2\n')
# Should not raise
git_ops.stage_and_commit([p1, p2], 'msg')
assert any(cmd[:2] == ['git', 'add'] for cmd in calls)
assert any(cmd[:2] == ['git', 'commit'] for cmd in calls)
def test_apply_diff_successful_unidiff(monkeypatch):
original = 'a\nb\n'
diff = 'dummy'
# Force 'patch' binary to fail so we exercise the unidiff fallback
monkeypatch.setattr('subprocess.run', lambda *a, **k: types.SimpleNamespace(returncode=2, stdout='', stderr=''))
class FakeLine:
def __init__(self, value, is_added=False, is_removed=False):
self.value = value
self.is_added = is_added
self.is_removed = is_removed
class FakeHunk(list):
def __init__(self):
# a and b are context lines; we insert c between them (is_added=True)
super().__init__([
FakeLine('a\n', is_added=False, is_removed=False),
FakeLine('c\n', is_added=True, is_removed=False),
FakeLine('b\n', is_added=False, is_removed=False),
])
self.source_start = 1
self.source_length = 2
class FakePatchedFile(list):
def __init__(self):
super().__init__([FakeHunk()])
class PatchSetOne(list):
def __init__(self, *a, **k):
super().__init__([FakePatchedFile()])
fake_unidiff = types.SimpleNamespace(PatchSet=PatchSetOne)
monkeypatch.setitem(sys.modules, 'unidiff', fake_unidiff)
patched = git_ops.apply_diff(original, diff)
# Expect 'c' to have been inserted between a and b
assert patched == 'a\nc\nb\n'