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

## 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.
This commit is contained in:
Pieter Noordhuis 2024-06-24 12:15:13 +02:00 committed by GitHub
parent 5ff06578ac
commit 8957f1e7cf
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 71 additions and 10 deletions

View File

@ -35,6 +35,17 @@ func (entry wsfsDirEntry) Info() (fs.FileInfo, error) {
return entry.wsfsFileInfo, nil 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 that implements fs.FileInfo for WSFS.
type wsfsFileInfo struct { type wsfsFileInfo struct {
oi workspace.ObjectInfo oi workspace.ObjectInfo
@ -50,7 +61,7 @@ func (info wsfsFileInfo) Size() int64 {
func (info wsfsFileInfo) Mode() fs.FileMode { func (info wsfsFileInfo) Mode() fs.FileMode {
switch info.oi.ObjectType { switch info.oi.ObjectType {
case workspace.ObjectTypeDirectory: case workspace.ObjectTypeDirectory, workspace.ObjectTypeRepo:
return fs.ModeDir return fs.ModeDir
default: default:
return fs.ModePerm return fs.ModePerm
@ -62,7 +73,7 @@ func (info wsfsFileInfo) ModTime() time.Time {
} }
func (info wsfsFileInfo) IsDir() bool { func (info wsfsFileInfo) IsDir() bool {
return info.oi.ObjectType == workspace.ObjectTypeDirectory return info.Mode() == fs.ModeDir
} }
func (info wsfsFileInfo) Sys() any { func (info wsfsFileInfo) Sys() any {
@ -262,14 +273,8 @@ func (w *WorkspaceFilesClient) ReadDir(ctx context.Context, name string) ([]fs.D
return nil, err return nil, err
} }
info := make([]fs.DirEntry, len(objects)) // Convert to fs.DirEntry.
for i, v := range objects { return wsfsDirEntriesFromObjectInfos(objects), nil
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
} }
func (w *WorkspaceFilesClient) Mkdir(ctx context.Context, name string) error { func (w *WorkspaceFilesClient) Mkdir(ctx context.Context, name string) error {

View File

@ -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())
}