Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix file.Readdir to not return sub-dir contents #55

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion fs/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ func (f *httpFile) Readdir(count int) ([]os.FileInfo, error) {
}
prefix := f.Name()
for fn, f := range f.file.fs.files {
if strings.HasPrefix(fn, prefix) && len(fn) > len(prefix) {
if strings.HasPrefix(fn, prefix) && len(fn) > len(prefix) && strings.Index(strings.TrimPrefix(fn, prefix), "/") < 1 {
fis = append(fis, f.FileInfo)
}
}
Expand Down
62 changes: 62 additions & 0 deletions fs/walk_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package fs

import (
"os"
"testing"
)

func TestWalk(t *testing.T) {
type wantPath struct {
isDir bool
}
tests := []struct {
description string
zipData string
wantPaths map[string]wantPath
}{
{
zipData: mustZipTree("../testdata/index"),
wantPaths: map[string]wantPath{
"/": wantPath{isDir: true},
"/index.html": wantPath{isDir: false},
"/sub_dir": wantPath{isDir: true},
"/sub_dir/index.html": wantPath{isDir: false},
},
},
}

for _, tc := range tests {
t.Run(tc.description, func(t *testing.T) {
Register(tc.zipData)
fs, err := New()
if err != nil {
t.Errorf("New() = %v", err)
return
}

err = Walk(fs, "/", func(path string, info os.FileInfo, err error) error {
if err != nil {
t.Errorf("unexpected error = %v", err)
}
if wantPath, ok := tc.wantPaths[path]; ok {
if got, want := info.IsDir(), wantPath.isDir; got != want {
t.Errorf("IsDir(%v) = %t; want %t", path, got, want)
}
delete(tc.wantPaths, path)
} else {
t.Errorf("unexpected path = %v (info = %#v)", path, info)
}

return nil
})

if err != nil {
t.Errorf("Walk(fs, \"/\", WalkFunc) = %v", err)
}

if len(tc.wantPaths) != 0 {
t.Errorf("ignored paths: %v", tc.wantPaths)
}
})
}
}