github.com/bazelbuild/bazel-gazelle@v0.36.1-0.20240520142334-61b277ba6fed/language/bazel/visibility/lang_test.go (about) 1 /* Copyright 2023 The Bazel Authors. All rights reserved. 2 3 Licensed under the Apache License, Version 2.0 (the "License"); 4 you may not use this file except in compliance with the License. 5 You may obtain a copy of the License at 6 7 http://www.apache.org/licenses/LICENSE-2.0 8 9 Unless required by applicable law or agreed to in writing, software 10 distributed under the License is distributed on an "AS IS" BASIS, 11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 See the License for the specific language governing permissions and 13 limitations under the License. 14 */ 15 16 package visibility_test 17 18 import ( 19 "fmt" 20 "testing" 21 22 "github.com/bazelbuild/bazel-gazelle/config" 23 "github.com/bazelbuild/bazel-gazelle/label" 24 "github.com/bazelbuild/bazel-gazelle/language" 25 "github.com/bazelbuild/bazel-gazelle/language/bazel/visibility" 26 "github.com/bazelbuild/bazel-gazelle/rule" 27 ) 28 29 func TestNoopsBecauseILoveCoverage(t *testing.T) { 30 ext := visibility.NewLanguage() 31 32 ext.RegisterFlags(nil /* flagset */, "command", nil /* config */) 33 ext.Resolve(nil /* config */, nil /* RuleIndex */, nil /* RemoteCache */, nil /* Rule */, nil /* imports */, label.New("repo", "pkg", "name")) 34 ext.Fix(nil /* config */, nil /* File */) 35 if ext.CheckFlags(nil /* flagset */, nil /* config */) != nil { 36 t.Fatal("expected nil") 37 } 38 if ext.Imports(nil /* rule */, nil /* rule */, nil /* file */) != nil { 39 t.Fatal("expected nil") 40 } 41 if ext.Embeds(nil /* rule */, label.New("repo", "pkg", "name")) != nil { 42 t.Fatal("expected nil") 43 } 44 if ext.KnownDirectives() == nil { 45 t.Fatal("expected not nil") 46 } 47 if ext.Name() == "" { 48 t.Fatal("expected not empty name") 49 } 50 } 51 52 func Test_NoDirective(t *testing.T) { 53 cfg := config.New() 54 file := rule.EmptyFile("path", "pkg") 55 56 ext := visibility.NewLanguage() 57 ext.Configure(cfg, "rel", file) 58 res := ext.GenerateRules(language.GenerateArgs{ 59 Config: cfg, 60 File: rule.EmptyFile("path/file", "pkg"), 61 }) 62 63 if len(res.Imports) != 0 { 64 t.Fatal("expected empty array") 65 } 66 if len(res.Gen) != 0 { 67 t.Fatal("expected empty array") 68 } 69 } 70 71 func Test_NewDirective(t *testing.T) { 72 testVis := "//src:__subpackages__" 73 cfg := config.New() 74 file, err := rule.LoadData("path", "pkg", []byte(fmt.Sprintf("# gazelle:default_visibility %s", testVis))) 75 if err != nil { 76 t.Fatal("expected nil") 77 } 78 79 ext := visibility.NewLanguage() 80 ext.Configure(cfg, "rel", file) 81 res := ext.GenerateRules(language.GenerateArgs{ 82 Config: cfg, 83 File: rule.EmptyFile("path/file", "pkg"), 84 }) 85 86 if len(res.Gen) != 1 { 87 t.Fatal("expected array of length 1") 88 } 89 if len(res.Imports) != 1 { 90 t.Fatal("expected array of length 1") 91 } 92 if len(res.Gen[0].AttrStrings("default_visibility")) != 1 { 93 t.Fatal("expected array of length 1") 94 } 95 if testVis != res.Gen[0].AttrStrings("default_visibility")[0] { 96 t.Fatal("expected returned visibility to match 'testVis'") 97 } 98 } 99 100 func Test_ReplacementDirective(t *testing.T) { 101 testVis := "//src:__subpackages__" 102 cfg := config.New() 103 file, err := rule.LoadData("path", "pkg", []byte(fmt.Sprintf(` 104 # gazelle:default_visibility %s 105 106 package(default_visibility = "//not-src:__subpackages__") 107 `, testVis))) 108 if err != nil { 109 t.Fatalf("expected not nil - %+v", err) 110 } 111 112 ext := visibility.NewLanguage() 113 ext.Configure(cfg, "rel", file) 114 res := ext.GenerateRules(language.GenerateArgs{ 115 Config: cfg, 116 File: rule.EmptyFile("path/file", "pkg"), 117 }) 118 119 if len(res.Gen) != 1 { 120 t.Fatal("expected array of length 1") 121 } 122 if len(res.Imports) != 1 { 123 t.Fatal("expected array of length 1") 124 } 125 if len(res.Gen[0].AttrStrings("default_visibility")) != 1 { 126 t.Fatal("expected array of length 1") 127 } 128 if testVis != res.Gen[0].AttrStrings("default_visibility")[0] { 129 t.Fatal("expected returned visibility to match '//src:__subpackages__'") 130 } 131 } 132 133 func Test_MultipleDirectives(t *testing.T) { 134 testVis1 := "//src:__subpackages__" 135 testVis2 := "//src2:__subpackages__" 136 cfg := config.New() 137 file, err := rule.LoadData("path", "pkg", []byte(fmt.Sprintf(` 138 # gazelle:default_visibility %s 139 # gazelle:default_visibility %s 140 `, testVis1, testVis2))) 141 if err != nil { 142 t.Fatalf("expected not nil - %+v", err) 143 } 144 145 ext := visibility.NewLanguage() 146 ext.Configure(cfg, "rel", file) 147 res := ext.GenerateRules(language.GenerateArgs{ 148 Config: cfg, 149 File: rule.EmptyFile("path/file", "pkg"), 150 }) 151 152 if len(res.Gen) != 1 { 153 t.Fatal("expected array of length 1") 154 } 155 if len(res.Imports) != 1 { 156 t.Fatal("expected array of length 1") 157 } 158 if len(res.Gen[0].AttrStrings("default_visibility")) != 2 { 159 t.Fatal("expected array of length 2") 160 } 161 if testVis1 != res.Gen[0].AttrStrings("default_visibility")[0] { 162 t.Fatal("expected returned visibility to match '//src:__subpackages__'") 163 } 164 if testVis2 != res.Gen[0].AttrStrings("default_visibility")[1] { 165 t.Fatal("expected returned visibility to match '//src2:__subpackages__'") 166 } 167 } 168 169 func Test_MultipleDefaultsSingleDirective(t *testing.T) { 170 testVis1 := "//src:__subpackages__" 171 testVis2 := "//src2:__subpackages__" 172 cfg := config.New() 173 file, err := rule.LoadData("path", "pkg", []byte(fmt.Sprintf(` 174 # gazelle:default_visibility %s,%s 175 `, testVis1, testVis2))) 176 if err != nil { 177 t.Fatalf("expected not nil - %+v", err) 178 } 179 180 ext := visibility.NewLanguage() 181 ext.Configure(cfg, "rel", file) 182 res := ext.GenerateRules(language.GenerateArgs{ 183 Config: cfg, 184 File: rule.EmptyFile("path/file", "pkg"), 185 }) 186 187 if len(res.Gen) != 1 { 188 t.Fatal("expected array of length 1") 189 } 190 if len(res.Imports) != 1 { 191 t.Fatal("expected array of length 1") 192 } 193 if len(res.Gen[0].AttrStrings("default_visibility")) != 2 { 194 t.Fatal("expected array of length 2") 195 } 196 if testVis1 != res.Gen[0].AttrStrings("default_visibility")[0] { 197 t.Fatal("expected returned visibility to match '//src:__subpackages__'") 198 } 199 if testVis2 != res.Gen[0].AttrStrings("default_visibility")[1] { 200 t.Fatal("expected returned visibility to match '//src2:__subpackages__'") 201 } 202 } 203 204 func Test_NoRuleIfNoBuildFile(t *testing.T) { 205 testVis1 := "//src:__subpackages__" 206 cfg := config.New() 207 file, err := rule.LoadData("path", "pkg", []byte(fmt.Sprintf(` 208 # gazelle:default_visibility %s 209 `, testVis1))) 210 if err != nil { 211 t.Fatalf("expected not nil - %+v", err) 212 } 213 214 ext := visibility.NewLanguage() 215 ext.Configure(cfg, "rel", file) 216 res := ext.GenerateRules(language.GenerateArgs{ 217 Config: cfg, 218 File: nil, 219 }) 220 221 if len(res.Gen) != 0 { 222 t.Fatal("expected array of length 0, no rules generated for missing BUILD.bazel file") 223 } 224 if len(res.Imports) != 0 { 225 t.Fatal("expected array of length 0") 226 } 227 } 228 229 func Test_MultipleDirectivesAcrossFilesSupercede(t *testing.T) { 230 testVis1 := "//src:__subpackages__" 231 testVis2 := "//src2:__subpackages__" 232 file1, err := rule.LoadData("path", "pkg", []byte(fmt.Sprintf(` 233 # gazelle:default_visibility %s 234 `, testVis1))) 235 if err != nil { 236 t.Fatalf("expected not nil - %+v", err) 237 } 238 file2, err := rule.LoadData("path/path", "pkg", []byte(fmt.Sprintf(` 239 # gazelle:default_visibility %s 240 `, testVis2))) 241 if err != nil { 242 t.Fatalf("expected not nil - %+v", err) 243 } 244 245 cfg := config.New() 246 ext := visibility.NewLanguage() 247 ext.Configure(cfg, "path", file1) 248 249 // clone the config as if we were decending through Walk 250 cfg2 := cfg.Clone() 251 ext.Configure(cfg2, "path/path", file2) 252 253 res2 := ext.GenerateRules(language.GenerateArgs{ 254 Config: cfg2, 255 File: rule.EmptyFile("path/path/file", "pkg"), 256 }) 257 258 if len(res2.Gen) != 1 { 259 t.Fatal("expected array of length 1") 260 } 261 if len(res2.Imports) != 1 { 262 t.Fatal("expected array of length 1") 263 } 264 if len(res2.Gen[0].AttrStrings("default_visibility")) != 1 { 265 t.Fatal("expected array of length 1") 266 } 267 if testVis2 != res2.Gen[0].AttrStrings("default_visibility")[0] { 268 t.Fatal("expected returned visibility to match '//src2:__subpackages__'") 269 } 270 271 res1 := ext.GenerateRules(language.GenerateArgs{ 272 Config: cfg, 273 File: rule.EmptyFile("path/file", "pkg"), 274 }) 275 276 if len(res1.Gen) != 1 { 277 t.Fatal("expected array of length 1") 278 } 279 if len(res1.Imports) != 1 { 280 t.Fatal("expected array of length 1") 281 } 282 if len(res1.Gen[0].AttrStrings("default_visibility")) != 1 { 283 t.Fatal("expected array of length 1") 284 } 285 if testVis1 != res1.Gen[0].AttrStrings("default_visibility")[0] { 286 t.Fatal("expected returned visibility to match '//src:__subpackages__'") 287 } 288 }