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 #include <memory>
34 #include <string>
35
36 #include <google/protobuf/testing/file.h>
37 #include <google/protobuf/testing/file.h>
38 #include <google/protobuf/compiler/command_line_interface.h>
39 #include <google/protobuf/compiler/python/generator.h>
40 #include <google/protobuf/io/printer.h>
41 #include <google/protobuf/io/zero_copy_stream.h>
42 #include <google/protobuf/stubs/strutil.h>
43 #include <google/protobuf/testing/googletest.h>
44 #include <gtest/gtest.h>
45
46 namespace google {
47 namespace protobuf {
48 namespace compiler {
49 namespace python {
50 namespace {
51
52 class TestGenerator : public CodeGenerator {
53 public:
TestGenerator()54 TestGenerator() {}
~TestGenerator()55 ~TestGenerator() override {}
56
Generate(const FileDescriptor * file,const std::string & parameter,GeneratorContext * context,std::string * error) const57 bool Generate(const FileDescriptor* file, const std::string& parameter,
58 GeneratorContext* context, std::string* error) const override {
59 TryInsert("test_pb2.py", "imports", context);
60 TryInsert("test_pb2.py", "module_scope", context);
61 TryInsert("test_pb2.py", "class_scope:foo.Bar", context);
62 TryInsert("test_pb2.py", "class_scope:foo.Bar.Baz", context);
63 return true;
64 }
65
TryInsert(const std::string & filename,const std::string & insertion_point,GeneratorContext * context) const66 void TryInsert(const std::string& filename,
67 const std::string& insertion_point,
68 GeneratorContext* context) const {
69 std::unique_ptr<io::ZeroCopyOutputStream> output(
70 context->OpenForInsert(filename, insertion_point));
71 io::Printer printer(output.get(), '$');
72 printer.Print("// inserted $name$\n", "name", insertion_point);
73 }
74 };
75
76 // opposed to importlib) in the usual case where the .proto file paths do not
77 // not contain any Python keywords.
TEST(PythonPluginTest,ImportTest)78 TEST(PythonPluginTest, ImportTest) {
79 // Create files test1.proto and test2.proto with the former importing the
80 // latter.
81 GOOGLE_CHECK_OK(File::SetContents(TestTempDir() + "/test1.proto",
82 "syntax = \"proto3\";\n"
83 "package foo;\n"
84 "import \"test2.proto\";"
85 "message Message1 {\n"
86 " Message2 message_2 = 1;\n"
87 "}\n",
88 true));
89 GOOGLE_CHECK_OK(File::SetContents(TestTempDir() + "/test2.proto",
90 "syntax = \"proto3\";\n"
91 "package foo;\n"
92 "message Message2 {}\n",
93 true));
94
95 compiler::CommandLineInterface cli;
96 cli.SetInputsAreProtoPathRelative(true);
97 python::Generator python_generator;
98 cli.RegisterGenerator("--python_out", &python_generator, "");
99 std::string proto_path = "-I" + TestTempDir();
100 std::string python_out = "--python_out=" + TestTempDir();
101 const char* argv[] = {"protoc", proto_path.c_str(), "-I.", python_out.c_str(),
102 "test1.proto"};
103 ASSERT_EQ(0, cli.Run(5, argv));
104
105 // Loop over the lines of the generated code and verify that we find an
106 // ordinary Python import but do not find the string "importlib".
107 std::string output;
108 GOOGLE_CHECK_OK(File::GetContents(TestTempDir() + "/test1_pb2.py", &output,
109 true));
110 std::vector<std::string> lines = Split(output, "\n");
111 std::string expected_import = "import test2_pb2";
112 bool found_expected_import = false;
113 for (int i = 0; i < lines.size(); ++i) {
114 if (lines[i].find(expected_import) != std::string::npos) {
115 found_expected_import = true;
116 }
117 EXPECT_EQ(std::string::npos, lines[i].find("importlib"));
118 }
119 EXPECT_TRUE(found_expected_import);
120 }
121
122 } // namespace
123 } // namespace python
124 } // namespace compiler
125 } // namespace protobuf
126 } // namespace google
127