-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwatchfile_test.go
168 lines (143 loc) · 3.92 KB
/
watchfile_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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package args_test
import (
"fmt"
"time"
"io/ioutil"
"os"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/pkg/errors"
"github.com/thrawn01/args"
)
func saveFile(fileName string, content []byte) error {
err := ioutil.WriteFile(fileName, content, os.ModePerm)
if err != nil {
return errors.Wrapf(err, "Failed to write '%s'", fileName)
}
return nil
}
var _ = Describe("args.WatchFile()", func() {
var iniFile *os.File
var log *TestLogger
iniVersion1 := []byte(`
value=my-value
version=1
`)
iniVersion2 := []byte(`
value=new-value
version=2
`)
BeforeEach(func() {
log = NewTestLogger()
})
It("Should reload a config file when watched file is modified", func() {
parser := args.NewParser()
parser.SetLog(log)
parser.AddConfig("value")
parser.AddConfig("version").IsInt()
opt, err := parser.Parse(nil)
Expect(err).To(BeNil())
Expect(log.GetEntry()).To(Equal(""))
Expect(opt.String("value")).To(Equal(""))
Expect(opt.Int("version")).To(Equal(0))
iniFile, err = ioutil.TempFile("/tmp", "args-test")
if err != nil {
Fail(err.Error())
}
defer os.Remove(iniFile.Name())
// Write version 1 of the ini file
if err := saveFile(iniFile.Name(), iniVersion1); err != nil {
Fail(err.Error())
}
// Load the INI file
content, err := args.LoadFile(iniFile.Name())
if err != nil {
Fail(err.Error())
}
// Parse the ini file
opt, err = parser.FromINI(content)
Expect(err).To(BeNil())
Expect(log.GetEntry()).To(Equal(""))
Expect(opt.String("value")).To(Equal("my-value"))
Expect(opt.Int("version")).To(Equal(1))
done := make(chan struct{})
cancelWatch, err := args.WatchFile(iniFile.Name(), time.Second, func(err error) {
parser.FromINIFile(iniFile.Name())
// Tell the test to continue, Change event was handled
close(done)
})
if err != nil {
Fail(err.Error())
}
if err := saveFile(iniFile.Name(), iniVersion2); err != nil {
Fail(err.Error())
}
// Wait until the new file was loaded
<-done
// Stop the watch
cancelWatch()
// Get the updated options
opts := parser.GetOpts()
Expect(log.GetEntry()).To(Equal(""))
Expect(opts.String("value")).To(Equal("new-value"))
Expect(opts.Int("version")).To(Equal(2))
})
It("Should signal a modification if the file is deleted and re-created", func() {
testFile, err := ioutil.TempFile("/tmp", "args-test")
if err != nil {
Fail(err.Error())
}
testFile.Close()
var watchErr error
done := make(chan struct{})
cancelWatch, err := args.WatchFile(testFile.Name(), time.Second, func(err error) {
if err != nil {
fmt.Printf("Watch Error %s\n", err.Error())
}
// Tell the test to continue, Change event was handled
close(done)
})
if err != nil {
Fail(err.Error())
}
// Quickly Remove the file and replace it
os.Remove(testFile.Name())
testFile, err = os.OpenFile(testFile.Name(), os.O_RDWR|os.O_CREATE|os.O_EXCL, 0600)
testFile.Close()
defer os.Remove(testFile.Name())
// Wait until the new file was loaded
<-done
Expect(watchErr).To(BeNil())
// Stop the watch
cancelWatch()
})
// Apparently VIM does this to files on OSX
It("Should signal a modification if the file is renamed and renamed back", func() {
testFile, err := ioutil.TempFile("/tmp", "args-test")
if err != nil {
Fail(err.Error())
}
testFile.Close()
var watchErr error
done := make(chan struct{})
cancelWatch, err := args.WatchFile(testFile.Name(), time.Second, func(err error) {
if err != nil {
fmt.Printf("Watch Error %s\n", err.Error())
}
// Tell the test to continue, Change event was handled
close(done)
})
if err != nil {
Fail(err.Error())
}
// Quickly Remove the file and replace it
os.Rename(testFile.Name(), testFile.Name()+"-new")
os.Rename(testFile.Name()+"-new", testFile.Name())
defer os.Remove(testFile.Name())
// Wait until the new file was loaded
<-done
Expect(watchErr).To(BeNil())
// Stop the watch
cancelWatch()
})
})