Fix bazel grpc_tools test

pull/21458/head
Richard Belleville 5 years ago
parent 568dd00774
commit 60f88e20a4
  1. 27
      tools/distrib/python/grpcio_tools/grpc_tools/protoc.py
  2. 4
      tools/distrib/python/grpcio_tools/grpc_tools/test/BUILD.bazel
  3. 6
      tools/distrib/python/grpcio_tools/grpc_tools/test/complicated.proto
  4. 43
      tools/distrib/python/grpcio_tools/grpc_tools/test/protoc_test.py
  5. 6
      tools/distrib/python/grpcio_tools/grpc_tools/test/simple.proto
  6. 6
      tools/distrib/python/grpcio_tools/grpc_tools/test/simpler.proto
  7. 2
      tools/distrib/python/grpcio_tools/grpc_tools/test/simplest.proto

@ -24,6 +24,8 @@ from grpc_tools import _protoc_compiler
_PROTO_MODULE_SUFFIX = "_pb2" _PROTO_MODULE_SUFFIX = "_pb2"
_SERVICE_MODULE_SUFFIX = "_pb2_grpc" _SERVICE_MODULE_SUFFIX = "_pb2_grpc"
_DISABLE_DYNAMIC_STUBS = "GRPC_PYTHON_DISABLE_DYNAMIC_STUBS"
def main(command_arguments): def main(command_arguments):
"""Run the protocol buffer compiler with the given command-line arguments. """Run the protocol buffer compiler with the given command-line arguments.
@ -42,6 +44,21 @@ if sys.version_info[0] > 2:
import importlib.machinery import importlib.machinery
import threading import threading
_FINDERS_INSTALLED = False
_FINDERS_INSTALLED_LOCK = threading.Lock()
def _maybe_install_proto_finders():
global _FINDERS_INSTALLED
with _FINDERS_INSTALLED_LOCK:
if not _FINDERS_INSTALLED:
sys.meta_path.extend([
ProtoFinder(_PROTO_MODULE_SUFFIX,
_protoc_compiler.get_protos),
ProtoFinder(_SERVICE_MODULE_SUFFIX,
_protoc_compiler.get_services)
])
_FINDERS_INSTALLED = True
def _module_name_to_proto_file(suffix, module_name): def _module_name_to_proto_file(suffix, module_name):
components = module_name.split(".") components = module_name.split(".")
proto_name = components[-1][:-1 * len(suffix)] proto_name = components[-1][:-1 * len(suffix)]
@ -54,6 +71,7 @@ if sys.version_info[0] > 2:
def _protos(protobuf_path): def _protos(protobuf_path):
"""Returns a gRPC module generated from the indicated proto file.""" """Returns a gRPC module generated from the indicated proto file."""
_maybe_install_proto_finders()
module_name = _proto_file_to_module_name(_PROTO_MODULE_SUFFIX, module_name = _proto_file_to_module_name(_PROTO_MODULE_SUFFIX,
protobuf_path) protobuf_path)
module = importlib.import_module(module_name) module = importlib.import_module(module_name)
@ -61,6 +79,7 @@ if sys.version_info[0] > 2:
def _services(protobuf_path): def _services(protobuf_path):
"""Returns a module generated from the indicated proto file.""" """Returns a module generated from the indicated proto file."""
_maybe_install_proto_finders()
_protos(protobuf_path) _protos(protobuf_path)
module_name = _proto_file_to_module_name(_SERVICE_MODULE_SUFFIX, module_name = _proto_file_to_module_name(_SERVICE_MODULE_SUFFIX,
protobuf_path) protobuf_path)
@ -137,10 +156,10 @@ if sys.version_info[0] > 2:
ProtoLoader(self._suffix, self._codegen_fn, fullname, ProtoLoader(self._suffix, self._codegen_fn, fullname,
filepath, search_path)) filepath, search_path))
sys.meta_path.extend([ # NOTE(rbellevi): We provide an environment variable that enables users to completely
ProtoFinder(_PROTO_MODULE_SUFFIX, _protoc_compiler.get_protos), # disable this behavior if it is not desired, e.g. for performance reasons.
ProtoFinder(_SERVICE_MODULE_SUFFIX, _protoc_compiler.get_services) if not os.getenv(_DISABLE_DYNAMIC_STUBS):
]) _maybe_install_proto_finders()
if __name__ == '__main__': if __name__ == '__main__':
proto_include = pkg_resources.resource_filename('grpc_tools', '_proto') proto_include = pkg_resources.resource_filename('grpc_tools', '_proto')

