Commit 3f20f01
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 8b1bfc9 commit 3f20f01
1 file changed
+12
-8
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
300 | 300 | | |
301 | 301 | | |
302 | 302 | | |
303 | | - | |
| 303 | + | |
| 304 | + | |
304 | 305 | | |
305 | 306 | | |
306 | 307 | | |
| |||
368 | 369 | | |
369 | 370 | | |
370 | 371 | | |
371 | | - | |
372 | | - | |
373 | | - | |
374 | | - | |
| 372 | + | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
| 376 | + | |
375 | 377 | | |
376 | 378 | | |
377 | 379 | | |
| |||
381 | 383 | | |
382 | 384 | | |
383 | 385 | | |
384 | | - | |
385 | | - | |
386 | | - | |
| 386 | + | |
| 387 | + | |
| 388 | + | |
| 389 | + | |
| 390 | + | |
387 | 391 | | |
388 | 392 | | |
389 | 393 | | |
| |||
0 commit comments