from copy import deepcopy from pathlib import Path import json import pytest from tools import fw_cli as fw @pytest.fixture def cached(tmp_path): root=tmp_path/'old';root.mkdir() plan={'schema':'prod2u-rtl-input-closure-v1','inputs':{'fixture':{'source.v':'c'*64}}, 'tools':{'iverilog':{'sha256':'d'*64},'vvp':{'sha256':'e'*64}}, 'test_configuration':{'fixture':['tb']}, 'steps':[{'name':'tb-compile','command':['iverilog','-o','{run_root}/tb.vvp','source.v'],'marker':None,'binary':'tb.vvp'}, {'name':'tb','command':['vvp','{run_root}/tb.vvp'],'marker':'tb PASS','binary':None}]} rows=[] for step in plan['steps']: command=fw._render_command(step,root);log=root/(step['name']+'.log') log.write_text(json.dumps(command)+'\n'+(step['marker'] or 'compiled')+'\n') rows.append({'name':step['name'],'command':command,'required_marker':step['marker'], 'status':'PASS','exit_code':0,'log':log.name,'log_sha256':fw._file_hash(log)}) (root/'tb.vvp').write_bytes(b'unit cache fixture') result={'schema':'prod2u-rtl-regression-v2','status':'PASS','complete':True,'expected_check_count':2, 'plan':plan,'cache_key':fw._json_hash(plan),'inputs':plan['inputs'],'native_run_root':str(root), 'source':{'commit':'a'*40,'tree':'b'*40},'commit':'a'*40,'tree':'b'*40,'results':rows, 'artifacts':{'tb.vvp':fw._file_hash(root/'tb.vvp')}} path=root/'RESULT.json';path.write_text(json.dumps(result)) return path,plan,result def test_valid_cache_preserves_native_commands_and_copies_verified_bytes(cached): path,plan,_=cached result,copies,original=fw.validate_rtl_cache(path,plan) assert copies['tb.vvp']==b'unit cache fixture' assert result['results'][0]['command'][2].startswith(str(path.parent)) assert original==path.read_bytes() @pytest.mark.parametrize('change',['source','tool','command','test_list']) def test_changed_content_closure_is_rejected(cached,change): path,plan,_=cached;wanted=deepcopy(plan) if change=='source':wanted['inputs']['fixture']['source.v']='f'*64 elif change=='tool':wanted['tools']['vvp']['sha256']='f'*64 elif change=='command':wanted['steps'][1]['command'].append('+different_seed') else:wanted['test_configuration']['fixture'].append('another_tb') with pytest.raises(ValueError,match='closure differs'):fw.validate_rtl_cache(path,wanted) @pytest.mark.parametrize('change',['running','failed','partial','empty','missing_result','wrong_command','wrong_marker','source_identity','missing_binary']) def test_partial_or_inconsistent_receipt_is_rejected(cached,change): path,plan,result=cached if change=='running':result['status']='RUNNING' elif change=='failed':result['status']='FAIL' elif change=='partial':result['complete']=False elif change=='empty':result['results']=[];result['expected_check_count']=0 elif change=='missing_result':result['results'].pop() elif change=='wrong_command':result['results'][0]['command'][0]='true' elif change=='wrong_marker':result['results'][1]['required_marker']=None elif change=='source_identity':result['source']['commit']='unknown' else:result['artifacts']={} path.write_text(json.dumps(result)) with pytest.raises(ValueError,match='RTL_REUSE_REJECTED'):fw.validate_rtl_cache(path,plan) @pytest.mark.parametrize('name',['tb.log','tb.vvp']) def test_changed_native_bytes_are_rejected(cached,name): path,plan,_=cached;(path.parent/name).write_bytes(b'changed') with pytest.raises(ValueError,match='hash mismatch'):fw.validate_rtl_cache(path,plan) def test_missing_pass_marker_is_rejected_even_if_receipt_hash_was_updated(cached): path,plan,result=cached log=path.parent/'tb.log';log.write_text(json.dumps(result['results'][1]['command'])+'\nno result\n') result['results'][1]['log_sha256']=fw._file_hash(log);path.write_text(json.dumps(result)) with pytest.raises(ValueError,match='marker mismatch'):fw.validate_rtl_cache(path,plan) def test_readme_and_unit_list_are_not_part_of_rtl_configuration(tmp_path,monkeypatch): monkeypatch.setattr(fw,'ROOT',tmp_path);monkeypatch.setattr(fw,'_generator_dependencies',lambda:set()) registry={'targets':{'fixture':{'project_id':'fixture','project_config':'products/fixture/project.json'}}} monkeypatch.setattr(fw,'load_registry',lambda _:registry) for name,data in {'products/fixture/source-closure.json':json.dumps({'sources':['src/top.v'],'top_module':'top'}), 'products/fixture/project.json':'{}','src/top.v':'module top;endmodule', 'tests/sim/tb.v':'module tb;endmodule','tools/fw_cli.py':'runner', 'projects/registry.json':json.dumps(registry),'README.md':'old', 'config/current-product-tests.json':'old unit list'}.items(): path=tmp_path/name;path.parent.mkdir(parents=True,exist_ok=True);path.write_text(data) tools={'iverilog':{'library_root':'/fixture/lib'}} before=fw.rtl_plan(['fixture'],{'fixture':['tb']},tools) (tmp_path/'README.md').write_text('new docs');(tmp_path/'config/current-product-tests.json').write_text('new Python test list') assert before==fw.rtl_plan(['fixture'],{'fixture':['tb']},tools) (tmp_path/'src/top.v').write_text('changed RTL') assert before!=fw.rtl_plan(['fixture'],{'fixture':['tb']},tools)