mirror of https://github.com/databricks/cli.git
Add check for path is a directory in filer.ReadDir (#426)
## Tests Integration tests
This commit is contained in:
parent
2b56af6016
commit
91097856b5
|
@ -159,6 +159,26 @@ func runFilerReadDirTest(t *testing.T, ctx context.Context, f filer.Filer) {
|
||||||
assert.Len(t, entries, 1)
|
assert.Len(t, entries, 1)
|
||||||
assert.Equal(t, "c", entries[0].Name())
|
assert.Equal(t, "c", entries[0].Name())
|
||||||
assert.True(t, entries[0].IsDir())
|
assert.True(t, entries[0].IsDir())
|
||||||
|
|
||||||
|
// Expect an error trying to call ReadDir on a file
|
||||||
|
_, err = f.ReadDir(ctx, "/hello.txt")
|
||||||
|
assert.ErrorIs(t, err, fs.ErrInvalid)
|
||||||
|
|
||||||
|
// Expect 0 entries for an empty directory
|
||||||
|
err = f.Mkdir(ctx, "empty-dir")
|
||||||
|
require.NoError(t, err)
|
||||||
|
entries, err = f.ReadDir(ctx, "empty-dir")
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Len(t, entries, 0)
|
||||||
|
|
||||||
|
// Expect one entry for a directory with a file in it
|
||||||
|
err = f.Write(ctx, "dir-with-one-file/my-file.txt", strings.NewReader("abc"), filer.CreateParentDirectories)
|
||||||
|
require.NoError(t, err)
|
||||||
|
entries, err = f.ReadDir(ctx, "dir-with-one-file")
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Len(t, entries, 1)
|
||||||
|
assert.Equal(t, entries[0].Name(), "my-file.txt")
|
||||||
|
assert.False(t, entries[0].IsDir())
|
||||||
}
|
}
|
||||||
|
|
||||||
func temporaryWorkspaceDir(t *testing.T, w *databricks.WorkspaceClient) string {
|
func temporaryWorkspaceDir(t *testing.T, w *databricks.WorkspaceClient) string {
|
||||||
|
@ -166,7 +186,7 @@ func temporaryWorkspaceDir(t *testing.T, w *databricks.WorkspaceClient) string {
|
||||||
me, err := w.CurrentUser.Me(ctx)
|
me, err := w.CurrentUser.Me(ctx)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
path := fmt.Sprintf("/Users/%s/%s", me.UserName, RandomName("integration-test-filer-wsfs-"))
|
path := fmt.Sprintf("/Users/%s/%s", me.UserName, RandomName("integration-test-wsfs-"))
|
||||||
|
|
||||||
// Ensure directory exists, but doesn't exist YET!
|
// Ensure directory exists, but doesn't exist YET!
|
||||||
// Otherwise we could inadvertently remove a directory that already exists on cleanup.
|
// Otherwise we could inadvertently remove a directory that already exists on cleanup.
|
||||||
|
|
|
@ -222,6 +222,10 @@ func (w *DbfsClient) ReadDir(ctx context.Context, name string) ([]fs.DirEntry, e
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if len(res.Files) == 1 && res.Files[0].Path == absPath {
|
||||||
|
return nil, NotADirectory{absPath}
|
||||||
|
}
|
||||||
|
|
||||||
info := make([]fs.DirEntry, len(res.Files))
|
info := make([]fs.DirEntry, len(res.Files))
|
||||||
for i, v := range res.Files {
|
for i, v := range res.Files {
|
||||||
info[i] = dbfsDirEntry{dbfsFileInfo: dbfsFileInfo{fi: v}}
|
info[i] = dbfsDirEntry{dbfsFileInfo: dbfsFileInfo{fi: v}}
|
||||||
|
|
|
@ -50,6 +50,18 @@ func (err NoSuchDirectoryError) Is(other error) bool {
|
||||||
return other == fs.ErrNotExist
|
return other == fs.ErrNotExist
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type NotADirectory struct {
|
||||||
|
path string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (err NotADirectory) Error() string {
|
||||||
|
return fmt.Sprintf("not a directory: %s", err.path)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (err NotADirectory) Is(other error) bool {
|
||||||
|
return other == fs.ErrInvalid
|
||||||
|
}
|
||||||
|
|
||||||
// Filer is used to access files in a workspace.
|
// Filer is used to access files in a workspace.
|
||||||
// It has implementations for accessing files in WSFS and in DBFS.
|
// It has implementations for accessing files in WSFS and in DBFS.
|
||||||
type Filer interface {
|
type Filer interface {
|
||||||
|
|
|
@ -222,6 +222,11 @@ func (w *WorkspaceFilesClient) ReadDir(ctx context.Context, name string) ([]fs.D
|
||||||
objects, err := w.workspaceClient.Workspace.ListAll(ctx, workspace.ListWorkspaceRequest{
|
objects, err := w.workspaceClient.Workspace.ListAll(ctx, workspace.ListWorkspaceRequest{
|
||||||
Path: absPath,
|
Path: absPath,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
if len(objects) == 1 && objects[0].Path == absPath {
|
||||||
|
return nil, NotADirectory{absPath}
|
||||||
|
}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// If we got an API error we deal with it below.
|
// If we got an API error we deal with it below.
|
||||||
var aerr *apierr.APIError
|
var aerr *apierr.APIError
|
||||||
|
|
Loading…
Reference in New Issue