from pathlib import Path
import subprocess, tempfile, shutil, json
root = Path(__file__).resolve().parent
report = {}
for name in ('before','fixed'):
    p = subprocess.run(['python3','-m','unittest','-v'], cwd=root/name, text=True, capture_output=True)
    report[name] = {'exit_code':p.returncode,'output':p.stdout+p.stderr}
assert report['fixed']['exit_code']==0, report['fixed']
assert (report['before']['exit_code']==0)==True, report['before']
with tempfile.TemporaryDirectory() as td:
    target=Path(td)/'negative'
    shutil.copytree(root/'fixed',target)
    app=target/'app.py'
    app.write_text("raise RuntimeError('intentional negative mutation')\n",encoding='utf-8')
    bad=subprocess.run(['python3','-m','unittest','-v'],cwd=target,capture_output=True)
    assert bad.returncode!=0, 'negative mutation escaped tests'
report['negative']={'exit_code':bad.returncode,'passed_rejection':True}
print(json.dumps(report,ensure_ascii=False,indent=2))
