github.com/stackb/rules_proto@v0.0.0-20240221195024-5428336c51f1/pkg/protoc/proto_plugin_config_test.go (about) 1 package protoc 2 3 import ( 4 "testing" 5 6 "github.com/bazelbuild/bazel-gazelle/label" 7 ) 8 9 type LanguagePluginConfigCheck func(t *testing.T, cfg *LanguagePluginConfig) 10 11 func TestPluginDirectives(t *testing.T) { 12 testDirectives(t, map[string]packageConfigTestCase{ 13 "proto_plugin label": { 14 directives: withDirectives("proto_plugin", "fake_proto label //protoc:fake_proto_plugin"), 15 check: withPlugin("fake_proto", withPluginLabelEquals("", "protoc", "fake_proto_plugin")), 16 }, 17 "proto_plugin option": { 18 directives: withDirectives("proto_plugin", "fake_proto option grpc"), 19 check: withPlugin("fake_proto", withPluginOptionsEquals("grpc")), 20 }, 21 "proto_plugin +option": { 22 directives: withDirectives("proto_plugin", "fake_proto +option grpc"), 23 check: withPlugin("fake_proto", withPluginOptionsEquals("grpc")), 24 }, 25 "proto_plugin -option": { 26 directives: withDirectives( 27 "proto_plugin", "fake_proto +option grpc", 28 "proto_plugin", "fake_proto -option grpc", 29 ), 30 check: withPlugin("fake_proto", withPluginOptionsEquals()), 31 }, 32 }) 33 } 34 35 func withPlugin(name string, checks ...LanguagePluginConfigCheck) packageConfigCheck { 36 return func(t *testing.T, cfg *PackageConfig) { 37 plugin, ok := cfg.plugins[name] 38 if !ok { 39 t.Fatal("plugin not found", name) 40 } 41 for _, check := range checks { 42 check(t, plugin) 43 } 44 } 45 } 46 47 func withPluginLabelEquals(repo, pkg, name string) LanguagePluginConfigCheck { 48 return func(t *testing.T, cfg *LanguagePluginConfig) { 49 want := label.New(repo, pkg, name) 50 got := cfg.Label 51 if want.String() != got.String() { 52 t.Errorf("plugin label: want %s, got %s", want, got) 53 } 54 } 55 } 56 57 func withPluginOptionsEquals(opts ...string) LanguagePluginConfigCheck { 58 return func(t *testing.T, cfg *LanguagePluginConfig) { 59 got := cfg.GetOptions() 60 if len(opts) != len(got) { 61 t.Fatalf("plugin options: want %d, got %d", len(opts), len(got)) 62 } 63 for i := 0; i < len(got); i++ { 64 expected := opts[i] 65 actual := got[i] 66 if expected != actual { 67 t.Errorf("plugin option #%d: want %s, got %s", i, expected, actual) 68 } 69 } 70 } 71 }