1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved.
3 //
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file or at
6 // https://developers.google.com/open-source/licenses/bsd
7
8 // Author: kenton@google.com (Kenton Varda)
9
10 #include <memory>
11 #include <string>
12 #include <vector>
13
14 #include "google/protobuf/testing/file.h"
15 #include "google/protobuf/testing/file.h"
16 #include "google/protobuf/compiler/command_line_interface.h"
17 #include "google/protobuf/compiler/python/generator.h"
18 #include <gtest/gtest.h>
19 #include "absl/log/absl_check.h"
20 #include "absl/strings/str_split.h"
21 #include "google/protobuf/io/printer.h"
22 #include "google/protobuf/io/zero_copy_stream.h"
23
24 namespace google {
25 namespace protobuf {
26 namespace compiler {
27 namespace python {
28 namespace {
29
30 class TestGenerator : public CodeGenerator {
31 public:
TestGenerator()32 TestGenerator() {}
~TestGenerator()33 ~TestGenerator() override {}
34
Generate(const FileDescriptor * file,const std::string & parameter,GeneratorContext * context,std::string * error) const35 bool Generate(const FileDescriptor* file, const std::string& parameter,
36 GeneratorContext* context, std::string* error) const override {
37 TryInsert("test_pb2.py", "imports", context);
38 TryInsert("test_pb2.py", "module_scope", context);
39 TryInsert("test_pb2.py", "class_scope:foo.Bar", context);
40 TryInsert("test_pb2.py", "class_scope:foo.Bar.Baz", context);
41 return true;
42 }
43
TryInsert(const std::string & filename,const std::string & insertion_point,GeneratorContext * context) const44 void TryInsert(const std::string& filename,
45 const std::string& insertion_point,
46 GeneratorContext* context) const {
47 std::unique_ptr<io::ZeroCopyOutputStream> output(
48 context->OpenForInsert(filename, insertion_point));
49 io::Printer printer(output.get(), '$');
50 printer.Print("// inserted $name$\n", "name", insertion_point);
51 }
52 };
53
54 // opposed to importlib) in the usual case where the .proto file paths do not
55 // not contain any Python keywords.
TEST(PythonPluginTest,ImportTest)56 TEST(PythonPluginTest, ImportTest) {
57 // Create files test1.proto and test2.proto with the former importing the
58 // latter.
59 ABSL_CHECK_OK(
60 File::SetContents(absl::StrCat(::testing::TempDir(), "/test1.proto"),
61 "syntax = \"proto3\";\n"
62 "package foo;\n"
63 "import \"test2.proto\";"
64 "message Message1 {\n"
65 " Message2 message_2 = 1;\n"
66 "}\n",
67 true));
68 ABSL_CHECK_OK(
69 File::SetContents(absl::StrCat(::testing::TempDir(), "/test2.proto"),
70 "syntax = \"proto3\";\n"
71 "package foo;\n"
72 "message Message2 {}\n",
73 true));
74
75 compiler::CommandLineInterface cli;
76 cli.SetInputsAreProtoPathRelative(true);
77 python::Generator python_generator;
78 cli.RegisterGenerator("--python_out", &python_generator, "");
79 std::string proto_path = absl::StrCat("-I", ::testing::TempDir());
80 std::string python_out = absl::StrCat("--python_out=", ::testing::TempDir());
81 const char* argv[] = {"protoc", proto_path.c_str(), "-I.", python_out.c_str(),
82 "test1.proto"};
83 ASSERT_EQ(0, cli.Run(5, argv));
84
85 // Loop over the lines of the generated code and verify that we find an
86 // ordinary Python import but do not find the string "importlib".
87 std::string output;
88 ABSL_CHECK_OK(
89 File::GetContents(absl::StrCat(::testing::TempDir(), "/test1_pb2.py"),
90 &output, true));
91 std::vector<absl::string_view> lines = absl::StrSplit(output, '\n');
92 std::string expected_import = "import test2_pb2";
93 bool found_expected_import = false;
94 for (absl::string_view line : lines) {
95 if (absl::StrContains(line, expected_import)) {
96 found_expected_import = true;
97 }
98 EXPECT_FALSE(absl::StrContains(line, "importlib"));
99 }
100 EXPECT_TRUE(found_expected_import);
101 }
102
103 } // namespace
104 } // namespace python
105 } // namespace compiler
106 } // namespace protobuf
107 } // namespace google
108