1- import filecmp
21import pathlib
32import shutil
43import unittest
54import tempfile
65import subprocess
76import os
7+ import hashlib
88
99import util
1010
@@ -19,33 +19,20 @@ def run_symqemu_and_assert_correct_result(self, binary_name):
1919
2020 util .run_symqemu_on_test_binary (binary_name = binary_name , generated_test_cases_output_dir = symqemu_gen_output_dir )
2121
22- # `filecmp.dircmp` does a "shallow" comparison, but this is not a problem here because
23- # the timestamps should always be different, so the actual content of the files will be compared.
24- # See https://docs.python.org/3/library/filecmp.html#filecmp.dircmp
25- expected_vs_actual_output_comparison = filecmp . dircmp ( symqemu_gen_output_dir , symqemu_ref_output_dir )
22+ expected_hashes = set ()
23+ for ref_file in symqemu_ref_output_dir . iterdir ():
24+ with open ( ref_file , 'rb' , buffering = 0 ) as f :
25+ expected_hashes . add ( hashlib . file_digest ( f , "sha256" ). hexdigest () )
2626
27- for diff_file in expected_vs_actual_output_comparison .diff_files :
28- ref_file = symqemu_ref_output_dir / diff_file
29- gen_file = symqemu_gen_output_dir / diff_file
27+ testcase_not_found = False
28+ for gen_file in symqemu_gen_output_dir .iterdir ():
29+ with open (gen_file , 'rb' , buffering = 0 ) as f :
30+ f_hash = hashlib .file_digest (f , "sha256" ).hexdigest ()
31+ if not f_hash in expected_hashes :
32+ print (f"Error: content of file { gen_file } not found in expected testcases." );
33+ testcase_not_found = True
3034
31- tmp_ref = tempfile .NamedTemporaryFile ("w+" )
32- subprocess .run (["xxd" , f"{ ref_file } " ], stdout = tmp_ref , check = True )
33-
34- tmp_gen = tempfile .NamedTemporaryFile ("w+" )
35- subprocess .run (["xxd" , f"{ gen_file } " ], stdout = tmp_gen , check = True )
36-
37- wdiff = subprocess .run (["wdiff" , f"{ tmp_ref .name } " , f"{ tmp_gen .name } " ], capture_output = True )
38- colordiff = subprocess .run (["colordiff" ], input = wdiff .stdout , capture_output = True )
39-
40- print (f"===== Diff found in { diff_file } ======" )
41- print (f"{ colordiff .stdout .decode ('utf-8' ).strip ()} " )
42- print (f"=================================" )
43- print ()
44-
45- self .assertEqual (expected_vs_actual_output_comparison .diff_files , [])
46- self .assertEqual (expected_vs_actual_output_comparison .left_only , [])
47- self .assertEqual (expected_vs_actual_output_comparison .right_only , [])
48- self .assertEqual (expected_vs_actual_output_comparison .funny_files , [])
35+ self .assertFalse (testcase_not_found )
4936
5037 def test_simple (self ):
5138 self .run_symqemu_and_assert_correct_result ('simple' )
0 commit comments