@@ -1155,18 +1155,35 @@ func TestCustomOptionsDiscoveredInDependencies(t *testing.T) {
11551155 fileDesc , err := file .Build ()
11561156 testutil .Ok (t , err )
11571157
1158+ // Another file that imports the options. Since it's not a public import, presence
1159+ // of this file should not prevent builder from correctly adding options.proto dependency
1160+ // in the "auto" case below.
1161+ otherFileDesc , err := NewFile ("other.proto" ).AddImportedDependency (fileDesc ).Build ()
1162+ testutil .Ok (t , err )
1163+
1164+ regWithOpts := & dynamic.ExtensionRegistry {}
1165+ regWithOpts .AddExtensionsFromFileRecursively (fileDesc )
1166+
11581167 // Now we can test referring to these and making sure they show up correctly
11591168 // in built descriptors
1160- for name , useBuilder := range map [string ]bool {"descriptor" : false , "builder" : true } {
1169+ for name , useBuilder := range map [string ]* bool {"descriptor" : proto . Bool ( false ) , "builder" : proto . Bool ( true ), "auto" : nil } {
11611170 newFile := func () * FileBuilder {
1162- fb := NewFile ("foo.proto" )
1163- if useBuilder {
1164- fb .AddDependency (file )
1165- } else {
1166- fb .AddImportedDependency (fileDesc )
1171+ fb := NewFile ("foo.proto" ).AddImportedDependency (otherFileDesc )
1172+ if useBuilder != nil {
1173+ if * useBuilder {
1174+ fb .AddDependency (file )
1175+ } else {
1176+ fb .AddImportedDependency (fileDesc )
1177+ }
11671178 }
11681179 return fb
11691180 }
1181+ var extReg * dynamic.ExtensionRegistry
1182+ if useBuilder == nil {
1183+ // if providing neither builder nor descriptor, we need to provide
1184+ // a registry for resolving custom options
1185+ extReg = regWithOpts
1186+ }
11701187 t .Run (name , func (t * testing.T ) {
11711188 t .Run ("file options" , func (t * testing.T ) {
11721189 fb := newFile ()
@@ -1175,7 +1192,7 @@ func TestCustomOptionsDiscoveredInDependencies(t *testing.T) {
11751192 testutil .Ok (t , err )
11761193 err = dynamic .SetExtension (fb .Options , ext , "fubar" )
11771194 testutil .Ok (t , err )
1178- checkBuildWithImportedExtensions (t , fb )
1195+ checkBuildWithImportedExtensions (t , fb , extReg )
11791196 })
11801197
11811198 t .Run ("message options" , func (t * testing.T ) {
@@ -1188,7 +1205,7 @@ func TestCustomOptionsDiscoveredInDependencies(t *testing.T) {
11881205
11891206 fb := newFile ()
11901207 fb .AddMessage (mb )
1191- checkBuildWithImportedExtensions (t , mb )
1208+ checkBuildWithImportedExtensions (t , mb , extReg )
11921209 })
11931210
11941211 t .Run ("field options" , func (t * testing.T ) {
@@ -1203,7 +1220,7 @@ func TestCustomOptionsDiscoveredInDependencies(t *testing.T) {
12031220
12041221 fb := newFile ()
12051222 fb .AddMessage (mb )
1206- checkBuildWithImportedExtensions (t , flb )
1223+ checkBuildWithImportedExtensions (t , flb , extReg )
12071224 })
12081225
12091226 t .Run ("oneof options" , func (t * testing.T ) {
@@ -1219,7 +1236,7 @@ func TestCustomOptionsDiscoveredInDependencies(t *testing.T) {
12191236
12201237 fb := newFile ()
12211238 fb .AddMessage (mb )
1222- checkBuildWithImportedExtensions (t , oob )
1239+ checkBuildWithImportedExtensions (t , oob , extReg )
12231240 })
12241241
12251242 t .Run ("extension range options" , func (t * testing.T ) {
@@ -1232,7 +1249,7 @@ func TestCustomOptionsDiscoveredInDependencies(t *testing.T) {
12321249
12331250 fb := newFile ()
12341251 fb .AddMessage (mb )
1235- checkBuildWithImportedExtensions (t , mb )
1252+ checkBuildWithImportedExtensions (t , mb , extReg )
12361253 })
12371254
12381255 t .Run ("enum options" , func (t * testing.T ) {
@@ -1246,7 +1263,7 @@ func TestCustomOptionsDiscoveredInDependencies(t *testing.T) {
12461263
12471264 fb := newFile ()
12481265 fb .AddEnum (eb )
1249- checkBuildWithImportedExtensions (t , eb )
1266+ checkBuildWithImportedExtensions (t , eb , extReg )
12501267 })
12511268
12521269 t .Run ("enum val options" , func (t * testing.T ) {
@@ -1261,7 +1278,7 @@ func TestCustomOptionsDiscoveredInDependencies(t *testing.T) {
12611278
12621279 fb := newFile ()
12631280 fb .AddEnum (eb )
1264- checkBuildWithImportedExtensions (t , evb )
1281+ checkBuildWithImportedExtensions (t , evb , extReg )
12651282 })
12661283
12671284 t .Run ("service options" , func (t * testing.T ) {
@@ -1274,7 +1291,7 @@ func TestCustomOptionsDiscoveredInDependencies(t *testing.T) {
12741291
12751292 fb := newFile ()
12761293 fb .AddService (sb )
1277- checkBuildWithImportedExtensions (t , sb )
1294+ checkBuildWithImportedExtensions (t , sb , extReg )
12781295 })
12791296
12801297 t .Run ("method options" , func (t * testing.T ) {
@@ -1293,20 +1310,22 @@ func TestCustomOptionsDiscoveredInDependencies(t *testing.T) {
12931310
12941311 fb := newFile ()
12951312 fb .AddService (sb ).AddMessage (req ).AddMessage (resp )
1296- checkBuildWithImportedExtensions (t , mtb )
1313+ checkBuildWithImportedExtensions (t , mtb , extReg )
12971314 })
12981315 })
12991316 }
13001317}
13011318
1302- func checkBuildWithImportedExtensions (t * testing.T , builder Builder ) {
1319+ func checkBuildWithImportedExtensions (t * testing.T , builder Builder , extReg * dynamic. ExtensionRegistry ) {
13031320 // requiring options and succeeding (since they are defined in explicit import)
1304- var opts BuilderOptions
1305- opts .RequireInterpretedOptions = true
1321+ opts := BuilderOptions {
1322+ RequireInterpretedOptions : true ,
1323+ Extensions : extReg ,
1324+ }
13061325 d , err := opts .Build (builder )
13071326 testutil .Ok (t , err )
1308- // the only import is for the custom options
1309- testutil .Eq (t , []string {"options.proto" }, d .GetFile ().AsFileDescriptorProto ().GetDependency ())
1327+ // the only import is the explicitly added one and one added for the custom options
1328+ testutil .Eq (t , []string {"options.proto" , "other.proto" }, d .GetFile ().AsFileDescriptorProto ().GetDependency ())
13101329}
13111330
13121331func TestUseOfExtensionRegistry (t * testing.T ) {
0 commit comments