import os,sys,subprocess,signal,time,json,re from pathlib import Path root=Path(__file__).resolve().parents[4] packet=Path(__file__).resolve().parent name=sys.argv[1]; command=sys.argv[2:] assert Path(subprocess.check_output(['git','rev-parse','--show-toplevel'],cwd=root,text=True).strip()).resolve() == root for arg in command: if arg.endswith('.test.mjs'): assert (root/arg).is_file(), arg start=time.time(); timed_out=False p=subprocess.Popen(command,cwd=root,env=os.environ.copy(),stdout=subprocess.PIPE,stderr=subprocess.STDOUT,start_new_session=True) try: out=p.communicate(timeout=180)[0] except subprocess.TimeoutExpired: timed_out=True;os.killpg(p.pid,signal.SIGKILL);out=p.communicate()[0] text=out.decode(errors='replace');(packet/'logs').mkdir(exist_ok=True);(packet/'logs'/f'{name}.log').write_text(text) counts={k:int(v) for k,v in re.findall(r'^# (tests|pass|fail|skipped|cancelled) (\d+)$',text,re.M)} result={'step':name,'start':time.strftime('%Y-%m-%dT%H:%M:%SZ',time.gmtime(start)),'end':time.strftime('%Y-%m-%dT%H:%M:%SZ',time.gmtime()),'seconds':round(time.time()-start,3),'exit':p.returncode,'timeout':timed_out,'counts':counts,'command':command} f=packet/'results.json'; rows=json.loads(f.read_text()) if f.exists() else []; rows.append(result);f.write_text(json.dumps(rows,indent=2)+'\n');print(json.dumps(result),flush=True);print(text[-4500:],flush=True) if name.startswith('red-') or name.startswith('M'): expected=p.returncode==1 and not timed_out and 'ERR_ASSERTION' in text and counts.get('fail',0)>0 print('EXPECTED_ASSERTION_RED',expected);sys.exit(0 if expected else 1) sys.exit(p.returncode if not timed_out else 124)