Skip to content

Commit 2fae23a

Browse files
committed
fix: add grpc check back to test modules to prevent ImportError on non-grpc environments
1 parent 393fd85 commit 2fae23a

4 files changed

Lines changed: 39 additions & 12 deletions

File tree

packages/google-api-core/tests/unit/gapic/test_client_cert.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@
1818

1919
import pytest
2020

21+
# We need to skip this test module if grpc is not installed because importing
22+
# gapic_v1._client_cert will load gapic_v1/__init__, which unconditionally
23+
# imports gapic_v1.config, which imports grpc.
24+
try:
25+
import grpc # noqa: F401
26+
except ImportError:
27+
pytest.skip("No GRPC", allow_module_level=True)
28+
2129
from google.api_core.gapic_v1._client_cert import (
2230
_get_client_cert_source,
2331
_use_client_cert_effective,
@@ -60,12 +68,8 @@ def test_use_client_cert_effective_fallback():
6068
_use_client_cert_effective()
6169

6270

63-
@mock.patch(
64-
"google.auth.transport.mtls.has_default_client_cert_source", create=True
65-
) # noqa: E501
66-
@mock.patch(
67-
"google.auth.transport.mtls.default_client_cert_source", create=True
68-
) # noqa: E501
71+
@mock.patch("google.auth.transport.mtls.has_default_client_cert_source", create=True) # noqa: E501
72+
@mock.patch("google.auth.transport.mtls.default_client_cert_source", create=True) # noqa: E501
6973
def test_get_client_cert_source(mock_default, mock_has_default):
7074
mock_default.return_value = b"default_cert"
7175
mock_has_default.return_value = True

packages/google-api-core/tests/unit/gapic/test_config_helpers.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,20 @@
1818

1919
import pytest
2020

21+
# We need to skip this test module if grpc is not installed because importing
22+
# gapic_v1._config_helpers will load gapic_v1/__init__, which unconditionally
23+
# imports gapic_v1.config, which imports grpc.
24+
try:
25+
import grpc # noqa: F401
26+
except ImportError:
27+
pytest.skip("No GRPC", allow_module_level=True)
28+
2129
from google.auth.exceptions import MutualTLSChannelError
2230

2331
from google.api_core.gapic_v1._config_helpers import _read_environment_variables
2432

2533

26-
@mock.patch(
27-
"google.api_core.gapic_v1._config_helpers._use_client_cert_effective"
28-
) # noqa: E501
34+
@mock.patch("google.api_core.gapic_v1._config_helpers._use_client_cert_effective") # noqa: E501
2935
@mock.patch.dict(os.environ, clear=True)
3036
def test_read_environment_variables(mock_effective):
3137
mock_effective.return_value = True

packages/google-api-core/tests/unit/gapic/test_method_helpers.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,17 @@
1414
# limitations under the License.
1515

1616

17+
import pytest
18+
19+
# We need to skip this test module if grpc is not installed because importing
20+
# gapic_v1._method_helpers will load gapic_v1/__init__, which unconditionally
21+
# imports gapic_v1.config, which imports grpc.
22+
try:
23+
import grpc # noqa: F401
24+
except ImportError:
25+
pytest.skip("No GRPC", allow_module_level=True)
26+
27+
1728
from google.api_core.gapic_v1._method_helpers import _setup_request_id
1829

1930

packages/google-api-core/tests/unit/gapic/test_routing.py

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

1818
import pytest
1919

20+
# We need to skip this test module if grpc is not installed because importing
21+
# gapic_v1._routing will load gapic_v1/__init__, which unconditionally
22+
# imports gapic_v1.config, which imports grpc.
23+
try:
24+
import grpc # noqa: F401
25+
except ImportError:
26+
pytest.skip("No GRPC", allow_module_level=True)
27+
2028
from google.auth.exceptions import MutualTLSChannelError
2129

2230
from google.api_core.gapic_v1._routing import (
@@ -144,9 +152,7 @@ def test_get_universe_domain():
144152
)
145153

146154
# fallback to default
147-
assert (
148-
_get_universe_domain(None, None, "default.com") == "default.com"
149-
) # noqa: E501
155+
assert _get_universe_domain(None, None, "default.com") == "default.com" # noqa: E501
150156

151157

152158
def test_get_universe_domain_strip():

0 commit comments

Comments
 (0)