Skip to content
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
13 changes: 8 additions & 5 deletions base/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@ package base

import (
"context"
"github.com/viant/afs/file"
"github.com/viant/afs/object"
"github.com/viant/afs/option"
"github.com/viant/afs/storage"
"github.com/viant/afs/url"
"io"
"os"
"path"
"reflect"
"sync"

"github.com/viant/afs/file"
"github.com/viant/afs/object"
"github.com/viant/afs/option"
"github.com/viant/afs/storage"
"github.com/viant/afs/url"
)

// Manager represents Storager base manager
Expand Down Expand Up @@ -67,8 +68,10 @@ func (m *Manager) List(ctx context.Context, URL string, options ...storage.Optio

_, name := path.Split(URLPath)
if files[0].Name() == "" {
// To verify: this seems to be a special handler for embedded URLs
files[0] = file.NewInfo(name, files[0].Size(), files[0].Mode(), files[0].ModTime(), files[0].IsDir())
}

fileURL := ""
for i := 0; i < len(files); i++ {
if i == 0 && files[i].Name() == name {
Expand Down
112 changes: 93 additions & 19 deletions list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@ package afs
import (
"context"
"fmt"
"os"
"path"
"testing"

"github.com/stretchr/testify/assert"
"github.com/viant/afs/asset"
"github.com/viant/afs/file"
"github.com/viant/afs/option"
"os"
"path"
"testing"
)

func TestService_List(t *testing.T) {
func TestServiceList(t *testing.T) {
baseDir := os.TempDir()
ctx := context.Background()

Expand All @@ -24,8 +25,8 @@ func TestService_List(t *testing.T) {
assets []*asset.Resource
}{
{
description: "multi resource walk",
baseLocation: "file://localhost/" + path.Join(baseDir, "service_walk"),
description: "l2 walk",
baseLocation: "file://localhost" + path.Join(baseDir, "service_walk_l2"),
assets: []*asset.Resource{
asset.NewFile("foo1.txt", []byte("abc"), 0644),
asset.NewDir("s1", file.DefaultDirOsMode),
Expand All @@ -37,8 +38,8 @@ func TestService_List(t *testing.T) {
},
},
{
description: "multi resource walk",
baseLocation: "file://localhost/" + path.Join(baseDir, "service_walk"),
description: "l3 walk",
baseLocation: "file://localhost" + path.Join(baseDir, "service_walk_l3"),
assets: []*asset.Resource{
asset.NewFile("foo1.txt", []byte("abc"), 0644),
asset.NewDir("r/s1/s1", file.DefaultDirOsMode),
Expand All @@ -59,20 +60,93 @@ func TestService_List(t *testing.T) {
err := asset.Create(fileManager, useCase.baseLocation, useCase.assets)
assert.Nil(t, err, useCase.description)

actuals := map[string]bool{}
removePrefixLen := len(useCase.baseLocation)

// Non-recursive List
{
actuals := map[string]bool{}
objects, err := service.List(ctx, useCase.baseLocation)
assert.Nil(t, err)
for _, object := range objects {
URL := object.URL()

alsoRemoveSlash := 0
if len(URL) > removePrefixLen {
alsoRemoveSlash = 1
}

renamed := string(URL[removePrefixLen+alsoRemoveSlash:])

actuals[renamed] = true
}

objects, err := service.List(ctx, useCase.baseLocation, option.NewRecursive(true))
assert.Nil(t, err)
for _, object := range objects {
URL := object.URL()
relative := string(URL[len(useCase.baseLocation):])
actuals[relative] = true
assert.Nil(t, err, useCase.description)
for _, asset := range useCase.assets {
dir := path.Dir(asset.Name)
if dir != "." {
continue
}

_, ok := actuals[asset.Name]
assert.True(t, ok, fmt.Sprintf(useCase.description+" missing %v, %v", asset.Name, actuals))
}

assetMap := map[string]bool{}
for _, asset := range useCase.assets {
assetMap[asset.Name] = true

// include all subdirs as visible items
dir := path.Dir(asset.Name)
for dir != "." && assetMap[dir] != true {
assetMap[dir] = true
dir = path.Dir(dir)
}
}

// non-recursive list includes root named ""
assetMap[""] = true

for filename := range actuals {
_, ok := assetMap[filename]
assert.True(t, ok, fmt.Sprintf(useCase.description+" unexpected %v, %v", filename, actuals))
}
}

assert.Nil(t, err, useCase.description)
for _, asset := range useCase.assets {
_, ok := actuals[asset.Name]
assert.True(t, ok, fmt.Sprintf(useCase.description+" %v, %v", asset.Name, actuals))
// Recursive List
{
actuals := map[string]bool{}
objects, err := service.List(ctx, useCase.baseLocation, option.NewRecursive(true))
assert.Nil(t, err)
for _, object := range objects {
URL := object.URL()
relative := string(URL[removePrefixLen+1:])
actuals[relative] = true
}

assert.Nil(t, err, useCase.description)
for _, asset := range useCase.assets {
_, ok := actuals[asset.Name]
assert.True(t, ok, fmt.Sprintf(useCase.description+" missing %v, %v", asset.Name, actuals))
}

assetMap := map[string]bool{}
for _, asset := range useCase.assets {
assetMap[asset.Name] = true

// include all subdirs as visible items
dir := path.Dir(asset.Name)
for dir != "." && assetMap[dir] != true {
assetMap[dir] = true
dir = path.Dir(dir)
}
}

// recursive list does not include root named ""

for filename := range actuals {
_, ok := assetMap[filename]
assert.True(t, ok, fmt.Sprintf(useCase.description+" unexpected %v, %v", filename, actuals))
}
}

}
Expand Down
Loading