"""Verify the example and exercise solution in a disposable copy."""
import os
import shutil
import subprocess
import tempfile
from pathlib import Path

ROOT = Path(__file__).resolve().parent
FILES = ['pom.xml', 'src/main/java/tutorial/Application.java', 'src/test/java/tutorial/LessonTest.java', 'src/main/resources/application.properties', 'verify.py', 'solution/LessonTest.java']
COMMANDS = [['python3', 'verify.py'], ['python3', '-c', "from pathlib import Path\nPath('src/test/java/tutorial/LessonTest.java').write_text(Path('solution/LessonTest.java').read_text())"], ['python3', 'verify.py']]

with tempfile.TemporaryDirectory(prefix="appfabrik-learning-") as temporary:
    work = Path(temporary)
    for name in FILES:
        target = work / name
        target.parent.mkdir(parents=True, exist_ok=True)
        shutil.copyfile(ROOT / name, target)
    for command in COMMANDS:
        print("Running:", " ".join(command), flush=True)
        subprocess.run(command, cwd=work, check=True, timeout=900)
print("Example and solution checks completed.")
