@@ -29,6 +29,11 @@ def is_in_list(self, warnings_list):
2929 return True
3030 return False
3131
32+ def print_with_prefix (self , prefix ):
33+ print (prefix + self .firstline )
34+ for line in self .otherlines :
35+ print (prefix + ' ' + line )
36+
3237
3338def build_warnings_list (all_lines ):
3439 """Create a list of the warnings in this file."""
@@ -70,6 +75,15 @@ def ignore_too_many_nodes(all_lines):
7075 return [x for x in all_lines if not too_many_nodes_expr .match (x )]
7176
7277
78+ def list1_is_in_list2 (list1 , list2 , prefix ):
79+ missing_element_found = False
80+ for warning in list1 :
81+ if not warning .is_in_list (list2 ):
82+ missing_element_found = True
83+ warning .print_with_prefix (prefix )
84+ return missing_element_found
85+
86+
7387def filter_expected_warnings (expected_warnings_path ):
7488 """Filter lines from stdin and print to stdout."""
7589 with open (expected_warnings_path , "r" ) as warnings_file :
@@ -79,26 +93,27 @@ def filter_expected_warnings(expected_warnings_path):
7993 ignore_too_many_nodes (sys .stdin .readlines ()))
8094
8195 # print unexpected warnings
82- for warning in new_warnings :
83- if not warning .is_in_list (expected_warnings ):
84- print (warning .firstline )
85- for line in warning .otherlines :
86- print (' ' + line )
96+ unexpected_warning_found = list1_is_in_list2 (
97+ new_warnings , expected_warnings , '' )
98+
99+ # print expected warnings which aren't found
100+ expected_warning_not_found = list1_is_in_list2 (
101+ expected_warnings , new_warnings , '-' )
102+
103+ if expected_warning_not_found :
104+ print ('NOTE: Warnings prefixed with \' -\' are expected ' +
105+ 'warnings which weren\' t found.' )
106+ print (' Please update the list of expected warnings.' )
87107
88- # complain if expected warnings aren't found
89- for warning in expected_warnings :
90- if not warning .is_in_list (new_warnings ):
91- print ('Expected warning not found:' )
92- print ('-' + warning .firstline )
93- for line in warning .otherlines :
94- print ('- ' + line )
108+ return unexpected_warning_found or expected_warning_not_found
95109
96110
97111if __name__ == "__main__" :
98112
99113 if len (sys .argv ) != 2 :
100114 print ('usage: filter_expected_warnings.py <expected_warnings_file>' )
101115 print ('(warnings from stdin are filtered and printed to stdout)' )
102- sys .exit ()
116+ sys .exit (1 )
103117
104- filter_expected_warnings (sys .argv [1 ])
118+ problem_found = filter_expected_warnings (sys .argv [1 ])
119+ sys .exit (1 if problem_found else 0 )
0 commit comments