github.com/GoogleContainerTools/skaffold@v1.39.18/pkg/skaffold/build/ko/dependencies_test.go (about)

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