sigs.k8s.io/prow@v0.0.0-20240503223140-c5e374dc7eb1/pkg/plugins/buildifier/buildifier_test.go (about) 1 /* 2 Copyright 2017 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package buildifier 18 19 import ( 20 "reflect" 21 "testing" 22 23 "github.com/sirupsen/logrus" 24 25 "sigs.k8s.io/prow/pkg/git/localgit" 26 "sigs.k8s.io/prow/pkg/github" 27 ) 28 29 var initialFiles = map[string][]byte{ 30 "BUILD": []byte(`package(default_visibility = ["//visibility:public"])`), 31 "WORKSPACE": []byte(`workspace(name = "io_foo_bar")`), 32 } 33 34 var pullFiles = map[string][]byte{ 35 "BUILD": []byte(`package(default_visibility = ["//visibility:public"]) 36 37 docker_build( 38 name = "blah", 39 ) 40 `), 41 "WORKSPACE": []byte(`workspace(name = "io_foo_bar") 42 43 foo_repositories() 44 `), 45 "blah.bzl": []byte(`def foo(): 46 print("bar") # should have four spaces 47 `), 48 } 49 50 type ghc struct { 51 genfile []byte 52 pr github.PullRequest 53 changes []github.PullRequestChange 54 oldComments []github.ReviewComment 55 comment github.DraftReview 56 } 57 58 func (g *ghc) GetPullRequestChanges(org, repo string, number int) ([]github.PullRequestChange, error) { 59 return g.changes, nil 60 } 61 62 func (g *ghc) CreateReview(org, repo string, number int, r github.DraftReview) error { 63 g.comment = r 64 return nil 65 } 66 67 func (g *ghc) ListPullRequestComments(org, repo string, number int) ([]github.ReviewComment, error) { 68 return g.oldComments, nil 69 } 70 71 func (g *ghc) GetFile(org, repo, filepath, commit string) ([]byte, error) { 72 return g.genfile, nil 73 } 74 75 func (g *ghc) GetPullRequest(org, repo string, number int) (*github.PullRequest, error) { 76 return &g.pr, nil 77 } 78 79 var e = &github.GenericCommentEvent{ 80 Action: github.GenericCommentActionCreated, 81 IssueState: "open", 82 Body: "/buildify", 83 User: github.User{Login: "mattmoor"}, 84 Number: 42, 85 IsPR: true, 86 Repo: github.Repo{ 87 Owner: github.User{Login: "foo"}, 88 Name: "bar", 89 FullName: "foo/bar", 90 }, 91 } 92 93 func TestBuildifyV2(t *testing.T) { 94 testBuildify(localgit.NewV2, t) 95 } 96 97 func testBuildify(clients localgit.Clients, t *testing.T) { 98 lg, c, err := clients() 99 if err != nil { 100 t.Fatalf("Making localgit: %v", err) 101 } 102 defer func() { 103 if err := lg.Clean(); err != nil { 104 t.Errorf("Cleaning up localgit: %v", err) 105 } 106 if err := c.Clean(); err != nil { 107 t.Errorf("Cleaning up client: %v", err) 108 } 109 }() 110 if err := lg.MakeFakeRepo("foo", "bar"); err != nil { 111 t.Fatalf("Making fake repo: %v", err) 112 } 113 if err := lg.AddCommit("foo", "bar", initialFiles); err != nil { 114 t.Fatalf("Adding initial commit: %v", err) 115 } 116 if err := lg.CheckoutNewBranch("foo", "bar", "pull/42/head"); err != nil { 117 t.Fatalf("Checking out pull branch: %v", err) 118 } 119 if err := lg.AddCommit("foo", "bar", pullFiles); err != nil { 120 t.Fatalf("Adding PR commit: %v", err) 121 } 122 123 gh := &ghc{ 124 changes: []github.PullRequestChange{ 125 { 126 Filename: "BUILD", 127 }, 128 { 129 Filename: "WORKSPACE", 130 }, 131 { 132 Filename: "blah.bzl", 133 }, 134 }, 135 } 136 if err := handle(gh, c, logrus.NewEntry(logrus.New()), e); err != nil { 137 t.Fatalf("Got error from handle: %v", err) 138 } 139 if len(gh.comment.Comments) != 1 { 140 t.Fatalf("Expected one comment, got %d: %v.", len(gh.comment.Comments), gh.comment.Comments) 141 } 142 for _, c := range gh.comment.Comments { 143 pos := c.Position 144 gh.oldComments = append(gh.oldComments, github.ReviewComment{ 145 Path: c.Path, 146 Position: &pos, 147 Body: c.Body, 148 }) 149 } 150 } 151 152 func TestModifiedBazelFilesV2(t *testing.T) { 153 testModifiedBazelFiles(localgit.NewV2, t) 154 } 155 156 func testModifiedBazelFiles(clients localgit.Clients, t *testing.T) { 157 lg, c, err := clients() 158 if err != nil { 159 t.Fatalf("Making localgit: %v", err) 160 } 161 defer func() { 162 if err := lg.Clean(); err != nil { 163 t.Errorf("Cleaning up localgit: %v", err) 164 } 165 if err := c.Clean(); err != nil { 166 t.Errorf("Cleaning up client: %v", err) 167 } 168 }() 169 if err := lg.MakeFakeRepo("foo", "bar"); err != nil { 170 t.Fatalf("Making fake repo: %v", err) 171 } 172 if err := lg.AddCommit("foo", "bar", initialFiles); err != nil { 173 t.Fatalf("Adding initial commit: %v", err) 174 } 175 if err := lg.CheckoutNewBranch("foo", "bar", "pull/42/head"); err != nil { 176 t.Fatalf("Checking out pull branch: %v", err) 177 } 178 if err := lg.AddCommit("foo", "bar", pullFiles); err != nil { 179 t.Fatalf("Adding PR commit: %v", err) 180 } 181 182 var testcases = []struct { 183 name string 184 gh *ghc 185 expectedModifiedFiles map[string]string 186 }{ 187 { 188 name: "modified files include BUILD", 189 gh: &ghc{ 190 changes: []github.PullRequestChange{ 191 { 192 Filename: "BUILD", 193 }, 194 { 195 Filename: "foo.go", 196 }, 197 }, 198 }, 199 expectedModifiedFiles: map[string]string{ 200 "BUILD": "", 201 }, 202 }, 203 { 204 name: "modified files include BUILD.bazel", 205 gh: &ghc{ 206 changes: []github.PullRequestChange{ 207 { 208 Filename: "BUILD.bazel", 209 }, 210 { 211 Filename: "foo.go", 212 }, 213 }, 214 }, 215 expectedModifiedFiles: map[string]string{ 216 "BUILD.bazel": "", 217 }, 218 }, 219 { 220 name: "modified files include WORKSPACE", 221 gh: &ghc{ 222 changes: []github.PullRequestChange{ 223 { 224 Filename: "WORKSPACE", 225 }, 226 { 227 Filename: "foo.md", 228 }, 229 }, 230 }, 231 expectedModifiedFiles: map[string]string{ 232 "WORKSPACE": "", 233 }, 234 }, 235 { 236 name: "modified files include .bzl file", 237 gh: &ghc{ 238 changes: []github.PullRequestChange{ 239 { 240 Filename: "qux.bzl", 241 }, 242 { 243 Filename: "blah.md", 244 }, 245 }, 246 }, 247 expectedModifiedFiles: map[string]string{ 248 "qux.bzl": "", 249 }, 250 }, 251 { 252 name: "modified files include removed BUILD file", 253 gh: &ghc{ 254 changes: []github.PullRequestChange{ 255 { 256 Filename: "qux.go", 257 }, 258 { 259 Filename: "BUILD", 260 Status: github.PullRequestFileRemoved, 261 }, 262 }, 263 }, 264 expectedModifiedFiles: map[string]string{}, 265 }, 266 { 267 name: "modified files include renamed file", 268 gh: &ghc{ 269 changes: []github.PullRequestChange{ 270 { 271 Filename: "qux.go", 272 }, 273 { 274 Filename: "BUILD", 275 Status: github.PullRequestFileRenamed, 276 }, 277 }, 278 }, 279 expectedModifiedFiles: map[string]string{}, 280 }, 281 { 282 name: "added and modified files", 283 gh: &ghc{ 284 changes: []github.PullRequestChange{ 285 { 286 Filename: "foo/BUILD.bazel", 287 Status: github.PullRequestFileAdded, 288 }, 289 { 290 Filename: "bar/blah.bzl", 291 }, 292 }, 293 }, 294 expectedModifiedFiles: map[string]string{ 295 "foo/BUILD.bazel": "", 296 "bar/blah.bzl": "", 297 }, 298 }, 299 } 300 for _, tc := range testcases { 301 actualModifiedFiles, _ := modifiedBazelFiles(tc.gh, "foo", "bar", 9527, "0ebb33b") 302 if !reflect.DeepEqual(tc.expectedModifiedFiles, actualModifiedFiles) { 303 t.Errorf("Expected: %#v, Got %#v in case %s.", tc.expectedModifiedFiles, actualModifiedFiles, tc.name) 304 } 305 } 306 }