@@ -79,16 +79,39 @@ def __init__(self,args,test):
7979 self .test_out = self .args .outputdir + '/test_output_' + self .test_id
8080 self .prepare_test ()
8181
82- def compare_ok (self ,got_file ,expected_file ,name ):
82+ # Compares 'got_file' against 'expected_file'.
83+ # Returns (True,Msg) if there is a difference or one of the files does not exist and (False,'') otherwise
84+ def compare_ok (self , got_file , expected_file ):
8385 if not os .path .isfile (got_file ):
84- return (True ,'%s absent' % got_file )
85- elif not os .path .isfile (expected_file ):
86- return (True ,'%s absent' % expected_file )
87- else :
88- diff = xpopen ('diff -b -w -u %s %s' % (got_file ,expected_file ))
86+ return (True , f'{ got_file } absent' )
87+ if not os .path .isfile (expected_file ):
88+ return (True , f'{ expected_file } absent' )
89+
90+ def _write_filtered_tmp (src_path , tmp_path ):
91+ # Read, split on tags to add newlines, drop empty lines, and write to tmp
92+ with xopen (src_path , 'r' ) as f , xopen (tmp_path , 'w' ) as out_file :
93+ filtered = [line for line in f .read ().
94+ replace ("<" , "\n <" ).replace (">" , ">\n " ).splitlines () if line .strip ()]
95+ out_file .write ('\n ' .join (filtered )+ '\n ' )
96+
97+ got_file_tmp = got_file + "_got"
98+ expected_file_tmp = got_file + "_ref"
99+
100+ try :
101+ for src , tmp in ((got_file , got_file_tmp ), (expected_file , expected_file_tmp )):
102+ _write_filtered_tmp (src , tmp )
103+
104+ diff = xpopen (f'diff -b -w -u { got_file_tmp } { expected_file_tmp } ' )
89105 if diff and not diff .startswith ("No differences" ):
90- return (True ,'Difference between generated output and reference:\n %s' % diff )
91- return (False ,'' )
106+ return (True , f'Difference between generated output and reference:\n { diff } ' )
107+ return (False , '' )
108+ finally :
109+ # Ensure cleanup even if diff or write fails
110+ for tmp in (got_file_tmp , expected_file_tmp ):
111+ try :
112+ os .remove (tmp )
113+ except OSError :
114+ pass
92115
93116 def cleanup_xmllint (self ,errmsg ):
94117 msg = errmsg .split ('\n ' )
@@ -328,7 +351,7 @@ def perform_test(self,testmgr):
328351 with xopen (out_file ,'w' ) as f :
329352 print (data ,file = f )
330353 ref_file = '%s/%s/%s' % (self .args .inputdir ,self .test_id ,check )
331- (failed_xml ,xml_msg ) = self .compare_ok (out_file ,ref_file , self . test_name )
354+ (failed_xml ,xml_msg ) = self .compare_ok (out_file ,ref_file )
332355 if failed_xml :
333356 msg += (xml_msg ,)
334357 break
0 commit comments