io_v2_context.py (2353B)
1 #!/usr/bin/env python3 2 """Actual read-only cwd/environment observations, independent of source handlers.""" 3 import argparse 4 import json 5 import os 6 from pathlib import Path 7 import subprocess 8 from source_order import Runner 9 from http_headers import result, tree_list, parse_tree_output 10 from io_v2_text import text, problem 11 ROOT=Path(__file__).resolve().parent.parent 12 13 def main(): 14 p=argparse.ArgumentParser(description=__doc__);p.add_argument('tricu',type=Path);p.add_argument('--directory',type=Path,required=True) 15 a=p.parse_args();d=a.directory.resolve();d.mkdir(parents=True,exist_ok=False);r=Runner(a.tricu,d);bundle=d/'context.arboricx' 16 r.run('compile',['compile','--file',ROOT/'demos/io-v2/context.tri','--store',r.store,'--output',bundle]);pin=r.imported(bundle,'import');checks=[] 17 key='TRICU_CONTEXT_TEST_VALUE' 18 def run(label,name,argument,env,wanted): 19 cmd=[str(r.exe),'arboricx','run','--module',pin,'--name',name,'--store',str(r.store),'--io','--format','ternary'] 20 if argument is not None: 21 arg=d/(label+'.arg');arg.write_bytes(text(argument));cmd+=['--arg-tree-file',str(arg)] 22 proc=subprocess.run(cmd,cwd=d,env=env,capture_output=True,timeout=20) 23 (d/(label+'.stdout')).write_bytes(proc.stdout);(d/(label+'.stderr')).write_bytes(proc.stderr) 24 assert proc.returncode==0,(label,proc.stderr) 25 assert parse_tree_output(proc.stdout.decode())==wanted,label 26 checks.append(label) 27 env=dict(os.environ);env.pop(key,None) 28 run('cwd','directory',None,env,result(True,text(str(d)))) 29 run('missing','variable',key,env,result(True,tree_list([text('missing')]))) 30 for i,value in enumerate(['','λ\n\ufeff','x'*4096]): 31 run(f'value-{i}','variable',key,dict(env,**{key:value}),result(True,tree_list([text('value'),text(value)]))) 32 run('oversized','variable',key,dict(env,**{key:'x'*4097}),result(False,problem('invalid-bound','process/environment-variable','host context text exceeds scalar limit'))) 33 for i,name in enumerate(['','A=B','A\0B']): 34 run(f'invalid-{i}','variable',name,env,result(False,problem('invalid-argument','process/environment-variable',''))) 35 (d/'report.json').write_text(json.dumps(dict(passed=checks,records=r.records),indent=2)+'\n') 36 print(f'PASS {len(checks)} host-context controls') 37 if __name__=='__main__':main()