forked from direnv/direnv
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfile_times_test.go
97 lines (83 loc) · 2.24 KB
/
file_times_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
package main
import (
"bytes"
"encoding/json"
"testing"
"time"
)
func TestUpdate(t *testing.T) {
times := NewFileTimes()
times.Update("file_times.go")
if len(*times.list) != 1 {
t.Error("Length of updated list not 1")
}
if !(*times.list)[0].Exists {
t.Error("Existing file marked not existing")
}
}
func TestFTJsons(t *testing.T) {
ft := FileTime{"something.txt", time.Now().Unix(), true}
marshalled, err := json.Marshal(ft)
if err != nil {
t.Error("Filetime failed to marshal:", err)
}
if bytes.NewBuffer(marshalled).String() == "{}" {
t.Error(ft, "marshals as empty object")
}
}
func TestRoundTrip(t *testing.T) {
watches := NewFileTimes()
watches.Update("file_times.go")
rt_chk := NewFileTimes()
rt_chk.Unmarshal(watches.Marshal())
compareFTs(t, watches, rt_chk, "length", func(ft FileTimes) interface{} { return len(*ft.list) })
compareFTs(t, watches, rt_chk, "first path", func(ft FileTimes) interface{} { return (*ft.list)[0].Path })
}
func compareFTs(t *testing.T, left, right FileTimes, desc string, compare func(ft FileTimes) (res interface{})) {
lc, rc := compare(left), compare(right)
if lc != rc {
t.Error("Filetimes didn't round trip.",
"Original", desc, "was:", lc,
"RT", desc, "was:", rc)
}
}
func TestCanonicalAdds(t *testing.T) {
fts := NewFileTimes()
fts.NewTime("docs/../file_times.go", 0, true)
fts.NewTime("file_times.go", 0, true)
if len(*fts.list) > 1 {
t.Error("Double add of the same file")
}
}
func TestCheckPasses(t *testing.T) {
fts := NewFileTimes()
fts.Update("file_times.go")
err := fts.Check()
if err != nil {
t.Error("Check that should pass fails with:", err)
}
}
func TestCheckStale(t *testing.T) {
fts := NewFileTimes()
fts.NewTime("file_times.go", 0, true)
err := fts.Check()
if err == nil {
t.Error("Check that should fail because stale passes")
}
}
func TestCheckAppeared(t *testing.T) {
fts := NewFileTimes()
fts.NewTime("file_times.go", 0, false)
err := fts.Check()
if err == nil {
t.Error("Check that should fail because appeared passes")
}
}
func TestCheckGone(t *testing.T) {
fts := NewFileTimes()
fts.NewTime("nosuchfileevarright.go", time.Now().Unix()+1000, true)
err := fts.Check()
if err == nil {
t.Error("Check that should fail because gone passes")
}
}