-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_test.go
38 lines (31 loc) · 957 Bytes
/
file_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
package scli
import (
"os"
"testing"
"github.com/stretchr/testify/assert"
)
type testConfig struct {
Foo string
}
func TestFile(t *testing.T) {
const confFile = "config.json"
var a File[testConfig, DisableLiveUpdate]
os.WriteFile(confFile, []byte(`{"foo": "bar"}`), 0o640)
a.FromString(confFile)
assert.Equal(t, &testConfig{Foo: "bar"}, a.Get())
}
func TestFileLiveUpdate(t *testing.T) {
const confFile = "config.json"
var a File[testConfig, EnableLiveUpdate]
// initial content
os.WriteFile(confFile, []byte(`{"foo": "bar"}`), 0o640)
a.FromString(confFile)
oldPtr := a.Get()
assert.Equal(t, &testConfig{Foo: "bar"}, oldPtr, "test init content")
os.WriteFile(confFile, []byte(`{"foo": "baz"}`), 0o640)
<-a.UpdateEvents() // wait update done
// test new value
assert.Equal(t, &testConfig{Foo: "baz"}, a.Get(), "test new value")
// verify old value still valid
assert.Equal(t, &testConfig{Foo: "bar"}, oldPtr, "test old value")
}