-
Notifications
You must be signed in to change notification settings - Fork 5
/
fs_test.go
66 lines (57 loc) · 1.74 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
package t
import (
"io/fs"
"os"
"testing"
"path/filepath"
. "github.com/smartystreets/goconvey/convey"
)
func Test_asFS(t *testing.T) {
Convey("asFS", t, func() {
Convey("dir", func() {
fsys := asFS("testdata")
dir, err := fsys.Open(".")
So(err, ShouldBeNil)
So(dir, ShouldNotBeNil)
fi, err := dir.Stat()
So(err, ShouldBeNil)
So(fi.IsDir(), ShouldBeTrue)
entry, err := fs.ReadDir(fsys, ".")
So(err, ShouldBeNil)
So(len(entry) > 0, ShouldBeTrue)
fs.WalkDir(fsys, ".", func(path string, d fs.DirEntry, err error) error {
t.Logf("dir as FS: path=%v, err=%v entry: name=%v, isDir=%v\n", path, err, d.Name(), d.IsDir())
return err
})
})
Convey("file", func() {
fsys := asFS("testdata/zh_CN.po")
file, err := fsys.Open(".")
So(err, ShouldBeNil)
So(file, ShouldNotBeNil)
bytes, err := fs.ReadFile(fsys, "")
So(err, ShouldBeNil)
So(len(bytes) > 0, ShouldBeTrue)
fs.WalkDir(fsys, ".", func(path string, d fs.DirEntry, err error) error {
t.Logf("file as FS: path=%v, err=%v entry: name=%v, isDir=%v\n", path, err, d.Name(), d.IsDir())
return err
})
})
})
// Join 会去除后面的点
t.Logf("%v", filepath.Join("testdata/zh_CN.mo", "."))
// os.DirFS Open 时,是直接用 / 连接的
f := os.DirFS("testdata/zh_CN.mo")
fs.WalkDir(f, ".", func(path string, d fs.DirEntry, err error) error {
// path=. | d= <nil> | err=stat testdata/zh_CN.mo/.: not a directory
t.Logf("path=%v | d= %v | err=%v", path, d, err)
return err
})
// adFS Open 时,用的 Join
f = asFS("testdata/zh_CN.mo")
fs.WalkDir(f, ".", func(path string, d fs.DirEntry, err error) error {
// path=. | d= zh_CN.mo | err=<nil>
t.Logf("path=%v | d= %v | err=%v", path, d.Name(), err)
return err
})
}