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

storage/local: fix directory list url join #583

Merged
merged 2 commits into from
Jun 6, 2018
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
6 changes: 5 additions & 1 deletion storage/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,12 @@ func (local *Local) List(ctx context.Context, url string) ([]*Object, error) {

var objects []*Object
for _, f := range files {
url, err := local.Join(url, f.Rel)
if err != nil {
return nil, err
}
objects = append(objects, &Object{
URL: filepath.Join(url, f.Rel),
URL: url,
Name: f.Rel,
LastModified: f.LastModified,
Size: f.Size,
Expand Down
93 changes: 93 additions & 0 deletions storage/local_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ import (
"io/ioutil"
"os"
"path"
"path/filepath"
"testing"

"github.com/go-test/deep"
)

func TestLocalSupports(t *testing.T) {
Expand Down Expand Up @@ -50,6 +53,96 @@ func TestLocalGet(t *testing.T) {
}
}

// Tests List on a local directory.
func TestLocalListDirectory(t *testing.T) {
ctx := context.Background()
d, err := filepath.Abs("../tests/storage/testdata/")
if err != nil {
t.Fatal(err)
}

l := &Local{
allowedDirs: []string{d},
}

stat1, _ := os.Stat("../tests/storage/testdata/test_dir/subdir/test_dir_file1")
stat2, _ := os.Stat("../tests/storage/testdata/test_dir/test_dir_file2")
lm1 := stat1.ModTime()
lm2 := stat2.ModTime()

list, gerr := l.List(ctx, "file://"+d+"/test_dir")
if gerr != nil {
t.Fatal(gerr)
}

expected1 := []*Object{
{
URL: "file://" + d + "/test_dir/subdir/test_dir_file1",
Name: "subdir/test_dir_file1",
LastModified: lm1,
Size: 14,
},
{
URL: "file://" + d + "/test_dir/test_dir_file2",
Name: "test_dir_file2",
LastModified: lm2,
Size: 14,
},
}

for _, diff := range deep.Equal(list, expected1) {
t.Error(diff)
}

list, gerr = l.List(ctx, d+"/test_dir")
if gerr != nil {
t.Fatal(gerr)
}

expected2 := []*Object{
{
URL: d + "/test_dir/subdir/test_dir_file1",
Name: "subdir/test_dir_file1",
LastModified: lm1,
Size: 14,
},
{
URL: d + "/test_dir/test_dir_file2",
Name: "test_dir_file2",
LastModified: lm2,
Size: 14,
},
}

for _, diff := range deep.Equal(list, expected2) {
t.Error(diff)
}

list, gerr = l.List(ctx, "../tests/storage/testdata/test_dir")
if gerr != nil {
t.Fatal(gerr)
}

expected3 := []*Object{
{
URL: "../tests/storage/testdata/test_dir/subdir/test_dir_file1",
Name: "subdir/test_dir_file1",
LastModified: lm1,
Size: 14,
},
{
URL: "../tests/storage/testdata/test_dir/test_dir_file2",
Name: "test_dir_file2",
LastModified: lm2,
Size: 14,
},
}

for _, diff := range deep.Equal(list, expected3) {
t.Error(diff)
}
}

func TestLocalJoin(t *testing.T) {
l := &Local{}
j1, _ := l.Join("file:///foo/bar", "baz")
Expand Down