1 // Copyright 2016 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
13 "golang.org/x/tools/godoc/vfs"
14 "golang.org/x/tools/godoc/vfs/mapfs"
17 func TestNewNameSpace(t *testing.T) {
19 // We will mount this filesystem under /fs1
20 mount := mapfs.New(map[string]string{"fs1file": "abcdefgh"})
22 // Existing process. This should give error on Stat("/")
24 t1.Bind("/fs1", mount, "/", vfs.BindReplace)
26 // using NewNameSpace. This should work fine.
27 t2 := vfs.NewNameSpace()
28 t2.Bind("/fs1", mount, "/", vfs.BindReplace)
30 testcases := map[string][]bool{
33 "/fs1/fs1file": {true, true},
36 fss := []vfs.FileSystem{t1, t2}
38 for j, fs := range fss {
39 for k, v := range testcases {
43 t.Errorf("fs: %d, testcase: %s, want: %v, got: %v, err: %s", j, k, v[j], result, err)
48 fi, err := t2.Stat("/")
54 t.Errorf("t2.Name() : want:%s got:%s", "/", fi.Name())
57 if !fi.ModTime().IsZero() {
58 t.Errorf("t2.ModTime() : want:%v got:%v", time.Time{}, fi.ModTime())
62 func TestReadDirUnion(t *testing.T) {
63 for _, tc := range []struct {
70 ns: func() vfs.NameSpace {
71 rootFs := mapfs.New(map[string]string{
74 "doc/dir1/d1.txt": "",
76 docFs := mapfs.New(map[string]string{
78 "doc/dir2/d2.txt": "",
81 ns.Bind("/", rootFs, "/", vfs.BindReplace)
82 ns.Bind("/doc", docFs, "/doc", vfs.BindBefore)
86 want: "a.txt:2,b.txt:1,dir1:0,dir2:0",
88 desc: "have_go_files",
89 ns: func() vfs.NameSpace {
90 a := mapfs.New(map[string]string{
92 "src/x/suba/sub.txt": "",
94 b := mapfs.New(map[string]string{
95 "src/x/b.go": "package b",
96 "src/x/subb/sub.txt": "",
98 c := mapfs.New(map[string]string{
100 "src/x/subc/sub.txt": "",
102 ns := vfs.NameSpace{}
103 ns.Bind("/", a, "/", vfs.BindReplace)
104 ns.Bind("/", b, "/", vfs.BindAfter)
105 ns.Bind("/", c, "/", vfs.BindAfter)
109 want: "b.go:9,suba:0,subb:0,subc:0",
112 ns: func() vfs.NameSpace {
113 ns := vfs.NameSpace{}
114 ns.Bind("/empty", mapfs.New(nil), "/empty", vfs.BindReplace)
121 t.Run(tc.desc, func(t *testing.T) {
122 fis, err := tc.ns.ReadDir(tc.path)
126 buf := &strings.Builder{}
128 for _, fi := range fis {
129 fmt.Fprintf(buf, "%s%s:%d", sep, fi.Name(), fi.Size())
132 if got := buf.String(); got != tc.want {
133 t.Errorf("got %q; want %q", got, tc.want)