github.com/GoogleContainerTools/skaffold@v1.39.18/pkg/skaffold/filemon/changes_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 filemon
    18  
    19  import (
    20  	"fmt"
    21  	"testing"
    22  	"time"
    23  
    24  	"github.com/GoogleContainerTools/skaffold/testutil"
    25  )
    26  
    27  var (
    28  	yesterday, _ = time.Parse(
    29  		time.RFC3339,
    30  		"1991-11-26T22:08:41+00:00")
    31  	today, _ = time.Parse(
    32  		time.RFC3339,
    33  		"1991-11-27T22:08:41+00:00")
    34  )
    35  
    36  func TestEvents(t *testing.T) {
    37  	tests := []struct {
    38  		description   string
    39  		prev, current FileMap
    40  		expected      Events
    41  	}{
    42  		{
    43  			description: "added, modified, and deleted files",
    44  			prev: map[string]time.Time{
    45  				"a": yesterday,
    46  				"b": yesterday,
    47  			},
    48  			current: map[string]time.Time{
    49  				"a": today,
    50  				"c": today,
    51  			},
    52  			expected: Events{
    53  				Added:    []string{"c"},
    54  				Modified: []string{"a"},
    55  				Deleted:  []string{"b"},
    56  			},
    57  		},
    58  		{
    59  			description: "no changes",
    60  			prev: map[string]time.Time{
    61  				"a": today,
    62  				"b": today,
    63  			},
    64  			current: map[string]time.Time{
    65  				"a": today,
    66  				"b": today,
    67  			},
    68  			expected: Events{},
    69  		},
    70  		{
    71  			description: "added all",
    72  			prev:        map[string]time.Time{},
    73  			current: map[string]time.Time{
    74  				"a": today,
    75  				"b": today,
    76  				"c": today,
    77  			},
    78  			expected: Events{Added: []string{"a", "b", "c"}},
    79  		},
    80  		{
    81  			description: "deleted all",
    82  			prev: map[string]time.Time{
    83  				"a": today,
    84  				"b": today,
    85  				"c": today,
    86  			},
    87  			current:  map[string]time.Time{},
    88  			expected: Events{Deleted: []string{"a", "b", "c"}},
    89  		},
    90  	}
    91  	for _, test := range tests {
    92  		testutil.Run(t, test.description, func(t *testutil.T) {
    93  			t.CheckDeepEqual(test.expected, events(test.prev, test.current))
    94  		})
    95  	}
    96  }
    97  
    98  func TestStat(t *testing.T) {
    99  	testutil.Run(t, "", func(t *testutil.T) {
   100  		tmpDir := t.NewTempDir().
   101  			Write("file", "content")
   102  
   103  		list, _ := tmpDir.List()
   104  		actual, err := Stat(tmpDir.List)
   105  
   106  		t.CheckNoError(err)
   107  		t.CheckDeepEqual(len(list), len(actual))
   108  		for _, f := range list {
   109  			_, present := actual[f]
   110  
   111  			t.CheckTrue(present)
   112  		}
   113  	})
   114  }
   115  
   116  func TestStatNotExist(t *testing.T) {
   117  	tests := []struct {
   118  		description string
   119  		deps        []string
   120  		depsErr     error
   121  		shouldErr   bool
   122  	}{
   123  		{
   124  			description: "no error when deps returns nonexistent file",
   125  			deps:        []string{"file/that/does/not/exist/anymore"},
   126  		},
   127  		{
   128  			description: "deps function error",
   129  			deps:        []string{"file/that/does/not/exist/anymore"},
   130  			depsErr:     fmt.Errorf(""),
   131  			shouldErr:   true,
   132  		},
   133  	}
   134  	for _, test := range tests {
   135  		testutil.Run(t, test.description, func(t *testutil.T) {
   136  			t.NewTempDir().
   137  				Write("file", "content")
   138  
   139  			_, err := Stat(func() ([]string, error) { return test.deps, test.depsErr })
   140  
   141  			t.CheckError(test.shouldErr, err)
   142  		})
   143  	}
   144  }