-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathwatcher_test.go
99 lines (76 loc) · 2.23 KB
/
watcher_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
package main
import (
"github.com/stretchr/testify/assert"
"github.com/tanin47/git-notes/internal/test_helpers"
"log"
"os"
"testing"
"time"
)
type listener struct {
paths []string
}
func setup() (*GitWatcher, *listener, string, chan string) {
var channel chan string = make(chan string)
var watcher = GitWatcher {
git: &GitCmd{},
running: false,
checkInterval: 10 * time.Millisecond,
delayBeforeFiringEvent: 0,
delayAfterFiringEvent: 1 * time.Second,
}
var path = test_helpers.SetupGitRepo("watcher", false)
var listener listener
go func() {
for {
path = <- channel
listener.paths = append(listener.paths, path)
}
}()
return &watcher, &listener, path, channel
}
func cleanup(watcher *GitWatcher, path string) {
err := os.RemoveAll(path)
if err != nil {
log.Fatalf("Unable to remove %s. Error: %v", path, err)
}
watcher.Stop()
}
func commit(t *testing.T, path string) {
test_helpers.PerformCmd(t, path, "git", "add", "--all")
test_helpers.PerformCmd(t, path, "git", "commit", "-m", "Test")
}
func TestGitWatcher_Watch(t *testing.T) {
var watcher, listener, path, channel = setup()
defer cleanup(watcher, path)
watcher.Watch(path, channel)
assert.Equal(t, 0, len(listener.paths))
test_helpers.WriteFile(t, path, "test.md", "Watch")
time.Sleep(1 * time.Second)
assert.Greater(t, len(listener.paths), 0)
assert.Equal(t, path, listener.paths[0])
}
func TestGitWatcher_CreateAndModify(t *testing.T) {
var watcher, listener, path, channel = setup()
defer cleanup(watcher, path)
watcher.Check(path, channel)
assert.Equal(t, 0, len(listener.paths))
test_helpers.WriteFile(t, path, "test.md", "Hello")
watcher.Check(path, channel)
assert.Equal(t, 1, len(listener.paths))
assert.Equal(t, path, listener.paths[0])
commit(t, path)
watcher.Check(path, channel)
assert.Equal(t, 1, len(listener.paths))
assert.Equal(t, path, listener.paths[0])
test_helpers.WriteFile(t, path, "test.md", "Hello2")
watcher.Check(path, channel)
assert.Equal(t, 2, len(listener.paths))
assert.Equal(t, path, listener.paths[0])
assert.Equal(t, path, listener.paths[1])
commit(t, path)
// No change
test_helpers.WriteFile(t, path, "test.md", "Hello2")
watcher.Check(path, channel)
assert.Equal(t, 2, len(listener.paths))
}