@ -206,6 +206,8 @@ class CommandLineInterfaceTest : public CommandLineInterfaceTester {
void WriteDescriptorSet ( absl : : string_view filename ,
const FileDescriptorSet * descriptor_set ) ;
FeatureSetDefaults ReadEditionDefaults ( absl : : string_view filename ) ;
// The default code generators support all features. Use this to create a
// code generator that omits the given feature(s).
void CreateGeneratorWithMissingFeatures ( const std : : string & name ,
@ -357,6 +359,15 @@ void CommandLineInterfaceTest::ReadDescriptorSet(
}
}
FeatureSetDefaults CommandLineInterfaceTest : : ReadEditionDefaults (
absl : : string_view filename ) {
FeatureSetDefaults defaults ;
std : : string file_contents = ReadFile ( filename ) ;
ABSL_CHECK ( defaults . ParseFromString ( file_contents ) )
< < " Could not parse file contents: " < < filename ;
return defaults ;
}
void CommandLineInterfaceTest : : WriteDescriptorSet (
absl : : string_view filename , const FileDescriptorSet * descriptor_set ) {
std : : string binary_proto ;
@ -1749,6 +1760,233 @@ TEST_F(CommandLineInterfaceTest, PluginNoEditionsSupport) {
" code generator prefix-gen-plug hasn't been updated to support editions " ) ;
}
TEST_F ( CommandLineInterfaceTest , EditionDefaults ) {
CreateTempFile ( " google/protobuf/descriptor.proto " ,
google : : protobuf : : DescriptorProto : : descriptor ( ) - > file ( ) - > DebugString ( ) ) ;
Run ( " protocol_compiler --proto_path=$tmpdir "
" --experimental_edition_defaults_out=$tmpdir/defaults "
" google/protobuf/descriptor.proto " ) ;
ExpectNoErrors ( ) ;
FeatureSetDefaults defaults = ReadEditionDefaults ( " defaults " ) ;
EXPECT_THAT ( defaults , EqualsProto ( R " pb(
defaults {
edition_enum : EDITION_2023
features {
field_presence : EXPLICIT
enum_type : OPEN
repeated_field_encoding : PACKED
message_encoding : LENGTH_PREFIXED
json_format : ALLOW
}
}
minimum_edition_enum : EDITION_2023
maximum_edition_enum : EDITION_2023
) pb " ));
}
TEST_F ( CommandLineInterfaceTest , EditionDefaultsWithMaximum ) {
CreateTempFile ( " google/protobuf/descriptor.proto " ,
google : : protobuf : : DescriptorProto : : descriptor ( ) - > file ( ) - > DebugString ( ) ) ;
Run ( " protocol_compiler --proto_path=$tmpdir "
" --experimental_edition_defaults_out=$tmpdir/defaults "
" --experimental_edition_defaults_maximum=99997_TEST_ONLY "
" google/protobuf/descriptor.proto " ) ;
ExpectNoErrors ( ) ;
FeatureSetDefaults defaults = ReadEditionDefaults ( " defaults " ) ;
EXPECT_THAT ( defaults , EqualsProto ( R " pb(
defaults {
edition_enum : EDITION_2023
features {
field_presence : EXPLICIT
enum_type : OPEN
repeated_field_encoding : PACKED
message_encoding : LENGTH_PREFIXED
json_format : ALLOW
}
}
minimum_edition_enum : EDITION_2023
maximum_edition_enum : EDITION_99997_TEST_ONLY
) pb " ));
}
TEST_F ( CommandLineInterfaceTest , EditionDefaultsWithMinimum ) {
CreateTempFile ( " google/protobuf/descriptor.proto " ,
google : : protobuf : : DescriptorProto : : descriptor ( ) - > file ( ) - > DebugString ( ) ) ;
Run ( " protocol_compiler --proto_path=$tmpdir "
" --experimental_edition_defaults_out=$tmpdir/defaults "
" --experimental_edition_defaults_minimum=99997_TEST_ONLY "
" --experimental_edition_defaults_maximum=99999_TEST_ONLY "
" google/protobuf/descriptor.proto " ) ;
ExpectNoErrors ( ) ;
FeatureSetDefaults defaults = ReadEditionDefaults ( " defaults " ) ;
EXPECT_THAT ( defaults , EqualsProto ( R " pb(
defaults {
edition_enum : EDITION_2023
features {
field_presence : EXPLICIT
enum_type : OPEN
repeated_field_encoding : PACKED
message_encoding : LENGTH_PREFIXED
json_format : ALLOW
}
}
minimum_edition_enum : EDITION_99997_TEST_ONLY
maximum_edition_enum : EDITION_99999_TEST_ONLY
) pb " ));
}
TEST_F ( CommandLineInterfaceTest , EditionDefaultsWithExtension ) {
CreateTempFile ( " google/protobuf/descriptor.proto " ,
google : : protobuf : : DescriptorProto : : descriptor ( ) - > file ( ) - > DebugString ( ) ) ;
CreateTempFile ( " features.proto " ,
pb : : TestFeatures : : descriptor ( ) - > file ( ) - > DebugString ( ) ) ;
Run ( " protocol_compiler --proto_path=$tmpdir "
" --experimental_edition_defaults_out=$tmpdir/defaults "
" --experimental_edition_defaults_maximum=99999_TEST_ONLY "
" features.proto google/protobuf/descriptor.proto " ) ;
ExpectNoErrors ( ) ;
FeatureSetDefaults defaults = ReadEditionDefaults ( " defaults " ) ;
EXPECT_EQ ( defaults . minimum_edition_enum ( ) , EDITION_2023 ) ;
EXPECT_EQ ( defaults . maximum_edition_enum ( ) , EDITION_99999_TEST_ONLY ) ;
ASSERT_EQ ( defaults . defaults_size ( ) , 3 ) ;
EXPECT_EQ ( defaults . defaults ( 0 ) . edition_enum ( ) , EDITION_2023 ) ;
EXPECT_EQ ( defaults . defaults ( 1 ) . edition_enum ( ) , EDITION_99997_TEST_ONLY ) ;
EXPECT_EQ ( defaults . defaults ( 2 ) . edition_enum ( ) , EDITION_99998_TEST_ONLY ) ;
EXPECT_EQ (
defaults . defaults ( 0 ) . features ( ) . GetExtension ( pb : : test ) . int_file_feature ( ) ,
1 ) ;
EXPECT_EQ (
defaults . defaults ( 1 ) . features ( ) . GetExtension ( pb : : test ) . int_file_feature ( ) ,
2 ) ;
EXPECT_EQ (
defaults . defaults ( 2 ) . features ( ) . GetExtension ( pb : : test ) . int_file_feature ( ) ,
3 ) ;
}
# ifndef _WIN32
TEST_F ( CommandLineInterfaceTest , EditionDefaultsDependencyManifest ) {
CreateTempFile ( " google/protobuf/descriptor.proto " ,
google : : protobuf : : DescriptorProto : : descriptor ( ) - > file ( ) - > DebugString ( ) ) ;
CreateTempFile ( " features.proto " ,
pb : : TestFeatures : : descriptor ( ) - > file ( ) - > DebugString ( ) ) ;
Run ( " protocol_compiler --dependency_out=$tmpdir/manifest "
" --experimental_edition_defaults_out=$tmpdir/defaults "
" --proto_path=$tmpdir features.proto " ) ;
ExpectNoErrors ( ) ;
ExpectFileContent (
" manifest " ,
" $tmpdir/defaults: "
" $tmpdir/google/protobuf/descriptor.proto \\ \n $tmpdir/features.proto " ) ;
}
# endif // _WIN32
TEST_F ( CommandLineInterfaceTest , EditionDefaultsInvalidMissingDescriptor ) {
CreateTempFile ( " features.proto " , R " schema(
syntax = " proto2 " ;
message Foo { }
) schema " );
Run ( " protocol_compiler --proto_path=$tmpdir "
" --experimental_edition_defaults_out=$tmpdir/defaults "
" features.proto " ) ;
ExpectErrorSubstring ( " Could not find FeatureSet in descriptor pool " ) ;
}
TEST_F ( CommandLineInterfaceTest , EditionDefaultsInvalidTwice ) {
CreateTempFile ( " google/protobuf/descriptor.proto " ,
google : : protobuf : : DescriptorProto : : descriptor ( ) - > file ( ) - > DebugString ( ) ) ;
Run ( " protocol_compiler --proto_path=$tmpdir "
" --experimental_edition_defaults_out=$tmpdir/defaults "
" --experimental_edition_defaults_out=$tmpdir/defaults "
" google/protobuf/descriptor.proto " ) ;
ExpectErrorSubstring (
" experimental_edition_defaults_out may only be passed once " ) ;
}
TEST_F ( CommandLineInterfaceTest , EditionDefaultsInvalidEmpty ) {
CreateTempFile ( " google/protobuf/descriptor.proto " ,
google : : protobuf : : DescriptorProto : : descriptor ( ) - > file ( ) - > DebugString ( ) ) ;
Run ( " protocol_compiler --proto_path=$tmpdir "
" --experimental_edition_defaults_out= "
" google/protobuf/descriptor.proto " ) ;
ExpectErrorSubstring (
" experimental_edition_defaults_out requires a non-empty value " ) ;
}
TEST_F ( CommandLineInterfaceTest , EditionDefaultsInvalidCompile ) {
CreateTempFile ( " google/protobuf/descriptor.proto " ,
google : : protobuf : : DescriptorProto : : descriptor ( ) - > file ( ) - > DebugString ( ) ) ;
Run ( " protocol_compiler --proto_path=$tmpdir "
" --encode=pb.CppFeatures "
" --experimental_edition_defaults_out=$tmpdir/defaults "
" google/protobuf/descriptor.proto " ) ;
ExpectErrorSubstring ( " Cannot use --encode or --decode and generate defaults " ) ;
}
TEST_F ( CommandLineInterfaceTest , EditionDefaultsInvalidMinimumTwice ) {
CreateTempFile ( " google/protobuf/descriptor.proto " ,
google : : protobuf : : DescriptorProto : : descriptor ( ) - > file ( ) - > DebugString ( ) ) ;
Run ( " protocol_compiler --proto_path=$tmpdir "
" --experimental_edition_defaults_minimum=2023 "
" --experimental_edition_defaults_minimum=2023 "
" google/protobuf/descriptor.proto " ) ;
ExpectErrorSubstring (
" experimental_edition_defaults_minimum may only be passed once " ) ;
}
TEST_F ( CommandLineInterfaceTest , EditionDefaultsInvalidMinimumEmpty ) {
CreateTempFile ( " google/protobuf/descriptor.proto " ,
google : : protobuf : : DescriptorProto : : descriptor ( ) - > file ( ) - > DebugString ( ) ) ;
Run ( " protocol_compiler --proto_path=$tmpdir "
" --experimental_edition_defaults_minimum= "
" google/protobuf/descriptor.proto " ) ;
ExpectErrorSubstring ( " unknown edition \" \" " ) ;
}
TEST_F ( CommandLineInterfaceTest , EditionDefaultsInvalidMinimumUnknown ) {
CreateTempFile ( " google/protobuf/descriptor.proto " ,
google : : protobuf : : DescriptorProto : : descriptor ( ) - > file ( ) - > DebugString ( ) ) ;
Run ( " protocol_compiler --proto_path=$tmpdir "
" --experimental_edition_defaults_minimum=2022 "
" google/protobuf/descriptor.proto " ) ;
ExpectErrorSubstring ( " unknown edition \" 2022 \" " ) ;
}
TEST_F ( CommandLineInterfaceTest , EditionDefaultsInvalidMaximumTwice ) {
CreateTempFile ( " google/protobuf/descriptor.proto " ,
google : : protobuf : : DescriptorProto : : descriptor ( ) - > file ( ) - > DebugString ( ) ) ;
Run ( " protocol_compiler --proto_path=$tmpdir "
" --experimental_edition_defaults_maximum=2023 "
" --experimental_edition_defaults_maximum=2023 "
" google/protobuf/descriptor.proto " ) ;
ExpectErrorSubstring (
" experimental_edition_defaults_maximum may only be passed once " ) ;
}
TEST_F ( CommandLineInterfaceTest , EditionDefaultsInvalidMaximumEmpty ) {
CreateTempFile ( " google/protobuf/descriptor.proto " ,
google : : protobuf : : DescriptorProto : : descriptor ( ) - > file ( ) - > DebugString ( ) ) ;
Run ( " protocol_compiler --proto_path=$tmpdir "
" --experimental_edition_defaults_maximum= "
" google/protobuf/descriptor.proto " ) ;
ExpectErrorSubstring ( " unknown edition \" \" " ) ;
}
TEST_F ( CommandLineInterfaceTest , EditionDefaultsInvalidMaximumUnknown ) {
CreateTempFile ( " google/protobuf/descriptor.proto " ,
google : : protobuf : : DescriptorProto : : descriptor ( ) - > file ( ) - > DebugString ( ) ) ;
Run ( " protocol_compiler --proto_path=$tmpdir "
" --experimental_edition_defaults_maximum=2022 "
" google/protobuf/descriptor.proto " ) ;
ExpectErrorSubstring ( " unknown edition \" 2022 \" " ) ;
}
TEST_F ( CommandLineInterfaceTest , DirectDependencies_Missing_EmptyList ) {
CreateTempFile ( " foo.proto " ,