Skip to content

Commit 1fd7ca5

Browse files
nicksndeloof
authored andcommitted
testing: update internal/watch to use the tempdir fixture (docker#862)
1 parent 139edc4 commit 1fd7ca5

File tree

1 file changed

+60
-125
lines changed

1 file changed

+60
-125
lines changed

pkg/watch/notify_test.go

Lines changed: 60 additions & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88
"strings"
99
"testing"
1010
"time"
11+
12+
"github.com/windmilleng/tilt/internal/testutils/tempdir"
1113
)
1214

1315
// Each implementation of the notify interface should have the same basic
@@ -26,12 +28,9 @@ func TestEventOrdering(t *testing.T) {
2628
count := 8
2729
dirs := make([]string, count)
2830
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)
3534
if err != nil {
3635
t.Fatal(err)
3736
}
@@ -58,20 +57,14 @@ func TestWatchesAreRecursive(t *testing.T) {
5857
f := newNotifyFixture(t)
5958
defer f.tearDown()
6059

61-
root, err := f.root.NewDir("root")
62-
if err != nil {
63-
t.Fatal(err)
64-
}
60+
root := f.TempDir("root")
6561

6662
// 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)
7265

7366
// watch parent
74-
err = f.notify.Add(root.Path())
67+
err := f.notify.Add(root)
7568
if err != nil {
7669
t.Fatal(err)
7770
}
@@ -92,24 +85,20 @@ func TestNewDirectoriesAreRecursivelyWatched(t *testing.T) {
9285
f := newNotifyFixture(t)
9386
defer f.tearDown()
9487

95-
root, err := f.root.NewDir("root")
96-
if err != nil {
97-
t.Fatal(err)
98-
}
88+
root := f.TempDir("root")
9989

10090
// watch parent
101-
err = f.notify.Add(root.Path())
91+
err := f.notify.Add(root)
10292
if err != nil {
10393
t.Fatal(err)
10494
}
10595
f.fsync()
10696
f.events = nil
97+
10798
// 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+
113102
// change something inside sub directory
114103
changeFilePath := filepath.Join(subPath, "change")
115104
_, err = os.OpenFile(changeFilePath, os.O_RDONLY|os.O_CREATE, 0666)
@@ -123,48 +112,32 @@ func TestWatchNonExistentPath(t *testing.T) {
123112
f := newNotifyFixture(t)
124113
defer f.tearDown()
125114

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")
130117

131-
path := filepath.Join(root.Path(), "change")
132-
133-
err = f.notify.Add(path)
118+
err := f.notify.Add(path)
134119
if err != nil {
135120
t.Fatal(err)
136121
}
137122

138123
f.fsync()
139124

140-
d1 := []byte("hello\ngo\n")
141-
err = ioutil.WriteFile(path, d1, 0644)
142-
if err != nil {
143-
t.Fatal(err)
144-
}
125+
d1 := "hello\ngo\n"
126+
f.WriteFile(path, d1)
145127
f.assertEvents(path)
146128
}
147129

148130
func TestRemove(t *testing.T) {
149131
f := newNotifyFixture(t)
150132
defer f.tearDown()
151133

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")
156136

157-
path := filepath.Join(root.Path(), "change")
137+
d1 := "hello\ngo\n"
138+
f.WriteFile(path, d1)
158139

159-
if err != nil {
160-
t.Fatal(err)
161-
}
162-
d1 := []byte("hello\ngo\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)
168141
if err != nil {
169142
t.Fatal(err)
170143
}
@@ -181,7 +154,7 @@ func TestRemoveAndAddBack(t *testing.T) {
181154
f := newNotifyFixture(t)
182155
defer f.tearDown()
183156

184-
path := filepath.Join(f.watched.Path(), "change")
157+
path := filepath.Join(f.watched, "change")
185158

186159
d1 := []byte("hello\ngo\n")
187160
err := ioutil.WriteFile(path, d1, 0644)
@@ -214,23 +187,13 @@ func TestSingleFile(t *testing.T) {
214187
f := newNotifyFixture(t)
215188
defer f.tearDown()
216189

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")
221192

222-
path := filepath.Join(root.Path(), "change")
193+
d1 := "hello\ngo\n"
194+
f.WriteFile(path, d1)
223195

224-
if err != nil {
225-
t.Fatal(err)
226-
}
227-
d1 := []byte("hello\ngo\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)
234197
if err != nil {
235198
t.Fatal(err)
236199
}
@@ -248,8 +211,8 @@ func TestWriteBrokenLink(t *testing.T) {
248211
f := newNotifyFixture(t)
249212
defer f.tearDown()
250213

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")
253216
err := os.Symlink(missingFile, link)
254217
if err != nil {
255218
t.Fatal(err)
@@ -262,13 +225,13 @@ func TestWriteGoodLink(t *testing.T) {
262225
f := newNotifyFixture(t)
263226
defer f.tearDown()
264227

265-
goodFile := filepath.Join(f.watched.Path(), "goodFile")
228+
goodFile := filepath.Join(f.watched, "goodFile")
266229
err := ioutil.WriteFile(goodFile, []byte("hello"), 0644)
267230
if err != nil {
268231
t.Fatal(err)
269232
}
270233

271-
link := filepath.Join(f.watched.Path(), "goodFileSymlink")
234+
link := filepath.Join(f.watched, "goodFileSymlink")
272235
err = os.Symlink(goodFile, link)
273236
if err != nil {
274237
t.Fatal(err)
@@ -307,27 +270,17 @@ func TestMoveAndReplace(t *testing.T) {
307270
f := newNotifyFixture(t)
308271
defer f.tearDown()
309272

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")
320276

321-
err = f.notify.Add(file)
277+
err := f.notify.Add(file)
322278
if err != nil {
323279
t.Fatal(err)
324280
}
325281

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")
331284

332285
err = os.Rename(tmpFile, file)
333286
if err != nil {
@@ -338,10 +291,9 @@ func TestMoveAndReplace(t *testing.T) {
338291
}
339292

340293
type notifyFixture struct {
341-
t *testing.T
342-
root *TempDir
343-
watched *TempDir
294+
*tempdir.TempDirFixture
344295
notify Notify
296+
watched string
345297
events []FileEvent
346298
}
347299

@@ -352,59 +304,48 @@ func newNotifyFixture(t *testing.T) *notifyFixture {
352304
t.Fatal(err)
353305
}
354306

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")
359309

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)
366311
if err != nil {
367312
t.Fatal(err)
368313
}
369314
return &notifyFixture{
370-
t: t,
371-
root: root,
372-
watched: watched,
373-
notify: notify,
315+
TempDirFixture: f,
316+
watched: watched,
317+
notify: notify,
374318
}
375319
}
376320

377321
func (f *notifyFixture) assertEvents(expected ...string) {
378322
f.fsync()
379323

380324
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)
382326
}
383327

384328
for i, actual := range f.events {
385329
e := FileEvent{expected[i]}
386330
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)
388332
}
389333
}
390334
}
391335

392336
func (f *notifyFixture) fsync() {
393337
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-")
396340
timeout := time.After(time.Second)
397341

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()))
402343

403344
F:
404345
for {
405346
select {
406347
case err := <-f.notify.Errors():
407-
f.t.Fatal(err)
348+
f.T().Fatal(err)
408349

409350
case event := <-f.notify.Events():
410351
if strings.Contains(event.Path, syncPath) {
@@ -423,24 +364,18 @@ F:
423364
f.events = append(f.events, event)
424365

425366
case <-timeout:
426-
f.t.Fatalf("fsync: timeout")
367+
f.T().Fatalf("fsync: timeout")
427368
}
428369
}
429-
430-
if err != nil {
431-
f.t.Fatal(err)
432-
}
433370
}
434371

435372
func (f *notifyFixture) tearDown() {
436373
SetLimitChecksEnabled(true)
437-
err := f.root.TearDown()
438-
if err != nil {
439-
f.t.Fatal(err)
440-
}
441374

442-
err = f.notify.Close()
375+
err := f.notify.Close()
443376
if err != nil {
444-
f.t.Fatal(err)
377+
f.T().Fatal(err)
445378
}
379+
380+
f.TempDirFixture.TearDown()
446381
}

0 commit comments

Comments
 (0)