-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile_test.go
86 lines (67 loc) · 1.65 KB
/
file_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
package memfs
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestFile(t *testing.T) {
fs, err := New(fsDef, nil)
assert.NoError(t, err)
assert.NotNil(t, fs)
// regular file
f, err := fs.Open("/root/home/1.txt")
assert.NoError(t, err)
assert.NotNil(t, f)
fi, err := f.Stat()
assert.NoError(t, err)
assert.False(t, fi.IsDir())
assert.Equal(t, "1.txt", fi.Name())
assert.EqualValues(t, 17, fi.Size())
_, err = f.Readdir(10)
assert.Error(t, err)
buf := make([]byte, 6)
n, err := f.Read(buf)
assert.NoError(t, err)
assert.Equal(t, 6, n)
assert.Equal(t, []byte("@/root"), buf)
pos, err := f.Seek(6, 1)
assert.NoError(t, err)
assert.EqualValues(t, 12, pos)
buf = make([]byte, 5)
n, err = f.Read(buf)
assert.NoError(t, err)
assert.Equal(t, 5, n)
assert.Equal(t, []byte("1.txt"), buf)
pos, err = f.Seek(7, 0)
assert.NoError(t, err)
assert.EqualValues(t, 7, pos)
buf = make([]byte, 4)
n, err = f.Read(buf)
assert.NoError(t, err)
assert.Equal(t, 4, n)
assert.Equal(t, []byte("home"), buf)
pos, err = f.Seek(-3, 2)
assert.NoError(t, err)
assert.EqualValues(t, 14, pos)
buf = make([]byte, 3)
n, err = f.Read(buf)
assert.NoError(t, err)
assert.Equal(t, 3, n)
assert.Equal(t, []byte("txt"), buf)
// dir
d, err := fs.Open("/root/home/")
assert.NoError(t, err)
assert.NotNil(t, d)
fi, err = d.Stat()
assert.NoError(t, err)
assert.True(t, fi.IsDir())
assert.Equal(t, "home", fi.Name())
assert.EqualValues(t, 0, fi.Size())
fis, err := d.Readdir(10)
assert.NoError(t, err)
assert.Equal(t, 4, len(fis))
buf = make([]byte, 6)
_, err = d.Read(buf)
assert.Error(t, err)
_, err = d.Seek(6, 1)
assert.Error(t, err)
}