Skip to content

Commit

Permalink
Return fs.ModeDir for Git folders in the workspace (#1521)
Browse files Browse the repository at this point in the history
## Changes

Not doing this meant file system traversal ended upon reaching a Git
folder. By marking these objects as a directory globbing traverses into
these folders as well.

## Tests

Added a unit test for coverage.
  • Loading branch information
pietern authored Jun 24, 2024
1 parent 5ff0657 commit 8957f1e
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 10 deletions.
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())
}

0 comments on commit 8957f1e

Please sign in to comment.