|
| 1 | +/* |
| 2 | +Copyright 2021 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 ko |
| 18 | + |
| 19 | +// TODO(halvards)[09/17/2021]: Replace the latestV1 import path with the |
| 20 | +// real schema import path once the contents of ./schema has been added to |
| 21 | +// the real schema in pkg/skaffold/schema/latest/v1. |
| 22 | +import ( |
| 23 | + "context" |
| 24 | + "path/filepath" |
| 25 | + "testing" |
| 26 | + |
| 27 | + "github.com/google/go-cmp/cmp/cmpopts" |
| 28 | + |
| 29 | + // latestV1 "github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest/v1" |
| 30 | + latestV1 "github.com/GoogleContainerTools/skaffold/pkg/skaffold/build/ko/schema" |
| 31 | + "github.com/GoogleContainerTools/skaffold/testutil" |
| 32 | +) |
| 33 | + |
| 34 | +func TestGetDependencies(t *testing.T) { |
| 35 | + allFiles := []string{ |
| 36 | + ".ko.yaml", |
| 37 | + "cmd/foo/root.go", |
| 38 | + "go.mod", |
| 39 | + "main.go", |
| 40 | + "pkg/bar/bar.go", |
| 41 | + } |
| 42 | + tmpDir := testutil.NewTempDir(t).Touch(allFiles...) |
| 43 | + |
| 44 | + tests := []struct { |
| 45 | + description string |
| 46 | + paths []string |
| 47 | + ignore []string |
| 48 | + expected []string |
| 49 | + shouldErr bool |
| 50 | + }{ |
| 51 | + { |
| 52 | + description: "default is to watch everything", |
| 53 | + expected: allFiles, |
| 54 | + }, |
| 55 | + { |
| 56 | + description: "ignore everything with nil paths", |
| 57 | + ignore: []string{"."}, |
| 58 | + }, |
| 59 | + { |
| 60 | + description: "ignore everything", |
| 61 | + paths: []string{"."}, |
| 62 | + ignore: []string{"."}, |
| 63 | + }, |
| 64 | + { |
| 65 | + description: "watch everything with empty string path", |
| 66 | + paths: []string{""}, |
| 67 | + expected: allFiles, |
| 68 | + }, |
| 69 | + { |
| 70 | + description: "watch everything star", |
| 71 | + paths: []string{"*"}, |
| 72 | + expected: allFiles, |
| 73 | + }, |
| 74 | + { |
| 75 | + description: "watch everything globstar", |
| 76 | + paths: []string{"**"}, |
| 77 | + expected: allFiles, |
| 78 | + }, |
| 79 | + { |
| 80 | + description: "ignore a directory", |
| 81 | + paths: []string{"."}, |
| 82 | + ignore: []string{"cmd"}, |
| 83 | + expected: []string{ |
| 84 | + ".ko.yaml", |
| 85 | + "go.mod", |
| 86 | + "main.go", |
| 87 | + "pkg/bar/bar.go", |
| 88 | + }, |
| 89 | + }, |
| 90 | + // { |
| 91 | + // // TODO(halvards)[09/17/2021]: Uncomment this test case after #6605 has been merged. |
| 92 | + // description: "globstar with file extension matching", |
| 93 | + // paths: []string{"**/*.go"}, |
| 94 | + // expected: []string{ |
| 95 | + // "main.go", |
| 96 | + // "cmd/foo/root.go", |
| 97 | + // "pkg/bar/bar.go", |
| 98 | + // }, |
| 99 | + // }, |
| 100 | + { |
| 101 | + description: "error", |
| 102 | + paths: []string{"unknown"}, |
| 103 | + shouldErr: true, |
| 104 | + }, |
| 105 | + } |
| 106 | + for _, test := range tests { |
| 107 | + testutil.Run(t, test.description, func(t *testutil.T) { |
| 108 | + deps, err := GetDependencies(context.Background(), tmpDir.Root(), &latestV1.KoArtifact{ |
| 109 | + Dependencies: &latestV1.KoDependencies{ |
| 110 | + Paths: test.paths, |
| 111 | + Ignore: test.ignore, |
| 112 | + }, |
| 113 | + }) |
| 114 | + t.CheckError(test.shouldErr, err) |
| 115 | + t.CheckDeepEqual(test.expected, deps, |
| 116 | + cmpopts.AcyclicTransformer("separator", filepath.FromSlash), |
| 117 | + ) |
| 118 | + }) |
| 119 | + } |
| 120 | +} |
0 commit comments