• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
31 // Author: kenton@google.com (Kenton Varda)
32 //
33 // TODO(kenton):  Share code with the versions of this test in other languages?
34 //   It seemed like parameterizing it would add more complexity than it is
35 //   worth.
36 
37 #include <memory>
38 
39 #include <google/protobuf/compiler/command_line_interface.h>
40 #include <google/protobuf/compiler/python/python_generator.h>
41 #include <google/protobuf/io/printer.h>
42 #include <google/protobuf/io/zero_copy_stream.h>
43 
44 #include <google/protobuf/testing/file.h>
45 #include <google/protobuf/testing/file.h>
46 #include <google/protobuf/stubs/strutil.h>
47 #include <google/protobuf/testing/googletest.h>
48 #include <gtest/gtest.h>
49 
50 namespace google {
51 namespace protobuf {
52 namespace compiler {
53 namespace python {
54 namespace {
55 
56 class TestGenerator : public CodeGenerator {
57  public:
TestGenerator()58   TestGenerator() {}
~TestGenerator()59   ~TestGenerator() {}
60 
Generate(const FileDescriptor * file,const std::string & parameter,GeneratorContext * context,std::string * error) const61   virtual bool Generate(const FileDescriptor* file,
62                         const std::string& parameter, GeneratorContext* context,
63                         std::string* error) const {
64     TryInsert("test_pb2.py", "imports", context);
65     TryInsert("test_pb2.py", "module_scope", context);
66     TryInsert("test_pb2.py", "class_scope:foo.Bar", context);
67     TryInsert("test_pb2.py", "class_scope:foo.Bar.Baz", context);
68     return true;
69   }
70 
TryInsert(const std::string & filename,const std::string & insertion_point,GeneratorContext * context) const71   void TryInsert(const std::string& filename,
72                  const std::string& insertion_point,
73                  GeneratorContext* context) const {
74     std::unique_ptr<io::ZeroCopyOutputStream> output(
75         context->OpenForInsert(filename, insertion_point));
76     io::Printer printer(output.get(), '$');
77     printer.Print("// inserted $name$\n", "name", insertion_point);
78   }
79 };
80 
81 // This test verifies that all the expected insertion points exist.  It does
82 // not verify that they are correctly-placed; that would require actually
83 // compiling the output which is a bit more than I care to do for this test.
TEST(PythonPluginTest,PluginTest)84 TEST(PythonPluginTest, PluginTest) {
85   GOOGLE_CHECK_OK(File::SetContents(TestTempDir() + "/test.proto",
86                              "syntax = \"proto2\";\n"
87                              "package foo;\n"
88                              "message Bar {\n"
89                              "  message Baz {}\n"
90                              "}\n",
91                              true));
92 
93   compiler::CommandLineInterface cli;
94   cli.SetInputsAreProtoPathRelative(true);
95 
96   python::Generator python_generator;
97   TestGenerator test_generator;
98   cli.RegisterGenerator("--python_out", &python_generator, "");
99   cli.RegisterGenerator("--test_out", &test_generator, "");
100 
101   std::string proto_path = "-I" + TestTempDir();
102   std::string python_out = "--python_out=" + TestTempDir();
103   std::string test_out = "--test_out=" + TestTempDir();
104 
105   const char* argv[] = {"protoc", proto_path.c_str(), python_out.c_str(),
106                         test_out.c_str(), "test.proto"};
107 
108   EXPECT_EQ(0, cli.Run(5, argv));
109 }
110 
111 // This test verifies that the generated Python output uses regular imports (as
112 // opposed to importlib) in the usual case where the .proto file paths do not
113 // not contain any Python keywords.
TEST(PythonPluginTest,ImportTest)114 TEST(PythonPluginTest, ImportTest) {
115   // Create files test1.proto and test2.proto with the former importing the
116   // latter.
117   GOOGLE_CHECK_OK(File::SetContents(TestTempDir() + "/test1.proto",
118                              "syntax = \"proto3\";\n"
119                              "package foo;\n"
120                              "import \"test2.proto\";"
121                              "message Message1 {\n"
122                              "  Message2 message_2 = 1;\n"
123                              "}\n",
124                              true));
125   GOOGLE_CHECK_OK(File::SetContents(TestTempDir() + "/test2.proto",
126                              "syntax = \"proto3\";\n"
127                              "package foo;\n"
128                              "message Message2 {}\n",
129                              true));
130 
131   compiler::CommandLineInterface cli;
132   cli.SetInputsAreProtoPathRelative(true);
133   python::Generator python_generator;
134   cli.RegisterGenerator("--python_out", &python_generator, "");
135   std::string proto_path = "-I" + TestTempDir();
136   std::string python_out = "--python_out=" + TestTempDir();
137   const char* argv[] = {"protoc", proto_path.c_str(), "-I.", python_out.c_str(),
138                         "test1.proto"};
139   ASSERT_EQ(0, cli.Run(5, argv));
140 
141   // Loop over the lines of the generated code and verify that we find an
142   // ordinary Python import but do not find the string "importlib".
143   std::string output;
144   GOOGLE_CHECK_OK(File::GetContents(TestTempDir() + "/test1_pb2.py", &output,
145                              true));
146   std::vector<std::string> lines = Split(output, "\n");
147   std::string expected_import = "import test2_pb2";
148   bool found_expected_import = false;
149   for (int i = 0; i < lines.size(); ++i) {
150     if (lines[i].find(expected_import) != std::string::npos) {
151       found_expected_import = true;
152     }
153     EXPECT_EQ(std::string::npos, lines[i].find("importlib"));
154   }
155   EXPECT_TRUE(found_expected_import);
156 }
157 
158 }  // namespace
159 }  // namespace python
160 }  // namespace compiler
161 }  // namespace protobuf
162 }  // namespace google
163