import pytest
import types
import sys
import subprocess
from domin8 import git_ops
def test_stage_and_commit_tracks_deletion(monkeypatch, tmp_path):
calls = []
def fake_run(cmd, **kwargs):
calls.append(cmd)
class R:
returncode = 0
stdout = ''
stderr = ''
# Simulate `git ls-files --error-unmatch <path>` returning 0 (tracked)
if cmd[:3] == ['git', 'ls-files', '--error-unmatch']:
r = R()
r.returncode = 0
return r
# For git rm and git commit assume success
return R()
monkeypatch.setattr('subprocess.run', fake_run)
p = tmp_path / 'file.txt'
# File doesn't exist on disk, but is reported as tracked
# Should not raise
git_ops.stage_and_commit(p, 'msg')
# Ensure git ls-files and git rm were called and commit invoked
assert any(cmd[:3] == ['git', 'ls-files', '--error-unmatch'] for cmd in calls)
assert any(cmd[:3] == ['git', 'rm', '-f'] for cmd in calls)
assert any(cmd[:1] == ['git'] and cmd[1] == 'commit' for cmd in calls)