@@ -98,6 +98,25 @@ def escape_special_characters(string):
9898 return string .translate (translation_table )
9999
100100
101+ def normalize_test_line (line ):
102+ """Normalize a tests-list / allow|block list line for matching.
103+
104+ - Strip whitespace/newlines
105+ - Strip optional surrounding double quotes (legacy dry-run / convert_yaml
106+ wrap patterns as ``".*[lb].*"`` while OTE ``list -o names`` is unquoted)
107+ - Drop OTE/klog noise lines that land in list output (``I0810 ...``)
108+ """
109+ s = line .strip ()
110+ if not s :
111+ return None
112+ # klog-style lines mixed into OTE list stdout
113+ if re .match (r'I\d{4}\s' , s ) or 'test_context.go:' in s :
114+ return None
115+ if len (s ) >= 2 and s [0 ] == '"' and s [- 1 ] == '"' :
116+ s = s [1 :- 1 ].strip ()
117+ return s or None
118+
119+
101120def run_module ():
102121 # define the AnsibleModule object with the available
103122 # arguments/parameters a user can pass to the module
@@ -128,10 +147,17 @@ def run_module():
128147
129148 try :
130149 with open (input_tests_file , 'r' ) as f :
131- input_tests = set ([line for line in f ])
150+ # Preserve original lines for output, keyed by normalized name.
151+ input_tests_raw = [line for line in f ]
132152 except IOError :
133153 module .fail_json (msg = "Error opening the input tests file" )
134154
155+ input_tests = []
156+ for line in input_tests_raw :
157+ normalized = normalize_test_line (line )
158+ if normalized is not None :
159+ input_tests .append ((normalized , line if line .endswith ('\n ' ) else line + '\n ' ))
160+
135161 if allowlist_file and blocklist_file :
136162 module .fail_json (msg = "parameters are mutually exclusive: "
137163 "allowlist_file|blocklist_file" , ** result )
@@ -146,12 +172,15 @@ def run_module():
146172 module .fail_json (msg = "Error opening the allowlist file" )
147173
148174 for allowlist_test in allowlist :
175+ allow_norm = normalize_test_line (allowlist_test )
176+ if allow_norm is None :
177+ continue
149178 allowlist_test_in_input_tests = False
179+ escaped_allow_test = escape_special_characters (allow_norm )
150180
151- for test in input_tests :
152- escaped_allow_test = escape_special_characters (allowlist_test )
153- if re .fullmatch (escaped_allow_test , test ):
154- tests_to_run .append (test )
181+ for test_norm , test_raw in input_tests :
182+ if re .fullmatch (escaped_allow_test , test_norm ):
183+ tests_to_run .append (test_raw )
155184 allowlist_test_in_input_tests = True
156185
157186 if not allowlist_test_in_input_tests :
@@ -173,35 +202,42 @@ def run_module():
173202 elif blocklist_file :
174203 try :
175204 with open (blocklist_file , 'r' ) as f :
176- blocklist = set ( [line for line in f ])
205+ blocklist = [line for line in f ]
177206 except IOError :
178207 module .fail_json (msg = "Error opening the blocklist file" )
179208
209+ blocklist_norms = []
210+ for blocklist_test in blocklist :
211+ block_norm = normalize_test_line (blocklist_test )
212+ if block_norm is not None :
213+ blocklist_norms .append ((block_norm , blocklist_test ))
214+
180215 # initialize lists for tests
181216 tests_to_run = []
182217 blocked_tests = []
183218 unused_blocklist_tests = []
184219
185220 # iterate over the list of tests and set the tests to run
186- for test in input_tests :
221+ for test_norm , test_raw in input_tests :
187222 test_in_blocklist = False
188223
189- for blocklist_test in blocklist :
190- escaped_block_test = escape_special_characters (blocklist_test )
191- if re .fullmatch (escaped_block_test , test ):
224+ for block_norm , _block_raw in blocklist_norms :
225+ escaped_block_test = escape_special_characters (block_norm )
226+ if re .fullmatch (escaped_block_test , test_norm ):
192227 test_in_blocklist = True
193228 break
194229
195230 if test_in_blocklist :
196- blocked_tests .append (test )
231+ blocked_tests .append (test_raw )
197232 else :
198- tests_to_run .append (test )
233+ tests_to_run .append (test_raw )
199234
200235 # set the unused blocklist tests
201- for blocklist_test in blocklist :
202- escaped_block_test = escape_special_characters (blocklist_test )
203- if not any (re .fullmatch (escaped_block_test , test ) for test in input_tests ):
204- unused_blocklist_tests .append (blocklist_test )
236+ for block_norm , block_raw in blocklist_norms :
237+ escaped_block_test = escape_special_characters (block_norm )
238+ if not any (re .fullmatch (escaped_block_test , test_norm )
239+ for test_norm , _ in input_tests ):
240+ unused_blocklist_tests .append (block_raw )
205241 if unused_blocklist_tests :
206242 module .warn ("Warning! Some tests in the blocklist were not used" )
207243 result ['unused_blocklist_tests' ] = unused_blocklist_tests
@@ -227,7 +263,13 @@ def run_module():
227263 result ['changed' ] = True
228264
229265 else :
230- shutil .copyfile (input_tests_file , output_file )
266+ # Drop klog noise even when no allow/block filter is applied (OTE list).
267+ try :
268+ with open (output_file , 'w' ) as f :
269+ for _test_norm , test_raw in input_tests :
270+ f .write (test_raw )
271+ except IOError :
272+ module .fail_json (msg = "Error writing to output file" )
231273
232274 result ['filter_type' ] = 'no filter applied'
233275 result ['changed' ] = True
0 commit comments