-
Notifications
You must be signed in to change notification settings - Fork 7
/
fs_test.go
54 lines (48 loc) · 1.2 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
package docsite
import (
"context"
"net/http"
"path/filepath"
"reflect"
"sort"
"testing"
"github.com/pkg/errors"
"golang.org/x/tools/godoc/vfs/httpfs"
"golang.org/x/tools/godoc/vfs/mapfs"
)
func TestWalkFileSystem(t *testing.T) {
wantAllPaths := []string{
"a/b.md",
"a/c/d.md",
"e.md",
"f/g.md",
"f/h.md",
}
files := make(map[string]string, len(wantAllPaths))
for _, path := range wantAllPaths {
files[path] = ""
}
files["x/y.png"] = "" // add file that does not pass the isMarkdown filter
fs := httpfs.New(mapfs.New(files))
var allPaths []string
isMarkdown := func(path string) bool { return filepath.Ext(path) == ".md" }
collect := func(path string) error {
allPaths = append(allPaths, path)
return nil
}
if err := WalkFileSystem(fs, isMarkdown, collect); err != nil {
t.Fatal(err)
}
sort.Strings(allPaths)
if !reflect.DeepEqual(allPaths, wantAllPaths) {
t.Errorf("got paths %v, want %v", allPaths, wantAllPaths)
}
}
type versionedFileSystem map[string]http.FileSystem
func (vfs versionedFileSystem) OpenVersion(_ context.Context, version string) (http.FileSystem, error) {
fs, ok := vfs[version]
if !ok {
return nil, errors.New("version not found")
}
return fs, nil
}