python_plugin_unittest.cc 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc. All rights reserved.
  3. // https://developers.google.com/protocol-buffers/
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  9. // * Redistributions of source code must retain the above copyright
  10. // notice, this list of conditions and the following disclaimer.
  11. // * Redistributions in binary form must reproduce the above
  12. // copyright notice, this list of conditions and the following disclaimer
  13. // in the documentation and/or other materials provided with the
  14. // distribution.
  15. // * Neither the name of Google Inc. nor the names of its
  16. // contributors may be used to endorse or promote products derived from
  17. // this software without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. // Author: kenton@google.com (Kenton Varda)
  31. //
  32. // TODO(kenton): Share code with the versions of this test in other languages?
  33. // It seemed like parameterizing it would add more complexity than it is
  34. // worth.
  35. #include <memory>
  36. #include <google/protobuf/compiler/python/python_generator.h>
  37. #include <google/protobuf/compiler/command_line_interface.h>
  38. #include <google/protobuf/io/zero_copy_stream.h>
  39. #include <google/protobuf/io/printer.h>
  40. #include <google/protobuf/testing/file.h>
  41. #include <google/protobuf/testing/file.h>
  42. #include <google/protobuf/stubs/strutil.h>
  43. #include <google/protobuf/testing/googletest.h>
  44. #include <gtest/gtest.h>
  45. namespace google {
  46. namespace protobuf {
  47. namespace compiler {
  48. namespace python {
  49. namespace {
  50. class TestGenerator : public CodeGenerator {
  51. public:
  52. TestGenerator() {}
  53. ~TestGenerator() {}
  54. virtual bool Generate(const FileDescriptor* file,
  55. const string& parameter,
  56. GeneratorContext* context,
  57. string* error) const {
  58. TryInsert("test_pb2.py", "imports", context);
  59. TryInsert("test_pb2.py", "module_scope", context);
  60. TryInsert("test_pb2.py", "class_scope:foo.Bar", context);
  61. TryInsert("test_pb2.py", "class_scope:foo.Bar.Baz", context);
  62. return true;
  63. }
  64. void TryInsert(const string& filename, const string& insertion_point,
  65. GeneratorContext* context) const {
  66. std::unique_ptr<io::ZeroCopyOutputStream> output(
  67. context->OpenForInsert(filename, insertion_point));
  68. io::Printer printer(output.get(), '$');
  69. printer.Print("// inserted $name$\n", "name", insertion_point);
  70. }
  71. };
  72. // This test verifies that all the expected insertion points exist. It does
  73. // not verify that they are correctly-placed; that would require actually
  74. // compiling the output which is a bit more than I care to do for this test.
  75. TEST(PythonPluginTest, PluginTest) {
  76. GOOGLE_CHECK_OK(File::SetContents(TestTempDir() + "/test.proto",
  77. "syntax = \"proto2\";\n"
  78. "package foo;\n"
  79. "message Bar {\n"
  80. " message Baz {}\n"
  81. "}\n",
  82. true));
  83. google::protobuf::compiler::CommandLineInterface cli;
  84. cli.SetInputsAreProtoPathRelative(true);
  85. python::Generator python_generator;
  86. TestGenerator test_generator;
  87. cli.RegisterGenerator("--python_out", &python_generator, "");
  88. cli.RegisterGenerator("--test_out", &test_generator, "");
  89. string proto_path = "-I" + TestTempDir();
  90. string python_out = "--python_out=" + TestTempDir();
  91. string test_out = "--test_out=" + TestTempDir();
  92. const char* argv[] = {
  93. "protoc",
  94. proto_path.c_str(),
  95. python_out.c_str(),
  96. test_out.c_str(),
  97. "test.proto"
  98. };
  99. EXPECT_EQ(0, cli.Run(5, argv));
  100. }
  101. // This test verifies that the generated Python output uses regular imports (as
  102. // opposed to importlib) in the usual case where the .proto file paths do not
  103. // not contain any Python keywords.
  104. TEST(PythonPluginTest, ImportTest) {
  105. // Create files test1.proto and test2.proto with the former importing the
  106. // latter.
  107. GOOGLE_CHECK_OK(File::SetContents(TestTempDir() + "/test1.proto",
  108. "syntax = \"proto3\";\n"
  109. "package foo;\n"
  110. "import \"test2.proto\";"
  111. "message Message1 {\n"
  112. " Message2 message_2 = 1;\n"
  113. "}\n",
  114. true));
  115. GOOGLE_CHECK_OK(File::SetContents(TestTempDir() + "/test2.proto",
  116. "syntax = \"proto3\";\n"
  117. "package foo;\n"
  118. "message Message2 {}\n",
  119. true));
  120. google::protobuf::compiler::CommandLineInterface cli;
  121. cli.SetInputsAreProtoPathRelative(true);
  122. python::Generator python_generator;
  123. cli.RegisterGenerator("--python_out", &python_generator, "");
  124. string proto_path = "-I" + TestTempDir();
  125. string python_out = "--python_out=" + TestTempDir();
  126. const char* argv[] = {"protoc", proto_path.c_str(), "-I.", python_out.c_str(),
  127. "test1.proto"};
  128. ASSERT_EQ(0, cli.Run(5, argv));
  129. // Loop over the lines of the generated code and verify that we find an
  130. // ordinary Python import but do not find the string "importlib".
  131. string output;
  132. GOOGLE_CHECK_OK(File::GetContents(TestTempDir() + "/test1_pb2.py", &output,
  133. true));
  134. std::vector<string> lines = Split(output, "\n");
  135. string expected_import = "import test2_pb2";
  136. bool found_expected_import = false;
  137. for (int i = 0; i < lines.size(); ++i) {
  138. if (lines[i].find(expected_import) != string::npos) {
  139. found_expected_import = true;
  140. }
  141. EXPECT_EQ(string::npos, lines[i].find("importlib"));
  142. }
  143. EXPECT_TRUE(found_expected_import);
  144. }
  145. } // namespace
  146. } // namespace python
  147. } // namespace compiler
  148. } // namespace protobuf
  149. } // namespace google