mirror of https://github.com/dirtbags/moth.git
new SubFS that can tell you the full FS path
This commit is contained in:
parent
b6eea388d9
commit
55254234bf
|
@ -0,0 +1,28 @@
|
|||
package transpile
|
||||
|
||||
import (
|
||||
"io/fs"
|
||||
"path"
|
||||
)
|
||||
|
||||
func Sub(fsys fs.FS, dir string) (*SubFS, error) {
|
||||
return &SubFS{fsys, dir}, nil
|
||||
}
|
||||
|
||||
type SubFS struct {
|
||||
fs.FS
|
||||
dir string
|
||||
}
|
||||
|
||||
func (f *SubFS) FullName(name string) string {
|
||||
return path.Join(f.dir, name)
|
||||
}
|
||||
|
||||
func (f *SubFS) Sub(dir string) (*SubFS, error) {
|
||||
newFS, err := fs.Sub(f, dir)
|
||||
newSubFS := SubFS{
|
||||
FS: newFS,
|
||||
dir: f.FullName(dir),
|
||||
}
|
||||
return &newSubFS, err
|
||||
}
|
|
@ -0,0 +1,26 @@
|
|||
package transpile
|
||||
|
||||
import (
|
||||
"io/fs"
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestSubFS(t *testing.T) {
|
||||
testdata := os.DirFS("testdata")
|
||||
if static, err := Sub(testdata, "static"); err != nil {
|
||||
t.Error(err)
|
||||
} else if buf, err := fs.ReadFile(static, "moo.txt"); err != nil {
|
||||
t.Error(err)
|
||||
} else if string(buf) != "moo.\n" {
|
||||
t.Error("Wrong file contents")
|
||||
} else if subdir, err := static.Sub("subdir"); err != nil {
|
||||
t.Error(err)
|
||||
} else if buf, err := fs.ReadFile(subdir, "moo2.txt"); err != nil {
|
||||
t.Error(err)
|
||||
} else if string(buf) != "moo too.\n" {
|
||||
t.Error("Wrong file contents too")
|
||||
} else if subdir.FullName("glue") != "static/subdir/glue" {
|
||||
t.Error("Wrong full name")
|
||||
}
|
||||
}
|
|
@ -0,0 +1 @@
|
|||
moo.
|
|
@ -0,0 +1 @@
|
|||
moo too.
|
Loading…
Reference in New Issue