wa-lang.org/wazero@v1.0.2/cmd/wazero/compositefs_test.go (about) 1 package main 2 3 import ( 4 "bytes" 5 "embed" 6 "fmt" 7 "io/fs" 8 "testing" 9 "testing/fstest" 10 11 "wa-lang.org/wazero/internal/testing/require" 12 ) 13 14 //go:embed testdata/fs 15 var testFS embed.FS 16 17 func TestCompositeFS(t *testing.T) { 18 tests := []struct { 19 name string 20 fs *compositeFS 21 path string 22 content string 23 }{ 24 { 25 name: "empty", 26 fs: &compositeFS{}, 27 path: "bear.txt", 28 }, 29 { 30 name: "single mount to root", 31 fs: &compositeFS{ 32 paths: map[string]fs.FS{ 33 "": testFSSub(""), 34 }, 35 }, 36 path: "bear.txt", 37 content: "pooh", 38 }, 39 { 40 name: "single mount to root", 41 fs: &compositeFS{ 42 paths: map[string]fs.FS{ 43 "": testFSSub(""), 44 }, 45 }, 46 path: "fish/clownfish.txt", 47 content: "nemo", 48 }, 49 { 50 name: "single mount to root", 51 fs: &compositeFS{ 52 paths: map[string]fs.FS{ 53 "": testFSSub(""), 54 }, 55 }, 56 path: "mammals/primates/ape.txt", 57 content: "king kong", 58 }, 59 { 60 name: "single mount to path", 61 fs: &compositeFS{ 62 paths: map[string]fs.FS{ 63 "mammals": testFSSub(""), 64 }, 65 }, 66 path: "mammals/bear.txt", 67 content: "pooh", 68 }, 69 { 70 name: "single mount to path", 71 fs: &compositeFS{ 72 paths: map[string]fs.FS{ 73 "mammals": testFSSub(""), 74 }, 75 }, 76 path: "mammals/whale.txt", 77 }, 78 { 79 name: "single mount to path", 80 fs: &compositeFS{ 81 paths: map[string]fs.FS{ 82 "mammals": testFSSub(""), 83 }, 84 }, 85 path: "bear.txt", 86 }, 87 { 88 name: "non-overlapping mounts", 89 fs: &compositeFS{ 90 paths: map[string]fs.FS{ 91 "fish": testFSSub("fish"), 92 "mammals": testFSSub("mammals"), 93 }, 94 }, 95 path: "fish/clownfish.txt", 96 content: "nemo", 97 }, 98 { 99 name: "non-overlapping mounts", 100 fs: &compositeFS{ 101 paths: map[string]fs.FS{ 102 "fish": testFSSub("fish"), 103 "mammals": testFSSub("mammals"), 104 }, 105 }, 106 path: "mammals/whale.txt", 107 content: "moby dick", 108 }, 109 { 110 name: "non-overlapping mounts", 111 fs: &compositeFS{ 112 paths: map[string]fs.FS{ 113 "fish": testFSSub("fish"), 114 "mammals": testFSSub("mammals"), 115 }, 116 }, 117 path: "mammals/primates/ape.txt", 118 content: "king kong", 119 }, 120 { 121 name: "non-overlapping mounts", 122 fs: &compositeFS{ 123 paths: map[string]fs.FS{ 124 "fish": testFSSub("fish"), 125 "mammals": testFSSub("mammals"), 126 }, 127 }, 128 path: "bear.txt", 129 }, 130 { 131 name: "overlapping mounts, deep first", 132 fs: &compositeFS{ 133 paths: map[string]fs.FS{ 134 "animals/fish": testFSSub("fish"), 135 "animals": testFSSub("mammals"), 136 }, 137 }, 138 path: "animals/fish/clownfish.txt", 139 content: "nemo", 140 }, 141 { 142 name: "overlapping mounts, deep first", 143 fs: &compositeFS{ 144 paths: map[string]fs.FS{ 145 "animals/fish": testFSSub("fish"), 146 "animals": testFSSub("mammals"), 147 }, 148 }, 149 path: "animals/whale.txt", 150 content: "moby dick", 151 }, 152 { 153 name: "overlapping mounts, deep first", 154 fs: &compositeFS{ 155 paths: map[string]fs.FS{ 156 "animals/fish": testFSSub("fish"), 157 "animals": testFSSub("mammals"), 158 }, 159 }, 160 path: "animals/bear.txt", 161 }, 162 { 163 name: "overlapping mounts, shallow first", 164 fs: &compositeFS{ 165 paths: map[string]fs.FS{ 166 "animals": testFSSub("mammals"), 167 "animals/fish": testFSSub("fish"), 168 }, 169 }, 170 path: "animals/fish/clownfish.txt", 171 content: "nemo", 172 }, 173 { 174 name: "overlapping mounts, shallow first", 175 fs: &compositeFS{ 176 paths: map[string]fs.FS{ 177 "animals": testFSSub("mammals"), 178 "animals/fish": testFSSub("fish"), 179 }, 180 }, 181 path: "animals/whale.txt", 182 content: "moby dick", 183 }, 184 { 185 name: "overlapping mounts, shallow first", 186 fs: &compositeFS{ 187 paths: map[string]fs.FS{ 188 "animals": testFSSub("mammals"), 189 "animals/fish": testFSSub("fish"), 190 }, 191 }, 192 path: "animals/bear.txt", 193 }, 194 } 195 196 for _, tc := range tests { 197 tt := tc 198 t.Run(fmt.Sprintf("%s - %s", tt.name, tt.path), func(t *testing.T) { 199 content, err := fs.ReadFile(tt.fs, tt.path) 200 if tt.content != "" { 201 require.NoError(t, err) 202 require.Equal(t, tt.content, string(bytes.TrimSpace(content))) 203 } else { 204 require.ErrorIs(t, err, fs.ErrNotExist) 205 } 206 }) 207 } 208 } 209 210 func TestFSTest(t *testing.T) { 211 fs := &compositeFS{ 212 paths: map[string]fs.FS{ 213 // TestFS requires non-rooted paths to be read from current directory so we mount 214 // both with . and no prefix. 215 ".": testFSSub(""), 216 "": testFSSub(""), 217 }, 218 } 219 220 require.NoError(t, fstest.TestFS(fs, "bear.txt")) 221 } 222 223 func testFSSub(path string) fs.FS { 224 // Can't use filepath.Join because we need unix behavior even on Windows. 225 p := "testdata/fs" 226 if len(path) > 0 { 227 p = fmt.Sprintf("%s/%s", p, path) 228 } 229 f, err := fs.Sub(testFS, p) 230 if err != nil { 231 panic(err) 232 } 233 return f 234 }