import subprocess
import runpy
from pathlib import Path
import pytest
from domin8 import git_ops
def test_get_repo_root_failure(monkeypatch):
# Simulate git command failing
def fake_run(*args, **kwargs):
raise subprocess.CalledProcessError(1, cmd=args[0], stderr=b'error')
monkeypatch.setattr('subprocess.run', fake_run)
with pytest.raises(RuntimeError):
git_ops.get_repo_root()
def test_get_file_sha_none(monkeypatch, tmp_path):
monkeypatch.setattr('subprocess.run', lambda *a, **k: (_ for _ in ()).throw(subprocess.CalledProcessError(1, cmd=a[0])))
assert git_ops.get_file_sha(Path('nonexistent')) is None
def test_stage_and_commit_failure(monkeypatch, tmp_path):
# Simulate git add failing on commit
def fake_run(cmd, **kwargs):
if cmd[0] == 'git':
raise subprocess.CalledProcessError(1, cmd=cmd, stderr=b'fail')
monkeypatch.setattr('subprocess.run', fake_run)
with pytest.raises(RuntimeError):
git_ops.stage_and_commit(Path(tmp_path / 'file.txt'), 'msg')
def test_run_module_main(monkeypatch):
# Run domin8.server as __main__ to hit the guarded asyncio.run(main()) block
# Monkeypatch stdio_server and app.run to no-op
# Prevent actually running the server main loop by patching asyncio.run
# Ensure asyncio.run will not leave an un-awaited coroutine to avoid runtime warnings
def fake_run(coro):
if hasattr(coro, 'close'):
try:
coro.close()
except Exception:
pass
return None
monkeypatch.setattr('asyncio.run', fake_run)
# Ensure we won't have a stale module entry which runpy sometimes warns about
import sys
sys.modules.pop('domin8.server', None)
# Running as __main__ should complete quickly with asyncio.run patched
runpy.run_module('domin8.server', run_name='__main__')