github.com/GoogleContainerTools/skaffold@v1.39.18/pkg/skaffold/build/list/list_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 list
    18  
    19  import (
    20  	"path/filepath"
    21  	"testing"
    22  
    23  	"github.com/google/go-cmp/cmp/cmpopts"
    24  
    25  	"github.com/GoogleContainerTools/skaffold/testutil"
    26  )
    27  
    28  func TestFiles(t *testing.T) {
    29  	tmpDir := testutil.NewTempDir(t).
    30  		Touch(
    31  			"bar.yaml",
    32  			"baz.yaml",
    33  			"foo.go",
    34  			"sub1/bar.yaml",
    35  			"sub1/baz.yaml",
    36  			"sub1/foo.go",
    37  			"sub1/sub2/bar.yaml",
    38  			"sub1/sub2/baz.yaml",
    39  			"sub1/sub2/foo.go",
    40  		)
    41  	tests := []struct {
    42  		description   string
    43  		workspace     string
    44  		patterns      []string
    45  		excludes      []string
    46  		shouldErr     bool
    47  		expectedFiles []string
    48  	}{
    49  		{
    50  			description: "watch nothing",
    51  			workspace:   tmpDir.Root(),
    52  		},
    53  		{
    54  			description: "error on no matches",
    55  			patterns:    []string{"this-pattern-does-not-match-any-files"},
    56  			shouldErr:   true,
    57  		},
    58  		{
    59  			description: "include all files using path",
    60  			workspace:   tmpDir.Root(),
    61  			patterns:    []string{"."},
    62  			excludes:    nil,
    63  			expectedFiles: []string{
    64  				"bar.yaml",
    65  				"baz.yaml",
    66  				"foo.go",
    67  				"sub1/bar.yaml",
    68  				"sub1/baz.yaml",
    69  				"sub1/foo.go",
    70  				"sub1/sub2/bar.yaml",
    71  				"sub1/sub2/baz.yaml",
    72  				"sub1/sub2/foo.go",
    73  			},
    74  		},
    75  		{
    76  			description: "include all files using non-globstar wildcard",
    77  			// this test case seems odd, but it is current behavior
    78  			workspace: tmpDir.Root(),
    79  			patterns:  []string{"*"},
    80  			excludes:  nil,
    81  			expectedFiles: []string{
    82  				"bar.yaml",
    83  				"baz.yaml",
    84  				"foo.go",
    85  				"sub1/bar.yaml",
    86  				"sub1/baz.yaml",
    87  				"sub1/foo.go",
    88  				"sub1/sub2/bar.yaml",
    89  				"sub1/sub2/baz.yaml",
    90  				"sub1/sub2/foo.go",
    91  			},
    92  		},
    93  		{
    94  			description: "include all files using globstar",
    95  			workspace:   tmpDir.Root(),
    96  			patterns:    []string{"**"},
    97  			excludes:    nil,
    98  			expectedFiles: []string{
    99  				"bar.yaml",
   100  				"baz.yaml",
   101  				"foo.go",
   102  				"sub1/bar.yaml",
   103  				"sub1/baz.yaml",
   104  				"sub1/foo.go",
   105  				"sub1/sub2/bar.yaml",
   106  				"sub1/sub2/baz.yaml",
   107  				"sub1/sub2/foo.go",
   108  			},
   109  		},
   110  		{
   111  			description: "globstar pattern with file extension matching",
   112  			workspace:   tmpDir.Root(),
   113  			patterns:    []string{"**/*.yaml"},
   114  			excludes:    nil,
   115  			expectedFiles: []string{
   116  				"bar.yaml",
   117  				"baz.yaml",
   118  				"sub1/bar.yaml",
   119  				"sub1/baz.yaml",
   120  				"sub1/sub2/bar.yaml",
   121  				"sub1/sub2/baz.yaml",
   122  			},
   123  		},
   124  		{
   125  			description: "non-globstar wildcard pattern with file extension match does not recurse subdirectories",
   126  			workspace:   tmpDir.Root(),
   127  			patterns:    []string{"*/*.go"},
   128  			excludes:    nil,
   129  			expectedFiles: []string{
   130  				"sub1/foo.go",
   131  			},
   132  		},
   133  		{
   134  			description: "globstar pattern recurses multiple levels of subdirectories",
   135  			workspace:   tmpDir.Root(),
   136  			patterns:    []string{"**/*.go"},
   137  			excludes:    nil,
   138  			expectedFiles: []string{
   139  				"foo.go",
   140  				"sub1/foo.go",
   141  				"sub1/sub2/foo.go",
   142  			},
   143  		},
   144  		{
   145  			description: "globstar excludes recurses multiple levels of subdirectories",
   146  			workspace:   tmpDir.Root(),
   147  			patterns:    []string{"**/*.yaml"},
   148  			excludes:    []string{"**/baz.yaml"},
   149  			expectedFiles: []string{
   150  				"bar.yaml",
   151  				"sub1/bar.yaml",
   152  				"sub1/sub2/bar.yaml",
   153  			},
   154  		},
   155  		{
   156  			description: "include and exclude all",
   157  			workspace:   tmpDir.Root(),
   158  			patterns:    []string{"**"},
   159  			excludes:    []string{"**"},
   160  		},
   161  		{
   162  			description: "include and exclude all with globstar and file extension matching",
   163  			workspace:   tmpDir.Root(),
   164  			patterns:    []string{"**/*.go"},
   165  			excludes:    []string{"**/*.go"},
   166  		},
   167  		{
   168  			description: "avoid duplicates for overlapping patterns",
   169  			workspace:   tmpDir.Root(),
   170  			patterns: []string{
   171  				"**/*.go",
   172  				"*.go",
   173  				"*/*.go",
   174  				"*/*/*.go",
   175  				"sub1/*.go",
   176  				"sub1/sub2/*.go",
   177  			},
   178  			excludes: nil,
   179  			expectedFiles: []string{
   180  				"foo.go",
   181  				"sub1/foo.go",
   182  				"sub1/sub2/foo.go",
   183  			},
   184  		},
   185  		{
   186  			description: "workspace is relative path",
   187  			workspace:   ".",
   188  			patterns: []string{
   189  				".",
   190  			},
   191  			excludes: nil,
   192  			expectedFiles: []string{
   193  				"list.go",
   194  				"list_test.go",
   195  			},
   196  		},
   197  	}
   198  	for _, test := range tests {
   199  		testutil.Run(t, test.description, func(t *testutil.T) {
   200  			files, err := Files(test.workspace, test.patterns, test.excludes)
   201  			t.CheckError(test.shouldErr, err)
   202  			t.CheckDeepEqual(test.expectedFiles, files,
   203  				cmpopts.AcyclicTransformer("separator", filepath.FromSlash),
   204  			)
   205  		})
   206  	}
   207  }