Skip to content

test(storage): isolate environment in client tests#16664

Open
chalmerlowe wants to merge 6 commits intomainfrom
fix-storage-test-env
Open

test(storage): isolate environment in client tests#16664
chalmerlowe wants to merge 6 commits intomainfrom
fix-storage-test-env

Conversation

@chalmerlowe
Copy link
Copy Markdown
Contributor

@chalmerlowe chalmerlowe commented Apr 15, 2026

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.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request updates several unit tests in test_client.py to isolate the environment by patching os.environ during client initialization. The review feedback suggests using mock.patch.dict with clear=True instead of mock.patch to ensure more robust environment isolation and to avoid potential issues if os.environ was imported directly in the source code.

Comment thread packages/google-cloud-storage/tests/unit/test_client.py Outdated
Comment thread packages/google-cloud-storage/tests/unit/test_client.py Outdated
Comment thread packages/google-cloud-storage/tests/unit/test_client.py Outdated
@chalmerlowe chalmerlowe marked this pull request as ready for review April 15, 2026 18:45
@chalmerlowe chalmerlowe requested a review from a team as a code owner April 15, 2026 18:45
chalmerlowe and others added 4 commits April 16, 2026 05:45
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
@chalmerlowe chalmerlowe added the automerge Merge the pull request once unit tests and other checks pass. label Apr 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

automerge Merge the pull request once unit tests and other checks pass.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant