Send full feature sets to plugins.

There are 4 different feature sets for every descriptor:
* Runtime/resolved - Used to make runtime decisions
* Source/resolved - Used to make codegen decisions
* Runtime/unresolved - Used to reason about the original proto file during runtime
* Source/unresolved - Used to validate features during codegen

Plugins will receive all 4 of these.  Unresolved features will show up in the `raw_features` fields nested inside `features`.  The runtime features will be in the `proto_file` field of the request, while the source features will be in the unstripped `source_file_descriptors` field.

PiperOrigin-RevId: 547925275
pull/13316/head
Mike Kruskal 1 year ago committed by Mike Kruskal
parent c5a1dbeb1e
commit f1de28a435
  1. 1
      src/google/protobuf/compiler/BUILD.bazel
  2. 41
      src/google/protobuf/compiler/command_line_interface.cc
  3. 22
      src/google/protobuf/compiler/command_line_interface.h
  4. 221
      src/google/protobuf/compiler/command_line_interface_unittest.cc
  5. 6
      src/google/protobuf/compiler/cpp/bootstrap_unittest.cc
  6. 10
      src/google/protobuf/compiler/cpp/generator_unittest.cc
  7. 4
      src/google/protobuf/compiler/cpp/unittest.cc
  8. 6
      src/google/protobuf/compiler/cpp/unittest.inc
  9. 2
      src/google/protobuf/compiler/java/message_serialization_unittest.cc
  10. 5
      src/google/protobuf/compiler/parser_unittest.cc
  11. 1
      src/google/protobuf/descriptor.h
  12. 4
      src/google/protobuf/extension_set_unittest.cc
  13. 2
      src/google/protobuf/io/zero_copy_stream_unittest.cc
  14. 2
      src/google/protobuf/json/json_test.cc
  15. 8
      src/google/protobuf/map_test.inc
  16. 4
      src/google/protobuf/message_unittest.inc
  17. 24
      src/google/protobuf/test_util2.h
  18. 4
      src/google/protobuf/text_format_unittest.cc

@ -295,6 +295,7 @@ cc_test(
":mock_code_generator",
"//:protobuf",
"//src/google/protobuf:cc_test_protos",
"//src/google/protobuf:test_textproto",
"//src/google/protobuf:test_util2",
"//src/google/protobuf/compiler/cpp:names",
"//src/google/protobuf/io",

@ -302,27 +302,13 @@ std::string PluginName(absl::string_view plugin_prefix,
}
// Get all transitive dependencies of the given file (including the file
// itself), adding them to the given list of FileDescriptorProtos. The
// protos will be ordered such that every file is listed before any file that
// depends on it, so that you can call DescriptorPool::BuildFile() on them
// in order. Any files in *already_seen will not be added, and each file
// added will be inserted into *already_seen. If include_source_code_info is
// true then include the source code information in the FileDescriptorProtos.
// If include_json_name is true, populate the json_name field of
// FieldDescriptorProto for all fields.
struct TransitiveDependencyOptions {
bool include_json_name = false;
bool include_source_code_info = false;
bool retain_options = false;
};
} // namespace
void GetTransitiveDependencies(
void CommandLineInterface::GetTransitiveDependencies(
const FileDescriptor* file,
absl::flat_hash_set<const FileDescriptor*>* already_seen,
RepeatedPtrField<FileDescriptorProto>* output,
const TransitiveDependencyOptions& options =
TransitiveDependencyOptions()) {
const TransitiveDependencyOptions& options) {
if (!already_seen->insert(file).second) {
// Already saw this file. Skip.
return;
@ -336,22 +322,19 @@ void GetTransitiveDependencies(
// Add this file.
FileDescriptorProto* new_descriptor = output->Add();
if (options.retain_options) {
file->CopyTo(new_descriptor);
if (options.include_source_code_info) {
file->CopySourceCodeInfoTo(new_descriptor);
}
} else {
*new_descriptor =
StripSourceRetentionOptions(*file, options.include_source_code_info);
*new_descriptor =
google::protobuf::internal::InternalFeatureHelper::GetGeneratorProto(*file);
if (options.include_source_code_info) {
file->CopySourceCodeInfoTo(new_descriptor);
}
if (!options.retain_options) {
StripSourceRetentionOptions(*file->pool(), *new_descriptor);
}
if (options.include_json_name) {
file->CopyJsonNameTo(new_descriptor);
}
}
} // namespace
// A MultiFileErrorCollector that prints errors to stderr.
class CommandLineInterface::ErrorPrinter
: public MultiFileErrorCollector,
@ -2629,9 +2612,11 @@ bool CommandLineInterface::GeneratePluginOutput(
if (files_to_generate.contains(file_proto.name())) {
const FileDescriptor* file = pool->FindFileByName(file_proto.name());
*request.add_source_file_descriptors() = std::move(file_proto);
file_proto = StripSourceRetentionOptions(*file);
file_proto =
google::protobuf::internal::InternalFeatureHelper::GetGeneratorProto(*file);
file->CopySourceCodeInfoTo(&file_proto);
file->CopyJsonNameTo(&file_proto);
StripSourceRetentionOptions(*file->pool(), file_proto);
}
}

@ -73,6 +73,12 @@ class CodeGenerator; // code_generator.h
class GeneratorContext; // code_generator.h
class DiskSourceTree; // importer.h
struct TransitiveDependencyOptions {
bool include_json_name = false;
bool include_source_code_info = false;
bool retain_options = false;
};
// This class implements the command-line interface to the protocol compiler.
// It is designed to make it very easy to create a custom protocol compiler
// supporting the languages of your choice. For example, if you wanted to
@ -325,6 +331,22 @@ class PROTOC_EXPORT CommandLineInterface {
// listed as free numbers in the output.
void PrintFreeFieldNumbers(const Descriptor* descriptor);
// Get all transitive dependencies of the given file (including the file
// itself), adding them to the given list of FileDescriptorProtos. The
// protos will be ordered such that every file is listed before any file that
// depends on it, so that you can call DescriptorPool::BuildFile() on them
// in order. Any files in *already_seen will not be added, and each file
// added will be inserted into *already_seen. If include_source_code_info
// (from TransitiveDependencyOptions) is true then include the source code
// information in the FileDescriptorProtos. If include_json_name is true,
// populate the json_name field of FieldDescriptorProto for all fields.
void GetTransitiveDependencies(
const FileDescriptor* file,
absl::flat_hash_set<const FileDescriptor*>* already_seen,
RepeatedPtrField<FileDescriptorProto>* output,
const TransitiveDependencyOptions& options =
TransitiveDependencyOptions());
// -----------------------------------------------------------------
// The name of the executable as invoked (i.e. argv[0]).

@ -43,6 +43,7 @@
#include "absl/strings/str_cat.h"
#include "absl/strings/str_format.h"
#include "google/protobuf/compiler/command_line_interface_tester.h"
#include "google/protobuf/unittest_features.pb.h"
#ifndef _MSC_VER
#include <unistd.h>
@ -67,6 +68,7 @@
#include "google/protobuf/compiler/plugin.pb.h"
#include "google/protobuf/compiler/subprocess.h"
#include "google/protobuf/io/io_win32.h"
#include "google/protobuf/test_textproto.h"
#include "google/protobuf/test_util2.h"
#include "google/protobuf/unittest.pb.h"
#include "google/protobuf/unittest_custom_options.pb.h"
@ -439,7 +441,7 @@ TEST_F(CommandLineInterfaceTest, Plugin_OptionRetention) {
std::string plugin_path = GOOGLE_PROTOBUF_FAKE_PLUGIN_PATH;
#else
std::string plugin_path = absl::StrCat(
TestUtil::TestSourceDir(), "/third_party/protobuf/compiler/fake_plugin");
TestUtil::TestSourceDir(), "/google/protobuf/compiler/fake_plugin");
#endif
// Invoke protoc with fake_plugin to get ahold of the CodeGeneratorRequest
@ -510,7 +512,7 @@ TEST_F(CommandLineInterfaceTest, Plugin_SourceFileDescriptors) {
std::string plugin_path = GOOGLE_PROTOBUF_FAKE_PLUGIN_PATH;
#else
std::string plugin_path = absl::StrCat(
TestUtil::TestSourceDir(), "/third_party/protobuf/compiler/fake_plugin");
TestUtil::TestSourceDir(), "/google/protobuf/compiler/fake_plugin");
#endif
// Invoke protoc with fake_plugin to get ahold of the CodeGeneratorRequest
@ -1384,13 +1386,13 @@ TEST_F(CommandLineInterfaceTest, FeaturesEditionZero) {
}
TEST_F(CommandLineInterfaceTest, FeatureExtensions) {
CreateTempFile("net/proto2/proto/descriptor.proto",
CreateTempFile("google/protobuf/descriptor.proto",
google::protobuf::DescriptorProto::descriptor()->file()->DebugString());
CreateTempFile("features.proto",
R"schema(
syntax = "proto2";
package pb;
import "net/proto2/proto/descriptor.proto";
import "google/protobuf/descriptor.proto";
extend google.protobuf.FeatureSet {
optional TestFeatures test = 9999;
}
@ -1449,13 +1451,13 @@ TEST_F(CommandLineInterfaceTest, FeatureTargetError) {
}
TEST_F(CommandLineInterfaceTest, FeatureExtensionError) {
CreateTempFile("net/proto2/proto/descriptor.proto",
CreateTempFile("google/protobuf/descriptor.proto",
google::protobuf::DescriptorProto::descriptor()->file()->DebugString());
CreateTempFile("features.proto",
R"schema(
syntax = "proto2";
package pb;
import "net/proto2/proto/descriptor.proto";
import "google/protobuf/descriptor.proto";
extend google.protobuf.FeatureSet {
optional TestFeatures test = 9999;
}
@ -1482,6 +1484,165 @@ TEST_F(CommandLineInterfaceTest, FeatureExtensionError) {
"repeated field");
}
TEST_F(CommandLineInterfaceTest, Plugin_LegacyFeatures) {
CreateTempFile("foo.proto",
R"schema(
syntax = "proto2";
package foo;
message Foo {
optional int32 b = 1;
})schema");
#ifdef GOOGLE_PROTOBUF_FAKE_PLUGIN_PATH
std::string plugin_path = GOOGLE_PROTOBUF_FAKE_PLUGIN_PATH;
#else
std::string plugin_path = absl::StrCat(
TestUtil::TestSourceDir(), "/google/protobuf/compiler/fake_plugin");
#endif
// Invoke protoc with fake_plugin to get ahold of the CodeGeneratorRequest
// sent by protoc.
Run(absl::StrCat(
"protocol_compiler --fake_plugin_out=$tmpdir --proto_path=$tmpdir "
"foo.proto --plugin=prefix-gen-fake_plugin=",
plugin_path));
ExpectNoErrors();
std::string base64_output = ReadFile("foo.proto.request");
std::string binary_request;
ASSERT_TRUE(absl::Base64Unescape(base64_output, &binary_request));
CodeGeneratorRequest request;
ASSERT_TRUE(request.ParseFromString(binary_request));
EXPECT_FALSE(
request.proto_file(0).message_type(0).field(0).options().has_features());
EXPECT_FALSE(request.source_file_descriptors(0)
.message_type(0)
.field(0)
.options()
.has_features());
}
TEST_F(CommandLineInterfaceTest, Plugin_RuntimeFeatures) {
CreateTempFile("foo.proto",
R"schema(
edition = "2023";
package foo;
message Foo {
int32 b = 1 [features.field_presence = IMPLICIT];
})schema");
#ifdef GOOGLE_PROTOBUF_FAKE_PLUGIN_PATH
std::string plugin_path = GOOGLE_PROTOBUF_FAKE_PLUGIN_PATH;
#else
std::string plugin_path = absl::StrCat(
TestUtil::TestSourceDir(), "/google/protobuf/compiler/fake_plugin");
#endif
// Invoke protoc with fake_plugin to get ahold of the CodeGeneratorRequest
// sent by protoc.
Run(
absl::StrCat("protocol_compiler --experimental_editions "
"--fake_plugin_out=$tmpdir --proto_path=$tmpdir "
"foo.proto --plugin=prefix-gen-fake_plugin=",
plugin_path));
ExpectNoErrors();
std::string base64_output = ReadFile("foo.proto.request");
std::string binary_request;
ASSERT_TRUE(absl::Base64Unescape(base64_output, &binary_request));
CodeGeneratorRequest request;
ASSERT_TRUE(request.ParseFromString(binary_request));
EXPECT_THAT(
request.proto_file(0).message_type(0).field(0).options().features(),
EqualsProto(R"pb(field_presence: IMPLICIT
enum_type: OPEN
repeated_field_encoding: PACKED
string_field_validation: MANDATORY
message_encoding: LENGTH_PREFIXED
json_format: ALLOW
raw_features { field_presence: IMPLICIT }
)pb"));
EXPECT_THAT(request.source_file_descriptors(0)
.message_type(0)
.field(0)
.options()
.features(),
EqualsProto(R"pb(field_presence: IMPLICIT
enum_type: OPEN
repeated_field_encoding: PACKED
string_field_validation: MANDATORY
message_encoding: LENGTH_PREFIXED
json_format: ALLOW
raw_features { field_presence: IMPLICIT }
)pb"));
}
TEST_F(CommandLineInterfaceTest, Plugin_SourceFeatures) {
CreateTempFile("google/protobuf/descriptor.proto",
google::protobuf::DescriptorProto::descriptor()->file()->DebugString());
CreateTempFile("google/protobuf/unittest_features.proto",
pb::TestFeatures::descriptor()->file()->DebugString());
CreateTempFile("foo.proto",
R"schema(
edition = "2023";
import "google/protobuf/unittest_features.proto";
package foo;
message Foo {
int32 b = 1 [
features.(pb.test).int_field_feature = 99,
features.(pb.test).int_source_feature = 87
];
}
)schema");
#ifdef GOOGLE_PROTOBUF_FAKE_PLUGIN_PATH
std::string plugin_path = GOOGLE_PROTOBUF_FAKE_PLUGIN_PATH;
#else
std::string plugin_path = absl::StrCat(
TestUtil::TestSourceDir(), "/google/protobuf/compiler/fake_plugin");
#endif
// Invoke protoc with fake_plugin to get ahold of the CodeGeneratorRequest
// sent by protoc.
Run(
absl::StrCat("protocol_compiler --experimental_editions "
"--fake_plugin_out=$tmpdir --proto_path=$tmpdir "
"foo.proto --plugin=prefix-gen-fake_plugin=",
plugin_path));
ExpectNoErrors();
std::string base64_output = ReadFile("foo.proto.request");
std::string binary_request;
ASSERT_TRUE(absl::Base64Unescape(base64_output, &binary_request));
CodeGeneratorRequest request;
ASSERT_TRUE(request.ParseFromString(binary_request));
{
ASSERT_EQ(request.proto_file(2).name(), "foo.proto");
const FeatureSet& features =
request.proto_file(2).message_type(0).field(0).options().features();
EXPECT_THAT(features.raw_features(),
EqualsProto(R"pb([pb.test] { int_field_feature: 99 })pb"));
EXPECT_FALSE(features.GetExtension(pb::test).has_int_source_feature());
EXPECT_EQ(features.GetExtension(pb::test).int_field_feature(), 99);
}
{
ASSERT_EQ(request.source_file_descriptors(0).name(), "foo.proto");
const FeatureSet& features = request.source_file_descriptors(0)
.message_type(0)
.field(0)
.options()
.features();
EXPECT_THAT(features.raw_features(),
EqualsProto(R"pb([pb.test] {
int_field_feature: 99
int_source_feature: 87
})pb"));
EXPECT_EQ(features.GetExtension(pb::test).int_field_feature(), 99);
EXPECT_EQ(features.GetExtension(pb::test).int_source_feature(), 87);
}
}
#endif // PROTOBUF_FUTURE_EDITIONS
@ -2766,13 +2927,13 @@ TEST_F(CommandLineInterfaceTest, TargetTypeEnforcement) {
// The target option on a field indicates what kind of entity it may apply to
// when it is used as an option. This test verifies that the enforcement
// works correctly on all entity types.
CreateTempFile("net/proto2/proto/descriptor.proto",
CreateTempFile("google/protobuf/descriptor.proto",
google::protobuf::DescriptorProto::descriptor()->file()->DebugString());
CreateTempFile("foo.proto",
R"schema(
syntax = "proto2";
package protobuf_unittest;
import "net/proto2/proto/descriptor.proto";
import "google/protobuf/descriptor.proto";
message MyOptions {
optional string file_option = 1 [targets = TARGET_TYPE_FILE];
optional string extension_range_option = 2 [targets =
@ -2866,13 +3027,13 @@ TEST_F(CommandLineInterfaceTest, TargetTypeEnforcement) {
}
TEST_F(CommandLineInterfaceTest, TargetTypeEnforcementMultipleTargetsValid) {
CreateTempFile("net/proto2/proto/descriptor.proto",
CreateTempFile("google/protobuf/descriptor.proto",
google::protobuf::DescriptorProto::descriptor()->file()->DebugString());
CreateTempFile("foo.proto",
R"schema(
syntax = "proto2";
package protobuf_unittest;
import "net/proto2/proto/descriptor.proto";
import "google/protobuf/descriptor.proto";
message MyOptions {
optional string message_or_file_option = 1 [
targets = TARGET_TYPE_MESSAGE, targets = TARGET_TYPE_FILE];
@ -2894,13 +3055,13 @@ TEST_F(CommandLineInterfaceTest, TargetTypeEnforcementMultipleTargetsValid) {
}
TEST_F(CommandLineInterfaceTest, TargetTypeEnforcementMultipleTargetsInvalid) {
CreateTempFile("net/proto2/proto/descriptor.proto",
CreateTempFile("google/protobuf/descriptor.proto",
google::protobuf::DescriptorProto::descriptor()->file()->DebugString());
CreateTempFile("foo.proto",
R"schema(
syntax = "proto2";
package protobuf_unittest;
import "net/proto2/proto/descriptor.proto";
import "google/protobuf/descriptor.proto";
message MyOptions {
optional string message_or_file_option = 1 [
targets = TARGET_TYPE_MESSAGE, targets = TARGET_TYPE_FILE];
@ -2922,13 +3083,13 @@ TEST_F(CommandLineInterfaceTest, TargetTypeEnforcementMultipleTargetsInvalid) {
TEST_F(CommandLineInterfaceTest,
TargetTypeEnforcementMultipleEdgesWithConstraintsValid) {
CreateTempFile("net/proto2/proto/descriptor.proto",
CreateTempFile("google/protobuf/descriptor.proto",
google::protobuf::DescriptorProto::descriptor()->file()->DebugString());
CreateTempFile("foo.proto",
R"schema(
syntax = "proto2";
package protobuf_unittest;
import "net/proto2/proto/descriptor.proto";
import "google/protobuf/descriptor.proto";
message A {
optional B b = 1 [targets = TARGET_TYPE_FILE,
targets = TARGET_TYPE_ENUM];
@ -2949,13 +3110,13 @@ TEST_F(CommandLineInterfaceTest,
TEST_F(CommandLineInterfaceTest,
TargetTypeEnforcementMultipleEdgesWithConstraintsInvalid) {
CreateTempFile("net/proto2/proto/descriptor.proto",
CreateTempFile("google/protobuf/descriptor.proto",
google::protobuf::DescriptorProto::descriptor()->file()->DebugString());
CreateTempFile("foo.proto",
R"schema(
syntax = "proto2";
package protobuf_unittest;
import "net/proto2/proto/descriptor.proto";
import "google/protobuf/descriptor.proto";
message A {
optional B b = 1 [targets = TARGET_TYPE_ENUM];
}
@ -3129,28 +3290,27 @@ class EncodeDecodeTest : public testing::TestWithParam<EncodeDecodeTestMode> {
TEST_P(EncodeDecodeTest, Encode) {
RedirectStdinFromFile(TestUtil::GetTestDataPath(
"third_party/protobuf/"
"google/protobuf/"
"testdata/text_format_unittest_data_oneof_implemented.txt"));
std::string args;
if (GetParam() != DESCRIPTOR_SET_IN) {
args.append(
TestUtil::MaybeTranslatePath("third_party/protobuf/unittest.proto"));
args.append("google/protobuf/unittest.proto");
}
EXPECT_TRUE(
Run(absl::StrCat(args, " --encode=protobuf_unittest.TestAllTypes")));
ExpectStdoutMatchesBinaryFile(TestUtil::GetTestDataPath(
"third_party/protobuf/testdata/golden_message_oneof_implemented"));
"google/protobuf/testdata/golden_message_oneof_implemented"));
ExpectStderrMatchesText("");
}
TEST_P(EncodeDecodeTest, Decode) {
RedirectStdinFromFile(TestUtil::GetTestDataPath(
"third_party/protobuf/testdata/golden_message_oneof_implemented"));
"google/protobuf/testdata/golden_message_oneof_implemented"));
EXPECT_TRUE(
Run(TestUtil::MaybeTranslatePath("third_party/protobuf/unittest.proto") +
Run("google/protobuf/unittest.proto"
" --decode=protobuf_unittest.TestAllTypes"));
ExpectStdoutMatchesTextFile(TestUtil::GetTestDataPath(
"third_party/protobuf/"
"google/protobuf/"
"testdata/text_format_unittest_data_oneof_implemented.txt"));
ExpectStderrMatchesText("");
}
@ -3158,7 +3318,7 @@ TEST_P(EncodeDecodeTest, Decode) {
TEST_P(EncodeDecodeTest, Partial) {
RedirectStdinFromText("");
EXPECT_TRUE(
Run(TestUtil::MaybeTranslatePath("third_party/protobuf/unittest.proto") +
Run("google/protobuf/unittest.proto"
" --encode=protobuf_unittest.TestRequired"));
ExpectStdoutMatchesText("");
ExpectStderrMatchesText(
@ -3182,7 +3342,7 @@ TEST_P(EncodeDecodeTest, DecodeRaw) {
TEST_P(EncodeDecodeTest, UnknownType) {
EXPECT_FALSE(
Run(TestUtil::MaybeTranslatePath("third_party/protobuf/unittest.proto") +
Run("google/protobuf/unittest.proto"
" --encode=NoSuchType"));
ExpectStdoutMatchesText("");
ExpectStderrMatchesText("Type not defined: NoSuchType\n");
@ -3199,25 +3359,24 @@ TEST_P(EncodeDecodeTest, ProtoParseError) {
TEST_P(EncodeDecodeTest, EncodeDeterministicOutput) {
RedirectStdinFromFile(TestUtil::GetTestDataPath(
"third_party/protobuf/"
"google/protobuf/"
"testdata/text_format_unittest_data_oneof_implemented.txt"));
std::string args;
if (GetParam() != DESCRIPTOR_SET_IN) {
args.append(
TestUtil::MaybeTranslatePath("third_party/protobuf/unittest.proto"));
args.append("google/protobuf/unittest.proto");
}
EXPECT_TRUE(Run(absl::StrCat(
args, " --encode=protobuf_unittest.TestAllTypes --deterministic_output")));
ExpectStdoutMatchesBinaryFile(TestUtil::GetTestDataPath(
"third_party/protobuf/testdata/golden_message_oneof_implemented"));
"google/protobuf/testdata/golden_message_oneof_implemented"));
ExpectStderrMatchesText("");
}
TEST_P(EncodeDecodeTest, DecodeDeterministicOutput) {
RedirectStdinFromFile(TestUtil::GetTestDataPath(
"third_party/protobuf/testdata/golden_message_oneof_implemented"));
"google/protobuf/testdata/golden_message_oneof_implemented"));
EXPECT_FALSE(
Run(TestUtil::MaybeTranslatePath("third_party/protobuf/unittest.proto") +
Run("google/protobuf/unittest.proto"
" --decode=protobuf_unittest.TestAllTypes --deterministic_output"));
ExpectStderrMatchesText(
"Can only use --deterministic_output with --encode.\n");

@ -32,7 +32,7 @@
// Based on original Protocol Buffers design by
// Sanjay Ghemawat, Jeff Dean, and others.
//
// This test insures that net/proto2/proto/descriptor.pb.{h,cc} match exactly
// This test insures that google/protobuf/descriptor.pb.{h,cc} match exactly
// what would be generated by the protocol compiler. These files are not
// generated automatically at build time because they are compiled into the
// protocol compiler itself. So, if they were auto-generated, you'd have a
@ -144,9 +144,9 @@ TEST(BootstrapTest, GeneratedFilesMatch) {
// of the data to compare to.
absl::flat_hash_map<absl::string_view, std::string> vpath_map;
absl::flat_hash_map<absl::string_view, std::string> rpath_map;
rpath_map["third_party/protobuf/test_messages_proto2"] =
rpath_map["google/protobuf/test_messages_proto2"] =
"net/proto2/z_generated_example/test_messages_proto2";
rpath_map["third_party/protobuf/test_messages_proto3"] =
rpath_map["google/protobuf/test_messages_proto3"] =
"net/proto2/z_generated_example/test_messages_proto3";
rpath_map["net/proto2/internal/proto2_weak"] =
"net/proto2/z_generated_example/proto2_weak";

@ -56,7 +56,7 @@ class CppGeneratorTest : public CommandLineInterfaceTester {
"google/protobuf/descriptor.proto",
google::protobuf::DescriptorProto::descriptor()->file()->DebugString());
#ifdef PROTOBUF_FUTURE_EDITIONS
CreateTempFile("third_party/protobuf/cpp_features.proto",
CreateTempFile("google/protobuf/cpp_features.proto",
pb::CppFeatures::descriptor()->file()->DebugString());
#endif // PROTOBUF_FUTURE_EDITIONS
}
@ -97,7 +97,7 @@ TEST_F(CppGeneratorTest, LegacyClosedEnumOnNonEnumField) {
CreateTempFile("foo.proto",
R"schema(
edition = "2023";
import "third_party/protobuf/cpp_features.proto";
import "google/protobuf/cpp_features.proto";
message Foo {
int32 bar = 1 [features.(pb.cpp).legacy_closed_enum = true];
@ -116,7 +116,7 @@ TEST_F(CppGeneratorTest, LegacyClosedEnum) {
CreateTempFile("foo.proto",
R"schema(
edition = "2023";
import "third_party/protobuf/cpp_features.proto";
import "google/protobuf/cpp_features.proto";
enum TestEnum {
TEST_ENUM_UNKNOWN = 0;
@ -136,7 +136,7 @@ TEST_F(CppGeneratorTest, LegacyClosedEnumInherited) {
CreateTempFile("foo.proto",
R"schema(
edition = "2023";
import "third_party/protobuf/cpp_features.proto";
import "google/protobuf/cpp_features.proto";
option features.(pb.cpp).legacy_closed_enum = true;
enum TestEnum {
@ -158,7 +158,7 @@ TEST_F(CppGeneratorTest, LegacyClosedEnumImplicit) {
CreateTempFile("foo.proto",
R"schema(
edition = "2023";
import "third_party/protobuf/cpp_features.proto";
import "google/protobuf/cpp_features.proto";
option features.(pb.cpp).legacy_closed_enum = true;
enum TestEnum {

@ -33,7 +33,7 @@
// Sanjay Ghemawat, Jeff Dean, and others.
//
// To test the code generator, we actually use it to generate code for
// third_party/protobuf/unittest.proto, then test that. This means that we
// google/protobuf/unittest.proto, then test that. This means that we
// are actually testing the parser and other parts of the system at the same
// time, and that problems in the generator may show up as compile-time errors
// rather than unittest failures, which may be surprising. However, testing
@ -59,7 +59,7 @@
#define HELPERS_TEST_NAME HelpersTest
#define DESCRIPTOR_INIT_TEST_NAME DescriptorInitializationTest
#define UNITTEST_PROTO_PATH "third_party/protobuf/unittest.proto"
#define UNITTEST_PROTO_PATH "google/protobuf/unittest.proto"
#define UNITTEST ::protobuf_unittest
#define UNITTEST_IMPORT ::protobuf_unittest_import

@ -34,7 +34,7 @@
// Sanjay Ghemawat, Jeff Dean, and others.
//
// To test the code generator, we actually use it to generate code for
// third_party/protobuf/unittest.proto, then test that. This means that we
// google/protobuf/unittest.proto, then test that. This means that we
// are actually testing the parser and other parts of the system at the same
// time, and that problems in the generator may show up as compile-time errors
// rather than unittest failures, which may be surprising. However, testing
@ -124,7 +124,7 @@ TEST(GENERATED_DESCRIPTOR_TEST_NAME, IdenticalDescriptors) {
// Import (parse) unittest.proto.
const FileDescriptor* parsed_descriptor =
importer.Import(TestUtil::MaybeTranslatePath(UNITTEST_PROTO_PATH));
importer.Import(UNITTEST_PROTO_PATH);
EXPECT_EQ("", error_collector.text_);
ASSERT_TRUE(parsed_descriptor != nullptr);
@ -2217,7 +2217,7 @@ TEST(DESCRIPTOR_INIT_TEST_NAME, Initialized) {
EXPECT_EQ(should_have_descriptors,
DescriptorPool::generated_pool()->InternalIsFileLoaded(
TestUtil::MaybeTranslatePath(UNITTEST_PROTO_PATH)));
UNITTEST_PROTO_PATH));
}
} // namespace cpp_unittest

@ -63,7 +63,7 @@ int CompileJavaProto(std::string proto_file_name) {
std::string proto_path = absl::StrCat(
"--proto_path=",
TestUtil::GetTestDataPath("third_party/protobuf/compiler/java"));
TestUtil::GetTestDataPath("google/protobuf/compiler/java"));
std::string java_out = absl::StrCat("--java_out=", TestTempDir());
const char* argv[] = {

@ -2318,7 +2318,7 @@ TEST_F(ParserValidationErrorTest, ResovledUndefinedOptionError) {
// base2.proto:
// package baz
// import net/proto2/proto/descriptor.proto
// import google/protobuf/descriptor.proto
// message Bar { optional int32 foo = 1; }
// extend FileOptions { optional Bar bar = 7672757; }
FileDescriptorProto other_file;
@ -2461,8 +2461,7 @@ TEST_F(ParseDescriptorDebugTest, TestAllDescriptorTypes) {
// We now have a FileDescriptorProto, but to compare with the expected we
// need to link to a FileDecriptor, then output back to a proto. We'll
// also need to give it the same name as the original.
parsed.set_name(
TestUtil::MaybeTranslatePath("third_party/protobuf/unittest.proto"));
parsed.set_name("google/protobuf/unittest.proto");
// We need the imported dependency before we can build our parsed proto
const FileDescriptor* public_import =
protobuf_unittest_import::PublicImportMessage::descriptor()->file();

@ -271,6 +271,7 @@ class PROTOBUF_EXPORT InternalFeatureHelper {
private:
friend class ::google::protobuf::compiler::CodeGenerator;
friend class ::google::protobuf::compiler::CommandLineInterface;
// Provides a restricted view exclusively to code generators. Raw features
// haven't been resolved, and are virtually meaningless to everyone else. Code

@ -65,7 +65,7 @@ namespace {
using ::google::protobuf::internal::DownCast;
using TestUtil::EqualsToSerialized;
// This test closely mirrors third_party/protobuf/compiler/cpp/unittest.cc
// This test closely mirrors google/protobuf/compiler/cpp/unittest.cc
// except that it uses extensions rather than regular fields.
TEST(ExtensionSetTest, Defaults) {
@ -1368,7 +1368,7 @@ TEST(ExtensionSetTest, Proto3PackedDynamicExtensions) {
google::protobuf::FileDescriptorProto file_descriptor_proto;
file_descriptor_proto.set_syntax("proto3");
file_descriptor_proto.set_name(
"third_party/protobuf/unittest_proto3_packed_extension.proto");
"google/protobuf/unittest_proto3_packed_extension.proto");
file_descriptor_proto.set_package("proto3_unittest");
file_descriptor_proto.add_dependency(
DescriptorProto::descriptor()->file()->name());

@ -582,7 +582,7 @@ TEST_F(IoTest, CompressionOptions) {
// Some ad-hoc testing of compression options.
std::string golden_filename =
TestUtil::GetTestDataPath("third_party/protobuf/testdata/golden_message");
TestUtil::GetTestDataPath("google/protobuf/testdata/golden_message");
std::string golden;
ABSL_CHECK_OK(File::GetContents(golden_filename, &golden, true));

@ -1165,7 +1165,7 @@ TEST_P(JsonTest, TestDuration) {
EXPECT_THAT(ToJson(m5->value()), IsOkAndHolds("\"0.500s\""));
}
// These tests are not exhaustive; tests in //third_party/protobuf/conformance
// These tests are not exhaustive; tests in //google/protobuf/conformance
// are more comprehensive.
TEST_P(JsonTest, TestTimestamp) {
auto m = ToProto<proto3::TestTimestamp>(R"json(

@ -3968,7 +3968,7 @@ TEST(MapSerializationTest, DeterministicSubmessage) {
std::string golden;
ABSL_CHECK_OK(
File::GetContents(TestUtil::GetTestDataPath(absl::StrCat(
"third_party/protobuf/testdata/", filename)),
"google/protobuf/testdata/", filename)),
&golden, true));
t.ParseFromString(golden);
*(p.mutable_m()) = t;
@ -4010,7 +4010,7 @@ TEST(TextFormatMapTest, DynamicMessage) {
std::string expected_text;
ABSL_CHECK_OK(
File::GetContents(TestUtil::GetTestDataPath("third_party/protobuf/"
File::GetContents(TestUtil::GetTestDataPath("google/protobuf/"
"testdata/map_test_data.txt"),
&expected_text, true));
@ -4026,7 +4026,7 @@ TEST(TextFormatMapTest, Sorted) {
std::string expected_text;
ABSL_CHECK_OK(
File::GetContents(TestUtil::GetTestDataPath("third_party/protobuf/"
File::GetContents(TestUtil::GetTestDataPath("google/protobuf/"
"testdata/map_test_data.txt"),
&expected_text, true));
@ -4047,7 +4047,7 @@ TEST(TextFormatMapTest, ParseCorruptedString) {
std::string serialized_message;
ABSL_CHECK_OK(File::GetContents(
TestUtil::GetTestDataPath(
"third_party/protobuf/testdata/golden_message_maps"),
"google/protobuf/testdata/golden_message_maps"),
&serialized_message, true));
UNITTEST::TestMaps message;
ABSL_CHECK(message.ParseFromString(serialized_message));

@ -132,7 +132,7 @@ TEST(MESSAGE_TEST_NAME, SerializeToBrokenOstream) {
TEST(MESSAGE_TEST_NAME, ParseFromFileDescriptor) {
std::string filename =
TestUtil::GetTestDataPath("third_party/protobuf/testdata/golden_message");
TestUtil::GetTestDataPath("google/protobuf/testdata/golden_message");
int file = open(filename.c_str(), O_RDONLY | O_BINARY);
ASSERT_GE(file, 0);
@ -145,7 +145,7 @@ TEST(MESSAGE_TEST_NAME, ParseFromFileDescriptor) {
TEST(MESSAGE_TEST_NAME, ParsePackedFromFileDescriptor) {
std::string filename = TestUtil::GetTestDataPath(
"third_party/protobuf/testdata/golden_packed_fields_message");
"google/protobuf/testdata/golden_packed_fields_message");
int file = open(filename.c_str(), O_RDONLY | O_BINARY);
ASSERT_GE(file, 0);

@ -45,32 +45,12 @@ namespace google {
namespace protobuf {
namespace TestUtil {
// Translate net/proto2/* or third_party/protobuf/* to google/protobuf/*.
inline std::string TranslatePathToOpensource(absl::string_view google3_path) {
constexpr absl::string_view net_proto2 = "net/proto2/";
constexpr absl::string_view third_party_protobuf = "third_party/protobuf/";
if (!absl::ConsumePrefix(&google3_path, net_proto2)) {
ABSL_CHECK(absl::ConsumePrefix(&google3_path, third_party_protobuf))
<< google3_path;
}
absl::ConsumePrefix(&google3_path, "internal/");
absl::ConsumePrefix(&google3_path, "proto/");
absl::ConsumeSuffix(&google3_path, "public/");
return absl::StrCat("google/protobuf/", google3_path);
}
inline std::string MaybeTranslatePath(absl::string_view google3_path) {
return TranslatePathToOpensource(google3_path);
return std::string(google3_path);
}
inline std::string TestSourceDir() {
return google::protobuf::TestSourceDir();
}
inline std::string GetTestDataPath(absl::string_view google3_path) {
return absl::StrCat(TestSourceDir(), "/", MaybeTranslatePath(google3_path));
inline std::string GetTestDataPath(absl::string_view path) {
return absl::StrCat(TestSourceDir(), "/", path);
}
// Checks the equality of "message" and serialized proto of type "ProtoType".

@ -97,7 +97,7 @@ class TextFormatTest : public testing::Test {
static void SetUpTestSuite() {
ABSL_CHECK_OK(File::GetContents(
TestUtil::GetTestDataPath(
"third_party/protobuf/"
"google/protobuf/"
"testdata/text_format_unittest_data_oneof_implemented.txt"),
&static_proto_text_format_, true));
}
@ -118,7 +118,7 @@ class TextFormatExtensionsTest : public testing::Test {
public:
static void SetUpTestSuite() {
ABSL_CHECK_OK(File::GetContents(
TestUtil::GetTestDataPath("third_party/protobuf/testdata/"
TestUtil::GetTestDataPath("google/protobuf/testdata/"
"text_format_unittest_extensions_data.txt"),
&static_proto_text_format_, true));
}

Loading…
Cancel
Save