github.com/stackb/rules_proto@v0.0.0-20240221195024-5428336c51f1/pkg/protoc/language_config_test.go (about) 1 package protoc 2 3 import "testing" 4 5 type languageConfigCheck func(t *testing.T, cfg *LanguageConfig) 6 7 func NoTestLanguageConfigClone(t *testing.T) { 8 a := newLanguageConfig("fake") 9 b := a.clone() 10 11 check := allLanguageChecks( 12 withLanguageEnabled(true), 13 ) 14 15 check(t, a) 16 check(t, b) 17 18 b.Enabled = false 19 b.Plugins["fake_proto"] = false 20 b.Rules["fake_proto_library"] = false 21 22 withLanguageEnabled(true)(t, a) 23 withLanguageEnabled(false)(t, b) 24 25 withLanguagePluginEnabled("fake_proto", true)(t, a) 26 withLanguagePluginEnabled("fake_proto", false)(t, b) 27 28 withNamedRuleEnabled("fake_proto_library", true)(t, a) 29 withNamedRuleEnabled("fake_proto_library", false)(t, b) 30 } 31 32 func TestLanguageDirectives(t *testing.T) { 33 testDirectives(t, map[string]packageConfigTestCase{ 34 // "proto_language enabled": { 35 // directives: withDirectives("proto_language", "fake enabled true"), 36 // check: withLanguage("fake", withLanguageEnabled(true)), 37 // }, 38 // "proto_language disabled": { 39 // directives: withDirectives("proto_language", "fake enabled false"), 40 // check: withLanguage("fake", withLanguageEnabled(false)), 41 // }, 42 // "proto_language plugin": { 43 // directives: withDirectives( 44 // "proto_plugin", "fake_proto implementation builtin:fake", 45 // "proto_language", "fake plugin fake_proto", 46 // ), 47 // check: withLanguage("fake", 48 // withLanguageEnabled(true), 49 // withLanguagePluginEnabled("fake_proto", true), 50 // ), 51 // }, 52 // "proto_language -plugin": { 53 // directives: withDirectives( 54 // "proto_plugin", "fake_proto implementation builtin:fake", 55 // "proto_language", "fake +plugin fake_proto", 56 // "proto_language", "fake -plugin fake_proto", 57 // ), 58 // check: withLanguage("fake", 59 // withLanguageEnabled(true), 60 // withLanguagePluginEnabled("fake_proto", false), 61 // ), 62 // }, 63 // "proto_language rule": { 64 // directives: withDirectives( 65 // "proto_rule", "fake_proto_library enabled true", 66 // "proto_language", "fake rule fake_proto_library", 67 // ), 68 // check: withLanguage("fake", 69 // withLanguageEnabled(true), 70 // withNamedRuleEnabled("fake_proto_library", true), 71 // ), 72 // }, 73 // "proto_language -rule": { 74 // directives: withDirectives( 75 // "proto_rule", "fake_proto_library enabled true", 76 // "proto_language", "fake +rule fake_proto_library", 77 // "proto_language", "fake -rule fake_proto_library", 78 // ), 79 // check: withLanguage("fake", 80 // withLanguageEnabled(true), 81 // withNamedRuleEnabled("fake_proto_library", false), 82 // ), 83 // }, 84 }) 85 } 86 87 func allLanguageChecks(checks ...languageConfigCheck) languageConfigCheck { 88 return func(t *testing.T, cfg *LanguageConfig) { 89 for _, check := range checks { 90 check(t, cfg) 91 } 92 } 93 } 94 95 func withLanguage(name string, checks ...languageConfigCheck) packageConfigCheck { 96 return func(t *testing.T, cfg *PackageConfig) { 97 lang, ok := cfg.langs[name] 98 if !ok { 99 t.Fatal("lang not found", name) 100 } 101 for _, check := range checks { 102 check(t, lang) 103 } 104 } 105 } 106 107 func withLanguageEnabled(enabled bool) languageConfigCheck { 108 return func(t *testing.T, cfg *LanguageConfig) { 109 if cfg.Enabled != enabled { 110 t.Logf("withLanguageEnabled cfg: %+v", cfg) 111 t.Errorf("lang enabled: want %t, got %t", enabled, cfg.Enabled) 112 } 113 } 114 } 115 116 func withLanguagePluginEnabled(name string, want bool) languageConfigCheck { 117 return func(t *testing.T, cfg *LanguageConfig) { 118 actual, ok := cfg.Plugins[name] 119 if !ok { 120 t.Fatal("plugin not found:", name) 121 } 122 if actual != want { 123 t.Logf("failing lang config: %+v", cfg) 124 t.Errorf("lang plugin enabled: want %t, got %t", want, actual) 125 } 126 } 127 } 128 129 func withNamedRuleEnabled(name string, want bool) languageConfigCheck { 130 return func(t *testing.T, cfg *LanguageConfig) { 131 got, ok := cfg.Rules[name] 132 if !ok { 133 t.Fatal("rule not found:", name) 134 } 135 if got != want { 136 t.Errorf("lang rule enabled: want %t, got %t", want, got) 137 } 138 } 139 }