-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathjustrun_test.go
333 lines (293 loc) · 8.65 KB
/
justrun_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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
// These will all be very flaky because they are dependent on actual
// filesystems.
package main
import (
"crypto/rand"
"os"
"path/filepath"
"runtime"
"testing"
"time"
)
const waitForMsg = 2 * time.Second
// Slow in the success case
func TestNoWatchingCreation(t *testing.T) {
fs := newFS(t)
fs.Create("foobar")
ch := make(chan event, 10)
cleanUp := watchTest(fs, []string{fs.Abs("foobar")}, []string{}, ch)
defer cleanUp()
fs.Create("baz")
seeNothing(fs, ch, "creation of baz")
}
// Slow in the success case
func TestSimpleWatch(t *testing.T) {
fs := newFS(t)
fs.Create("foobar")
ch := make(chan event, 10)
cleanUp := watchTest(fs, []string{fs.Abs("foobar")}, []string{fs.Abs("baz")}, ch)
defer cleanUp()
fs.ChangeContents("foobar")
seeChangeContents(fs, ch, "foobar")
// GitHub Actions watches will emit two, not one, WRITE event on a new
// directory's creation.
if runtime.GOOS == "linux" && os.Getenv("CI") == "true" {
seeChangeContents(fs, ch, "foobar")
}
fs.Create("baz")
seeNothing(fs, ch, "creation of baz")
}
func TestSimpleDirWatch(t *testing.T) {
fs := newFS(t)
fs.MkdirAll("simpleDir1")
ch := make(chan event, 10)
cleanUp := watchTest(fs, []string{fs.Abs("simpleDir1")}, []string{}, ch)
defer cleanUp()
fs.Create("simpleDir1/foobar")
seeCreation(fs, ch, "simpleDir1/foobar")
}
func TestCurrentDirWorks(t *testing.T) {
fs := newFS(t)
fs.Create("foobar")
ch := make(chan event, 10)
cleanUp := watchTest(fs, []string{fs.Abs(".")}, nil, ch)
defer cleanUp()
fs.ChangeContents("foobar")
seeChangeContents(fs, ch, "foobar")
fs.Create("baz")
seeCreation(fs, ch, "baz")
}
// Slow in the success case
func TestIgnoredDir(t *testing.T) {
fs := newFS(t)
fs.Create("foobar")
fs.MkdirAll("existdir/quuxdir")
ch := make(chan event, 10)
cleanUp := watchTest(fs, []string{fs.Abs(".")},
[]string{
fs.Abs("bardir"),
fs.Abs("existdir"),
},
ch)
defer cleanUp()
fs.MkdirAll("bardir")
fs.Create("bardir/barfile")
fs.RemoveAll("bardir")
fs.Create("existdir/level1")
fs.Create("existdir/quuxdir/level2")
fs.ChangeContents("existdir/level1")
fs.ChangeContents("existdir/quuxdir/level2")
seeNothing(fs, ch, "some ignored operations")
}
// Slow in the success case
func TestIgnoredDirOverlap(t *testing.T) {
fs := newFS(t)
fs.Create("foobar")
fs.MkdirAll("existdir/quuxdir")
ch := make(chan event, 10)
// The existdir and existdir/quuxdir cases seem silly but can
// happen accidentally in shell globbing. Better to be consistent
// about it.
cleanUp := watchTest(fs,
[]string{
fs.Abs("."),
fs.Abs("bardir"),
fs.Abs("existdir/quuxdir"),
},
[]string{
fs.Abs("bardir"),
fs.Abs("existdir/quuxdir"),
},
ch)
defer cleanUp()
fs.MkdirAll("bardir")
fs.Create("bardir/barfile")
fs.Create("existdir/level1")
fs.Create("existdir/quuxdir/level2")
fs.ChangeContents("existdir/level1")
fs.ChangeContents("existdir/quuxdir/level2")
seeNothing(fs, ch, "some ignored operations")
}
func TestNoSubdirRecursionWithoutGlobs(t *testing.T) {
fs := newFS(t)
fs.MkdirAll("some_dir")
ch := make(chan event, 10)
cleanUp := watchTest(fs, []string{fs.Abs(".")}, nil, ch)
defer cleanUp()
fs.Create("some_dir/level1")
seeNothing(fs, ch, "creation in level1")
}
func TestRenameFile(t *testing.T) {
fs := newFS(t)
fs.Create("foobar")
ch := make(chan event, 10)
cleanUp := watchTest(fs, []string{fs.Abs("foobar")}, []string{}, ch)
defer cleanUp()
renameTest(fs, ch, "foobar", "baz")
renameTest(fs, ch, "baz", "foobar")
}
func TestRenameDir(t *testing.T) {
fs := newFS(t)
fs.MkdirAll("foodir")
fs.Create("foodir/foobar")
ch := make(chan event, 10)
cleanUp := watchTest(fs, []string{fs.Abs("foodir/foobar")}, []string{}, ch)
defer cleanUp()
renameTest(fs, ch, "foodir/foobar", "baz")
renameTest(fs, ch, "baz", "foodir/foobar")
renameTest(fs, ch, "foodir", "bardir")
// TODO(jmhodges): this requires a better rename system than
// fsnotify provides or watching every dang directory in the
// entiry absolute path.
// renameTest(fs, ch, "bardir", "foodir")
}
func TestHiddenFilesHiddenByDefault(t *testing.T) {
fs := newFS(t)
fs.MkdirAll("hDir1")
fs.Create("hDir1/.hidden")
fs.MkdirAll("hDir2")
fs.MkdirAll("hDir3")
fs.Create("hDir3/.hiddenAndIgnored")
ch := make(chan event, 10)
cleanUp := watchTest(fs,
[]string{fs.Abs("hDir1/.hidden"), fs.Abs("hDir2")},
[]string{fs.Abs("hDir3/.hiddenAndIgnored")},
ch)
defer cleanUp()
fs.ChangeContents("hDir1/.hidden")
seeChangeContents(fs, ch, "hDir1/.hidden")
// GitHub Actions watches will emit two, not one, WRITE event on a new
// directory's creation.
if runtime.GOOS == "linux" && os.Getenv("CI") == "true" {
seeChangeContents(fs, ch, "hDir1/.hidden")
}
fs.Create("hDir2/.hidden")
seeNothing(fs, ch, "no hidden file creation")
fs.ChangeContents("hDir3/.hiddenAndIgnored")
seeNothing(fs, ch, "no event for changes to hDir3/.hiddenAndIgnored")
}
func renameTest(fs *fileSystem, ch <-chan event, oldpath, newpath string) {
fs.Rename(oldpath, newpath)
seeRename(fs, ch, oldpath, newpath)
}
func seeRename(fs *fileSystem, ch <-chan event, oldpath, newpath string) {
select {
case <-ch:
fs.t.Logf("successful catch of rename ('%s' -> '%s')", oldpath, newpath)
case <-time.After(waitForMsg):
fs.t.Errorf("did not see rename ('%s' -> '%s')", oldpath, newpath)
}
}
func seeNothing(fs *fileSystem, ch <-chan event, msg string) {
select {
case ev := <-ch:
fs.t.Errorf("should not have seen anything but saw %q for %s", ev.Event, msg)
case <-time.After(waitForMsg):
fs.t.Logf("successfully saw nothing for '%s'", msg)
}
}
func seeCreation(fs *fileSystem, ch <-chan event, path string) {
select {
case <-ch:
fs.t.Logf("successful catch of creation of '%s'", path)
case <-time.After(waitForMsg):
fs.t.Errorf("did not see creation of '%s'", path)
}
}
func seeChangeContents(fs *fileSystem, ch <-chan event, path string) {
select {
case ev := <-ch:
fs.t.Logf("successful catch of first event (%q) for changing contents of '%s'", ev.Event, path)
case <-time.After(waitForMsg):
fs.t.Errorf("did not see content change of '%s'", path)
}
select {
case ev := <-ch:
fs.t.Logf("successful catch of second event (%q) for changing contents of '%s'", ev.Event, path)
case <-time.After(waitForMsg):
fs.t.Errorf("did not see content change of '%s'", path)
}
}
func watchTest(fs *fileSystem, inputPaths, ignoredPaths []string, cmdCh chan<- event) func() {
w, err := watch(inputPaths, ignoredPaths, cmdCh)
if err != nil {
fs.t.Fatalf("unable to run watch: %#v", err)
return func() {}
}
return func() {
w.Close()
fs.Close()
}
}
func newFS(t *testing.T) *fileSystem {
name, err := os.MkdirTemp("", "justrun_tests_")
if err != nil {
t.Fatalf("unable to create temporary directory")
}
subdir := filepath.Join(name, "subdir_for_fds")
err = os.MkdirAll(subdir, 0700)
if err != nil {
t.Fatalf("unable to create temporary directory's subdirectory")
}
return &fileSystem{name: subdir, t: t}
}
type fileSystem struct {
name string
t *testing.T
}
func (fs *fileSystem) Create(path string) {
fullName := filepath.Join(fs.name, path)
// O_SYNC is important
f, err := os.OpenFile(fullName, os.O_RDWR|os.O_CREATE|os.O_TRUNC|os.O_SYNC, 0666)
if err != nil {
fs.t.Fatalf("unable to create '%s' in '%s': %#v", path, fs.name, err)
}
f.Close()
}
func (fs *fileSystem) MkdirAll(path string) {
fullName := filepath.Join(fs.name, path)
err := os.MkdirAll(fullName, 0700)
if err != nil {
fs.t.Fatalf("unable to create directory '%s' in '%s': %#v", path, fs.name, err)
}
}
func (fs *fileSystem) Rename(oldpath, newpath string) {
err := os.Rename(filepath.Join(fs.name, oldpath), filepath.Join(fs.name, newpath))
if err != nil {
fs.t.Fatalf("unable to rename '%s' to '%s' in '%s'", oldpath, newpath, fs.name)
}
}
func (fs *fileSystem) Abs(path string) string {
return filepath.Join(fs.name, path)
}
func (fs *fileSystem) RemoveAll(path string) {
if err := os.RemoveAll(path); err != nil {
fs.t.Fatalf("unable to delete '%s': %s", path, err)
}
}
func (fs *fileSystem) ChangeContents(path string) {
fullName := filepath.Join(fs.name, path)
b := make([]byte, 16)
_, err := rand.Read(b)
if err != nil {
fs.t.Fatalf("unable to get random data to change the contents of '%s': %s", path, err)
}
f, err := os.OpenFile(fullName, os.O_WRONLY|os.O_SYNC|os.O_TRUNC, 0666)
if err != nil {
fs.t.Fatalf("unable to open '%s' to change its contents: %s", path, err)
}
defer f.Close()
n, err := f.Write(b)
if err != nil {
fs.t.Fatalf("unable to write new contents to '%s': %s", path, err)
}
if n < len(b) {
fs.t.Fatalf("unable to write all contents to '%s'", path)
}
}
func (fs *fileSystem) Close() {
err := os.RemoveAll(fs.name)
if err != nil {
fs.t.Fatalf("unable to delete directory '%s'", fs.name)
}
}