github.com/GoogleContainerTools/skaffold@v1.39.18/pkg/skaffold/filemon/monitor_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 "testing" 21 "time" 22 23 "github.com/GoogleContainerTools/skaffold/testutil" 24 ) 25 26 func TestFileMonitor(t *testing.T) { 27 tests := []struct { 28 description string 29 makeChanges func(folder *testutil.TempDir) 30 }{ 31 { 32 description: "file change", 33 makeChanges: func(folder *testutil.TempDir) { 34 folder.Chtimes("file", time.Now().Add(2*time.Second)) 35 }, 36 }, 37 { 38 description: "file delete", 39 makeChanges: func(folder *testutil.TempDir) { 40 folder.Remove("file") 41 }, 42 }, 43 { 44 description: "file create", 45 makeChanges: func(folder *testutil.TempDir) { 46 folder.Touch("new") 47 }, 48 }, 49 } 50 for _, test := range tests { 51 testutil.Run(t, test.description, func(t *testutil.T) { 52 tmpDir := t.NewTempDir().Touch("file") 53 54 monitor := NewMonitor() 55 56 // Register files 57 changed := callback{} 58 err := monitor.Register(tmpDir.List, changed.call) 59 t.CheckNoError(err) 60 t.CheckDeepEqual(0, changed.calls()) 61 62 test.makeChanges(tmpDir) 63 64 // Verify the Monitor detects a change 65 err = monitor.Run(false) 66 t.CheckNoError(err) 67 t.CheckDeepEqual(1, changed.calls()) 68 69 // Verify the Monitor doesn't detect more changes 70 err = monitor.Run(false) 71 t.CheckNoError(err) 72 t.CheckDeepEqual(1, changed.calls()) 73 }) 74 } 75 } 76 77 type callback struct { 78 events []Events 79 } 80 81 func (c *callback) call(e Events) { 82 c.events = append(c.events, e) 83 } 84 85 func (c *callback) calls() int { 86 return len(c.events) 87 }