-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil_test.go
62 lines (50 loc) · 1.59 KB
/
util_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
package tdb
import (
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
)
func TestRandBytes(t *testing.T) {
assert.Len(t, randBytes(10), 10, "bytes length should be 10")
}
func BenchmarkRandBytes(b *testing.B) {
for i := 0; i < b.N; i++ {
randBytes(10)
}
}
func TestFolderActions(t *testing.T) {
demoFolder := "util_test_folder"
demoFile := "util_test_file"
exist, err := checkFolderExist(demoFolder)
assert.NoError(t, err, "should have no error")
assert.False(t, exist, "folder should not exist")
var f *os.File
f, err = os.Create(demoFile)
if assert.NoError(t, err, "should have no error to create file") {
defer func() {
f.Close()
err := os.Remove(demoFile)
assert.NoError(t, err, "should have no error to delete file")
}()
}
exist, err = checkFolderExist(demoFile)
assert.Equal(t, ErrPathNotFolder, err, "should have error when path is not folder")
assert.False(t, exist, "should return false if it is a file")
err = createFolder(demoFolder)
if assert.NoError(t, err, "should have no error to create folder") {
defer func() {
err := os.RemoveAll(demoFolder)
assert.NoError(t, err, "should have no error to delete folder")
}()
}
err = createFolder(demoFolder)
assert.NoError(t, err, "should have no error if folder exist")
exist, err = checkFolderExist(demoFolder)
assert.NoError(t, err, "should have no error when path is a folder")
assert.True(t, exist, "should return true if folder exist")
}
func TestGetFileName(t *testing.T) {
fPath := filepath.Join("201702", "050.idx")
assert.Equal(t, "050", getFileName(fPath), "file name wrong")
}