From 55254234bfd7398f0301dc1d3b062d9d77bc2283 Mon Sep 17 00:00:00 2001 From: Neale Pickett Date: Fri, 3 Dec 2021 17:58:08 -0700 Subject: [PATCH] new SubFS that can tell you the full FS path --- pkg/subfs/subfs.go | 28 ++++++++++++++++++++++++++++ pkg/subfs/subfs_test.go | 26 ++++++++++++++++++++++++++ pkg/subfs/testdata/moo.txt | 1 + pkg/subfs/testdata/subdir/moo2.txt | 1 + 4 files changed, 56 insertions(+) create mode 100644 pkg/subfs/subfs.go create mode 100644 pkg/subfs/subfs_test.go create mode 100644 pkg/subfs/testdata/moo.txt create mode 100644 pkg/subfs/testdata/subdir/moo2.txt diff --git a/pkg/subfs/subfs.go b/pkg/subfs/subfs.go new file mode 100644 index 0000000..05c4215 --- /dev/null +++ b/pkg/subfs/subfs.go @@ -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 +} diff --git a/pkg/subfs/subfs_test.go b/pkg/subfs/subfs_test.go new file mode 100644 index 0000000..f1a2583 --- /dev/null +++ b/pkg/subfs/subfs_test.go @@ -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") + } +} diff --git a/pkg/subfs/testdata/moo.txt b/pkg/subfs/testdata/moo.txt new file mode 100644 index 0000000..02259ea --- /dev/null +++ b/pkg/subfs/testdata/moo.txt @@ -0,0 +1 @@ +moo. diff --git a/pkg/subfs/testdata/subdir/moo2.txt b/pkg/subfs/testdata/subdir/moo2.txt new file mode 100644 index 0000000..edad669 --- /dev/null +++ b/pkg/subfs/testdata/subdir/moo2.txt @@ -0,0 +1 @@ +moo too.