@@ -102,7 +102,15 @@ def factory(**kwargs):
102102
103103@pytest .fixture (scope = "session" )
104104def buckets_to_delete ():
105- """A set to keep track of buckets created during the test session."""
105+ """
106+ Provides a session-scoped set to track the names of GCS buckets that are
107+ created by the test suite.
108+
109+ When tests run, they may create new GCS buckets. If these buckets are not
110+ deleted, they will persist after the test run, leading to resource leakage.
111+ This set acts as a registry of buckets that the `final_cleanup` fixture
112+ should remove at the end of the entire test session.
113+ """
106114 return set ()
107115
108116
@@ -113,6 +121,11 @@ def gcs(gcs_factory, buckets_to_delete, populate=True):
113121 # Create the bucket if it doesn't exist, otherwise clean it.
114122 if not gcs .exists (TEST_BUCKET ):
115123 gcs .mkdir (TEST_BUCKET )
124+ # By adding the bucket name to this set, we are marking it for
125+ # deletion at the end of the test session. This ensures that if
126+ # the test suite creates the bucket, it will also be responsible
127+ # for deleting it. If the bucket already existed, we assume it's
128+ # managed externally and should not be deleted by the tests.
116129 buckets_to_delete .add (TEST_BUCKET )
117130 else :
118131 try :
@@ -138,41 +151,24 @@ def _cleanup_gcs(gcs):
138151
139152@pytest .fixture (scope = "session" , autouse = True )
140153def final_cleanup (gcs_factory , buckets_to_delete ):
141- """A session-scoped fixture to delete the test buckets after all tests are run."""
154+ """
155+ A session-scoped, auto-use fixture that deletes all buckets registered
156+ in the `buckets_to_delete` set after the entire test session is complete.
157+ """
142158 yield
143159 # This code runs after the entire test session finishes
144- use_extended_gcs = os .getenv (
145- "GCSFS_EXPERIMENTAL_ZB_HNS_SUPPORT" , "false"
146- ).lower () in (
147- "true" ,
148- "1" ,
149- )
150160
151- if use_extended_gcs :
152- is_real_gcs = (
153- os .environ .get ("STORAGE_EMULATOR_HOST" ) == "https://storage.googleapis.com"
154- )
155- mock_authentication_manager = (
156- patch ("google.auth.default" , return_value = (None , "fake-project" ))
157- if not is_real_gcs
158- else nullcontext ()
159- )
160- else :
161- mock_authentication_manager = nullcontext ()
162-
163- with mock_authentication_manager :
164- gcs = gcs_factory ()
165- for bucket in buckets_to_delete :
166- # For real GCS, only delete if created by the test suite.
167- # For emulators, always delete.
168- try :
169- if gcs .exists (bucket ):
170- gcs .rm (bucket , recursive = True )
171- logging .info (f"Cleaned up bucket: { bucket } " )
172- except Exception as e :
173- logging .warning (
174- f"Failed to perform final cleanup for bucket { bucket } : { e } "
175- )
161+ gcs = gcs_factory ()
162+ for bucket in buckets_to_delete :
163+ # The cleanup logic attempts to delete every bucket that was
164+ # added to the set during the session. For real GCS, only delete if
165+ # created by the test suite.
166+ try :
167+ if gcs .exists (bucket ):
168+ gcs .rm (bucket , recursive = True )
169+ logging .info (f"Cleaned up bucket: { bucket } " )
170+ except Exception as e :
171+ logging .warning (f"Failed to perform final cleanup for bucket { bucket } : { e } " )
176172
177173
178174@pytest .fixture
@@ -219,6 +215,8 @@ def gcs_versioned(gcs_factory, buckets_to_delete):
219215 )
220216
221217 if _VERSIONED_BUCKET_CREATED_BY_TESTS :
218+ # If the versioned bucket was created by the test suite, it's added
219+ # here for cleanup.
222220 buckets_to_delete .add (TEST_VERSIONED_BUCKET )
223221 except ImportError :
224222 pass # test_core_versioned is not being run
@@ -231,6 +229,8 @@ def gcs_versioned(gcs_factory, buckets_to_delete):
231229 except FileNotFoundError :
232230 pass
233231 gcs .mkdir (TEST_VERSIONED_BUCKET , enable_versioning = True )
232+ # When using the emulator, the versioned bucket is always recreated
233+ # and added to the cleanup set.
234234 buckets_to_delete .add (TEST_VERSIONED_BUCKET )
235235 gcs .invalidate_cache ()
236236 yield gcs
0 commit comments