github.com/GoogleContainerTools/skaffold@v1.39.18/pkg/skaffold/build/bazel/dependencies_test.go (about) 1 /* 2 Copyright 2019 The Skaffold 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 bazel 18 19 import ( 20 "context" 21 "path/filepath" 22 "testing" 23 24 "github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest" 25 "github.com/GoogleContainerTools/skaffold/pkg/skaffold/util" 26 "github.com/GoogleContainerTools/skaffold/testutil" 27 ) 28 29 func TestGetDependencies(t *testing.T) { 30 tests := []struct { 31 description string 32 workspace string 33 target string 34 files map[string]string 35 expectedQuery string 36 output string 37 expected []string 38 shouldErr bool 39 }{ 40 { 41 description: "with WORKSPACE", 42 workspace: ".", 43 target: "target", 44 files: map[string]string{ 45 "WORKSPACE": "", 46 "BUILD": "", 47 "dep1": "", 48 "dep2": "", 49 }, 50 expectedQuery: "bazel query kind('source file', deps('target')) union buildfiles(deps('target')) --noimplicit_deps --order_output=no --output=label", 51 output: "@ignored\n//:BUILD\n//external/ignored\n\n//:dep1\n//:dep2\n", 52 expected: []string{"BUILD", "dep1", "dep2", "WORKSPACE"}, 53 }, 54 { 55 description: "with parent WORKSPACE", 56 workspace: "./sub/folder", 57 target: "target2", 58 files: map[string]string{ 59 "WORKSPACE": "", 60 "BUILD": "", 61 "sub/folder/BUILD": "", 62 "sub/folder/dep1": "", 63 "sub/folder/dep2": "", 64 "sub/folder/baz/dep3": "", 65 }, 66 expectedQuery: "bazel query kind('source file', deps('target2')) union buildfiles(deps('target2')) --noimplicit_deps --order_output=no --output=label", 67 output: "@ignored\n//:BUILD\n//sub/folder:BUILD\n//external/ignored\n\n//sub/folder:dep1\n//sub/folder:dep2\n//sub/folder/baz:dep3\n", 68 expected: []string{filepath.Join("..", "..", "BUILD"), "BUILD", "dep1", "dep2", filepath.Join("baz", "dep3"), filepath.Join("..", "..", "WORKSPACE")}, 69 }, 70 { 71 description: "without WORKSPACE", 72 workspace: ".", 73 target: "target", 74 shouldErr: true, 75 }, 76 } 77 for _, test := range tests { 78 testutil.Run(t, test.description, func(t *testutil.T) { 79 t.Override(&util.DefaultExecCommand, testutil.CmdRunOut( 80 test.expectedQuery, 81 test.output, 82 )) 83 t.NewTempDir().WriteFiles(test.files).Chdir() 84 85 deps, err := GetDependencies(context.Background(), test.workspace, &latest.BazelArtifact{ 86 BuildTarget: test.target, 87 }) 88 89 t.CheckErrorAndDeepEqual(test.shouldErr, err, test.expected, deps) 90 }) 91 } 92 } 93 94 func TestQuery(t *testing.T) { 95 query := query("//:skaffold_example.tar") 96 97 expectedQuery := `kind('source file', deps('//:skaffold_example.tar')) union buildfiles(deps('//:skaffold_example.tar'))` 98 if query != expectedQuery { 99 t.Errorf("Expected [%s]. Got [%s]", expectedQuery, query) 100 } 101 } 102 103 func TestDepToPath(t *testing.T) { 104 tests := []struct { 105 description string 106 dep string 107 expected string 108 }{ 109 { 110 description: "top level file", 111 dep: "//:dispatcher.go", 112 expected: "dispatcher.go", 113 }, 114 { 115 description: "vendored file", 116 dep: "//vendor/github.com/gorilla/mux:mux.go", 117 expected: "vendor/github.com/gorilla/mux/mux.go", 118 }, 119 } 120 for _, test := range tests { 121 testutil.Run(t, test.description, func(t *testutil.T) { 122 path := depToPath(test.dep) 123 124 t.CheckDeepEqual(test.expected, path) 125 }) 126 } 127 }