import pytest
from pathlib import Path
from domin8.tools.diff_utils import FileChange
from domin8.tools.pre_approval_checks import run_checks_for_changes
def make_fc(path: str, file_type: str = 'source'):
return FileChange(repo_relative_path=path, action='edit', old_path=path, new_path=path, hunks=[], file_type=file_type)
def test_pre_approval_checks_no_tools(monkeypatch, tmp_path):
repo_root = Path('.').resolve()
# Ensure tool detection returns False
monkeypatch.setattr('importlib.util.find_spec', lambda name: None)
changes = [make_fc('src/domin8/config.py')]
res = run_checks_for_changes(changes, repo_root)
assert 'per_type' in res
assert 'python' in res['per_type']
assert res['per_type']['python']['formatter']['tool'] == 'black'
assert res['per_type']['python']['formatter'].get('available') is False
assert 'policy' in res
assert isinstance(res['policy'].get('findings'), list)
def test_pre_approval_checks_tool_available(monkeypatch):
repo_root = Path('.').resolve()
# Simulate black and ruff available
monkeypatch.setattr('importlib.util.find_spec', lambda name: True)
# Simulate subprocess behavior
monkeypatch.setattr('subprocess.run', lambda *args, **kwargs: type('r', (), {'returncode': 0, 'stdout': 'ok', 'stderr': ''})())
changes = [make_fc('src/domin8/config.py')]
res = run_checks_for_changes(changes, repo_root)
assert res['per_type']['python']['formatter']['returncode'] == 0
assert res['per_type']['python']['linter']['returncode'] == 0