Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions packages/google-cloud-storage/tests/unit/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Comment on lines +303 to +304
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)


self.assertEqual(client.project, PROJECT)
self.assertIsInstance(client._connection, Connection)
Expand Down Expand Up @@ -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
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,
)

self.assertEqual(client._connection.API_BASE_URL, custom_endpoint)
self.assertEqual(client.project, None)
self.assertIsInstance(client._connection, Connection)
Expand All @@ -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
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}
)

self.assertEqual(client._connection.API_BASE_URL, custom_endpoint)
self.assertEqual(client.project, PROJECT)
self.assertIsInstance(client._connection, Connection)
Expand Down
Loading