@ -6,14 +6,14 @@ proto_library(
name = "simplest_proto", name = "simplest_proto",
testonly = True, testonly = True,
srcs = ["simplest.proto"], srcs = ["simplest.proto"],
strip_import_prefix = "/tools/distrib/python/grpcio_tools/", strip_import_prefix = "/tools/distrib/python/grpcio_tools/grpc_tools/test/",
) )
proto_library( proto_library(
name = "complicated_proto", name = "complicated_proto",
testonly = True, testonly = True,
srcs = ["complicated.proto"], srcs = ["complicated.proto"],
strip_import_prefix = "/tools/distrib/python/grpcio_tools/", strip_import_prefix = "/tools/distrib/python/grpcio_tools/grpc_tools/test/",
deps = [":simplest_proto"], deps = [":simplest_proto"],
) )

@ -1,12 +1,12 @@
syntax = "proto3"; syntax = "proto3";
package grpc_tools.test.complicated; package test.complicated;
import "grpc_tools/test/simplest.proto"; import "simplest.proto";
message ComplicatedMessage { message ComplicatedMessage {
bool yes = 1; bool yes = 1;
bool no = 2; bool no = 2;
bool why = 3; bool why = 3;
grpc_tools.test.simplest.SimplestMessage simplest_message = 4; simplest.SimplestMessage simplest_message = 4;
}; };

