2023-06-05 23:21:47 +00:00
|
|
|
package internal
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"io/fs"
|
|
|
|
"path"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/databricks/cli/libs/filer"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2024-02-20 16:14:37 +00:00
|
|
|
func TestAccFsRmFile(t *testing.T) {
|
|
|
|
t.Parallel()
|
2023-06-05 23:21:47 +00:00
|
|
|
|
2024-02-20 16:14:37 +00:00
|
|
|
for _, testCase := range fsTests {
|
|
|
|
tc := testCase
|
2023-06-05 23:21:47 +00:00
|
|
|
|
2024-02-20 16:14:37 +00:00
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
2023-06-05 23:21:47 +00:00
|
|
|
|
2024-02-20 16:14:37 +00:00
|
|
|
// Create a file
|
|
|
|
f, tmpDir := tc.setupFiler(t)
|
|
|
|
err := f.Write(context.Background(), "hello.txt", strings.NewReader("abcd"), filer.CreateParentDirectories)
|
|
|
|
require.NoError(t, err)
|
2023-06-05 23:21:47 +00:00
|
|
|
|
2024-02-20 16:14:37 +00:00
|
|
|
// Check file was created
|
|
|
|
_, err = f.Stat(context.Background(), "hello.txt")
|
|
|
|
assert.NoError(t, err)
|
2023-06-05 23:21:47 +00:00
|
|
|
|
2024-02-20 16:14:37 +00:00
|
|
|
// Run rm command
|
|
|
|
stdout, stderr := RequireSuccessfulRun(t, "fs", "rm", path.Join(tmpDir, "hello.txt"))
|
|
|
|
assert.Equal(t, "", stderr.String())
|
|
|
|
assert.Equal(t, "", stdout.String())
|
2023-06-05 23:21:47 +00:00
|
|
|
|
2024-02-20 16:14:37 +00:00
|
|
|
// Assert file was deleted
|
|
|
|
_, err = f.Stat(context.Background(), "hello.txt")
|
|
|
|
assert.ErrorIs(t, err, fs.ErrNotExist)
|
|
|
|
})
|
|
|
|
}
|
2023-06-05 23:21:47 +00:00
|
|
|
}
|
|
|
|
|
2024-02-20 16:14:37 +00:00
|
|
|
func TestAccFsRmEmptyDir(t *testing.T) {
|
|
|
|
t.Parallel()
|
2023-06-05 23:21:47 +00:00
|
|
|
|
2024-02-20 16:14:37 +00:00
|
|
|
for _, testCase := range fsTests {
|
|
|
|
tc := testCase
|
2023-06-05 23:21:47 +00:00
|
|
|
|
2024-02-20 16:14:37 +00:00
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
2023-06-05 23:21:47 +00:00
|
|
|
|
2024-02-20 16:14:37 +00:00
|
|
|
// Create a directory
|
|
|
|
f, tmpDir := tc.setupFiler(t)
|
|
|
|
err := f.Mkdir(context.Background(), "a")
|
|
|
|
require.NoError(t, err)
|
2023-06-05 23:21:47 +00:00
|
|
|
|
2024-02-20 16:14:37 +00:00
|
|
|
// Check directory was created
|
|
|
|
_, err = f.Stat(context.Background(), "a")
|
|
|
|
assert.NoError(t, err)
|
2023-06-05 23:21:47 +00:00
|
|
|
|
2024-02-20 16:14:37 +00:00
|
|
|
// Run rm command
|
|
|
|
stdout, stderr := RequireSuccessfulRun(t, "fs", "rm", path.Join(tmpDir, "a"))
|
|
|
|
assert.Equal(t, "", stderr.String())
|
|
|
|
assert.Equal(t, "", stdout.String())
|
2023-06-05 23:21:47 +00:00
|
|
|
|
2024-02-20 16:14:37 +00:00
|
|
|
// Assert directory was deleted
|
|
|
|
_, err = f.Stat(context.Background(), "a")
|
|
|
|
assert.ErrorIs(t, err, fs.ErrNotExist)
|
|
|
|
})
|
|
|
|
}
|
2023-06-05 23:21:47 +00:00
|
|
|
}
|
|
|
|
|
2024-02-20 16:14:37 +00:00
|
|
|
func TestAccFsRmNonEmptyDirectory(t *testing.T) {
|
|
|
|
t.Parallel()
|
2023-06-05 23:21:47 +00:00
|
|
|
|
2024-02-20 16:14:37 +00:00
|
|
|
for _, testCase := range fsTests {
|
|
|
|
tc := testCase
|
2023-06-05 23:21:47 +00:00
|
|
|
|
2024-02-20 16:14:37 +00:00
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
2023-06-05 23:21:47 +00:00
|
|
|
|
2024-02-20 16:14:37 +00:00
|
|
|
// Create a directory
|
|
|
|
f, tmpDir := tc.setupFiler(t)
|
|
|
|
err := f.Mkdir(context.Background(), "a")
|
|
|
|
require.NoError(t, err)
|
2023-06-05 23:21:47 +00:00
|
|
|
|
2024-02-20 16:14:37 +00:00
|
|
|
// Create a file in the directory
|
|
|
|
err = f.Write(context.Background(), "a/hello.txt", strings.NewReader("abcd"), filer.CreateParentDirectories)
|
|
|
|
require.NoError(t, err)
|
2023-06-05 23:21:47 +00:00
|
|
|
|
2024-02-20 16:14:37 +00:00
|
|
|
// Check file was created
|
|
|
|
_, err = f.Stat(context.Background(), "a/hello.txt")
|
|
|
|
assert.NoError(t, err)
|
2023-06-05 23:21:47 +00:00
|
|
|
|
2024-02-20 16:14:37 +00:00
|
|
|
// Run rm command
|
|
|
|
_, _, err = RequireErrorRun(t, "fs", "rm", path.Join(tmpDir, "a"))
|
|
|
|
assert.ErrorIs(t, err, fs.ErrInvalid)
|
|
|
|
assert.ErrorAs(t, err, &filer.DirectoryNotEmptyError{})
|
|
|
|
})
|
|
|
|
}
|
2023-06-05 23:21:47 +00:00
|
|
|
}
|
|
|
|
|
2023-07-26 13:03:10 +00:00
|
|
|
func TestAccFsRmForNonExistentFile(t *testing.T) {
|
2024-02-20 16:14:37 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
for _, testCase := range fsTests {
|
|
|
|
tc := testCase
|
|
|
|
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
_, tmpDir := tc.setupFiler(t)
|
|
|
|
|
|
|
|
// Expect error if file does not exist
|
|
|
|
_, _, err := RequireErrorRun(t, "fs", "rm", path.Join(tmpDir, "does-not-exist"))
|
|
|
|
assert.ErrorIs(t, err, fs.ErrNotExist)
|
|
|
|
})
|
|
|
|
}
|
2023-06-05 23:21:47 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2024-02-20 16:14:37 +00:00
|
|
|
func TestAccFsRmDirRecursively(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
for _, testCase := range fsTests {
|
|
|
|
tc := testCase
|
2023-06-05 23:21:47 +00:00
|
|
|
|
2024-02-20 16:14:37 +00:00
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
2023-06-05 23:21:47 +00:00
|
|
|
|
2024-02-20 16:14:37 +00:00
|
|
|
f, tmpDir := tc.setupFiler(t)
|
2023-06-05 23:21:47 +00:00
|
|
|
|
2024-02-20 16:14:37 +00:00
|
|
|
// Create a directory
|
|
|
|
err := f.Mkdir(context.Background(), "a")
|
|
|
|
require.NoError(t, err)
|
2023-06-05 23:21:47 +00:00
|
|
|
|
2024-02-20 16:14:37 +00:00
|
|
|
// Create a file in the directory
|
|
|
|
err = f.Write(context.Background(), "a/hello.txt", strings.NewReader("abcd"), filer.CreateParentDirectories)
|
|
|
|
require.NoError(t, err)
|
2023-06-05 23:21:47 +00:00
|
|
|
|
2024-02-20 16:14:37 +00:00
|
|
|
// Check file was created
|
|
|
|
_, err = f.Stat(context.Background(), "a/hello.txt")
|
|
|
|
assert.NoError(t, err)
|
2023-06-05 23:21:47 +00:00
|
|
|
|
2024-02-20 16:14:37 +00:00
|
|
|
// Run rm command
|
|
|
|
stdout, stderr := RequireSuccessfulRun(t, "fs", "rm", path.Join(tmpDir, "a"), "--recursive")
|
|
|
|
assert.Equal(t, "", stderr.String())
|
|
|
|
assert.Equal(t, "", stdout.String())
|
2023-06-05 23:21:47 +00:00
|
|
|
|
2024-02-20 16:14:37 +00:00
|
|
|
// Assert directory was deleted
|
|
|
|
_, err = f.Stat(context.Background(), "a")
|
|
|
|
assert.ErrorIs(t, err, fs.ErrNotExist)
|
|
|
|
})
|
|
|
|
}
|
2023-06-05 23:21:47 +00:00
|
|
|
}
|