Skip to content

Commit

Permalink
Support use io/fs.FS directly (#97)
Browse files Browse the repository at this point in the history
  • Loading branch information
lunny authored Nov 11, 2021
1 parent a3f5fc8 commit b467258
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 0 deletions.
30 changes: 30 additions & 0 deletions fs_embed.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
//go:build go1.16
// +build go1.16

package render

import (
"embed"
"io"
"io/fs"
"path/filepath"
)
Expand All @@ -29,3 +31,31 @@ func (e *EmbedFileSystem) Walk(root string, walkFn filepath.WalkFunc) error {
return walkFn(path, info, err)
})
}

type tmplFS struct {
fs.FS
}

func (tfs tmplFS) Walk(root string, walkFn filepath.WalkFunc) error {
return fs.WalkDir(tfs, root, func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
info, err := d.Info()
return walkFn(path, info, err)
})
}

func (tfs tmplFS) ReadFile(filename string) ([]byte, error) {
f, err := tfs.Open(filename)
if err != nil {
return nil, err
}
defer f.Close()
return io.ReadAll(f)
}

// FS converts io/fs.FS to FileSystem
func FS(oriFS fs.FS) FileSystem {
return tmplFS{oriFS}
}
91 changes: 91 additions & 0 deletions fs_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
//go:build go1.16
// +build go1.16

package render

import (
"net/http"
"net/http/httptest"
"os"
"testing"
)

func TestIOFSEmbedTemplateLookup(t *testing.T) {
baseDir := "testdata/template-dir-test"
fname0Rel := "0"
fname1Rel := "subdir/1"
fnameShouldParsedRel := "dedicated.tmpl/notbad"
dirShouldNotParsedRel := "dedicated"

r := New(Options{
Directory: baseDir,
Extensions: []string{".tmpl", ".html"},
FileSystem: FS(EmbedFixtures),
})

expect(t, r.TemplateLookup(fname1Rel) != nil, true)
expect(t, r.TemplateLookup(fname0Rel) != nil, true)
expect(t, r.TemplateLookup(fnameShouldParsedRel) != nil, true)
expect(t, r.TemplateLookup(dirShouldNotParsedRel) == nil, true)
}

func TestIOFSDirTemplateLookup(t *testing.T) {
baseDir := "testdata/template-dir-test"
fname0Rel := "0"
fname1Rel := "subdir/1"
fnameShouldParsedRel := "dedicated.tmpl/notbad"
dirShouldNotParsedRel := "dedicated"

r := New(Options{
Directory: ".",
Extensions: []string{".tmpl", ".html"},
FileSystem: FS(os.DirFS(baseDir)),
})

expect(t, r.TemplateLookup(fname1Rel) != nil, true)
expect(t, r.TemplateLookup(fname0Rel) != nil, true)
expect(t, r.TemplateLookup(fnameShouldParsedRel) != nil, true)
expect(t, r.TemplateLookup(dirShouldNotParsedRel) == nil, true)
}

func TestIOFSEmbedHTMLBasic(t *testing.T) {
render := New(Options{
Directory: "testdata/basic",
FileSystem: FS(EmbedFixtures),
})

var err error
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
err = render.HTML(w, http.StatusOK, "hello", "gophers")
})

res := httptest.NewRecorder()
req, _ := http.NewRequestWithContext(ctx, "GET", "/foo", nil)
h.ServeHTTP(res, req)

expectNil(t, err)
expect(t, res.Code, 200)
expect(t, res.Header().Get(ContentType), ContentHTML+"; charset=UTF-8")
expect(t, res.Body.String(), "<h1>Hello gophers</h1>\n")
}

func TestIOFSDirHTMLBasic(t *testing.T) {
render := New(Options{
Directory: ".",
FileSystem: FS(os.DirFS("testdata/basic")),
})

var err error
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
err = render.HTML(w, http.StatusOK, "hello", "gophers")
})

res := httptest.NewRecorder()
req, _ := http.NewRequestWithContext(ctx, "GET", "/foo", nil)
h.ServeHTTP(res, req)

expectNil(t, err)
expect(t, res.Code, 200)
expect(t, res.Header().Get(ContentType), ContentHTML+"; charset=UTF-8")
expect(t, res.Body.String(), "<h1>Hello gophers</h1>\n")
}

0 comments on commit b467258

Please sign in to comment.