@ -40,7 +40,7 @@ def _run_in_subprocess(test_case):
def _augmented_syspath(new_paths): def _augmented_syspath(new_paths):
original_sys_path = sys.path original_sys_path = sys.path
if new_paths is not None: if new_paths is not None:
sys.path = sys.path + list(new_paths) sys.path = list(new_paths) + sys.path
try: try:
yield yield
finally: finally:
@ -49,31 +49,36 @@ def _augmented_syspath(new_paths):
def _test_import_protos(): def _test_import_protos():
from grpc_tools import protoc from grpc_tools import protoc
with _augmented_syspath(("tools/distrib/python/grpcio_tools/",)): with _augmented_syspath(
protos = protoc._protos("grpc_tools/test/simple.proto") ("tools/distrib/python/grpcio_tools/grpc_tools/test/",)):
protos = protoc._protos("simple.proto")
assert protos.SimpleMessage is not None assert protos.SimpleMessage is not None
def _test_import_services(): def _test_import_services():
from grpc_tools import protoc from grpc_tools import protoc
with _augmented_syspath(("tools/distrib/python/grpcio_tools/",)): with _augmented_syspath(
protos = protoc._protos("grpc_tools/test/simple.proto") ("tools/distrib/python/grpcio_tools/grpc_tools/test/",)):
services = protoc._services("grpc_tools/test/simple.proto") protos = protoc._protos("simple.proto")
services = protoc._services("simple.proto")
assert services.SimpleMessageServiceStub is not None assert services.SimpleMessageServiceStub is not None
def _test_import_services_without_protos(): def _test_import_services_without_protos():
from grpc_tools import protoc from grpc_tools import protoc
services = protoc._services("grpc_tools/test/simple.proto") with _augmented_syspath(
assert services.SimpleMessageServiceStub is not None ("tools/distrib/python/grpcio_tools/grpc_tools/test/",)):
services = protoc._services("simple.proto")
assert services.SimpleMessageServiceStub is not None
def _test_proto_module_imported_once(): def _test_proto_module_imported_once():
from grpc_tools import protoc from grpc_tools import protoc
with _augmented_syspath(("tools/distrib/python/grpcio_tools/",)): with _augmented_syspath(
protos = protoc._protos("grpc_tools/test/simple.proto") ("tools/distrib/python/grpcio_tools/grpc_tools/test/",)):
services = protoc._services("grpc_tools/test/simple.proto") protos = protoc._protos("simple.proto")
complicated_protos = protoc._protos("grpc_tools/test/complicated.proto") services = protoc._services("simple.proto")
complicated_protos = protoc._protos("complicated.proto")
simple_message = protos.SimpleMessage() simple_message = protos.SimpleMessage()
complicated_message = complicated_protos.ComplicatedMessage() complicated_message = complicated_protos.ComplicatedMessage()
assert (simple_message.simpler_message.simplest_message.__class__ is assert (simple_message.simpler_message.simplest_message.__class__ is
@ -81,10 +86,11 @@ def _test_proto_module_imported_once():
def _test_static_dynamic_combo(): def _test_static_dynamic_combo():
from grpc_tools.test import complicated_pb2 with _augmented_syspath(
from grpc_tools import protoc ("tools/distrib/python/grpcio_tools/grpc_tools/test/",)):
with _augmented_syspath(("tools/distrib/python/grpcio_tools/",)): from grpc_tools import protoc
protos = protoc._protos("grpc_tools/test/simple.proto") import complicated_pb2
protos = protoc._protos("simple.proto")
static_message = complicated_pb2.ComplicatedMessage() static_message = complicated_pb2.ComplicatedMessage()
dynamic_message = protos.SimpleMessage() dynamic_message = protos.SimpleMessage()
assert (dynamic_message.simpler_message.simplest_message.__class__ is assert (dynamic_message.simpler_message.simplest_message.__class__ is
@ -93,8 +99,7 @@ def _test_static_dynamic_combo():
def _test_combined_import(): def _test_combined_import():
from grpc_tools import protoc from grpc_tools import protoc
protos, services = protoc._protos_and_services( protos, services = protoc._protos_and_services("simple.proto")
"grpc_tools/test/simple.proto")
assert protos.SimpleMessage is not None assert protos.SimpleMessage is not None
assert services.SimpleMessageServiceStub is not None assert services.SimpleMessageServiceStub is not None
@ -102,7 +107,7 @@ def _test_combined_import():
def _test_syntax_errors(): def _test_syntax_errors():
from grpc_tools import protoc from grpc_tools import protoc
try: try:
protos = protoc._protos("grpc_tools/test/flawed.proto") protos = protoc._protos("flawed.proto")
except Exception as e: except Exception as e:
error_str = str(e) error_str = str(e)
assert "flawed.proto" in error_str assert "flawed.proto" in error_str

@ -1,8 +1,8 @@
syntax = "proto3"; syntax = "proto3";
package grpc_tools.test.simple; package simple;
import "grpc_tools/test/simpler.proto"; import "simpler.proto";
message SimpleMessage { message SimpleMessage {
string msg = 1; string msg = 1;
@ -10,7 +10,7 @@ message SimpleMessage {
bool personal = 2; bool personal = 2;
bool business = 3; bool business = 3;
}; };
grpc_tools.test.simpler.SimplerMessage simpler_message = 4; simpler.SimplerMessage simpler_message = 4;
}; };
message SimpleMessageRequest { message SimpleMessageRequest {

@ -1,11 +1,11 @@
syntax = "proto3"; syntax = "proto3";
package grpc_tools.test.simpler; package simpler;
import "grpc_tools/test/simplest.proto"; import "simplest.proto";
message SimplerMessage { message SimplerMessage {
int64 do_i_even_exist = 1; int64 do_i_even_exist = 1;
grpc_tools.test.simplest.SimplestMessage simplest_message = 2; simplest.SimplestMessage simplest_message = 2;
}; };

@ -1,6 +1,6 @@
syntax = "proto3"; syntax = "proto3";
package grpc_tools.test.simplest; package simplest;
message SimplestMessage { message SimplestMessage {
int64 i_definitely_dont_exist = 1; int64 i_definitely_dont_exist = 1;

Loading…
Cancel
Save