2023-02-20 10:33:30 +00:00
|
|
|
package sync
|
|
|
|
|
|
|
|
import (
|
2023-10-13 13:04:15 +00:00
|
|
|
"context"
|
2023-02-20 10:33:30 +00:00
|
|
|
"flag"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
|
2023-05-16 16:35:39 +00:00
|
|
|
"github.com/databricks/cli/bundle"
|
|
|
|
"github.com/databricks/cli/bundle/config"
|
2023-10-13 13:04:15 +00:00
|
|
|
"github.com/databricks/cli/cmd/root"
|
2024-07-03 10:13:22 +00:00
|
|
|
"github.com/databricks/cli/libs/vfs"
|
2023-02-20 10:33:30 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestSyncOptionsFromBundle(t *testing.T) {
|
|
|
|
tempDir := t.TempDir()
|
|
|
|
b := &bundle.Bundle{
|
2024-07-03 10:13:22 +00:00
|
|
|
RootPath: tempDir,
|
|
|
|
BundleRoot: vfs.MustNew(tempDir),
|
2023-02-20 10:33:30 +00:00
|
|
|
Config: config.Root{
|
|
|
|
Bundle: config.Bundle{
|
2023-08-17 15:22:32 +00:00
|
|
|
Target: "default",
|
2023-02-20 10:33:30 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
Workspace: config.Workspace{
|
2023-11-15 13:37:26 +00:00
|
|
|
FilePath: "/Users/jane@doe.com/path",
|
2023-02-20 10:33:30 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-07-27 10:03:08 +00:00
|
|
|
f := syncFlags{}
|
|
|
|
opts, err := f.syncOptionsFromBundle(New(), []string{}, b)
|
2023-02-20 10:33:30 +00:00
|
|
|
require.NoError(t, err)
|
2024-05-30 07:41:50 +00:00
|
|
|
assert.Equal(t, tempDir, opts.LocalPath.Native())
|
2023-02-20 10:33:30 +00:00
|
|
|
assert.Equal(t, "/Users/jane@doe.com/path", opts.RemotePath)
|
|
|
|
assert.Equal(t, filepath.Join(tempDir, ".databricks", "bundle", "default"), opts.SnapshotBasePath)
|
|
|
|
assert.NotNil(t, opts.WorkspaceClient)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSyncOptionsFromArgsRequiredTwoArgs(t *testing.T) {
|
|
|
|
var err error
|
2023-07-27 10:03:08 +00:00
|
|
|
f := syncFlags{}
|
|
|
|
_, err = f.syncOptionsFromArgs(New(), []string{})
|
2023-02-20 10:33:30 +00:00
|
|
|
require.ErrorIs(t, err, flag.ErrHelp)
|
2023-07-27 10:03:08 +00:00
|
|
|
_, err = f.syncOptionsFromArgs(New(), []string{"foo"})
|
2023-02-20 10:33:30 +00:00
|
|
|
require.ErrorIs(t, err, flag.ErrHelp)
|
2023-07-27 10:03:08 +00:00
|
|
|
_, err = f.syncOptionsFromArgs(New(), []string{"foo", "bar", "qux"})
|
2023-02-20 10:33:30 +00:00
|
|
|
require.ErrorIs(t, err, flag.ErrHelp)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSyncOptionsFromArgs(t *testing.T) {
|
2024-05-30 07:41:50 +00:00
|
|
|
local := t.TempDir()
|
|
|
|
remote := "/remote"
|
|
|
|
|
2023-07-27 10:03:08 +00:00
|
|
|
f := syncFlags{}
|
2023-10-13 13:04:15 +00:00
|
|
|
cmd := New()
|
|
|
|
cmd.SetContext(root.SetWorkspaceClient(context.Background(), nil))
|
2024-05-30 07:41:50 +00:00
|
|
|
opts, err := f.syncOptionsFromArgs(cmd, []string{local, remote})
|
2023-02-20 10:33:30 +00:00
|
|
|
require.NoError(t, err)
|
2024-05-30 07:41:50 +00:00
|
|
|
assert.Equal(t, local, opts.LocalPath.Native())
|
|
|
|
assert.Equal(t, remote, opts.RemotePath)
|
2023-02-20 10:33:30 +00:00
|
|
|
}
|