import os
from pathlib import Path
import subprocess
from domin8.git_ops import create_diff, apply_diff, stage_and_commit, generate_commit_message
def test_apply_diff_roundtrip():
original = "line1\nline2\nline3\n"
new = "line1\nline2 modified\nline3\n"
diff = create_diff(original, new, "file.txt")
patched = apply_diff(original, diff)
assert patched == new
def test_stage_and_commit(tmp_path):
repo = tmp_path / "repo"
repo.mkdir()
cwd = os.getcwd()
try:
os.chdir(repo)
subprocess.run(["git", "init"], check=True)
subprocess.run(["git", "config", "user.email", "you@example.com"], check=True)
subprocess.run(["git", "config", "user.name", "Tester"], check=True)
f = repo / "hello.txt"
f.write_text("hello\n")
commit_msg = "test commit"
stage_and_commit(f, commit_msg)
# Check git log for commit message
result = subprocess.run(["git", "log", "-1", "--pretty=%B"], capture_output=True, text=True, check=True)
assert commit_msg in result.stdout
finally:
os.chdir(cwd)