-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmergefs_test.go
155 lines (129 loc) · 2.38 KB
/
mergefs_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
package mergefs_test
import (
"embed"
"fmt"
"io/fs"
"github.com/l4go/mergefs"
)
//go:embed test/fs1
var fs1Fs embed.FS
//go:embed test/fs2
var fs2Fs embed.FS
func new_fs1() fs.FS {
fs1fs, err := fs.Sub(fs1Fs, "test/fs1")
if err != nil {
panic("crash embed FS")
}
return fs1fs
}
func new_fs2() fs.FS {
fs2fs, err := fs.Sub(fs2Fs, "test/fs2")
if err != nil {
panic("crash embed FS")
}
return fs2fs
}
func ExampleMergeFS_Open() {
mfs := mergefs.New(new_fs1(), new_fs2())
var err error
_, err = mfs.Open("foo.txt")
fmt.Println(err == nil)
_, err = mfs.Open("bar.txt")
fmt.Println(err == nil)
_, err = mfs.Open("baz.txt")
fmt.Println(err == nil)
// Output:
// true
// true
// true
}
func ExampleMergeFS_Stat() {
mfs := mergefs.New(new_fs1(), new_fs2())
var err error
_, err = fs.Stat(mfs, "foo")
fmt.Println(err == nil)
_, err = fs.Stat(mfs, "bar")
fmt.Println(err == nil)
_, err = fs.Stat(mfs, "baz")
fmt.Println(err == nil)
_, err = fs.Stat(mfs, "boo")
fmt.Println(err == nil)
_, err = fs.Stat(mfs, "foo.txt")
fmt.Println(err == nil)
_, err = fs.Stat(mfs, "bar.txt")
fmt.Println(err == nil)
_, err = fs.Stat(mfs, "baz.txt")
fmt.Println(err == nil)
_, err = fs.Stat(mfs, "boo.txt")
fmt.Println(err == nil)
// Output:
// true
// true
// true
// false
// true
// true
// true
// false
}
func ExampleMergeFS_ReadFile() {
fs1 := new_fs1()
fs2 := new_fs2()
mfs := mergefs.New(fs1, fs2)
var buf []byte
var err error
buf, err = fs.ReadFile(fs1, "baz.txt")
if err != nil {
fmt.Println("error")
} else {
fmt.Print(string(buf))
}
buf, err = fs.ReadFile(fs2, "baz.txt")
if err != nil {
fmt.Println("error")
} else {
fmt.Print(string(buf))
}
buf, err = fs.ReadFile(mfs, "baz.txt")
if err != nil {
fmt.Println("error")
} else {
fmt.Print(string(buf))
}
// Output:
// baz
// hoge
// baz
}
func ExampleMergeFS_ReadDir() {
mfs := mergefs.New(new_fs1(), new_fs2())
ents, derr := fs.ReadDir(mfs, ".")
if derr != nil {
return
}
for _, e := range ents {
fmt.Println(e.Name())
}
// Unordered output:
// foo
// bar
// baz
// foo.txt
// bar.txt
// baz.txt
// hoge.txt
}
func ExampleMergeFS_Glob() {
mfs := mergefs.New(new_fs1(), new_fs2())
lst, err := fs.Glob(mfs, "foo/*.txt")
if err != nil {
return
}
for _, name := range lst {
fmt.Println(name)
}
// Unordered output:
// foo/foo.txt
// foo/bar.txt
// foo/baz.txt
}