github.com/bazelbuild/bazel-gazelle@v0.36.1-0.20240520142334-61b277ba6fed/resolve/resolve_test.go (about) 1 package resolve 2 3 import ( 4 "testing" 5 6 "github.com/bazelbuild/bazel-gazelle/rule" 7 "github.com/bazelbuild/bazel-gazelle/config" 8 "github.com/bazelbuild/bazel-gazelle/label" 9 "github.com/google/go-cmp/cmp" 10 ) 11 12 func TestFindRuleWithOverride_ParentTraversal(t *testing.T) { 13 rootCfg := getConfig(t, "", []rule.Directive{ 14 {Key: "resolve", Value: "go go github.com/root/repo @com_example//root:replacement"}, 15 {Key: "resolve_regexp", Value: "go ^github.com/root/(.*)$ @com_example//regexp:replacement"}, 16 }, nil) 17 18 childCfg := getConfig(t, "child/rel", []rule.Directive{ 19 {Key: "resolve", Value: "go github.com/child/repo //some/local/child:replacement"}, 20 {Key: "resolve_regexp", Value: "go ^github.com/child/(.*)$ relative/child/regexp"}, 21 }, rootCfg) 22 23 secondChildCfg := getConfig(t, "second/child/rel", nil, rootCfg) 24 25 tests := []struct { 26 name string 27 cfg *config.Config 28 importSpec ImportSpec 29 lang string 30 want label.Label 31 wantFound bool 32 }{ 33 { 34 name: "Child exact match", 35 cfg: childCfg, 36 importSpec: ImportSpec{Lang: "go", Imp: "github.com/child/repo"}, 37 lang: "go", 38 want: getTestLabel(t, "//some/local/child:replacement"), 39 wantFound: true, 40 }, 41 { 42 name: "Child regexp match", 43 cfg: childCfg, 44 importSpec: ImportSpec{Lang: "go", Imp: "github.com/child/other"}, 45 lang: "go", 46 want: getTestLabel(t, "//child/rel:relative/child/regexp"), 47 wantFound: true, 48 }, 49 { 50 name: "Root exact match from child", 51 cfg: childCfg, 52 importSpec: ImportSpec{Lang: "go", Imp: "github.com/root/repo"}, 53 lang: "go", 54 want: getTestLabel(t, "@com_example//root:replacement"), 55 wantFound: true, 56 }, 57 { 58 name: "Root regexp match from child", 59 cfg: childCfg, 60 importSpec: ImportSpec{Lang: "go", Imp: "github.com/root/some"}, 61 lang: "go", 62 want: getTestLabel(t, "@com_example//regexp:replacement"), 63 wantFound: true, 64 }, 65 { 66 name: "No match in child or root", 67 cfg: childCfg, 68 importSpec: ImportSpec{Lang: "go", Imp: "github.com/nonexistent/repo"}, 69 lang: "go", 70 want: label.NoLabel, 71 wantFound: false, 72 }, 73 { 74 name: "Second child does not find child directive", 75 cfg: secondChildCfg, 76 importSpec: ImportSpec{Lang: "go", Imp: "github.com/child/repo"}, 77 lang: "go", 78 want: label.NoLabel, 79 wantFound: false, 80 }, 81 { 82 name: "Second child finds root directive", 83 cfg: secondChildCfg, 84 importSpec: ImportSpec{Lang: "go", Imp: "github.com/root/repo"}, 85 lang: "go", 86 want: getTestLabel(t, "@com_example//root:replacement"), 87 wantFound: true, 88 }, 89 } 90 91 for _, tt := range tests { 92 t.Run(tt.name, func(t *testing.T) { 93 got, found := FindRuleWithOverride(tt.cfg, tt.importSpec, tt.lang) 94 if found != tt.wantFound { 95 t.Fatalf("FindRuleWithOverride() found = %v, wantFound %v", found, tt.wantFound) 96 } 97 if diff := cmp.Diff(tt.want, got); diff != "" { 98 t.Errorf("FindRuleWithOverride() mismatch (-want +got):\n%s", diff) 99 } 100 }) 101 } 102 } 103 104 func getConfig(t *testing.T, path string, directives []rule.Directive, parent *config.Config) *config.Config { 105 cfg := &config.Config{ 106 Exts: map[string]interface{}{}, 107 } 108 configurer := &Configurer{} 109 configurer.RegisterFlags(nil, "", cfg) 110 configurer.CheckFlags(nil, cfg) 111 112 if parent != nil { 113 cfg.Exts[resolveName] = parent.Exts[resolveName] 114 } 115 116 configurer.Configure(cfg, path, &rule.File{Directives: directives}) 117 return cfg 118 } 119 120 func getTestLabel(t *testing.T, str string) label.Label { 121 l, err := label.Parse(str) 122 if err != nil { 123 t.Fatal(err) 124 } 125 return l 126 }