-
Notifications
You must be signed in to change notification settings - Fork 147
/
watch_test.go
95 lines (82 loc) · 1.7 KB
/
watch_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
package devd
import (
"fmt"
"io/ioutil"
"os"
"sync"
"testing"
"time"
"github.com/cortesi/moddwatch"
"github.com/cortesi/termlog"
)
func addTempFile(t *testing.T, tmpFolder string, fname string, content string) {
if err := ioutil.WriteFile(tmpFolder+"/"+fname, []byte(content), 0644); err != nil {
t.Error(err)
}
}
func TestRouteWatch(t *testing.T) {
logger := termlog.NewLog()
logger.Quiet()
tmpFolder, err := ioutil.TempDir("", "")
if err != nil {
t.Error(err)
}
defer os.RemoveAll(tmpFolder)
// Ensure that using . for the path works:
os.Chdir(tmpFolder)
routes := make(RouteCollection)
routes.Add(".", nil)
changedFiles := make(map[string]int)
ch := make(chan []string, 1024)
var exited sync.WaitGroup
exited.Add(1)
var lck sync.Mutex
go func() {
for {
data, more := <-ch
if more {
for i := range data {
lck.Lock()
fmt.Println(data)
if _, ok := changedFiles[data[i]]; !ok {
changedFiles[data[i]] = 1
}
lck.Unlock()
}
} else {
exited.Done()
return
}
}
}()
watchers := make([]*moddwatch.Watcher, len(routes))
i := 0
for r := range routes {
watcher, err := routes[r].Watch(ch, nil, logger)
watchers[i] = watcher
if err != nil {
t.Error(err)
}
i++
}
addTempFile(t, tmpFolder, "a.txt", "foo\n")
addTempFile(t, tmpFolder, "c.txt", "bar\n")
addTempFile(t, tmpFolder, "another.file.txt", "bar\n")
for i := 0; i < 100; i++ {
lck.Lock()
if len(changedFiles) >= 3 {
lck.Unlock()
break
}
lck.Unlock()
time.Sleep(50 * time.Millisecond)
}
for _, v := range watchers {
v.Stop()
}
close(ch)
exited.Wait()
if len(changedFiles) != 3 {
t.Errorf("wanted 3 changed files, got %d", len(changedFiles))
}
}