tractor.dev/toolkit-go@v0.0.0-20241010005851-214d91207d07/engine/fs/makefs/makefs_test.go (about) 1 package makefs 2 3 import ( 4 "bytes" 5 "testing" 6 7 "tractor.dev/toolkit-go/engine/fs/fstest" 8 "tractor.dev/toolkit-go/engine/fs/memfs" 9 ) 10 11 type Apply struct { 12 Fn func([]byte) []byte 13 b bytes.Buffer 14 } 15 16 func (x *Apply) Reset() {} 17 18 func (x *Apply) Transform(dst, src []byte, atEOF bool) (nDst, nSrc int, err error) { 19 n := copy(dst, x.Fn(src)) 20 return n, n, nil 21 } 22 23 func TestMountOpener(t *testing.T) { 24 base := memfs.New() 25 26 fstest.WriteFS(t, base, map[string]string{ 27 "src/hello": "Hello World", 28 }) 29 30 fsys := MountOpener(base, "dst/upper", TransformFrom(base, "src/hello", &Apply{Fn: bytes.ToUpper})) 31 fsys = MountOpener(fsys, "dst/lower", TransformFrom(fsys, "src/hello", &Apply{Fn: bytes.ToLower})) 32 33 fstest.CheckFS(t, fsys, map[string]string{ 34 "src/hello": "Hello World", 35 "dst/upper": "HELLO WORLD", 36 "dst/lower": "hello world", 37 }) 38 39 fsys = MountOpener(fsys, "dst/bundle", MakeOpener(fsys, func(files []OpenFile) ([]byte, error) { 40 var buf bytes.Buffer 41 for _, f := range files { 42 buf.ReadFrom(f.File) 43 } 44 return buf.Bytes(), nil 45 }, "dst/*")) 46 47 fsys = MountOpener(base, "gen", MakeOpener(fsys, func(files []OpenFile) ([]byte, error) { 48 return []byte("pretend generated"), nil 49 })) 50 51 fstest.CheckFS(t, fsys, map[string]string{ 52 "src/hello": "Hello World", 53 "dst/upper": "HELLO WORLD", 54 "dst/lower": "hello world", 55 "dst/bundle": "hello worldHELLO WORLD", 56 "gen": "pretend generated", 57 }) 58 59 }