Skip to content

Commit c422cbb

Browse files
test(storage): isolate environment in client tests (#16664)
This PR resolves a unit test failure where the project ID leaked from the local environment into the client constructor tests, causing assertions to fail. **Problem**: Tests like `test_ctor_wo_project`, `test_ctor_w_custom_endpoint_bypass_auth`, and `test_ctor_w_custom_endpoint_w_credentials` were asserting that `client.project` was `None` or a specific project ID (like `"PROJECT"`). However, they were receiving a project ID set in the user's local environment (e.g., `"precise-truck-742"`), causing an AssertionError. **Solution**: Wrapped the client creation in these specific tests using a ``` with mock.patch.dict("os.environ", {}, clear=True): ``` code block to ensure the environment is clean and isolated during test execution. > [!Note] `os.environ` is not simply a Python `dict`, it is an `os._Environ` object with special characteristics (i.e. `putenv`, `unsetenv`, enforcing string-only keys/values, handling case-sensitivity in Windows OS). > > The approach in this PR ensures that: > * we don't just move **this** `os.environ` reference to point at an empty `dict` but rather we replace the existing `dict` _in situ_ so that any other references to those variables that might be been created earlier in code execution will now reference the correct values as well. > * We ensure we retain access to the characteristics of the `os._Environ` object. > * Using `clear=True` we explicitly state that we want a clean slate for this test. --------- Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
1 parent fcab64e commit c422cbb

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

packages/google-cloud-storage/tests/unit/test_client.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,8 @@ def test_ctor_wo_project(self):
300300
PROJECT = "PROJECT"
301301
credentials = _make_credentials(project=PROJECT)
302302

303-
client = self._make_one(credentials=credentials)
303+
with mock.patch.dict("os.environ", {}, clear=True):
304+
client = self._make_one(credentials=credentials)
304305

305306
self.assertEqual(client.project, PROJECT)
306307
self.assertIsInstance(client._connection, Connection)
@@ -368,10 +369,11 @@ def test_ctor_w_custom_endpoint_use_auth(self):
368369

369370
def test_ctor_w_custom_endpoint_bypass_auth(self):
370371
custom_endpoint = "storage-example.p.googleapis.com"
371-
client = self._make_one(
372-
client_options={"api_endpoint": custom_endpoint},
373-
use_auth_w_custom_endpoint=False,
374-
)
372+
with mock.patch.dict("os.environ", {}, clear=True):
373+
client = self._make_one(
374+
client_options={"api_endpoint": custom_endpoint},
375+
use_auth_w_custom_endpoint=False,
376+
)
375377
self.assertEqual(client._connection.API_BASE_URL, custom_endpoint)
376378
self.assertEqual(client.project, None)
377379
self.assertIsInstance(client._connection, Connection)
@@ -381,9 +383,11 @@ def test_ctor_w_custom_endpoint_w_credentials(self):
381383
PROJECT = "PROJECT"
382384
custom_endpoint = "storage-example.p.googleapis.com"
383385
credentials = _make_credentials(project=PROJECT)
384-
client = self._make_one(
385-
credentials=credentials, client_options={"api_endpoint": custom_endpoint}
386-
)
386+
with mock.patch.dict("os.environ", {}, clear=True):
387+
client = self._make_one(
388+
credentials=credentials,
389+
client_options={"api_endpoint": custom_endpoint},
390+
)
387391
self.assertEqual(client._connection.API_BASE_URL, custom_endpoint)
388392
self.assertEqual(client.project, PROJECT)
389393
self.assertIsInstance(client._connection, Connection)

0 commit comments

Comments
 (0)