-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfilepath_test.go
298 lines (237 loc) · 8.85 KB
/
filepath_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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
// Copyright (c) Jeevanandam M. (https://github.com/jeevatkm)
// go-aah/essentials source code and usage is governed by a MIT style
// license that can be found in the LICENSE file.
package ess
import (
"io/ioutil"
"os"
"runtime"
"strings"
"testing"
"aahframework.org/test.v0/assert"
)
func TestIsFileExists(t *testing.T) {
testdataPath := getTestdataPath()
assert.Equal(t, true, IsFileExists(join(testdataPath, "sample.txt")))
assert.Equal(t, false, IsFileExists(join(testdataPath, "sample-not-exists.txt")))
assert.Equal(t, true, IsFileExists(testdataPath))
assert.Equal(t, false, IsFileExists("testdata-not-exists"))
}
func TestIsDirEmpty(t *testing.T) {
testdataPath := getTestdataPath()
assert.Equal(t, false, IsDirEmpty(testdataPath))
assert.Equal(t, true, IsDirEmpty("testdata-not-exists.txt"))
dirPath := join(testdataPath, "path", "isdirempty")
_ = MkDirAll(dirPath, 0755)
assert.Equal(t, true, IsDirEmpty(dirPath))
}
func TestIsDir(t *testing.T) {
testdataPath := getTestdataPath()
result := IsDir(testdataPath)
assert.True(t, result)
result = IsDir(join(testdataPath, "sample.txt"))
assert.False(t, result)
result = IsDir(join(testdataPath, "not-exists.txt"))
assert.False(t, result)
}
func TestApplyFileMode(t *testing.T) {
fileName := join(getTestdataPath(), "FileMode.txt")
defer DeleteFiles(fileName)
err := ioutil.WriteFile(fileName,
[]byte(`This file is for file permission testing`), 0700)
assert.FailOnError(t, err, "file permission issue")
fileInfo, err := os.Stat(fileName)
assert.FailOnError(t, err, "couldn't to file stat")
if fileInfo.Mode() != os.FileMode(0700) {
t.Errorf("expected file mode: 0700 got %v", fileInfo.Mode())
}
err = ApplyFileMode(fileName, 0755)
assert.FailOnError(t, err, "couldn't apply file permission")
fileInfo, err = os.Stat(fileName)
assert.FailOnError(t, err, "couldn't to file stat")
if fileInfo.Mode() != os.FileMode(0755) {
t.Errorf("expected file mode: 0755 got %v", fileInfo.Mode())
}
if runtime.GOOS != "windows" {
// expected to fail
err = ApplyFileMode("/var", 0755)
assert.NotNil(t, err)
}
}
func TestLineCntByFilePath(t *testing.T) {
count := LineCnt(join(getTestdataPath(), "sample.txt"))
assert.Equal(t, 20, count)
count = LineCnt(join(getTestdataPath(), "sample-not.txt"))
assert.Equal(t, 0, count)
}
func TestLineCntByReader(t *testing.T) {
file, err := os.Open(join(getTestdataPath(), "sample.txt"))
assert.FailOnError(t, err, "unable to open file")
defer CloseQuietly(file)
assert.Equal(t, 20, LineCntr(file))
}
func TestWalk(t *testing.T) {
testdataPath := getTestdataPath()
fileName := join(testdataPath, "symlinktest.txt")
newName1 := join(testdataPath, "symlinktest1.txt")
newName2 := join(testdataPath, "symlinktest2.txt")
newName3 := join(testdataPath, "symlinkdata1")
tmpDir := os.TempDir()
defer func() {
DeleteFiles(fileName, newName1, newName2, newName3)
DeleteFiles(
join(testdataPath, "symlinkdata"),
join(tmpDir, "symlinktest"),
join(testdataPath, "symlinkdata1"),
)
}()
err := ioutil.WriteFile(fileName,
[]byte(`This file is for file permission testing 1`), 0755)
assert.FailOnError(t, err, "unable to create file")
err = MkDirAll(join(testdataPath, "symlinkdata"), 0755)
assert.FailOnError(t, err, "")
err = ioutil.WriteFile(join(testdataPath, "symlinkdata", "file1.txt"),
[]byte(`This file is for file permission testing 2`), 0755)
assert.FailOnError(t, err, "unable to create file")
// preparing symlink for test
err = os.Symlink(fileName, newName1)
assert.FailOnError(t, err, "unable to create symlink")
err = os.Symlink(fileName, newName2)
assert.FailOnError(t, err, "unable to create symlink")
err = os.Symlink(join(testdataPath, "symlinkdata"), newName3)
assert.FailOnError(t, err, "unable to create symlink")
err = CopyDir(join(tmpDir, "symlinktest"), testdataPath, Excludes{})
assert.FailOnError(t, err, "")
}
func TestExcludes(t *testing.T) {
errExcludes := Excludes{
".*",
"DS_Store.bak",
"[^",
"[]a]",
}
assert.Equal(t, true, errExcludes.Validate() != nil)
excludes := Excludes{
".*",
"*.bak",
"*.tmp",
"tmp",
}
assert.Equal(t, nil, excludes.Validate())
}
func TestCopyFile(t *testing.T) {
testdataPath := getTestdataPath()
_, err := CopyFile(
join(testdataPath, "file-found.txt"),
join(testdataPath, "file-not-exists.txt"),
)
assert.NotNil(t, err)
_, err = CopyFile(
join(testdataPath, "sample.txt"),
join(testdataPath, "sample.txt"),
)
assert.NotNil(t, err)
if runtime.GOOS != "windows" {
_, err = CopyFile(
"/var/you-will-not-be-able-to-create.txt",
join(testdataPath, "sample.txt"),
)
assert.NotNil(t, err)
}
}
func TestCopyDir(t *testing.T) {
testdataPath := getTestdataPath()
tmpDir := os.TempDir()
defer DeleteFiles(join(tmpDir, "test1"))
err1 := CopyDir(
join(tmpDir, "target"),
join(testdataPath, "not-exists-dir"),
Excludes{},
)
assert.NotNil(t, err1)
err2 := CopyDir(
join(tmpDir, "target"),
join(testdataPath, "sample.txt"),
Excludes{},
)
assert.NotNil(t, err2)
// err3 := CopyDir(tmpDir, testdataPath, Excludes{})
// assert.True(t, strings.HasPrefix(err3.Error(), "destination dir already exists"))
err4 := CopyDir(join(tmpDir, "target"), testdataPath, Excludes{"[]a]"})
assert.NotNil(t, err4)
pwd, _ := os.Getwd()
err5 := CopyDir(join(tmpDir, "test1"), pwd, Excludes{"test*", "*conf", ".*"})
assert.FailNowOnError(t, err5, "copy directory failed")
}
func TestStripExt(t *testing.T) {
name1 := StripExt("/sample/path/to/file/working.txt")
assert.Equal(t, "/sample/path/to/file/working", name1)
name2 := StripExt("woriking-fine.pdf")
assert.Equal(t, "woriking-fine", name2)
name3 := StripExt("")
assert.Equal(t, "", name3)
name4 := StripExt("myname")
assert.Equal(t, "myname", name4)
}
func TestDirPaths(t *testing.T) {
testdataPath := getTestdataPath()
path1 := join(testdataPath, "dirpaths", "level1", "level2", "level3")
path11 := join(testdataPath, "dirpaths", "level1", "level1-1")
path12 := join(testdataPath, "dirpaths", "level1", "level1-2")
path21 := join(testdataPath, "dirpaths", "level1", "level2", "level2-1")
path22 := join(testdataPath, "dirpaths", "level1", "level2", "level2-2")
defer DeleteFiles(join(testdataPath, "dirpaths"))
_ = MkDirAll(path1, 0755)
_ = MkDirAll(path11, 0755)
_ = MkDirAll(path12, 0755)
_ = MkDirAll(path21, 0755)
_ = MkDirAll(path22, 0755)
dirs, err := DirsPath(join(testdataPath, "dirpaths"), true)
assert.FailOnError(t, err, "unable to get directory list")
assert.True(t, IsSliceContainsString(dirs, path1))
assert.True(t, IsSliceContainsString(dirs, path11))
assert.True(t, IsSliceContainsString(dirs, path12))
assert.True(t, IsSliceContainsString(dirs, path21))
assert.True(t, IsSliceContainsString(dirs, path22))
assert.False(t, IsSliceContainsString(dirs, join(path22, "not-exists")))
dirs, err = DirsPath(join(testdataPath, "dirpaths", "level1"), false)
assert.FailOnError(t, err, "unable to get directory list")
assert.True(t, IsSliceContainsString(dirs, path11))
assert.True(t, IsSliceContainsString(dirs, path12))
assert.False(t, IsSliceContainsString(dirs, join(path22, "not-exists")))
dirs, err = DirsPathExcludes(join(testdataPath, "dirpaths"), true, Excludes{"level1-2", "level2-2"})
assert.FailOnError(t, err, "unable to get directory list")
assert.True(t, len(dirs) == 6)
}
func TestFilesPath(t *testing.T) {
testdataPath := getTestdataPath()
path1 := join(testdataPath, "dirpaths", "level1", "level2", "level3")
path11 := join(testdataPath, "dirpaths", "level1", "level1-1")
path12 := join(testdataPath, "dirpaths", "level1", "level1-2")
path21 := join(testdataPath, "dirpaths", "level1", "level2", "level2-1")
path22 := join(testdataPath, "dirpaths", "level1", "level2", "level2-2")
defer DeleteFiles(join(testdataPath, "dirpaths"))
_ = MkDirAll(path1, 0755)
_ = MkDirAll(path11, 0755)
_ = MkDirAll(path12, 0755)
_ = MkDirAll(path21, 0755)
_ = MkDirAll(path22, 0755)
_ = ioutil.WriteFile(join(path1, "file1.txt"), []byte("file1.txt"), 0600)
_ = ioutil.WriteFile(join(path11, "file11.txt"), []byte("file11.txt"), 0600)
_ = ioutil.WriteFile(join(path12, "file12.txt"), []byte("file12.txt"), 0600)
_ = ioutil.WriteFile(join(path21, "file21.txt"), []byte("file21.txt"), 0600)
_ = ioutil.WriteFile(join(path22, "file22.txt"), []byte("file22.txt"), 0600)
files, err := FilesPath(join(testdataPath, "dirpaths"), true)
assert.Nil(t, err)
assert.True(t, strings.HasSuffix(files[0], "file11.txt"))
assert.True(t, strings.HasSuffix(files[1], "file12.txt"))
assert.True(t, strings.HasSuffix(files[2], "file21.txt"))
assert.True(t, strings.HasSuffix(files[3], "file22.txt"))
assert.True(t, strings.HasSuffix(files[4], "file1.txt"))
files, err = FilesPath(path11, false)
assert.Nil(t, err)
assert.True(t, strings.HasSuffix(files[0], "file11.txt"))
files, err = FilesPathExcludes(join(testdataPath, "dirpaths"), true, Excludes{"file12.txt", "file22.txt"})
assert.Nil(t, err)
assert.True(t, len(files) == 3)
}