-
Notifications
You must be signed in to change notification settings - Fork 1.7k
test(storage): isolate environment in client tests #16664
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -300,7 +300,8 @@ def test_ctor_wo_project(self): | |||||||||||||||||||||
| PROJECT = "PROJECT" | ||||||||||||||||||||||
| credentials = _make_credentials(project=PROJECT) | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| client = self._make_one(credentials=credentials) | ||||||||||||||||||||||
| with mock.patch("os.environ", {}): | ||||||||||||||||||||||
| client = self._make_one(credentials=credentials) | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| self.assertEqual(client.project, PROJECT) | ||||||||||||||||||||||
| self.assertIsInstance(client._connection, Connection) | ||||||||||||||||||||||
|
|
@@ -368,10 +369,11 @@ def test_ctor_w_custom_endpoint_use_auth(self): | |||||||||||||||||||||
|
|
||||||||||||||||||||||
| def test_ctor_w_custom_endpoint_bypass_auth(self): | ||||||||||||||||||||||
| custom_endpoint = "storage-example.p.googleapis.com" | ||||||||||||||||||||||
| client = self._make_one( | ||||||||||||||||||||||
| client_options={"api_endpoint": custom_endpoint}, | ||||||||||||||||||||||
| use_auth_w_custom_endpoint=False, | ||||||||||||||||||||||
| ) | ||||||||||||||||||||||
| with mock.patch("os.environ", {}): | ||||||||||||||||||||||
| client = self._make_one( | ||||||||||||||||||||||
| client_options={"api_endpoint": custom_endpoint}, | ||||||||||||||||||||||
| use_auth_w_custom_endpoint=False, | ||||||||||||||||||||||
| ) | ||||||||||||||||||||||
|
Comment on lines
+372
to
+376
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using
Suggested change
|
||||||||||||||||||||||
| self.assertEqual(client._connection.API_BASE_URL, custom_endpoint) | ||||||||||||||||||||||
| self.assertEqual(client.project, None) | ||||||||||||||||||||||
| self.assertIsInstance(client._connection, Connection) | ||||||||||||||||||||||
|
|
@@ -381,9 +383,11 @@ def test_ctor_w_custom_endpoint_w_credentials(self): | |||||||||||||||||||||
| PROJECT = "PROJECT" | ||||||||||||||||||||||
| custom_endpoint = "storage-example.p.googleapis.com" | ||||||||||||||||||||||
| credentials = _make_credentials(project=PROJECT) | ||||||||||||||||||||||
| client = self._make_one( | ||||||||||||||||||||||
| credentials=credentials, client_options={"api_endpoint": custom_endpoint} | ||||||||||||||||||||||
| ) | ||||||||||||||||||||||
| with mock.patch("os.environ", {}): | ||||||||||||||||||||||
| client = self._make_one( | ||||||||||||||||||||||
| credentials=credentials, | ||||||||||||||||||||||
| client_options={"api_endpoint": custom_endpoint}, | ||||||||||||||||||||||
| ) | ||||||||||||||||||||||
|
Comment on lines
+386
to
+390
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using
Suggested change
|
||||||||||||||||||||||
| self.assertEqual(client._connection.API_BASE_URL, custom_endpoint) | ||||||||||||||||||||||
| self.assertEqual(client.project, PROJECT) | ||||||||||||||||||||||
| self.assertIsInstance(client._connection, Connection) | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using
mock.patch("os.environ", {})replaces theos.environobject entirely with a standard dictionary. This can be brittle because it won't affect code that has already importedenvironusingfrom os import environ. Additionally, it loses the special behaviors of theos._Environobject (such as callingputenv). It is recommended to usemock.patch.dictwithclear=Trueinstead, as it modifies the existing object in place and is more robust for environment isolation.