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

Return fs.ModeDir for Git folders in the workspace #1521

Merged
merged 1 commit into from
Jun 24, 2024
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
25 changes: 15 additions & 10 deletions libs/filer/workspace_files_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,17 @@ func (entry wsfsDirEntry) Info() (fs.FileInfo, error) {
return entry.wsfsFileInfo, nil
}

func wsfsDirEntriesFromObjectInfos(objects []workspace.ObjectInfo) []fs.DirEntry {
info := make([]fs.DirEntry, len(objects))
for i, v := range objects {
info[i] = wsfsDirEntry{wsfsFileInfo{oi: v}}
}

// Sort by name for parity with os.ReadDir.
sort.Slice(info, func(i, j int) bool { return info[i].Name() < info[j].Name() })
return info
}

// Type that implements fs.FileInfo for WSFS.
type wsfsFileInfo struct {
oi workspace.ObjectInfo
Expand All @@ -50,7 +61,7 @@ func (info wsfsFileInfo) Size() int64 {

func (info wsfsFileInfo) Mode() fs.FileMode {
switch info.oi.ObjectType {
case workspace.ObjectTypeDirectory:
case workspace.ObjectTypeDirectory, workspace.ObjectTypeRepo:
return fs.ModeDir
default:
return fs.ModePerm
Expand All @@ -62,7 +73,7 @@ func (info wsfsFileInfo) ModTime() time.Time {
}

func (info wsfsFileInfo) IsDir() bool {
return info.oi.ObjectType == workspace.ObjectTypeDirectory
return info.Mode() == fs.ModeDir
}

func (info wsfsFileInfo) Sys() any {
Expand Down Expand Up @@ -262,14 +273,8 @@ func (w *WorkspaceFilesClient) ReadDir(ctx context.Context, name string) ([]fs.D
return nil, err
}

info := make([]fs.DirEntry, len(objects))
for i, v := range objects {
info[i] = wsfsDirEntry{wsfsFileInfo{oi: v}}
}

// Sort by name for parity with os.ReadDir.
sort.Slice(info, func(i, j int) bool { return info[i].Name() < info[j].Name() })
return info, nil
// Convert to fs.DirEntry.
return wsfsDirEntriesFromObjectInfos(objects), nil
}

func (w *WorkspaceFilesClient) Mkdir(ctx context.Context, name string) error {
Expand Down
56 changes: 56 additions & 0 deletions libs/filer/workspace_files_client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package filer

import (
"io/fs"
"testing"

"github.com/databricks/databricks-sdk-go/service/workspace"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestWorkspaceFilesDirEntry(t *testing.T) {
entries := wsfsDirEntriesFromObjectInfos([]workspace.ObjectInfo{
{
Path: "/dir",
ObjectType: workspace.ObjectTypeDirectory,
},
{
Path: "/file",
ObjectType: workspace.ObjectTypeFile,
Size: 42,
},
{
Path: "/repo",
ObjectType: workspace.ObjectTypeRepo,
},
})

// Confirm the path is passed through correctly.
assert.Equal(t, "dir", entries[0].Name())
assert.Equal(t, "file", entries[1].Name())
assert.Equal(t, "repo", entries[2].Name())

// Confirm the type is passed through correctly.
assert.Equal(t, fs.ModeDir, entries[0].Type())
assert.Equal(t, fs.ModePerm, entries[1].Type())
assert.Equal(t, fs.ModeDir, entries[2].Type())

// Get [fs.FileInfo] from directory entry.
i0, err := entries[0].Info()
require.NoError(t, err)
i1, err := entries[1].Info()
require.NoError(t, err)
i2, err := entries[2].Info()
require.NoError(t, err)

// Confirm size.
assert.Equal(t, int64(0), i0.Size())
assert.Equal(t, int64(42), i1.Size())
assert.Equal(t, int64(0), i2.Size())

// Confirm IsDir.
assert.True(t, i0.IsDir())
assert.False(t, i1.IsDir())
assert.True(t, i2.IsDir())
}
Loading