@@ -79,30 +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- got_file_tmp = got_file + "_tmp"
89- with xopen ( got_file , 'r' ) as f :
90- with xopen (got_file_tmp , "w" ) as out_file :
91- lines = f .read ().replace ( "<" , " \n <" ). replace ( ">" , "> \n " ). splitlines ()
92- filtered = [ line for line in lines if line .strip () ]
93- out_file .write ('\n ' .join (filtered ))
94- expected_file_tmp = got_file + "_tmp1"
95- with xopen ( expected_file , 'r' ) as f :
96- with xopen ( expected_file_tmp , "w" ) as out_file :
97- lines = f . read (). replace ( "<" , " \n <" ). replace ( ">" , "> \n " ). splitlines ()
98- filtered = [ line for line in lines if line . strip () ]
99- out_file . write ( ' \n ' . join ( filtered ))
100- diff = xpopen ( 'diff -b -w -u %s %s' % ( got_file_tmp , expected_file_tmp ) )
101- os . remove ( got_file_tmp )
102- os . remove ( expected_file_tmp )
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 } ' )
103105 if diff and not diff .startswith ("No differences" ):
104- return (True ,'Difference between generated output and reference:\n %s' % diff )
105- 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
106115
107116 def cleanup_xmllint (self ,errmsg ):
108117 msg = errmsg .split ('\n ' )
@@ -342,7 +351,7 @@ def perform_test(self,testmgr):
342351 with xopen (out_file ,'w' ) as f :
343352 print (data ,file = f )
344353 ref_file = '%s/%s/%s' % (self .args .inputdir ,self .test_id ,check )
345- (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 )
346355 if failed_xml :
347356 msg += (xml_msg ,)
348357 break
0 commit comments