forked from gobuffalo/buffalo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fs_test.go
118 lines (84 loc) · 2.7 KB
/
fs_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
package buffalo
import (
"io"
"io/fs"
"testing"
"github.com/gobuffalo/buffalo/internal/testdata/embedded"
"github.com/stretchr/testify/require"
)
func Test_FS_Disallows_Parent_Folders(t *testing.T) {
r := require.New(t)
fsys := NewFS(embedded.FS(), "internal/testdata/disk")
r.NotNil(fsys)
f, err := fsys.Open("../panic.txt")
r.ErrorIs(err, fs.ErrNotExist)
r.Nil(f)
f, err = fsys.Open("try/../to/../trick/../panic.txt")
r.ErrorIs(err, fs.ErrNotExist)
r.Nil(f)
}
func Test_FS_Hides_embed_go(t *testing.T) {
r := require.New(t)
fsys := NewFS(embedded.FS(), "internal/testdata/disk")
r.NotNil(fsys)
f, err := fsys.Open("embed.go")
r.ErrorIs(err, fs.ErrNotExist)
r.Nil(f)
}
func Test_FS_Prioritizes_Disk(t *testing.T) {
r := require.New(t)
fsys := NewFS(embedded.FS(), "internal/testdata/disk")
r.NotNil(fsys)
f, err := fsys.Open("file.txt")
r.NoError(err)
b, err := io.ReadAll(f)
r.NoError(err)
r.Equal("This file is on disk.", string(b))
// should handle slash-separated path for all systems including Windows
f, err = fsys.Open("under/sub/subfile")
r.NoError(err)
b, err = io.ReadAll(f)
r.NoError(err)
r.Equal("This file is on disk/sub.", string(b))
}
func Test_FS_Uses_Embed_If_No_Disk(t *testing.T) {
r := require.New(t)
fsys := NewFS(embedded.FS(), "internal/testdata/empty")
r.NotNil(fsys)
f, err := fsys.Open("file.txt")
r.NoError(err)
b, err := io.ReadAll(f)
r.NoError(err)
r.Equal("This file is embedded.", string(b))
// should handle slash-separated path for all systems including Windows
f, err = fsys.Open("under/sub/subfile")
r.NoError(err)
b, err = io.ReadAll(f)
r.NoError(err)
r.Equal("This file is on embedded/sub.", string(b))
}
func Test_FS_ReadDirFile(t *testing.T) {
r := require.New(t)
fsys := NewFS(embedded.FS(), "internal/testdata/disk")
r.NotNil(fsys)
f, err := fsys.Open(".")
r.NoError(err)
dir, ok := f.(fs.ReadDirFile)
r.True(ok, "folder does not implement fs.ReadDirFile interface")
// First read should return at most 1 file
entries, err := dir.ReadDir(1)
r.NoError(err)
// The actual len will be 0 because the first file read is the embed.go file
// this is counter-intuitive, but it's how the fs.ReadDirFile interface is specified;
// if err == nil, just continue to call ReadDir until io.EOF is returned.
r.LessOrEqual(len(entries), 1, "a call to ReadDir must at most return n entries")
// Second read should return at most 2 files
entries, err = dir.ReadDir(3)
r.NoError(err)
// The actual len will be 2 (file.txt & file2.txt + under/)
r.LessOrEqual(len(entries), 3, "a call to ReadDir must at most return n entries")
// trying to read next 2 files (none left)
entries, err = dir.ReadDir(2)
r.ErrorIs(err, io.EOF)
r.Empty(entries)
}