From a80a3b72bbf13ac273a4e8b3cbcc3a85003f08e7 Mon Sep 17 00:00:00 2001 From: Richard Belleville Date: Tue, 22 Sep 2020 12:35:52 -0700 Subject: [PATCH] Do not import grpc_tools.protoc from grpc_tools --- .../protoc_plugin/_python_plugin_test.py | 28 +++++++++++++++++++ src/python/grpcio_tests/tests/tests.json | 1 + .../grpcio_tools/grpc_tools/__init__.py | 2 -- 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/src/python/grpcio_tests/tests/protoc_plugin/_python_plugin_test.py b/src/python/grpcio_tests/tests/protoc_plugin/_python_plugin_test.py index 96dc104a7e2..f3f79dc45ae 100644 --- a/src/python/grpcio_tests/tests/protoc_plugin/_python_plugin_test.py +++ b/src/python/grpcio_tests/tests/protoc_plugin/_python_plugin_test.py @@ -628,5 +628,33 @@ class SimpleStubsPluginTest(unittest.TestCase): self.assertEqual(expected_response, response) +class ModuleMainTest(unittest.TestCase): + """Test case for running `python -m grpc_tools.protoc`. + """ + + def test_clean_output(self): + if sys.executable is None: + raise unittest.SkipTest( + "Running on a interpreter that cannot be invoked from the CLI.") + proto_dir_path = os.path.join("src", "proto") + test_proto_path = os.path.join(proto_dir_path, "grpc", "testing", + "empty.proto") + streams = tuple(tempfile.TemporaryFile() for _ in range(2)) + with tempfile.TemporaryDirectory() as work_dir: + invocation = (sys.executable, "-m", "grpc_tools.protoc", + "--proto_path", proto_dir_path, "--python_out", + work_dir, "--grpc_python_out", work_dir, + test_proto_path) + proc = subprocess.Popen(invocation, + stdout=streams[0], + stderr=streams[1]) + proc.wait() + outs = [] + for stream in streams: + stream.seek(0) + self.assertEqual(0, len(stream.read())) + self.assertEqual(0, proc.returncode) + + if __name__ == '__main__': unittest.main(verbosity=2) diff --git a/src/python/grpcio_tests/tests/tests.json b/src/python/grpcio_tests/tests/tests.json index 3bd10bb00a1..4c174283f93 100644 --- a/src/python/grpcio_tests/tests/tests.json +++ b/src/python/grpcio_tests/tests/tests.json @@ -8,6 +8,7 @@ "interop._secure_intraop_test.SecureIntraopTest", "protoc_plugin._python_plugin_test.PythonPluginTest", "protoc_plugin._python_plugin_test.SimpleStubsPluginTest", + "protoc_plugin._python_plugin_test.ModuleMainTest", "protoc_plugin._split_definitions_test.SameProtoGrpcBeforeProtoProtocStyleTest", "protoc_plugin._split_definitions_test.SameProtoMid2016ProtocStyleTest", "protoc_plugin._split_definitions_test.SameProtoProtoBeforeGrpcProtocStyleTest", diff --git a/tools/distrib/python/grpcio_tools/grpc_tools/__init__.py b/tools/distrib/python/grpcio_tools/grpc_tools/__init__.py index 5a00f27976b..5772620b602 100644 --- a/tools/distrib/python/grpcio_tools/grpc_tools/__init__.py +++ b/tools/distrib/python/grpcio_tools/grpc_tools/__init__.py @@ -11,5 +11,3 @@ # 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. - -from .protoc import main