Skip to content

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

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

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

Conversation

@chalmerlowe
Copy link
Copy Markdown
Contributor

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 with a

with mock.patch("os.environ", {}): 

code block to ensure the environment is clean and isolated during test execution.

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 on lines +303 to +304
with mock.patch("os.environ", {}):
client = self._make_one(credentials=credentials)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

Using mock.patch("os.environ", {}) replaces the os.environ object entirely with a standard dictionary. This can be brittle because it won't affect code that has already imported environ using from os import environ. Additionally, it loses the special behaviors of the os._Environ object (such as calling putenv). It is recommended to use mock.patch.dict with clear=True instead, as it modifies the existing object in place and is more robust for environment isolation.

Suggested change
with mock.patch("os.environ", {}):
client = self._make_one(credentials=credentials)
with mock.patch.dict("os.environ", {}, clear=True):
client = self._make_one(credentials=credentials)

Comment on lines +372 to +376
with mock.patch("os.environ", {}):
client = self._make_one(
client_options={"api_endpoint": custom_endpoint},
use_auth_w_custom_endpoint=False,
)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

Using mock.patch("os.environ", {}) replaces the os.environ object entirely. It is safer and more idiomatic to use mock.patch.dict("os.environ", {}, clear=True) to ensure that the environment is isolated regardless of how os.environ was imported in the source code.

Suggested change
with mock.patch("os.environ", {}):
client = self._make_one(
client_options={"api_endpoint": custom_endpoint},
use_auth_w_custom_endpoint=False,
)
with mock.patch.dict("os.environ", {}, clear=True):
client = self._make_one(
client_options={"api_endpoint": custom_endpoint},
use_auth_w_custom_endpoint=False,
)

Comment on lines +386 to +389
with mock.patch("os.environ", {}):
client = self._make_one(
credentials=credentials, client_options={"api_endpoint": custom_endpoint}
)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

Using mock.patch("os.environ", {}) replaces the os.environ object entirely. It is safer and more idiomatic to use mock.patch.dict("os.environ", {}, clear=True) to ensure that the environment is isolated regardless of how os.environ was imported in the source code.

Suggested change
with mock.patch("os.environ", {}):
client = self._make_one(
credentials=credentials, client_options={"api_endpoint": custom_endpoint}
)
with mock.patch.dict("os.environ", {}, clear=True):
client = self._make_one(
credentials=credentials, client_options={"api_endpoint": custom_endpoint}
)

@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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant