diff --git a/tensorboard/defs/BUILD b/tensorboard/defs/BUILD index f219f54c9c..368b2908ef 100644 --- a/tensorboard/defs/BUILD +++ b/tensorboard/defs/BUILD @@ -1,5 +1,7 @@ package(default_visibility = ["//tensorboard:internal"]) +load("//tensorboard/defs:protos.bzl", "tb_proto_library") + licenses(["notice"]) # Apache 2.0 filegroup( @@ -13,4 +15,33 @@ filegroup( visibility = ["//visibility:public"], ) +py_test( + name = "tb_proto_library_test", + srcs = ["tb_proto_library_test.py"], + deps = [ + ":test_base_py_pb2", + ":test_base_py_pb2_grpc", + ":test_downstream_py_pb2", + ":test_downstream_py_pb2_grpc", + "//tensorboard:test", + ], +) + +tb_proto_library( + name = "test_base", + srcs = ["test_base.proto"], + has_services = True, + testonly = True, +) + +tb_proto_library( + name = "test_downstream", + srcs = ["test_downstream.proto"], + deps = [ + ":test_base", + ], + has_services = True, + testonly = True, +) + exports_files(["web_test_python_stub.template.py"]) diff --git a/tensorboard/defs/protos.bzl b/tensorboard/defs/protos.bzl index 17a054635e..3c18bc1000 100644 --- a/tensorboard/defs/protos.bzl +++ b/tensorboard/defs/protos.bzl @@ -17,6 +17,7 @@ load("@com_google_protobuf//:protobuf.bzl", "proto_gen") def tb_proto_library( name, srcs = None, + deps = [], visibility = None, testonly = None, has_services = False): @@ -24,10 +25,12 @@ def tb_proto_library( outs_grpc = _PyOuts(srcs, grpc = True) if has_services else [] outs_all = outs_proto + outs_grpc + runtime = "@com_google_protobuf//:protobuf_python" + proto_gen( - name = name + "_py_pb2_genproto", + name = name + "_genproto", srcs = srcs, - deps = ["@com_google_protobuf//:protobuf_python_genproto"], + deps = [s + "_genproto" for s in deps] + [runtime + "_genproto"], includes = [], protoc = "@com_google_protobuf//:protoc", gen_py = True, @@ -37,12 +40,13 @@ def tb_proto_library( plugin_language = "grpc", ) + py_deps = [s + "_py_pb2" for s in deps] + [runtime] native.py_library( name = name + "_py_pb2", srcs = outs_proto, imports = [], srcs_version = "PY2AND3", - deps = ["@com_google_protobuf//:protobuf_python"], + deps = py_deps, testonly = testonly, visibility = visibility, ) @@ -52,10 +56,7 @@ def tb_proto_library( srcs = outs_grpc, imports = [], srcs_version = "PY2AND3", - deps = [ - name + "_py_pb2", - "@com_google_protobuf//:protobuf_python", - ], + deps = [name + "_py_pb2"] + py_deps, testonly = testonly, visibility = visibility, ) diff --git a/tensorboard/defs/tb_proto_library_test.py b/tensorboard/defs/tb_proto_library_test.py new file mode 100644 index 0000000000..425e1d06f9 --- /dev/null +++ b/tensorboard/defs/tb_proto_library_test.py @@ -0,0 +1,44 @@ +# Copyright 2019 The TensorFlow Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ============================================================================== +"""Tests for the `tb_proto_library` build macro.""" + +from __future__ import absolute_import +from __future__ import division +from __future__ import print_function + +from tensorboard import test as tb_test +from tensorboard.defs import test_base_pb2 +from tensorboard.defs import test_base_pb2_grpc +from tensorboard.defs import test_downstream_pb2 +from tensorboard.defs import test_downstream_pb2_grpc + + +class TbProtoLibraryTest(tb_test.TestCase): + """Tests for `tb_proto_library`.""" + + def tests_with_deps(self): + foo = test_base_pb2.Foo() + foo.foo = 1 + bar = test_downstream_pb2.Bar() + bar.foo.foo = 1 + self.assertEqual(foo, bar.foo) + + def test_service_deps(self): + self.assertIsInstance(test_base_pb2_grpc.FooServiceServicer, type) + self.assertIsInstance(test_downstream_pb2_grpc.BarServiceServicer, type) + + +if __name__ == "__main__": + tb_test.main() diff --git a/tensorboard/defs/test_base.proto b/tensorboard/defs/test_base.proto new file mode 100644 index 0000000000..6795667916 --- /dev/null +++ b/tensorboard/defs/test_base.proto @@ -0,0 +1,33 @@ +/* Copyright 2019 The TensorFlow Authors. All Rights Reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +==============================================================================*/ + +syntax = "proto3"; + +message Foo { + int32 foo = 1; +} + +service FooService { + // Loads some objects. + rpc GetFoo(GetFooRequest) returns (GetFooResponse); +} + +message GetFooRequest { + int32 count = 1; +} + +message GetFooResponse { + repeated Foo foo = 1; +} diff --git a/tensorboard/defs/test_downstream.proto b/tensorboard/defs/test_downstream.proto new file mode 100644 index 0000000000..0ff23a0b82 --- /dev/null +++ b/tensorboard/defs/test_downstream.proto @@ -0,0 +1,36 @@ +/* Copyright 2019 The TensorFlow Authors. All Rights Reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +==============================================================================*/ + +syntax = "proto3"; + +import "tensorboard/defs/test_base.proto"; + +message Bar { + Foo foo = 1; + int32 bar = 2; +} + +service BarService { + // Loads some objects. + rpc GetBar(GetBarRequest) returns (GetBarResponse); +} + +message GetBarRequest { + int32 count = 1; +} + +message GetBarResponse { + repeated Bar bar = 1; +}