import hashlib
import json
from pathlib import Path
import sys
import tempfile
import unittest

R=Path(__file__).parent
root=Path('/workspace/contextra-analytics-runtime-lifecycle-refactor')
sys.path.insert(0,str(root))
(R/'test-tmp').mkdir(mode=0o700,exist_ok=True)
tempfile.tempdir=str(R/'test-tmp')
from app import start_diagnostics, stream_lifecycle
files=['app/start_diagnostics.py','app/stream_manager.py','tests/test_start_diagnostics.py']
print(json.dumps(dict(cwd=str(Path.cwd()),modules=[start_diagnostics.__file__,stream_lifecycle.__file__],
                     hashes={f:hashlib.sha256((root/f).read_bytes()).hexdigest() for f in files})))
suite=unittest.defaultTestLoader.loadTestsFromNames(['tests.test_start_diagnostics','tests.test_stream_lifecycle'])
result=unittest.TextTestRunner(verbosity=1).run(suite)
(R/'tests.json').write_text(json.dumps(dict(success=result.wasSuccessful(),tests=result.testsRun,
    failures=len(result.failures),errors=len(result.errors))))
sys.exit(not result.wasSuccessful())
