from pathlib import Path import subprocess,os,json,hashlib root=Path(__file__).resolve().parents[4] packet=Path(__file__).resolve().parent p=root/'packages/bus/tests/scratch-sshd.mjs';good=p.read_text() accept=""" if (await listening(port) && daemon.exitCode === null && daemon.signalCode === null && readFileSync(lastLog, 'utf8').split('\\n').some(line => (line === `Server listening on 127.0.0.1 port ${port}.` || line === `Server listening on 127.0.0.1 port ${port}.\\r`))) { saveLog(); return port; }""" a=good.index('export function pickPort(');b=good.index('\n}\n',a)+3 picker=good[a:b] sites=[{'id':'M1','old':accept,'new':' if (await listening(port)) return port;','pattern':'row 280: an occupied TCP port'}, {'id':'M2','old':picker,'new':'export function pickPort(rand = Math.random) {\n return 20000 + Math.floor(rand() * 20000);\n}\n','pattern':'row 280: every injected picker sample'}] (packet/'mutants.json').write_text(json.dumps(sites,indent=2)+'\n') try: for site in sites: assert good.count(site['old'])==1,site['id'] for file in ['grant-executor','host-run']: p.write_text(good.replace(site['old'],site['new'])) try: cmd=['python3',str(packet/'run-step.py'),site['id']+'-final-'+file,'node','--test','--test-reporter=tap','--test-name-pattern='+site['pattern'],'packages/bus/tests/'+file+'.test.mjs'] r=subprocess.run(cmd,cwd=root,env=os.environ.copy()) assert r.returncode==0,(site['id'],file) log=(packet/'logs'/(site['id']+'-final-'+file+'.log')).read_text() assert 'not ok 1 - '+site['pattern'] in log finally: p.write_text(good) finally: p.write_text(good) (packet/'mutant-restore.json').write_text(json.dumps({'helper_sha256':hashlib.sha256(p.read_bytes()).hexdigest(),'restored':p.read_text()==good},indent=2)+'\n')