github.com/wolfd/bazel-gazelle@v0.14.0/internal/language/proto/generate_test.go (about) 1 /* Copyright 2018 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 proto 17 18 import ( 19 "io/ioutil" 20 "path/filepath" 21 "reflect" 22 "strings" 23 "testing" 24 25 "github.com/bazelbuild/bazel-gazelle/internal/config" 26 "github.com/bazelbuild/bazel-gazelle/internal/merger" 27 "github.com/bazelbuild/bazel-gazelle/internal/rule" 28 "github.com/bazelbuild/bazel-gazelle/internal/walk" 29 30 bzl "github.com/bazelbuild/buildtools/build" 31 ) 32 33 func TestGenerateRules(t *testing.T) { 34 lang := New() 35 c := testConfig() 36 37 walk.Walk(c, []config.Configurer{lang}, func(dir, rel string, c *config.Config, update bool, oldFile *rule.File, subdirs, regularFiles, genFiles []string) { 38 isTest := false 39 for _, name := range regularFiles { 40 if name == "BUILD.want" { 41 isTest = true 42 break 43 } 44 } 45 if !isTest { 46 return 47 } 48 t.Run(rel, func(t *testing.T) { 49 empty, gen := lang.GenerateRules(c, dir, rel, oldFile, subdirs, regularFiles, genFiles, nil, nil) 50 if len(empty) > 0 { 51 t.Errorf("got %d empty rules; want 0", len(empty)) 52 } 53 f := rule.EmptyFile("test", "") 54 for _, r := range gen { 55 r.Insert(f) 56 } 57 merger.FixLoads(f, lang.Loads()) 58 f.Sync() 59 got := string(bzl.Format(f.File)) 60 wantPath := filepath.Join(dir, "BUILD.want") 61 wantBytes, err := ioutil.ReadFile(wantPath) 62 if err != nil { 63 t.Fatalf("error reading %s: %v", wantPath, err) 64 } 65 want := string(wantBytes) 66 67 if got != want { 68 t.Errorf("GenerateRules %q: got:\n%s\nwant:\n%s", rel, got, want) 69 } 70 }) 71 }) 72 } 73 74 func TestGenerateRulesEmpty(t *testing.T) { 75 lang := New() 76 c := config.New() 77 c.Exts[protoName] = &ProtoConfig{} 78 79 oldContent := []byte(` 80 proto_library( 81 name = "dead_proto", 82 srcs = ["foo.proto"], 83 ) 84 85 proto_library( 86 name = "live_proto", 87 srcs = ["bar.proto"], 88 ) 89 90 COMPLICATED_SRCS = ["baz.proto"] 91 92 proto_library( 93 name = "complicated_proto", 94 srcs = COMPLICATED_SRCS, 95 ) 96 `) 97 old, err := rule.LoadData("BUILD.bazel", "", oldContent) 98 if err != nil { 99 t.Fatal(err) 100 } 101 genFiles := []string{"bar.proto"} 102 empty, gen := lang.GenerateRules(c, "", "foo", old, nil, nil, genFiles, nil, nil) 103 if len(gen) > 0 { 104 t.Errorf("got %d generated rules; want 0", len(gen)) 105 } 106 f := rule.EmptyFile("test", "") 107 for _, r := range empty { 108 r.Insert(f) 109 } 110 f.Sync() 111 got := strings.TrimSpace(string(bzl.Format(f.File))) 112 want := `proto_library(name = "dead_proto")` 113 if got != want { 114 t.Errorf("got:\n%s\nwant:\n%s", got, want) 115 } 116 } 117 118 func TestGeneratePackage(t *testing.T) { 119 lang := New() 120 c := testConfig() 121 dir := filepath.FromSlash("testdata/protos") 122 _, gen := lang.GenerateRules(c, dir, "protos", nil, nil, []string{"foo.proto"}, nil, nil, nil) 123 r := gen[0] 124 got := r.PrivateAttr(PackageKey).(Package) 125 want := Package{ 126 Name: "bar.foo", 127 Files: map[string]FileInfo{ 128 "foo.proto": { 129 Path: filepath.Join(dir, "foo.proto"), 130 Name: "foo.proto", 131 PackageName: "bar.foo", 132 Options: []Option{{Key: "go_package", Value: "example.com/repo/protos"}}, 133 Imports: []string{ 134 "google/protobuf/any.proto", 135 "protos/sub/sub.proto", 136 }, 137 HasServices: true, 138 }, 139 }, 140 Imports: map[string]bool{ 141 "google/protobuf/any.proto": true, 142 "protos/sub/sub.proto": true, 143 }, 144 Options: map[string]string{ 145 "go_package": "example.com/repo/protos", 146 }, 147 HasServices: true, 148 } 149 if !reflect.DeepEqual(got, want) { 150 t.Errorf("got %#v; want %#v", got, want) 151 } 152 } 153 154 func testConfig() *config.Config { 155 c := config.New() 156 c.RepoRoot = "testdata" 157 c.Dirs = []string{c.RepoRoot} 158 c.ValidBuildFileNames = []string{"BUILD.old"} 159 c.Exts[protoName] = &ProtoConfig{} 160 return c 161 }