test(storage): isolate environment in client tests#16664
Open
chalmerlowe wants to merge 6 commits intomainfrom
Open
test(storage): isolate environment in client tests#16664chalmerlowe wants to merge 6 commits intomainfrom
chalmerlowe wants to merge 6 commits intomainfrom
Conversation
Contributor
There was a problem hiding this comment.
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.
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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, andtest_ctor_w_custom_endpoint_w_credentialswere asserting thatclient.projectwasNoneor 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
code block to ensure the environment is clean and isolated during test execution.
Note
os.environis not simply a Pythondict, it is anos._Environobject 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:
os.environreference to point at an emptydictbut rather we replace the existingdictin 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.os._Environobject.clear=Truewe explicitly state that we want a clean slate for this test.