| 1 | package embedfs |
| 2 | |
| 3 | import ( |
| 4 | "embed" |
| 5 | "fmt" |
| 6 | "io" |
| 7 | iofs "io/fs" |
| 8 | "os" |
| 9 | "testing" |
| 10 | "testing/fstest" |
| 11 | |
| 12 | "github.com/go-git/go-billy/v6" |
| 13 | "github.com/stretchr/testify/assert" |
| 14 | "github.com/stretchr/testify/require" |
| 15 | ) |
| 16 | |
| 17 | //go:embed testdata/empty.txt |
| 18 | var singleFile embed.FS |
| 19 | |
| 20 | //go:embed testdata |
| 21 | var testdataDir embed.FS |
| 22 | |
| 23 | var empty embed.FS |
| 24 | |
| 25 | func TestOpen(t *testing.T) { |
| 26 | t.Parallel() |
| 27 | |
| 28 | tests := []struct { |
| 29 | name string |
| 30 | want []byte |
| 31 | wantErr bool |
| 32 | }{ |
| 33 | { |
| 34 | name: "testdata/empty.txt", |
| 35 | want: []byte(""), |
| 36 | }, |
| 37 | { |
| 38 | name: "testdata/empty2.txt", |
| 39 | want: []byte("test"), |
| 40 | }, |
| 41 | { |
| 42 | name: "non-existent", |
| 43 | wantErr: true, |
| 44 | }, |
| 45 | } |
| 46 | |
| 47 | for _, tc := range tests { |
| 48 | t.Run(tc.name, func(t *testing.T) { |
| 49 | t.Parallel() |
| 50 | |
| 51 | fs := New(&testdataDir) |
| 52 | |
| 53 | var got []byte |
| 54 | f, err := fs.Open(tc.name) |
| 55 | if tc.wantErr { |
| 56 | require.Error(t, err) |
| 57 | } else { |
| 58 | require.NoError(t, err) |
| 59 | assert.NotNil(t, f) |
| 60 | |
| 61 | got, err = io.ReadAll(f) |
| 62 | require.NoError(t, err) |
| 63 | } |
| 64 | |
| 65 | assert.Equal(t, tc.want, got) |
| 66 | }) |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | func TestOpenFileFlags(t *testing.T) { |
| 71 | t.Parallel() |
| 72 | |
| 73 | tests := []struct { |
| 74 | name string |
| 75 | file string |
| 76 | flag int |
| 77 | wantErr string |
| 78 | }{ |
| 79 | { |
| 80 | name: "O_CREATE", |
| 81 | file: "testdata/empty.txt", |
| 82 | flag: os.O_CREATE, |
| 83 | wantErr: "read-only filesystem", |
| 84 | }, |
| 85 | { |
| 86 | name: "O_WRONLY", |
| 87 | file: "testdata/empty.txt", |
| 88 | flag: os.O_WRONLY, |
| 89 | wantErr: "read-only filesystem", |
| 90 | }, |
| 91 | { |
| 92 | name: "O_TRUNC", |
| 93 | file: "testdata/empty.txt", |
| 94 | flag: os.O_TRUNC, |
| 95 | wantErr: "read-only filesystem", |
| 96 | }, |
| 97 | { |
| 98 | name: "O_RDWR", |
| 99 | file: "testdata/empty.txt", |
| 100 | flag: os.O_RDWR, |
| 101 | wantErr: "read-only filesystem", |
| 102 | }, |
| 103 | { |
| 104 | name: "O_EXCL", |
| 105 | file: "testdata/empty.txt", |
| 106 | flag: os.O_EXCL, |
| 107 | wantErr: "read-only filesystem", |
| 108 | }, |
| 109 | { |
| 110 | name: "O_RDONLY", |
| 111 | file: "testdata/empty.txt", |
| 112 | flag: os.O_RDONLY, |
| 113 | }, |
| 114 | { |
| 115 | name: "no flags", |
| 116 | file: "testdata/empty.txt", |
| 117 | }, |
| 118 | } |
| 119 | |
| 120 | for _, tc := range tests { |
| 121 | t.Run(tc.name, func(t *testing.T) { |
| 122 | t.Parallel() |
| 123 | fs := New(&testdataDir) |
| 124 | |
| 125 | _, err := fs.OpenFile(tc.file, tc.flag, 0o700) |
| 126 | if tc.wantErr != "" { |
| 127 | require.ErrorContains(t, err, tc.wantErr) |
| 128 | } else { |
| 129 | require.NoError(t, err) |
| 130 | } |
| 131 | }) |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | func TestStat(t *testing.T) { |
| 136 | t.Parallel() |
| 137 | |
| 138 | tests := []struct { |
| 139 | name string |
| 140 | want string |
| 141 | isDir bool |
| 142 | wantErr bool |
| 143 | }{ |
| 144 | { |
| 145 | name: "testdata/empty.txt", |
| 146 | want: "empty.txt", |
| 147 | }, |
| 148 | { |
| 149 | name: "testdata/empty2.txt", |
| 150 | want: "empty2.txt", |
| 151 | }, |
| 152 | { |
| 153 | name: "non-existent", |
| 154 | wantErr: true, |
| 155 | }, |
| 156 | { |
| 157 | name: "testdata", |
| 158 | want: "testdata", |
| 159 | isDir: true, |
| 160 | }, |
| 161 | } |
| 162 | |
| 163 | for _, tc := range tests { |
| 164 | t.Run(tc.name, func(t *testing.T) { |
| 165 | t.Parallel() |
| 166 | fs := New(&testdataDir) |
| 167 | |
| 168 | fi, err := fs.Stat(tc.name) |
| 169 | if tc.wantErr { |
| 170 | require.Error(t, err) |
| 171 | } else { |
| 172 | require.NoError(t, err) |
| 173 | assert.NotNil(t, fi) |
| 174 | |
| 175 | assert.Equal(t, tc.want, fi.Name()) |
| 176 | assert.Equal(t, tc.isDir, fi.IsDir()) |
| 177 | } |
| 178 | }) |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | func TestStatClosesOpenedFile(t *testing.T) { |
| 183 | fsys := &closeCountingFS{ |
| 184 | FS: fstest.MapFS{ |
| 185 | "file.txt": &fstest.MapFile{Data: []byte("content")}, |
| 186 | }, |
| 187 | } |
| 188 | fs := &Embed{underlying: fsys} |
| 189 | |
| 190 | _, err := fs.Stat("file.txt") |
| 191 | require.NoError(t, err) |
| 192 | assert.Equal(t, fsys.opens, fsys.closes) |
| 193 | } |
| 194 | |
| 195 | func TestOpenFileClosesInternalFiles(t *testing.T) { |
| 196 | tests := []struct { |
| 197 | name string |
| 198 | path string |
| 199 | wantErr string |
| 200 | }{ |
| 201 | { |
| 202 | name: "file", |
| 203 | path: "file.txt", |
| 204 | }, |
| 205 | { |
| 206 | name: "directory", |
| 207 | path: "dir", |
| 208 | wantErr: "cannot open directory", |
| 209 | }, |
| 210 | } |
| 211 | |
| 212 | for _, tc := range tests { |
| 213 | t.Run(tc.name, func(t *testing.T) { |
| 214 | fsys := &closeCountingFS{ |
| 215 | FS: fstest.MapFS{ |
| 216 | "file.txt": &fstest.MapFile{Data: []byte("content")}, |
| 217 | "dir/file": &fstest.MapFile{Data: []byte("nested")}, |
| 218 | }, |
| 219 | } |
| 220 | fs := &Embed{underlying: fsys} |
| 221 | |
| 222 | f, err := fs.OpenFile(tc.path, os.O_RDONLY, 0) |
| 223 | if tc.wantErr != "" { |
| 224 | require.ErrorContains(t, err, tc.wantErr) |
| 225 | } else { |
| 226 | require.NoError(t, err) |
| 227 | require.NoError(t, f.Close()) |
| 228 | } |
| 229 | assert.Equal(t, fsys.opens, fsys.closes) |
| 230 | }) |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | type closeCountingFS struct { |
| 235 | iofs.FS |
| 236 | opens int |
| 237 | closes int |
| 238 | } |
| 239 | |
| 240 | func (fs *closeCountingFS) Open(name string) (iofs.File, error) { |
| 241 | f, err := fs.FS.Open(name) |
| 242 | if err != nil { |
| 243 | return nil, err |
| 244 | } |
| 245 | fs.opens++ |
| 246 | return &closeCountingFile{File: f, fs: fs}, nil |
| 247 | } |
| 248 | |
| 249 | type closeCountingFile struct { |
| 250 | iofs.File |
| 251 | fs *closeCountingFS |
| 252 | } |
| 253 | |
| 254 | func (f *closeCountingFile) Close() error { |
| 255 | f.fs.closes++ |
| 256 | return f.File.Close() |
| 257 | } |
| 258 | |
| 259 | func TestReadDir(t *testing.T) { |
| 260 | t.Parallel() |
| 261 | |
| 262 | tests := []struct { |
| 263 | name string |
| 264 | path string |
| 265 | fs *embed.FS |
| 266 | want []string |
| 267 | wantErr bool |
| 268 | }{ |
| 269 | { |
| 270 | name: "singleFile", |
| 271 | path: "testdata", |
| 272 | fs: &singleFile, |
| 273 | want: []string{"empty.txt"}, |
| 274 | }, |
| 275 | { |
| 276 | name: "empty", |
| 277 | path: "", |
| 278 | fs: &empty, |
| 279 | want: []string{}, |
| 280 | wantErr: true, |
| 281 | }, |
| 282 | { |
| 283 | name: "testdataDir w/ path", |
| 284 | path: "testdata", |
| 285 | fs: &testdataDir, |
| 286 | want: []string{"concurrent.bin", "empty.txt", "empty2.txt"}, |
| 287 | }, |
| 288 | { |
| 289 | name: "testdataDir return no dir names", |
| 290 | path: "", |
| 291 | fs: &testdataDir, |
| 292 | want: []string{}, |
| 293 | wantErr: true, |
| 294 | }, |
| 295 | } |
| 296 | |
| 297 | for _, tc := range tests { |
| 298 | t.Run(tc.name, func(t *testing.T) { |
| 299 | t.Parallel() |
| 300 | fs := New(tc.fs) |
| 301 | |
| 302 | fis, err := fs.ReadDir(tc.path) |
| 303 | if tc.wantErr { |
| 304 | require.Error(t, err) |
| 305 | } else { |
| 306 | require.NoError(t, err) |
| 307 | } |
| 308 | |
| 309 | assert.Len(t, fis, len(tc.want)) |
| 310 | matched := 0 |
| 311 | |
| 312 | for _, n := range fis { |
| 313 | for _, w := range tc.want { |
| 314 | if n.Name() == w { |
| 315 | matched++ |
| 316 | } |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | assert.Equal(t, len(tc.want), matched, "not all files matched") |
| 321 | }) |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | func TestUnsupported(t *testing.T) { |
| 326 | t.Parallel() |
| 327 | |
| 328 | fs := New(&testdataDir) |
| 329 | |
| 330 | _, err := fs.Create("test") |
| 331 | require.ErrorIs(t, err, billy.ErrReadOnly) |
| 332 | |
| 333 | err = fs.Remove("test") |
| 334 | require.ErrorIs(t, err, billy.ErrReadOnly) |
| 335 | |
| 336 | err = fs.Rename("test", "test") |
| 337 | require.ErrorIs(t, err, billy.ErrReadOnly) |
| 338 | |
| 339 | err = fs.MkdirAll("test", 0o700) |
| 340 | require.ErrorIs(t, err, billy.ErrReadOnly) |
| 341 | } |
| 342 | |
| 343 | func TestFileUnsupported(t *testing.T) { |
| 344 | t.Parallel() |
| 345 | |
| 346 | fs := New(&testdataDir) |
| 347 | |
| 348 | f, err := fs.Open("testdata/empty.txt") |
| 349 | require.NoError(t, err) |
| 350 | assert.NotNil(t, f) |
| 351 | |
| 352 | _, err = f.Write([]byte("foo")) |
| 353 | require.ErrorIs(t, err, billy.ErrReadOnly) |
| 354 | |
| 355 | err = f.Truncate(0) |
| 356 | require.ErrorIs(t, err, billy.ErrReadOnly) |
| 357 | } |
| 358 | |
| 359 | func TestFileSeek(t *testing.T) { |
| 360 | fs := New(&testdataDir) |
| 361 | |
| 362 | f, err := fs.Open("testdata/empty2.txt") |
| 363 | require.NoError(t, err) |
| 364 | assert.NotNil(t, f) |
| 365 | |
| 366 | tests := []struct { |
| 367 | seekOff int64 |
| 368 | seekWhence int |
| 369 | want string |
| 370 | }{ |
| 371 | {seekOff: 3, seekWhence: io.SeekStart, want: ""}, |
| 372 | {seekOff: 3, seekWhence: io.SeekStart, want: "t"}, |
| 373 | {seekOff: 2, seekWhence: io.SeekStart, want: "st"}, |
| 374 | {seekOff: 1, seekWhence: io.SeekStart, want: "est"}, |
| 375 | {seekOff: 0, seekWhence: io.SeekStart, want: "test"}, |
| 376 | {seekOff: 0, seekWhence: io.SeekStart, want: "t"}, |
| 377 | {seekOff: 1, seekWhence: io.SeekCurrent, want: "s"}, |
| 378 | {seekOff: -2, seekWhence: io.SeekEnd, want: "st"}, |
| 379 | } |
| 380 | |
| 381 | for i, tc := range tests { |
| 382 | t.Run(fmt.Sprintf("#%d", i), func(t *testing.T) { |
| 383 | //nolint:tparallel |
| 384 | _, err = f.Seek(tc.seekOff, tc.seekWhence) |
| 385 | require.NoError(t, err) |
| 386 | |
| 387 | data := make([]byte, len(tc.want)) |
| 388 | n, err := f.Read(data) |
| 389 | require.NoError(t, err) |
| 390 | assert.Equal(t, len(tc.want), n) |
| 391 | assert.Equal(t, []byte(tc.want), data) |
| 392 | }) |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | func TestJoin(t *testing.T) { |
| 397 | t.Parallel() |
| 398 | |
| 399 | tests := []struct { |
| 400 | name string |
| 401 | path []string |
| 402 | want string |
| 403 | }{ |
| 404 | { |
| 405 | name: "no leading slash", |
| 406 | path: []string{"data", "foo/bar"}, |
| 407 | want: "data/foo/bar", |
| 408 | }, |
| 409 | { |
| 410 | name: "w/ leading slash", |
| 411 | path: []string{"/data", "foo/bar"}, |
| 412 | want: "/data/foo/bar", |
| 413 | }, |
| 414 | { |
| 415 | name: "..", |
| 416 | path: []string{"/data", "../bar"}, |
| 417 | want: "/bar", |
| 418 | }, |
| 419 | { |
| 420 | name: ".", |
| 421 | path: []string{"/data", "./bar"}, |
| 422 | want: "/data/bar", |
| 423 | }, |
| 424 | } |
| 425 | |
| 426 | for _, tc := range tests { |
| 427 | t.Run(tc.name, func(t *testing.T) { |
| 428 | t.Parallel() |
| 429 | fs := New(&empty) |
| 430 | |
| 431 | got := fs.Join(tc.path...) |
| 432 | assert.Equal(t, tc.want, got) |
| 433 | }) |
| 434 | } |
| 435 | } |
| 436 | |
| 437 | func TestCapabilities(t *testing.T) { |
| 438 | fs := New(&testdataDir) |
| 439 | _, ok := fs.(billy.Capable) |
| 440 | assert.True(t, ok) |
| 441 | |
| 442 | want := billy.ReadCapability | billy.SeekCapability |
| 443 | got := billy.Capabilities(fs) |
| 444 | assert.Equal(t, want, got) |
| 445 | } |