Skip to content
This repository has been archived by the owner on Feb 24, 2024. It is now read-only.

using path rather than filepath for URL-ish internal expressions #2262

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,28 @@ func NewFS(embed fs.ReadDirFS, dir string) FS {
}
}

// Open implements the FS interface.
// Open opens the named file.
//
// When Open returns an error, it should be of type *PathError with the Op
// field set to "open", the Path field set to name, and the Err field
// describing the problem.
//
// Open should reject attempts to open names that do not satisfy
// ValidPath(name), returning a *PathError with Err set to ErrInvalid or
// ErrNotExist.
func (f FS) Open(name string) (fs.File, error) {
if name == "embed.go" {
return nil, fs.ErrNotExist
return nil, &fs.PathError{
Op: "open",
Path: name,
Err: fs.ErrNotExist,
}
}
file, err := f.getFile(name)
if name == "." {
// NOTE: It always returns the root from the "disk" instead
// "embed". However, it could be fine since the the purpose
// of buffalo.FS isn't supporting full featured filesystem.
return rootFile{file}, err
}
return file, err
Expand Down
24 changes: 21 additions & 3 deletions fs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,15 @@ func Test_FS_Prioritizes_Disk(t *testing.T) {
r.NoError(err)

r.Equal("This file is on disk.", string(b))

// should handle slash-separated path for all systems including Windows
f, err = fsys.Open("under/sub/subfile")
r.NoError(err)

b, err = io.ReadAll(f)
r.NoError(err)

r.Equal("This file is on disk/sub.", string(b))
}

func Test_FS_Uses_Embed_If_No_Disk(t *testing.T) {
Expand All @@ -63,6 +72,15 @@ func Test_FS_Uses_Embed_If_No_Disk(t *testing.T) {
r.NoError(err)

r.Equal("This file is embedded.", string(b))

// should handle slash-separated path for all systems including Windows
f, err = fsys.Open("under/sub/subfile")
r.NoError(err)

b, err = io.ReadAll(f)
r.NoError(err)

r.Equal("This file is on embedded/sub.", string(b))
}

func Test_FS_ReadDirFile(t *testing.T) {
Expand All @@ -87,11 +105,11 @@ func Test_FS_ReadDirFile(t *testing.T) {
r.LessOrEqual(len(entries), 1, "a call to ReadDir must at most return n entries")

// Second read should return at most 2 files
entries, err = dir.ReadDir(2)
entries, err = dir.ReadDir(3)
r.NoError(err)

// The actual len will be 2 (file.txt & file2.txt)
r.LessOrEqual(len(entries), 2, "a call to ReadDir must at most return n entries")
// The actual len will be 2 (file.txt & file2.txt + under/)
r.LessOrEqual(len(entries), 3, "a call to ReadDir must at most return n entries")

// trying to read next 2 files (none left)
entries, err = dir.ReadDir(2)
Expand Down
1 change: 1 addition & 0 deletions internal/testdata/disk/under/sub/subfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This file is on disk/sub.
1 change: 1 addition & 0 deletions internal/testdata/embedded/under/sub/subfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This file is on embedded/sub.
13 changes: 6 additions & 7 deletions render/auto.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"io"
"net/http"
"path"
"path/filepath"
"reflect"
"strings"

Expand Down Expand Up @@ -117,34 +116,34 @@ func (ir htmlAutoRenderer) Render(w io.Writer, data Data) error {
}

if data["method"] == "PUT" {
return ir.HTML(filepath.Join(templatePrefix.String(), "edit.html")).Render(w, data)
return ir.HTML(path.Join(templatePrefix.String(), "edit.html")).Render(w, data)
}

return ir.HTML(filepath.Join(templatePrefix.String(), "new.html")).Render(w, data)
return ir.HTML(path.Join(templatePrefix.String(), "new.html")).Render(w, data)
}
return nil
}

cp, ok := data["current_path"].(string)

defCase := func() error {
return ir.HTML(filepath.Join(templatePrefix.String(), "index.html")).Render(w, data)
return ir.HTML(path.Join(templatePrefix.String(), "index.html")).Render(w, data)
}

if !ok {
return defCase()
}

if strings.HasSuffix(cp, "/edit/") {
return ir.HTML(filepath.Join(templatePrefix.String(), "edit.html")).Render(w, data)
return ir.HTML(path.Join(templatePrefix.String(), "edit.html")).Render(w, data)
}

if strings.HasSuffix(cp, "/new/") {
return ir.HTML(filepath.Join(templatePrefix.String(), "new.html")).Render(w, data)
return ir.HTML(path.Join(templatePrefix.String(), "new.html")).Render(w, data)
}

if !isPlural {
return ir.HTML(filepath.Join(templatePrefix.String(), "show.html")).Render(w, data)
return ir.HTML(path.Join(templatePrefix.String(), "show.html")).Render(w, data)
}

return defCase()
Expand Down
7 changes: 5 additions & 2 deletions render/template_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"html/template"
"io"
"path"
"path/filepath"

ht "github.com/gobuffalo/helpers/tags"
Expand Down Expand Up @@ -49,7 +50,7 @@ func assetPathFor(file string) string {
if filePath == "" || !ok {
filePath = file
}
return filepath.ToSlash(filepath.Join("/assets", filePath))
return path.Join("/assets", filePath)
}

func loadManifest(manifest io.Reader) error {
Expand All @@ -59,7 +60,9 @@ func loadManifest(manifest io.Reader) error {
return err
}
for k, v := range m {
assetMap.Store(k, v)
// I don't think v has backslash but if so, correct them when
// creating the map instead using the value in `assetPathFor()`.
assetMap.Store(k, filepath.ToSlash(v))
}
return nil
}