forked from hairyhenderson/go-fsimpl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mux_test.go
74 lines (58 loc) · 1.8 KB
/
mux_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
package fsimpl
import (
"io/fs"
"net/url"
"os"
"testing"
"testing/fstest"
"github.com/helmwave/go-fsimpl/internal/tests"
"github.com/stretchr/testify/assert"
)
func TestFSMux(t *testing.T) {
fsys := os.DirFS("/tmp")
fn := func(u *url.URL) (fs.FS, error) { return fsys, nil }
fsp := FSProviderFunc(fn, "foo", "bar")
fsp2 := FSProviderFunc(fn, "baz", "qux")
m := NewMux()
_, err := m.Lookup(":bogus/url")
assert.Error(t, err)
_, err = m.Lookup("foo:///")
assert.Error(t, err)
m.Add(fsp)
m.Add(fsp2)
actual, err := m.Lookup("foo:///")
assert.NoError(t, err)
assert.Equal(t, fsys, actual)
actual, err = m.Lookup("bar:///")
assert.NoError(t, err)
assert.Equal(t, fsys, actual)
actual, err = m.Lookup("qux:///")
assert.NoError(t, err)
assert.Equal(t, fsys, actual)
_, err = m.Lookup("file:///")
assert.Error(t, err)
// test out FSProvider functionality
assert.Equal(t, []string{"bar", "baz", "foo", "qux"}, m.Schemes())
actual, err = m.New(tests.MustURL("foo:///"))
assert.NoError(t, err)
assert.Equal(t, fsys, actual)
actual, err = m.New(tests.MustURL("bar:///"))
assert.NoError(t, err)
assert.Equal(t, fsys, actual)
}
func TestWrappedFSProvider(t *testing.T) {
basefsys := fstest.MapFS{}
basefsys["root/sub/subsub/file.txt"] = &fstest.MapFile{Data: []byte("hello")}
basefsys["root/sub/subsub/file2.txt"] = &fstest.MapFile{Data: []byte("world")}
basefsys["rootfile.txt"] = &fstest.MapFile{Data: []byte("rootfile")}
fsp := WrappedFSProvider(&basefsys, "foo")
fsys, err := fsp.New(tests.MustURL("foo:///"))
assert.NoError(t, err)
assert.Same(t, &basefsys, fsys)
fsys, err = fsp.New(tests.MustURL("foo:///root/sub"))
assert.NoError(t, err)
assert.NotSame(t, &basefsys, fsys)
b, err := fs.ReadFile(fsys, "subsub/file.txt")
assert.NoError(t, err)
assert.Equal(t, []byte("hello"), b)
}