Skip to content

Commit 44bf385

Browse files
committed
chore: simplify warnings check
1 parent 8398248 commit 44bf385

File tree

5 files changed

+31
-65
lines changed

5 files changed

+31
-65
lines changed

tests/asyncio/test_grpc_helpers_async.py

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from unittest.mock import AsyncMock # pragma: NO COVER # noqa: F401
1818
except ImportError: # pragma: NO COVER
1919
import mock # type: ignore
20+
from ..helpers import warn_deprecated_credentials_file
2021
import pytest # noqa: I202
2122

2223
try:
@@ -522,10 +523,7 @@ def test_create_channel_explicit_with_duplicate_credentials():
522523
target = "example:443"
523524

524525
with pytest.raises(exceptions.DuplicateCredentialArgs) as excinfo:
525-
with pytest.warns(
526-
DeprecationWarning,
527-
match="argument is deprecated because of a potential security risk",
528-
):
526+
with warn_deprecated_credentials_file():
529527
grpc_helpers_async.create_channel(
530528
target,
531529
credentials_file="credentials.json",
@@ -645,10 +643,7 @@ def test_create_channel_with_credentials_file(
645643
credentials_file = "/path/to/credentials/file.json"
646644
composite_creds = composite_creds_call.return_value
647645

648-
with pytest.warns(
649-
DeprecationWarning,
650-
match="argument is deprecated because of a potential security risk",
651-
):
646+
with warn_deprecated_credentials_file():
652647
channel = grpc_helpers_async.create_channel(
653648
target, credentials_file=credentials_file
654649
)
@@ -678,10 +673,7 @@ def test_create_channel_with_credentials_file_and_scopes(
678673
credentials_file = "/path/to/credentials/file.json"
679674
composite_creds = composite_creds_call.return_value
680675

681-
with pytest.warns(
682-
DeprecationWarning,
683-
match="argument is deprecated because of a potential security risk",
684-
):
676+
with warn_deprecated_credentials_file():
685677
channel = grpc_helpers_async.create_channel(
686678
target, credentials_file=credentials_file, scopes=scopes
687679
)
@@ -711,10 +703,7 @@ def test_create_channel_with_credentials_file_and_default_scopes(
711703
credentials_file = "/path/to/credentials/file.json"
712704
composite_creds = composite_creds_call.return_value
713705

714-
with pytest.warns(
715-
DeprecationWarning,
716-
match="argument is deprecated because of a potential security risk",
717-
):
706+
with warn_deprecated_credentials_file():
718707
channel = grpc_helpers_async.create_channel(
719708
target, credentials_file=credentials_file, default_scopes=default_scopes
720709
)

tests/helpers.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414

1515
"""Helpers for tests"""
1616

17+
import functools
1718
import logging
19+
import pytest # noqa: I202
1820
from typing import List
1921

2022
import proto
@@ -69,3 +71,12 @@ def parse_responses(response_message_cls, all_responses: List[proto.Message]) ->
6971
logging.info(f"Sending JSON stream: {json_responses}")
7072
ret_val = "[{}]".format(",".join(json_responses))
7173
return bytes(ret_val, "utf-8")
74+
75+
76+
warn_deprecated_credentials_file = functools.partial(
77+
# This is used to test that the auth credentials file deprecation
78+
# warning is emitted as expected.
79+
pytest.warns,
80+
DeprecationWarning,
81+
match="argument is deprecated because of a potential security risk",
82+
)

tests/unit/operations_v1/test_operations_rest_client.py

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
import pytest
2525
from typing import Any, List
26+
from ...helpers import warn_deprecated_credentials_file
2627

2728
try:
2829
import grpc # noqa: F401
@@ -369,10 +370,7 @@ def test_operations_client_client_options(
369370
)
370371

371372
# Check the case credentials_file is provided
372-
with pytest.warns(
373-
DeprecationWarning,
374-
match="argument is deprecated because of a potential security risk",
375-
):
373+
with warn_deprecated_credentials_file():
376374
options = client_options.ClientOptions(credentials_file="credentials.json")
377375
with mock.patch.object(transport_class, "__init__") as patched:
378376
patched.return_value = None
@@ -543,18 +541,12 @@ def test_operations_client_client_options_credentials_file(
543541
client_class, transport_class, transport_name
544542
):
545543
# Check the case credentials file is provided.
546-
with pytest.warns(
547-
DeprecationWarning,
548-
match="argument is deprecated because of a potential security risk",
549-
):
544+
with warn_deprecated_credentials_file():
550545
options = client_options.ClientOptions(credentials_file="credentials.json")
551546
if "async" in str(client_class):
552547
# TODO(): Add support for credentials file to async REST transport.
553548
with pytest.raises(core_exceptions.AsyncRestUnsupportedParameterError):
554-
with pytest.warns(
555-
DeprecationWarning,
556-
match="argument is deprecated because of a potential security risk",
557-
):
549+
with warn_deprecated_credentials_file():
558550

559551
client_class(client_options=options, transport=transport_name)
560552
else:
@@ -584,10 +576,7 @@ def test_operations_client_client_options_credentials_file(
584576
)
585577
def test_list_operations_rest(google_auth_default, credentials_file):
586578
if credentials_file:
587-
with pytest.warns(
588-
DeprecationWarning,
589-
match="argument is deprecated because of a potential security risk",
590-
):
579+
with warn_deprecated_credentials_file():
591580
sync_transport = transports.rest.OperationsRestTransport(
592581
credentials_file=credentials_file,
593582
http_options=HTTP_OPTIONS,
@@ -1154,10 +1143,7 @@ def test_transport_adc(client_class, transport_class, credentials):
11541143
def test_operations_base_transport_error():
11551144
# Passing both a credentials object and credentials_file should raise an error
11561145
with pytest.raises(core_exceptions.DuplicateCredentialArgs):
1157-
with pytest.warns(
1158-
DeprecationWarning,
1159-
match="argument is deprecated because of a potential security risk",
1160-
):
1146+
with warn_deprecated_credentials_file():
11611147
transports.OperationsTransport(
11621148
credentials=ga_credentials.AnonymousCredentials(),
11631149
credentials_file="credentials.json",
@@ -1199,10 +1185,7 @@ def test_operations_base_transport_with_credentials_file():
11991185
) as Transport:
12001186
Transport.return_value = None
12011187
load_creds.return_value = (ga_credentials.AnonymousCredentials(), None)
1202-
with pytest.warns(
1203-
DeprecationWarning,
1204-
match="argument is deprecated because of a potential security risk",
1205-
):
1188+
with warn_deprecated_credentials_file():
12061189
transports.OperationsTransport(
12071190
credentials_file="credentials.json",
12081191
quota_project_id="octopus",

tests/unit/test_client_options.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
from re import match
1616
import pytest
17+
from ..helpers import warn_deprecated_credentials_file
1718

1819
from google.api_core import client_options
1920

@@ -27,11 +28,7 @@ def get_client_encrypted_cert():
2728

2829

2930
def test_constructor():
30-
31-
with pytest.warns(
32-
DeprecationWarning,
33-
match="argument is deprecated because of a potential security risk",
34-
):
31+
with warn_deprecated_credentials_file():
3532
options = client_options.ClientOptions(
3633
api_endpoint="foo.googleapis.com",
3734
client_cert_source=get_client_cert,
@@ -106,10 +103,7 @@ def test_constructor_with_api_key():
106103

107104
def test_constructor_with_both_api_key_and_credentials_file():
108105
with pytest.raises(ValueError):
109-
with pytest.warns(
110-
DeprecationWarning,
111-
match="argument is deprecated because of a potential security risk",
112-
):
106+
with warn_deprecated_credentials_file():
113107
client_options.ClientOptions(
114108
api_key="api-key",
115109
credentials_file="path/to/credentials.json",

tests/unit/test_grpc_helpers.py

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from unittest import mock
1616

1717
import pytest
18+
from ..helpers import warn_deprecated_credentials_file
1819

1920
try:
2021
import grpc
@@ -581,10 +582,7 @@ def test_create_channel_explicit_with_duplicate_credentials():
581582
target = "example.com:443"
582583

583584
with pytest.raises(exceptions.DuplicateCredentialArgs):
584-
with pytest.warns(
585-
DeprecationWarning,
586-
match="argument is deprecated because of a potential security risk",
587-
):
585+
with warn_deprecated_credentials_file():
588586
grpc_helpers.create_channel(
589587
target,
590588
credentials_file="credentials.json",
@@ -714,10 +712,7 @@ def test_create_channel_with_credentials_file(
714712
credentials_file = "/path/to/credentials/file.json"
715713
composite_creds = composite_creds_call.return_value
716714

717-
with pytest.warns(
718-
DeprecationWarning,
719-
match="argument is deprecated because of a potential security risk",
720-
):
715+
with warn_deprecated_credentials_file():
721716
channel = grpc_helpers.create_channel(target, credentials_file=credentials_file)
722717

723718
google.auth.load_credentials_from_file.assert_called_once_with(
@@ -750,10 +745,7 @@ def test_create_channel_with_credentials_file_and_scopes(
750745
credentials_file = "/path/to/credentials/file.json"
751746
composite_creds = composite_creds_call.return_value
752747

753-
with pytest.warns(
754-
DeprecationWarning,
755-
match="argument is deprecated because of a potential security risk",
756-
):
748+
with warn_deprecated_credentials_file():
757749
channel = grpc_helpers.create_channel(
758750
target, credentials_file=credentials_file, scopes=scopes
759751
)
@@ -788,10 +780,7 @@ def test_create_channel_with_credentials_file_and_default_scopes(
788780
credentials_file = "/path/to/credentials/file.json"
789781
composite_creds = composite_creds_call.return_value
790782

791-
with pytest.warns(
792-
DeprecationWarning,
793-
match="argument is deprecated because of a potential security risk",
794-
):
783+
with warn_deprecated_credentials_file():
795784
channel = grpc_helpers.create_channel(
796785
target, credentials_file=credentials_file, default_scopes=default_scopes
797786
)

0 commit comments

Comments
 (0)