/
fval_test.go
228 lines (188 loc) · 4.79 KB
/
fval_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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
package fval_test
import (
"io/ioutil"
"os"
"path/filepath"
"runtime"
"testing"
"github.com/walle/fval"
)
func TestFileExists(t *testing.T) {
tmpDir := createTmpDir(t)
defer os.RemoveAll(tmpDir)
path := filepath.Join(tmpDir, "test")
os.Create(path)
if !fval.FileExists(path) {
t.Errorf("Did not indicate that existing file %s exists", path)
}
os.Remove(path)
if fval.FileExists(path) {
t.Errorf("Did indicate that non existing file %s exists", path)
}
os.Mkdir(path, 0766)
if fval.FileExists(path) {
t.Errorf("Did indicate that directory %s is a file", path)
}
}
func TestDirExists(t *testing.T) {
tmpDir := createTmpDir(t)
defer os.RemoveAll(tmpDir)
path := filepath.Join(tmpDir, "test")
os.Mkdir(path, 0766)
if !fval.DirExists(path) {
t.Errorf("Did not indicate that existing directory %s exists", path)
}
os.Remove(path)
if fval.DirExists(path) {
t.Errorf("Did indicate that non existing directory %s exists", path)
}
os.Create(path)
if fval.DirExists(path) {
t.Errorf("Did indicate that file %s is a directory", path)
}
}
func TestFileOrDirExists(t *testing.T) {
tmpDir := createTmpDir(t)
defer os.RemoveAll(tmpDir)
fpath := filepath.Join(tmpDir, "test.txt")
dpath := filepath.Join(tmpDir, "test")
os.Create(fpath)
os.Mkdir(dpath, 0766)
if !fval.Exists(fpath) {
t.Errorf("Did not indicate that existing file %s exists", fpath)
}
if !fval.Exists(dpath) {
t.Errorf("Did not indicate that existing directory %s exists", dpath)
}
os.Remove(fpath)
os.Remove(dpath)
if fval.FileExists(fpath) {
t.Errorf("Did indicate that non existing file %s exists", fpath)
}
if fval.FileExists(dpath) {
t.Errorf("Did indicate that non existing directory %s exists", dpath)
}
}
func TestDirExistsOrCreate(t *testing.T) {
tmpDir := createTmpDir(t)
defer os.RemoveAll(tmpDir)
path := filepath.Join(tmpDir, "test1", "test2", "test3")
ok, err := fval.DirExistsOrCreate(path, 0766)
if err != nil {
t.Errorf("Error creating dir: %s", err)
}
if !ok {
t.Errorf("Directory was not created")
}
ok, err = fval.DirExistsOrCreate(path, 0766)
if err != nil {
t.Errorf("Error: %s", err)
}
if !ok {
t.Errorf("Directory did not exist")
}
os.Remove(path)
if fval.DirExists(path) {
t.Errorf("Directory %s should not exist", path)
}
ok, err = fval.DirExistsOrCreate(path, 0766)
if err != nil {
t.Errorf("Error creating dir: %s", err)
}
if !ok {
t.Errorf("Directory was not created")
}
if !fval.DirExists(path) {
t.Errorf("Directory %s was not created", path)
}
}
func TestDirExistsOrCreateError(t *testing.T) {
tmpDir := createTmpDir(t)
defer os.RemoveAll(tmpDir)
path := filepath.Join(tmpDir, "test")
os.Mkdir(path, 0400)
path = filepath.Join(path, "test2")
ok, err := fval.DirExistsOrCreate(path, 0766)
if ok {
t.Errorf("Directory should not have been created")
}
if err == nil {
t.Errorf("Error should not have been nil")
}
}
func TestDirPurgeAndCreate(t *testing.T) {
tmpDir := createTmpDir(t)
defer os.RemoveAll(tmpDir)
path := filepath.Join(tmpDir, "test")
os.Mkdir(path, 0766)
fpath := filepath.Join(path, "test2")
os.Create(fpath)
if !fval.FileExists(fpath) {
t.Errorf("File %s does not exist", fpath)
}
ok, err := fval.DirPurgeAndCreate(path, 0766)
if !ok {
t.Errorf("Directory %s was not deleted and created", path)
}
if err != nil {
t.Errorf("Error occured %s", err)
}
if fval.FileExists(fpath) {
t.Errorf("File %s exist", fpath)
}
}
func TestDirPurgeAndCreateNoDir(t *testing.T) {
tmpDir := createTmpDir(t)
defer os.RemoveAll(tmpDir)
path := filepath.Join(tmpDir, "test")
ok, err := fval.DirPurgeAndCreate(path, 0766)
if ok {
t.Errorf("Should not get ok")
}
if err == nil {
t.Errorf("Should not get no error")
}
if fval.DirExists(path) {
t.Errorf("Directory %s should not exist", path)
}
}
func TestDirPurgeAndCreateError(t *testing.T) {
tmpDir := createTmpDir(t)
defer os.RemoveAll(tmpDir)
// Use same way of testing error to os.RemoveAll as stdlib
// Determine if we should run the following test.
testit := true
if runtime.GOOS == "windows" {
// Chmod is not supported under windows.
testit = false
} else {
// Test fails as root.
testit = os.Getuid() != 0
}
if testit {
path := filepath.Join(tmpDir, "test")
fpath := filepath.Join(path, ".file")
os.Mkdir(path, 0766)
os.Create(fpath)
if err := os.Chmod(path, 0); err != nil {
t.Fatalf("Chmod %q 0: %s", path, err)
}
ok, err := fval.DirPurgeAndCreate(path, 0766)
if ok {
t.Errorf("Directory should not have been created")
}
if err == nil {
t.Errorf("Error should not have been nil")
}
if fval.FileExists(fpath) {
t.Errorf("File %s should not exist", fpath)
}
}
}
func createTmpDir(t *testing.T) string {
tmpDir, err := ioutil.TempDir("", "fval-test")
if err != nil {
t.Fatalf("TempDir error: %s", err)
}
return tmpDir
}