github.com/stackb/rules_proto@v0.0.0-20240221195024-5428336c51f1/cmd/gazelle/diff_test.go (about) 1 /* Copyright 2017 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 main 17 18 import ( 19 "fmt" 20 "path/filepath" 21 "testing" 22 23 "github.com/bazelbuild/bazel-gazelle/testtools" 24 ) 25 26 func TestDiffExisting(t *testing.T) { 27 files := []testtools.FileSpec{ 28 {Path: "WORKSPACE"}, 29 { 30 Path: "BUILD.bazel", 31 Content: ` 32 # gazelle:prefix example.com/hello 33 `, 34 }, { 35 Path: "hello.go", 36 Content: `package hello`, 37 }, 38 } 39 dir, cleanup := testtools.CreateFiles(t, files) 40 defer cleanup() 41 42 wantError := "encountered changes while running diff" 43 if err := runGazelle(dir, []string{"-mode=diff", "-patch=p"}); err.Error() != wantError { 44 t.Fatalf("got %q; want %q", err, wantError) 45 } 46 47 want := append(files, testtools.FileSpec{ 48 Path: "p", 49 Content: ` 50 --- BUILD.bazel 1970-01-01 00:00:00.000000000 +0000 51 +++ BUILD.bazel 1970-01-01 00:00:00.000000000 +0000 52 @@ -1,3 +1,11 @@ 53 +load("@io_bazel_rules_go//go:def.bzl", "go_library") 54 55 # gazelle:prefix example.com/hello 56 57 +go_library( 58 + name = "hello", 59 + srcs = ["hello.go"], 60 + importpath = "example.com/hello", 61 + visibility = ["//visibility:public"], 62 +) 63 + 64 `, 65 }) 66 testtools.CheckFiles(t, dir, want) 67 } 68 69 func TestDiffNew(t *testing.T) { 70 files := []testtools.FileSpec{ 71 {Path: "WORKSPACE"}, 72 { 73 Path: "hello.go", 74 Content: `package hello`, 75 }, 76 } 77 dir, cleanup := testtools.CreateFiles(t, files) 78 defer cleanup() 79 80 wantError := "encountered changes while running diff" 81 if err := runGazelle(dir, []string{"-go_prefix=example.com/hello", "-mode=diff", "-patch=p"}); err.Error() != wantError { 82 t.Fatalf("got %q; want %q", err, wantError) 83 } 84 85 want := append(files, testtools.FileSpec{ 86 Path: "p", 87 Content: ` 88 --- /dev/null 1970-01-01 00:00:00.000000000 +0000 89 +++ BUILD.bazel 1970-01-01 00:00:00.000000000 +0000 90 @@ -0,0 +1,9 @@ 91 +load("@io_bazel_rules_go//go:def.bzl", "go_library") 92 + 93 +go_library( 94 + name = "hello", 95 + srcs = ["hello.go"], 96 + importpath = "example.com/hello", 97 + visibility = ["//visibility:public"], 98 +) 99 + 100 `, 101 }) 102 testtools.CheckFiles(t, dir, want) 103 } 104 105 func TestDiffMissingAndNoChange(t *testing.T) { 106 files := []testtools.FileSpec{ 107 {Path: "WORKSPACE"}, 108 } 109 dir, cleanup := testtools.CreateFiles(t, files) 110 defer cleanup() 111 112 if err := runGazelle(dir, []string{"-go_prefix=example.com/hello", "-mode=diff", "-patch=p"}); err != nil { 113 t.Error("Expected no diff, but got a diff.") 114 } 115 testtools.CheckFiles(t, dir, []testtools.FileSpec{{Path: "p"}}) 116 } 117 118 func TestDiffEmptyAndNoChange(t *testing.T) { 119 files := []testtools.FileSpec{ 120 {Path: "WORKSPACE"}, 121 {Path: "BUILD.bazel"}, 122 } 123 dir, cleanup := testtools.CreateFiles(t, files) 124 defer cleanup() 125 126 if err := runGazelle(dir, []string{"-go_prefix=example.com/hello", "-mode=diff", "-patch=p"}); err != nil { 127 t.Error("Expected no diff, but got a diff.") 128 } 129 testtools.CheckFiles(t, dir, []testtools.FileSpec{{Path: "p"}}) 130 } 131 132 func TestDiffReadWriteDir(t *testing.T) { 133 files := []testtools.FileSpec{ 134 { 135 Path: "repo/hello.go", 136 Content: "package hello", 137 }, { 138 Path: "read/BUILD.bazel", 139 Content: "# gazelle:prefix example.com/hello", 140 }, 141 } 142 dir, cleanup := testtools.CreateFiles(t, files) 143 defer cleanup() 144 145 args := []string{ 146 "-repo_root=repo", 147 "-mode=diff", 148 "-patch=p", 149 "-experimental_read_build_files_dir=read", 150 "-experimental_write_build_files_dir=write", 151 "repo", 152 } 153 154 wantError := "encountered changes while running diff" 155 if err := runGazelle(dir, args); err.Error() != wantError { 156 t.Fatalf("got %q; want %q", err, wantError) 157 } 158 159 wantPatch := fmt.Sprintf(` 160 --- %s 1970-01-01 00:00:00.000000000 +0000 161 +++ %s 1970-01-01 00:00:00.000000000 +0000 162 @@ -1 +1,11 @@ 163 +load("@io_bazel_rules_go//go:def.bzl", "go_library") 164 + 165 # gazelle:prefix example.com/hello 166 + 167 +go_library( 168 + name = "hello", 169 + srcs = ["hello.go"], 170 + importpath = "example.com/hello", 171 + visibility = ["//visibility:public"], 172 +) 173 + 174 `, 175 filepath.Join(dir, "read", "BUILD.bazel"), 176 filepath.Join(dir, "write", "BUILD.bazel")) 177 want := append(files, testtools.FileSpec{Path: "p", Content: wantPatch}) 178 testtools.CheckFiles(t, dir, want) 179 }