github.com/afking/bazel-gazelle@v0.0.0-20180301150245-c02bc0f529e8/internal/repos/repo_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 repos 17 18 import ( 19 "io/ioutil" 20 "os" 21 "path/filepath" 22 "reflect" 23 "testing" 24 25 bf "github.com/bazelbuild/buildtools/build" 26 ) 27 28 func TestGenerateRepoRules(t *testing.T) { 29 repo := Repo{ 30 Name: "org_golang_x_tools", 31 GoPrefix: "golang.org/x/tools", 32 Commit: "123456", 33 } 34 got := bf.FormatString(GenerateRule(repo)) 35 want := `go_repository( 36 name = "org_golang_x_tools", 37 commit = "123456", 38 importpath = "golang.org/x/tools", 39 )` 40 if got != want { 41 t.Errorf("got %s ; want %s", got, want) 42 } 43 } 44 45 func TestFindExternalRepo(t *testing.T) { 46 dir, err := ioutil.TempDir(os.Getenv("TEST_TEMPDIR"), "TestFindExternalRepo") 47 if err != nil { 48 t.Fatal(err) 49 } 50 defer os.RemoveAll(dir) 51 dir, err = filepath.EvalSymlinks(dir) 52 if err != nil { 53 t.Fatal(err) 54 } 55 56 name := "foo" 57 externalPath := filepath.Join(dir, "bazel", "output-base", "external", name) 58 if err := os.MkdirAll(externalPath, 0777); err != nil { 59 t.Fatal(err) 60 } 61 62 bazelOutPath := filepath.Join(dir, "bazel", "output-base", "execroot", "test", "bazel-out") 63 if err := os.MkdirAll(bazelOutPath, 0777); err != nil { 64 t.Fatal(err) 65 } 66 67 workspacePath := filepath.Join(dir, "workspace") 68 if err := os.MkdirAll(workspacePath, 0777); err != nil { 69 t.Fatal(err) 70 } 71 if err := os.Symlink(bazelOutPath, filepath.Join(workspacePath, "bazel-out")); err != nil { 72 t.Fatal(err) 73 } 74 75 if got, err := FindExternalRepo(workspacePath, name); err != nil { 76 t.Fatal(err) 77 } else if got != externalPath { 78 t.Errorf("got %q ; want %q", got, externalPath) 79 } 80 } 81 82 func TestListRepositories(t *testing.T) { 83 for _, tc := range []struct { 84 desc, workspace string 85 want []Repo 86 }{ 87 { 88 desc: "empty", 89 want: nil, 90 }, { 91 desc: "go_repository", 92 workspace: ` 93 go_repository( 94 name = "custom_repo", 95 commit = "123456", 96 remote = "https://example.com/repo", 97 importpath = "example.com/repo", 98 ) 99 `, 100 want: []Repo{{ 101 Name: "custom_repo", 102 GoPrefix: "example.com/repo", 103 Remote: "https://example.com/repo", 104 Commit: "123456", 105 }}, 106 }, 107 } { 108 t.Run(tc.desc, func(t *testing.T) { 109 workspace, err := bf.Parse("WORKSPACE", []byte(tc.workspace)) 110 if err != nil { 111 t.Fatal(err) 112 } 113 if got := ListRepositories(workspace); !reflect.DeepEqual(got, tc.want) { 114 t.Errorf("got %#v ; want %#v", got, tc.want) 115 } 116 }) 117 } 118 }