-
Notifications
You must be signed in to change notification settings - Fork 0
/
filehandle_test.go
97 lines (83 loc) · 1.94 KB
/
filehandle_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
// SPDX-FileCopyrightText: 2022 Weston Schmidt <[email protected]>
// SPDX-License-Identifier: Apache-2.0
package githubfs
import (
"io"
"io/fs"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestFileHandle_All(t *testing.T) {
tests := []struct {
description string
name string
size int64
time time.Time
mode fs.FileMode
isDir bool
content []byte
}{
{
description: "file test",
name: "foo",
size: 1234,
time: time.Now(),
isDir: false,
content: []byte("hello, world"),
}, {
description: "empty file test",
name: "empty",
size: 0,
time: time.Now(),
isDir: true,
mode: fs.ModeDir,
content: []byte{},
},
}
for _, tc := range tests {
t.Run(tc.description, func(t *testing.T) {
assert := assert.New(t)
require := require.New(t)
info := fileInfo{
name: tc.name,
size: tc.size,
modTime: tc.time,
mode: tc.mode,
}
fh := newFileHandle(info, tc.content)
require.NotNil(fh)
fi, err := fh.Stat()
assert.NoError(err)
assert.Equal(tc.name, fi.Name())
assert.Equal(tc.size, fi.Size())
assert.Equal(tc.mode, fi.Mode())
assert.Equal(tc.time, fi.ModTime())
assert.Equal(tc.isDir, fi.IsDir())
assert.Nil(fi.Sys())
b := make([]byte, 20)
n, err := fh.Read(b)
if len(tc.content) != 0 {
assert.NoError(err)
} else {
assert.ErrorIs(err, io.EOF)
}
assert.Equal(len(tc.content), n)
assert.Equal(tc.content, b[:n])
n, err = fh.Read(b)
assert.ErrorIs(io.EOF, err)
assert.Equal(0, n)
err = fh.Close()
assert.NoError(err)
err = fh.Close()
assert.ErrorIs(err, fs.ErrClosed)
n, err = fh.Read(b)
assert.ErrorIs(err, fs.ErrClosed)
assert.Equal(0, n)
fi, err = fh.Stat()
assert.ErrorIs(err, fs.ErrClosed)
assert.Nil(fi)
})
}
}