Source file
src/io/fs/sub_test.go
1
2
3
4
5 package fs_test
6
7 import (
8 "errors"
9 . "io/fs"
10 "testing"
11 )
12
13 type subOnly struct{ SubFS }
14
15 func (subOnly) Open(name string) (File, error) { return nil, ErrNotExist }
16
17 func TestSub(t *testing.T) {
18 check := func(desc string, sub FS, err error) {
19 t.Helper()
20 if err != nil {
21 t.Errorf("Sub(sub): %v", err)
22 return
23 }
24 data, err := ReadFile(sub, "goodbye.txt")
25 if string(data) != "goodbye, world" || err != nil {
26 t.Errorf(`ReadFile(%s, "goodbye.txt" = %q, %v, want %q, nil`, desc, string(data), err, "goodbye, world")
27 }
28
29 dirs, err := ReadDir(sub, ".")
30 if err != nil || len(dirs) != 1 || dirs[0].Name() != "goodbye.txt" {
31 var names []string
32 for _, d := range dirs {
33 names = append(names, d.Name())
34 }
35 t.Errorf(`ReadDir(%s, ".") = %v, %v, want %v, nil`, desc, names, err, []string{"goodbye.txt"})
36 }
37 }
38
39
40 sub, err := Sub(subOnly{testFsys}, "sub")
41 check("subOnly", sub, err)
42
43
44 sub, err = Sub(openOnly{testFsys}, "sub")
45 check("openOnly", sub, err)
46
47 _, err = sub.Open("nonexist")
48 if err == nil {
49 t.Fatal("Open(nonexist): succeeded")
50 }
51 pe, ok := err.(*PathError)
52 if !ok {
53 t.Fatalf("Open(nonexist): error is %T, want *PathError", err)
54 }
55 if pe.Path != "nonexist" {
56 t.Fatalf("Open(nonexist): err.Path = %q, want %q", pe.Path, "nonexist")
57 }
58
59 _, err = sub.Open("./")
60 if !errors.Is(err, ErrInvalid) {
61 t.Fatalf("Open(./): error is %v, want %v", err, ErrInvalid)
62 }
63 }
64
View as plain text