-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfswatch_test.go
126 lines (101 loc) · 2.6 KB
/
fswatch_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package fswatch_test
import (
"fmt"
"os"
"path"
"path/filepath"
"sync"
"testing"
"time"
"github.com/dunglas/go-fswatch"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestCreateFile(t *testing.T) {
tmp, err := os.MkdirTemp("", "fswatch")
require.NoError(t, err)
tmp, _ = filepath.EvalSymlinks(tmp)
foo := path.Join(tmp, "foo")
bar := path.Join(tmp, "bar")
var (
wg sync.WaitGroup
i int
)
wg.Add(2)
s, err := fswatch.NewSession(
[]string{tmp},
func(e []fswatch.Event) {
assert.NotEmpty(t, e)
assert.LessOrEqual(t, e[0].Time, time.Now())
path, _ := filepath.EvalSymlinks(e[0].Path)
switch i {
case 0:
assert.Equal(t, tmp, path)
assert.Contains(t, e[0].Types, fswatch.IsDir)
case 1:
assert.Equal(t, foo, path)
assert.Contains(t, e[0].Types, fswatch.Created)
}
i++
wg.Done()
},
fswatch.WithMonitorType(fswatch.SystemDefaultMonitor),
fswatch.WithAllowOverflow(true),
fswatch.WithLatency(1),
fswatch.WithRecursive(true),
fswatch.WithDirectoryOnly(true),
fswatch.WithFollowSymlinks(true),
fswatch.WithEventTypeFilters([]fswatch.EventType{fswatch.Created, fswatch.Updated, fswatch.IsDir, fswatch.IsFile}),
fswatch.WithFilters([]fswatch.Filter{{Text: "bar$", FilterType: fswatch.FilterExclude, CaseSensitive: false, Extended: false}}),
fswatch.WithProperties(map[string]string{"foo": "bar"}),
)
require.NoError(t, err)
wg.Add(1)
go func() {
require.NoError(t, s.Start())
wg.Done()
}()
time.Sleep(5 * time.Second)
fooFile, err := os.Create(foo)
require.NoError(t, err)
require.NoError(t, fooFile.Close())
barFile, err := os.Create(bar)
require.NoError(t, err)
require.NoError(t, barFile.Close())
require.NoError(t, s.Stop())
time.Sleep(3 * time.Second)
require.NoError(t, s.Destroy())
wg.Wait()
time.Sleep(2 * time.Second)
}
func Example() {
s, err := fswatch.NewSession([]string{"/tmp"}, func(e []fswatch.Event) {
fmt.Printf("%s", filepath.Base(e[0].Path))
})
if err != nil {
panic(err)
}
// Start() is blocking, it must be called in a dedicated goroutine
go func() {
if err := s.Start(); err != nil {
panic(err)
}
}()
// Give some time to the monitor to start, this is a limitation of the underlying C library
time.Sleep(5 * time.Second)
f, err := os.Create("/tmp/foo.txt")
if err != nil {
panic(err)
}
defer f.Close()
if err := s.Stop(); err != nil {
panic(err)
}
// Give some time to the monitor to stop, this is a limitation of the underlying C library
time.Sleep(3 * time.Second)
if err := s.Destroy(); err != nil {
panic(err)
}
// Output:
// foo.txt
}