diff options
| author | Shulhan <ms@kilabit.info> | 2023-11-15 02:26:08 +0700 |
|---|---|---|
| committer | Shulhan <ms@kilabit.info> | 2023-11-15 02:26:08 +0700 |
| commit | d648ac73198f3a829c68a179c50643fca6cb66ed (patch) | |
| tree | 7efd5d469efcae626e3f98965218c3baf8ab4fd3 /lib | |
| parent | 0021f75d8abb4e107287781b88e2556c83c9dc28 (diff) | |
| download | pakakeh.go-d648ac73198f3a829c68a179c50643fca6cb66ed.tar.xz | |
lib/memfs: check for empty path on Get
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/memfs/memfs.go | 4 | ||||
| -rw-r--r-- | lib/memfs/memfs_test.go | 11 |
2 files changed, 11 insertions, 4 deletions
diff --git a/lib/memfs/memfs.go b/lib/memfs/memfs.go index 09623779..f75fe95d 100644 --- a/lib/memfs/memfs.go +++ b/lib/memfs/memfs.go @@ -247,6 +247,10 @@ func (mfs *MemFS) Get(path string) (node *Node, err error) { if mfs == nil || mfs.PathNodes == nil { return nil, fmt.Errorf("%s %s: %w", logp, path, os.ErrNotExist) } + path = strings.TrimSpace(path) + if len(path) == 0 { + return nil, fmt.Errorf(`%s: empty path`, logp) + } node = mfs.PathNodes.Get(path) if node == nil { diff --git a/lib/memfs/memfs_test.go b/lib/memfs/memfs_test.go index eb10bcd4..2e02c4f0 100644 --- a/lib/memfs/memfs_test.go +++ b/lib/memfs/memfs_test.go @@ -253,7 +253,7 @@ func TestMemFS_AddFile(t *testing.T) { func TestMemFS_Get(t *testing.T) { cases := []struct { - expErr error + expErr string path string expV []byte expContentType []string @@ -263,7 +263,7 @@ func TestMemFS_Get(t *testing.T) { path: "/exclude", }, { path: "/exclude/dir", - expErr: os.ErrNotExist, + expErr: os.ErrNotExist.Error(), }, { path: "/exclude/index-link.css", expV: []byte("body {\n}\n"), @@ -283,7 +283,7 @@ func TestMemFS_Get(t *testing.T) { path: "/include", }, { path: "/include/dir", - expErr: os.ErrNotExist, + expErr: os.ErrNotExist.Error(), }, { path: "/include/index.css", expV: []byte("body {\n}\n"), @@ -316,6 +316,9 @@ func TestMemFS_Get(t *testing.T) { }, { path: "/plain", expContentType: []string{"application/octet-stream"}, + }, { + path: ``, + expErr: `Get: empty path`, }} dir := filepath.Join(_testWD, "/testdata") @@ -334,7 +337,7 @@ func TestMemFS_Get(t *testing.T) { for _, c := range cases { got, err := mfs.Get(c.path) if err != nil { - test.Assert(t, c.path+": error", c.expErr, err) + test.Assert(t, c.path+": error", c.expErr, err.Error()) continue } |
