github.com/bazelbuild/bazel-gazelle@v0.36.1-0.20240520142334-61b277ba6fed/internal/runner_test.go (about) 1 /* Copyright 2020 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 bazel_test 17 18 import ( 19 "os" 20 "strings" 21 "testing" 22 23 "github.com/bazelbuild/rules_go/go/tools/bazel_testing" 24 ) 25 26 func TestRunner(t *testing.T) { 27 origBuildData, err := os.ReadFile("BUILD.bazel") 28 if err != nil { 29 t.Fatal(err) 30 } 31 defer func() { 32 if err := os.WriteFile("BUILD.bazel", origBuildData, 0o666); err != nil { 33 t.Fatalf("restoring build file: %v", err) 34 } 35 }() 36 37 if err := bazel_testing.RunBazel("run", "//:gazelle"); err != nil { 38 t.Fatal(err) 39 } 40 out, err := bazel_testing.BazelOutput("query", "//:all") 41 if err != nil { 42 t.Fatal(err) 43 } 44 got := make(map[string]bool) 45 for _, target := range strings.Split(strings.TrimSpace(string(out)), "\n") { 46 got[target] = true 47 } 48 want := []string{"//:m", "//:m_lib"} 49 for _, target := range want { 50 if !got[target] { 51 t.Errorf("target missing from query output: %s", target) 52 } 53 } 54 } 55 56 func TestRunnerUpdateReposFromGoMod(t *testing.T) { 57 origWorkspaceData, err := os.ReadFile("WORKSPACE") 58 if err != nil { 59 t.Fatal(err) 60 } 61 defer func() { 62 if err := os.WriteFile("WORKSPACE", origWorkspaceData, 0o666); err != nil { 63 t.Fatalf("restoring WORKSPACE: %v", err) 64 } 65 }() 66 67 if err := bazel_testing.RunBazel("run", "//:gazelle", "--", "update-repos", "-from_file=go.mod"); err != nil { 68 t.Fatal(err) 69 } 70 } 71 72 func TestRunnerUpdateReposCommand(t *testing.T) { 73 origWorkspaceData, err := os.ReadFile("WORKSPACE") 74 if err != nil { 75 t.Fatal(err) 76 } 77 defer func() { 78 if err := os.WriteFile("WORKSPACE", origWorkspaceData, 0o666); err != nil { 79 t.Fatalf("restoring WORKSPACE: %v", err) 80 } 81 }() 82 83 if err := bazel_testing.RunBazel("run", "//:gazelle-update-repos"); err != nil { 84 t.Fatal(err) 85 } 86 }