github.com/afking/bazel-gazelle@v0.0.0-20180301150245-c02bc0f529e8/internal/rules/generator_test.go (about) 1 /* Copyright 2016 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 rules_test 17 18 import ( 19 "io/ioutil" 20 "os" 21 "path/filepath" 22 "testing" 23 24 "github.com/bazelbuild/bazel-gazelle/internal/config" 25 "github.com/bazelbuild/bazel-gazelle/internal/label" 26 "github.com/bazelbuild/bazel-gazelle/internal/merger" 27 "github.com/bazelbuild/bazel-gazelle/internal/packages" 28 "github.com/bazelbuild/bazel-gazelle/internal/rules" 29 bf "github.com/bazelbuild/buildtools/build" 30 ) 31 32 func testConfig(repoRoot, goPrefix string) *config.Config { 33 c := &config.Config{ 34 RepoRoot: repoRoot, 35 Dirs: []string{repoRoot}, 36 GoPrefix: goPrefix, 37 GenericTags: config.BuildTags{}, 38 ValidBuildFileNames: []string{"BUILD.old"}, 39 } 40 c.PreprocessTags() 41 return c 42 } 43 44 func packageFromDir(c *config.Config, dir string) (*packages.Package, *bf.File) { 45 var pkg *packages.Package 46 var oldFile *bf.File 47 packages.Walk(c, dir, func(_, rel string, _ *config.Config, p *packages.Package, f *bf.File, _ bool) { 48 if p != nil && p.Dir == dir { 49 pkg = p 50 oldFile = f 51 } 52 }) 53 return pkg, oldFile 54 } 55 56 func TestGenerator(t *testing.T) { 57 repoRoot := filepath.FromSlash("testdata/repo") 58 goPrefix := "example.com/repo" 59 c := testConfig(repoRoot, goPrefix) 60 l := label.NewLabeler(c) 61 62 var dirs []string 63 err := filepath.Walk(repoRoot, func(path string, info os.FileInfo, err error) error { 64 if err != nil { 65 return err 66 } 67 if filepath.Base(path) == "BUILD.want" { 68 dirs = append(dirs, filepath.Dir(path)) 69 } 70 return nil 71 }) 72 if err != nil { 73 t.Fatal(err) 74 } 75 76 for _, dir := range dirs { 77 rel, err := filepath.Rel(repoRoot, dir) 78 if err != nil { 79 t.Fatal(err) 80 } 81 82 pkg, oldFile := packageFromDir(c, dir) 83 g := rules.NewGenerator(c, l, oldFile) 84 rs, _, err := g.GenerateRules(pkg) 85 if err != nil { 86 t.Fatal(err) 87 } 88 f := &bf.File{Stmt: rs} 89 rules.SortLabels(f) 90 f = merger.FixLoads(f) 91 got := string(bf.Format(f)) 92 93 wantPath := filepath.Join(pkg.Dir, "BUILD.want") 94 wantBytes, err := ioutil.ReadFile(wantPath) 95 if err != nil { 96 t.Errorf("error reading %s: %v", wantPath, err) 97 continue 98 } 99 want := string(wantBytes) 100 101 if got != want { 102 t.Errorf("g.Generate(%q, %#v) = %s; want %s", rel, pkg, got, want) 103 } 104 } 105 } 106 107 func TestGeneratorEmpty(t *testing.T) { 108 c := testConfig("", "example.com/repo") 109 l := label.NewLabeler(c) 110 g := rules.NewGenerator(c, l, nil) 111 112 pkg := packages.Package{Name: "foo"} 113 want := `filegroup(name = "go_default_library_protos") 114 115 proto_library(name = "foo_proto") 116 117 go_proto_library(name = "foo_go_proto") 118 119 go_library(name = "go_default_library") 120 121 go_binary(name = "repo") 122 123 go_test(name = "go_default_test") 124 125 go_test(name = "go_default_xtest") 126 ` 127 _, empty, err := g.GenerateRules(&pkg) 128 if err != nil { 129 t.Fatal(err) 130 } 131 emptyStmt := make([]bf.Expr, len(empty)) 132 for i, s := range empty { 133 emptyStmt[i] = s 134 } 135 got := string(bf.Format(&bf.File{Stmt: emptyStmt})) 136 if got != want { 137 t.Errorf("got '%s' ;\nwant %s", got, want) 138 } 139 } 140 141 func TestGeneratorEmptyLegacyProto(t *testing.T) { 142 c := testConfig("", "example.com/repo") 143 c.ProtoMode = config.LegacyProtoMode 144 l := label.NewLabeler(c) 145 g := rules.NewGenerator(c, l, nil) 146 147 pkg := packages.Package{Name: "foo"} 148 _, empty, err := g.GenerateRules(&pkg) 149 if err != nil { 150 t.Fatal(err) 151 } 152 for _, e := range empty { 153 rule := bf.Rule{Call: e.(*bf.CallExpr)} 154 kind := rule.Kind() 155 if kind == "proto_library" || kind == "go_proto_library" || kind == "go_grpc_library" { 156 t.Errorf("deleted rule %s ; should not delete in legacy proto mode", kind) 157 } 158 } 159 }