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