-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils_test.go
159 lines (123 loc) · 3.74 KB
/
utils_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
package main
import (
"io/ioutil"
"os"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestUniqueStrings(t *testing.T) {
assert := assert.New(t)
us := NewUniqueStrings()
assert.Len(us.Seen, 0)
us.Add("b")
us.Add("a")
assert.Len(us.Seen, 2)
us.Add("b")
us.Add("a")
assert.Len(us.Seen, 2)
// Test twice
assert.Equal([]string{"a", "b"}, us.Strings())
assert.Equal([]string{"a", "b"}, us.Strings())
}
func TestFindFirstFile(t *testing.T) {
assert := assert.New(t)
assert.Equal("utils_test.go", FirstFileFound("utils_test.go", "not-here"))
assert.Equal("utils_test.go", FirstFileFound("not-here", "utils_test.go"))
assert.Equal("utils_test.go", FirstFileFound("not-here", "utils_test.go", "not-here"))
assert.Equal("", FirstFileFound())
assert.Equal("", FirstFileFound(""))
assert.Equal("", FirstFileFound("."))
assert.Equal("", FirstFileFound(".."))
}
func TestTimeChecks(t *testing.T) {
assert := assert.New(t)
var ti time.Time
var e error
// Some super simple testing
ti, e = MinTime([]string{})
assert.Equal(time.Time{}, ti)
assert.NoError(e)
ti, e = MaxTime([]string{})
assert.Equal(time.Time{}, ti)
assert.NoError(e)
// Fail on missing file
ti, e = MinTime([]string{"/nothing/to/read"})
assert.Equal(time.Time{}, ti)
assert.Error(e)
ti, e = MaxTime([]string{"/nothing/to/read"})
assert.Equal(time.Time{}, ti)
assert.Error(e)
// Need a test file and it's time stamp
tmp, e := ioutil.TempFile("", "dmktest")
assert.Nil(e)
assert.NotNil(tmp)
defer os.Remove(tmp.Name())
if _, we := tmp.Write([]byte("yadda")); we != nil {
assert.Fail(we.Error())
}
tmp.Close()
st, e := os.Stat(tmp.Name())
pcheck(e)
expect := st.ModTime()
// Single file testing
ti, e = MinTime([]string{tmp.Name()})
assert.Equal(expect, ti)
assert.NoError(e)
ti, e = MaxTime([]string{tmp.Name()})
assert.Equal(expect, ti)
assert.NoError(e)
// Multi file testing
ti, e = MinTime([]string{tmp.Name(), tmp.Name(), tmp.Name()})
assert.Equal(expect, ti)
assert.NoError(e)
ti, e = MaxTime([]string{tmp.Name(), tmp.Name(), tmp.Name()})
assert.Equal(expect, ti)
assert.NoError(e)
ti, e = MinTime([]string{tmp.Name(), tmp.Name(), "/dev/null"})
assert.NotNil(ti) // No equal check
assert.NoError(e)
ti, e = MaxTime([]string{"/dev/null", tmp.Name(), tmp.Name()})
assert.Equal(expect, ti)
assert.NoError(e)
// Multi file with error
ti, e = MinTime([]string{tmp.Name(), tmp.Name(), "/nothing/to/read"})
assert.Equal(time.Time{}, ti)
assert.Error(e)
ti, e = MaxTime([]string{tmp.Name(), tmp.Name(), "/nothing/to/read"})
assert.Equal(time.Time{}, ti)
assert.Error(e)
}
func TestMultiGlob(t *testing.T) {
assert := assert.New(t)
var found []string
var err error
found, err = MultiGlob([]string{})
assert.Equal([]string{}, found)
assert.NoError(err)
found, err = MultiGlob([]string{""})
assert.Equal([]string{}, found)
assert.NoError(err)
found, err = MultiGlob([]string{"", " ", "a", "", "\t"})
assert.Equal([]string{"a"}, found)
assert.NoError(err)
found, err = MultiGlob([]string{"a"})
assert.Equal([]string{"a"}, found)
assert.NoError(err)
found, err = MultiGlob([]string{"a", "b"})
assert.Equal([]string{"a", "b"}, found)
assert.NoError(err)
found, err = MultiGlob([]string{"util*.go"})
assert.Equal([]string{"utils.go", "utils_test.go"}, found)
assert.NoError(err)
found, err = MultiGlob([]string{"a", "util*.go", "b"})
assert.Equal([]string{"a", "b", "utils.go", "utils_test.go"}, found)
assert.NoError(err)
found, err = MultiGlob([]string{"utils.go", "util*.go"})
assert.Equal([]string{"utils.go", "utils_test.go"}, found)
assert.NoError(err)
// Final test - make sure we correctly handle bad patterns
found, err = MultiGlob([]string{"a", "util*.go", "[]a]"})
assert.Equal([]string{}, found)
assert.Error(err)
}