@@ -266,41 +266,41 @@ def test_clean_modules_removes_from_sys_modules(minion_opts):
266266 f"{ loaded_base_name } .ext.{ tag } " ,
267267 }
268268
269+ # Prefixes for modules that belong specifically to this loader's tag.
270+ # clean_modules() only removes modules under these prefixes, so we only
271+ # check these prefixes — not ALL salt.loaded.* modules. Checking the
272+ # broader namespace would make the test sensitive to modules loaded by
273+ # other tests that ran in the same process (e.g. salt.loaded.int.modules.*
274+ # from execution-module unit tests).
275+ tag_prefixes = (
276+ f"{ loaded_base_name } .int.{ tag } ." ,
277+ f"{ loaded_base_name } .ext.{ tag } ." ,
278+ )
279+
269280 # Load some modules
270281 for key in list (loader .keys ())[:5 ]:
271282 try :
272283 _ = loader [key ]
273284 except Exception : # pylint: disable=broad-except
274285 pass
275286
276- # Find modules that were loaded
277- loaded_before = [m for m in sys .modules if m .startswith (loaded_base_name )]
287+ # Find tag-specific modules that were loaded
288+ loaded_before = [
289+ m for m in sys .modules if any (m .startswith (p ) for p in tag_prefixes )
290+ ]
278291 assert len (loaded_before ) > 0 , "No modules were loaded for testing"
279292
280293 # Clean modules
281294 loader .clean_modules ()
282295
283- # Verify actual loaded modules are removed but base stubs remain
284- remaining = [m for m in sys .modules if m .startswith (loaded_base_name )]
285-
286- # All remaining modules should be base stubs or utils modules (shared infrastructure)
287- # Filter out both base stubs and utils modules
288- unexpected = []
289- for m in remaining :
290- # Skip base stubs
291- if m in expected_base_stubs :
292- continue
293- # Skip utils modules (shared infrastructure)
294- parts = m .split ("." )
295- # Utils modules: salt.loaded.int.utils, salt.loaded.int.utils.*, etc.
296- if len (parts ) >= 4 and parts [3 ] in ("utils" , "wrapper" ):
297- continue
298- # Anything else is unexpected
299- unexpected .append (m )
296+ # All tag-specific modules should have been removed
297+ remaining_tag = [
298+ m for m in sys .modules if any (m .startswith (p ) for p in tag_prefixes )
299+ ]
300300
301301 assert (
302- len (unexpected ) == 0
303- ), f"clean_modules() failed to remove { len (unexpected )} modules: { unexpected } "
302+ len (remaining_tag ) == 0
303+ ), f"clean_modules() failed to remove { len (remaining_tag )} modules: { remaining_tag } "
304304
305305 # Base stubs should still be present
306306 for stub in expected_base_stubs :
0 commit comments