-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwatcher_test.go
92 lines (67 loc) · 2.01 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
package builder_test
import (
"errors"
"os"
"path/filepath"
"strconv"
"github.com/jtarchie/builder"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"go.uber.org/atomic"
)
var errTest = errors.New("some error")
var _ = Describe("Watcher", func() {
When("a file gets updated", func() {
It("executes the callback with the affected filename", func() {
var foundFilename atomic.String
sourceDir, err := os.MkdirTemp("", "")
Expect(err).NotTo(HaveOccurred())
watcher := builder.NewWatcher(sourceDir)
go func() {
defer GinkgoRecover()
err := watcher.Execute(func(filename string) error {
foundFilename.Store(filename)
return nil
})
Expect(err).NotTo(HaveOccurred())
}()
Consistently(foundFilename.Load).Should(Equal(""))
expectedFilename := filepath.Join(sourceDir, "file")
err = os.WriteFile(expectedFilename, []byte(""), os.ModePerm)
Expect(err).NotTo(HaveOccurred())
Eventually(foundFilename.Load).Should(Equal(expectedFilename))
})
})
When("the source path does not exists", func() {
It("returns an error", func() {
watcher := builder.NewWatcher("asdf")
err := watcher.Execute(func(s string) error {
return nil
})
Expect(err).To(HaveOccurred())
})
})
When("the callback returns an error", func() {
It("stops watching altogether", func() {
var callbackCount atomic.Int32
sourceDir, err := os.MkdirTemp("", "")
Expect(err).NotTo(HaveOccurred())
watcher := builder.NewWatcher(sourceDir)
go func() {
defer GinkgoRecover()
err := watcher.Execute(func(filename string) error {
callbackCount.Add(1)
return errTest
})
Expect(err).To(HaveOccurred())
}()
Consistently(callbackCount.Load).Should(BeEquivalentTo(0))
for i := 0; i < 10; i++ {
expectedFilename := filepath.Join(sourceDir, "file")
err = os.WriteFile(expectedFilename, []byte(strconv.Itoa(i)), os.ModePerm)
Expect(err).NotTo(HaveOccurred())
}
Consistently(callbackCount.Load).Should(BeEquivalentTo(1))
})
})
})