8
8
"strings"
9
9
"testing"
10
10
"time"
11
+
12
+ "github.com/windmilleng/tilt/internal/testutils/tempdir"
11
13
)
12
14
13
15
// Each implementation of the notify interface should have the same basic
@@ -26,12 +28,9 @@ func TestEventOrdering(t *testing.T) {
26
28
count := 8
27
29
dirs := make ([]string , count )
28
30
for i , _ := range dirs {
29
- dir , err := f .root .NewDir ("watched" )
30
- if err != nil {
31
- t .Fatal (err )
32
- }
33
- dirs [i ] = dir .Path ()
34
- err = f .notify .Add (dir .Path ())
31
+ dir := f .TempDir ("watched" )
32
+ dirs [i ] = dir
33
+ err := f .notify .Add (dir )
35
34
if err != nil {
36
35
t .Fatal (err )
37
36
}
@@ -58,20 +57,14 @@ func TestWatchesAreRecursive(t *testing.T) {
58
57
f := newNotifyFixture (t )
59
58
defer f .tearDown ()
60
59
61
- root , err := f .root .NewDir ("root" )
62
- if err != nil {
63
- t .Fatal (err )
64
- }
60
+ root := f .TempDir ("root" )
65
61
66
62
// add a sub directory
67
- subPath := filepath .Join (root .Path (), "sub" )
68
- err = os .MkdirAll (subPath , os .ModePerm )
69
- if err != nil {
70
- t .Fatal (err )
71
- }
63
+ subPath := filepath .Join (root , "sub" )
64
+ f .MkdirAll (subPath )
72
65
73
66
// watch parent
74
- err = f .notify .Add (root . Path () )
67
+ err : = f .notify .Add (root )
75
68
if err != nil {
76
69
t .Fatal (err )
77
70
}
@@ -92,24 +85,20 @@ func TestNewDirectoriesAreRecursivelyWatched(t *testing.T) {
92
85
f := newNotifyFixture (t )
93
86
defer f .tearDown ()
94
87
95
- root , err := f .root .NewDir ("root" )
96
- if err != nil {
97
- t .Fatal (err )
98
- }
88
+ root := f .TempDir ("root" )
99
89
100
90
// watch parent
101
- err = f .notify .Add (root . Path () )
91
+ err : = f .notify .Add (root )
102
92
if err != nil {
103
93
t .Fatal (err )
104
94
}
105
95
f .fsync ()
106
96
f .events = nil
97
+
107
98
// add a sub directory
108
- subPath := filepath .Join (root .Path (), "sub" )
109
- err = os .MkdirAll (subPath , os .ModePerm )
110
- if err != nil {
111
- f .t .Fatal (err )
112
- }
99
+ subPath := filepath .Join (root , "sub" )
100
+ f .MkdirAll (subPath )
101
+
113
102
// change something inside sub directory
114
103
changeFilePath := filepath .Join (subPath , "change" )
115
104
_ , err = os .OpenFile (changeFilePath , os .O_RDONLY | os .O_CREATE , 0666 )
@@ -123,48 +112,32 @@ func TestWatchNonExistentPath(t *testing.T) {
123
112
f := newNotifyFixture (t )
124
113
defer f .tearDown ()
125
114
126
- root , err := f .root .NewDir ("root" )
127
- if err != nil {
128
- t .Fatal (err )
129
- }
115
+ root := f .TempDir ("root" )
116
+ path := filepath .Join (root , "change" )
130
117
131
- path := filepath .Join (root .Path (), "change" )
132
-
133
- err = f .notify .Add (path )
118
+ err := f .notify .Add (path )
134
119
if err != nil {
135
120
t .Fatal (err )
136
121
}
137
122
138
123
f .fsync ()
139
124
140
- d1 := []byte ("hello\n go\n " )
141
- err = ioutil .WriteFile (path , d1 , 0644 )
142
- if err != nil {
143
- t .Fatal (err )
144
- }
125
+ d1 := "hello\n go\n "
126
+ f .WriteFile (path , d1 )
145
127
f .assertEvents (path )
146
128
}
147
129
148
130
func TestRemove (t * testing.T ) {
149
131
f := newNotifyFixture (t )
150
132
defer f .tearDown ()
151
133
152
- root , err := f .root .NewDir ("root" )
153
- if err != nil {
154
- t .Fatal (err )
155
- }
134
+ root := f .TempDir ("root" )
135
+ path := filepath .Join (root , "change" )
156
136
157
- path := filepath .Join (root .Path (), "change" )
137
+ d1 := "hello\n go\n "
138
+ f .WriteFile (path , d1 )
158
139
159
- if err != nil {
160
- t .Fatal (err )
161
- }
162
- d1 := []byte ("hello\n go\n " )
163
- err = ioutil .WriteFile (path , d1 , 0644 )
164
- if err != nil {
165
- t .Fatal (err )
166
- }
167
- err = f .notify .Add (path )
140
+ err := f .notify .Add (path )
168
141
if err != nil {
169
142
t .Fatal (err )
170
143
}
@@ -181,7 +154,7 @@ func TestRemoveAndAddBack(t *testing.T) {
181
154
f := newNotifyFixture (t )
182
155
defer f .tearDown ()
183
156
184
- path := filepath .Join (f .watched . Path () , "change" )
157
+ path := filepath .Join (f .watched , "change" )
185
158
186
159
d1 := []byte ("hello\n go\n " )
187
160
err := ioutil .WriteFile (path , d1 , 0644 )
@@ -214,23 +187,13 @@ func TestSingleFile(t *testing.T) {
214
187
f := newNotifyFixture (t )
215
188
defer f .tearDown ()
216
189
217
- root , err := f .root .NewDir ("root" )
218
- if err != nil {
219
- t .Fatal (err )
220
- }
190
+ root := f .TempDir ("root" )
191
+ path := filepath .Join (root , "change" )
221
192
222
- path := filepath .Join (root .Path (), "change" )
193
+ d1 := "hello\n go\n "
194
+ f .WriteFile (path , d1 )
223
195
224
- if err != nil {
225
- t .Fatal (err )
226
- }
227
- d1 := []byte ("hello\n go\n " )
228
- err = ioutil .WriteFile (path , d1 , 0644 )
229
- if err != nil {
230
- t .Fatal (err )
231
- }
232
-
233
- err = f .notify .Add (path )
196
+ err := f .notify .Add (path )
234
197
if err != nil {
235
198
t .Fatal (err )
236
199
}
@@ -248,8 +211,8 @@ func TestWriteBrokenLink(t *testing.T) {
248
211
f := newNotifyFixture (t )
249
212
defer f .tearDown ()
250
213
251
- link := filepath .Join (f .watched . Path () , "brokenLink" )
252
- missingFile := filepath .Join (f .watched . Path () , "missingFile" )
214
+ link := filepath .Join (f .watched , "brokenLink" )
215
+ missingFile := filepath .Join (f .watched , "missingFile" )
253
216
err := os .Symlink (missingFile , link )
254
217
if err != nil {
255
218
t .Fatal (err )
@@ -262,13 +225,13 @@ func TestWriteGoodLink(t *testing.T) {
262
225
f := newNotifyFixture (t )
263
226
defer f .tearDown ()
264
227
265
- goodFile := filepath .Join (f .watched . Path () , "goodFile" )
228
+ goodFile := filepath .Join (f .watched , "goodFile" )
266
229
err := ioutil .WriteFile (goodFile , []byte ("hello" ), 0644 )
267
230
if err != nil {
268
231
t .Fatal (err )
269
232
}
270
233
271
- link := filepath .Join (f .watched . Path () , "goodFileSymlink" )
234
+ link := filepath .Join (f .watched , "goodFileSymlink" )
272
235
err = os .Symlink (goodFile , link )
273
236
if err != nil {
274
237
t .Fatal (err )
@@ -307,27 +270,17 @@ func TestMoveAndReplace(t *testing.T) {
307
270
f := newNotifyFixture (t )
308
271
defer f .tearDown ()
309
272
310
- root , err := f .root .NewDir ("root" )
311
- if err != nil {
312
- t .Fatal (err )
313
- }
314
-
315
- file := filepath .Join (root .Path (), "myfile" )
316
- err = ioutil .WriteFile (file , []byte ("hello" ), 0777 )
317
- if err != nil {
318
- t .Fatal (err )
319
- }
273
+ root := f .TempDir ("root" )
274
+ file := filepath .Join (root , "myfile" )
275
+ f .WriteFile (file , "hello" )
320
276
321
- err = f .notify .Add (file )
277
+ err : = f .notify .Add (file )
322
278
if err != nil {
323
279
t .Fatal (err )
324
280
}
325
281
326
- tmpFile := filepath .Join (root .Path (), ".myfile.swp" )
327
- err = ioutil .WriteFile (tmpFile , []byte ("world" ), 0777 )
328
- if err != nil {
329
- t .Fatal (err )
330
- }
282
+ tmpFile := filepath .Join (root , ".myfile.swp" )
283
+ f .WriteFile (tmpFile , "world" )
331
284
332
285
err = os .Rename (tmpFile , file )
333
286
if err != nil {
@@ -338,10 +291,9 @@ func TestMoveAndReplace(t *testing.T) {
338
291
}
339
292
340
293
type notifyFixture struct {
341
- t * testing.T
342
- root * TempDir
343
- watched * TempDir
294
+ * tempdir.TempDirFixture
344
295
notify Notify
296
+ watched string
345
297
events []FileEvent
346
298
}
347
299
@@ -352,59 +304,48 @@ func newNotifyFixture(t *testing.T) *notifyFixture {
352
304
t .Fatal (err )
353
305
}
354
306
355
- root , err := NewDir (t .Name ())
356
- if err != nil {
357
- t .Fatal (err )
358
- }
307
+ f := tempdir .NewTempDirFixture (t )
308
+ watched := f .TempDir ("watched" )
359
309
360
- watched , err := root .NewDir ("watched" )
361
- if err != nil {
362
- t .Fatal (err )
363
- }
364
-
365
- err = notify .Add (watched .Path ())
310
+ err = notify .Add (watched )
366
311
if err != nil {
367
312
t .Fatal (err )
368
313
}
369
314
return & notifyFixture {
370
- t : t ,
371
- root : root ,
372
- watched : watched ,
373
- notify : notify ,
315
+ TempDirFixture : f ,
316
+ watched : watched ,
317
+ notify : notify ,
374
318
}
375
319
}
376
320
377
321
func (f * notifyFixture ) assertEvents (expected ... string ) {
378
322
f .fsync ()
379
323
380
324
if len (f .events ) != len (expected ) {
381
- f .t .Fatalf ("Got %d events (expected %d): %v %v" , len (f .events ), len (expected ), f .events , expected )
325
+ f .T () .Fatalf ("Got %d events (expected %d): %v %v" , len (f .events ), len (expected ), f .events , expected )
382
326
}
383
327
384
328
for i , actual := range f .events {
385
329
e := FileEvent {expected [i ]}
386
330
if actual != e {
387
- f .t .Fatalf ("Got event %v (expected %v)" , actual , e )
331
+ f .T () .Fatalf ("Got event %v (expected %v)" , actual , e )
388
332
}
389
333
}
390
334
}
391
335
392
336
func (f * notifyFixture ) fsync () {
393
337
syncPathBase := fmt .Sprintf ("sync-%d.txt" , time .Now ().UnixNano ())
394
- syncPath := filepath .Join (f .watched . Path () , syncPathBase )
395
- anySyncPath := filepath .Join (f .watched . Path () , "sync-" )
338
+ syncPath := filepath .Join (f .watched , syncPathBase )
339
+ anySyncPath := filepath .Join (f .watched , "sync-" )
396
340
timeout := time .After (time .Second )
397
341
398
- err := ioutil .WriteFile (syncPath , []byte (fmt .Sprintf ("%s" , time .Now ())), os .FileMode (0777 ))
399
- if err != nil {
400
- f .t .Fatal (err )
401
- }
342
+ f .WriteFile (syncPath , fmt .Sprintf ("%s" , time .Now ()))
402
343
403
344
F:
404
345
for {
405
346
select {
406
347
case err := <- f .notify .Errors ():
407
- f .t .Fatal (err )
348
+ f .T () .Fatal (err )
408
349
409
350
case event := <- f .notify .Events ():
410
351
if strings .Contains (event .Path , syncPath ) {
423
364
f .events = append (f .events , event )
424
365
425
366
case <- timeout :
426
- f .t .Fatalf ("fsync: timeout" )
367
+ f .T () .Fatalf ("fsync: timeout" )
427
368
}
428
369
}
429
-
430
- if err != nil {
431
- f .t .Fatal (err )
432
- }
433
370
}
434
371
435
372
func (f * notifyFixture ) tearDown () {
436
373
SetLimitChecksEnabled (true )
437
- err := f .root .TearDown ()
438
- if err != nil {
439
- f .t .Fatal (err )
440
- }
441
374
442
- err = f .notify .Close ()
375
+ err : = f .notify .Close ()
443
376
if err != nil {
444
- f .t .Fatal (err )
377
+ f .T () .Fatal (err )
445
378
}
379
+
380
+ f .TempDirFixture .TearDown ()
446
381
}
0 commit comments