akg-factor-bridge/offline_tests.py

41 lines
1.5 KiB
Python

"""逐批离线验证;空配置、临时目录,任何套接字访问都判失败。"""
import os
from pathlib import Path
import subprocess
import sys
import tempfile
ROOT = Path(__file__).resolve().parent
CHILD = r'''
import os, runpy, socket, sys
attempts=[]
def blocked(*a, **k):
attempts.append('socket')
raise RuntimeError('离线测试禁止联网')
socket.socket.connect=socket.socket.connect_ex=blocked
socket.create_connection=socket.getaddrinfo=blocked
import dotenv
dotenv.load_dotenv=lambda *a,**k:False
sys.path.insert(0,sys.argv[1])
target=sys.argv[2]; sys.argv=[target]
status=0
try: runpy.run_path(target,run_name='__main__')
except SystemExit as exc: status=exc.code or 0
if attempts: raise RuntimeError('测试曾尝试联网,禁止以吞异常通过')
raise SystemExit(status)
'''
if __name__ == '__main__':
files = [ROOT / p for p in sys.argv[1:]] if len(sys.argv)>1 else sorted(ROOT.glob('test_*.py'))
for path in files:
if path.name == 'test_api_support.py':
continue
if not path.is_file():
raise FileNotFoundError(path)
print('=== '+path.name+' ===', flush=True)
with tempfile.TemporaryDirectory() as tmp:
env={'PATH':os.environ.get('PATH',''), 'HOME':tmp, 'TZ':'Asia/Shanghai',
'PYTHONDONTWRITEBYTECODE':'1','PYTHONIOENCODING':'utf-8'}
subprocess.run([sys.executable,'-c',CHILD,str(ROOT),str(path)],cwd=tmp,env=env,check=True)
print('ALL SUITES PASS')