Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions cmd/bundle/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,24 @@ type syncFlags struct {
dryRun bool
}

func (f *syncFlags) syncOptionsFromBundle(cmd *cobra.Command, b *bundle.Bundle) (*sync.SyncOptions, error) {
opts, err := files.GetSyncOptions(cmd.Context(), b)
// syncOptionsFromBundle returns the options to sync the bundle, writing
// progress in the output format to stdout.
func (f *syncFlags) syncOptionsFromBundle(ctx context.Context, output flags.Output, stdout io.Writer, b *bundle.Bundle) (*sync.SyncOptions, error) {
opts, err := files.GetSyncOptions(ctx, b)
if err != nil {
return nil, fmt.Errorf("cannot get sync options: %w", err)
}

var outputFunc func(context.Context, <-chan sync.Event, io.Writer)
switch root.OutputType(cmd) {
switch output {
case flags.OutputText:
outputFunc = sync.TextOutput
case flags.OutputJSON:
outputFunc = sync.JsonOutput
}
if outputFunc != nil {
opts.OutputHandler = func(ctx context.Context, c <-chan sync.Event) {
outputFunc(ctx, c, cmd.OutOrStdout())
outputFunc(ctx, c, stdout)
}
}

Expand Down Expand Up @@ -78,7 +80,7 @@ Use 'databricks bundle deploy' for full resource deployment.`,
}
ctx := cmd.Context()

opts, err := f.syncOptionsFromBundle(cmd, b)
opts, err := f.syncOptionsFromBundle(ctx, root.OutputType(cmd), cmd.OutOrStdout(), b)
if err != nil {
return err
}
Expand Down
46 changes: 17 additions & 29 deletions cmd/bundle/sync_test.go
Original file line number Diff line number Diff line change
@@ -1,30 +1,28 @@
package bundle

import (
"io"
"testing"

"github.com/databricks/cli/bundle"
"github.com/databricks/cli/bundle/config"
"github.com/databricks/cli/cmd/root"
"github.com/databricks/cli/libs/flags"
"github.com/databricks/cli/libs/vfs"
"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

// newTestSyncCommand attaches the sync command to a root command so the root persistent flags resolve.
func newTestSyncCommand(t *testing.T) *cobra.Command {
syncCmd := newSyncCommand()
root.New(t.Context()).AddCommand(syncCmd)
return syncCmd
}

func TestBundleSyncShorthandFlags(t *testing.T) {
cmd := newTestSyncCommand(t)
require.NoError(t, cmd.ParseFlags([]string{"-o", "json", "-p", "myprofile"}))
assert.Equal(t, flags.OutputJSON, root.OutputType(cmd))
assert.Equal(t, "myprofile", cmd.Flag("profile").Value.String())
// TestBundleSyncDoesNotShadowRootFlags checks that bundle sync defines no
// local --output or --profile, which would hide the root's flags, and no -o or
// -p shorthand, which would conflict with them.
func TestBundleSyncDoesNotShadowRootFlags(t *testing.T) {
cmd := newSyncCommand()
for _, name := range []string{"output", "profile"} {
assert.Nil(t, cmd.LocalFlags().Lookup(name), name)
}
for _, shorthand := range []string{"o", "p"} {
assert.Nil(t, cmd.LocalFlags().ShorthandLookup(shorthand), shorthand)
}
}

func TestBundleSyncInstallsOutputHandler(t *testing.T) {
Expand All @@ -46,19 +44,9 @@ func TestBundleSyncInstallsOutputHandler(t *testing.T) {

f := syncFlags{}

// bundle sync defaults to text output, matching the standalone sync command:
// the root --output flag resolves to text when unset, so a handler is installed.
cmd := newTestSyncCommand(t)
cmd.SetContext(t.Context())
require.NoError(t, cmd.ParseFlags(nil))
opts, err := f.syncOptionsFromBundle(cmd, b)
require.NoError(t, err)
assert.NotNil(t, opts.OutputHandler)

cmd = newTestSyncCommand(t)
cmd.SetContext(t.Context())
require.NoError(t, cmd.ParseFlags([]string{"-o", "json"}))
opts, err = f.syncOptionsFromBundle(cmd, b)
require.NoError(t, err)
assert.NotNil(t, opts.OutputHandler)
for _, output := range []flags.Output{flags.OutputText, flags.OutputJSON} {
opts, err := f.syncOptionsFromBundle(t.Context(), output, io.Discard, b)
require.NoError(t, err)
assert.NotNil(t, opts.OutputHandler, output)
}
}
27 changes: 12 additions & 15 deletions cmd/sync/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,12 @@ func readPatternsFile(flagName, filePath string) ([]string, error) {
return patterns, nil
}

func (f *syncFlags) syncOptionsFromBundle(cmd *cobra.Command, args []string, b *bundle.Bundle) (*sync.SyncOptions, error) {
func (f *syncFlags) syncOptionsFromBundle(ctx context.Context, args []string, b *bundle.Bundle) (*sync.SyncOptions, error) {
if len(args) > 0 {
return nil, errors.New("SRC and DST are not configurable in the context of a bundle")
}

opts, err := files.GetSyncOptions(cmd.Context(), b)
opts, err := files.GetSyncOptions(ctx, b)
if err != nil {
return nil, fmt.Errorf("cannot get sync options: %w", err)
}
Expand All @@ -91,13 +91,11 @@ func (f *syncFlags) syncOptionsFromBundle(cmd *cobra.Command, args []string, b *
return opts, nil
}

func (f *syncFlags) syncOptionsFromArgs(cmd *cobra.Command, args []string) (*sync.SyncOptions, error) {
if err := root.ExactArgs(2)(cmd, args); err != nil {
return nil, err
}

// syncOptionsFromArgs returns the options to sync the SRC and DST in args,
// writing progress in the output format to stdout.
func (f *syncFlags) syncOptionsFromArgs(ctx context.Context, output flags.Output, stdout io.Writer, args []string) (*sync.SyncOptions, error) {
var outputFunc func(context.Context, <-chan sync.Event, io.Writer)
switch root.OutputType(cmd) {
switch output {
case flags.OutputText:
outputFunc = sync.TextOutput
case flags.OutputJSON:
Expand All @@ -107,11 +105,10 @@ func (f *syncFlags) syncOptionsFromArgs(cmd *cobra.Command, args []string) (*syn
var outputHandler sync.OutputHandler
if outputFunc != nil {
outputHandler = func(ctx context.Context, events <-chan sync.Event) {
outputFunc(ctx, events, cmd.OutOrStdout())
outputFunc(ctx, events, stdout)
}
}

ctx := cmd.Context()
client := cmdctx.WorkspaceClient(ctx)

if f.dryRun {
Expand Down Expand Up @@ -192,15 +189,15 @@ func New() *cobra.Command {

cmd.PreRunE = mustWorkspaceClient
cmd.RunE = func(cmd *cobra.Command, args []string) error {
var opts *sync.SyncOptions
var err error

opts, err = f.syncOptionsFromArgs(cmd, args)
ctx := cmd.Context()
if err := root.ExactArgs(2)(cmd, args); err != nil {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nice to have] The commit message says the argument-count cases "move to the sync-without-args acceptance test", but the PR changes no acceptance files, and acceptance/cmd/sync-without-args only runs databricks sync with no arguments. The deleted TestSyncOptionsFromArgsRequiredTwoArgs was the only test of the one-argument case (databricks sync ./dir, which should fail with "accepts 2 arg(s), received 1"). If this ExactArgs(2) check were dropped from RunE, syncOptionsFromArgs would panic on args[1] and no test would fail. Add a one-argument invocation to acceptance/cmd/sync-without-args/script (e.g. trace musterr $CLI sync .), or reword the commit message.

return err
}
opts, err := f.syncOptionsFromArgs(ctx, root.OutputType(cmd), cmd.OutOrStdout(), args)
if err != nil {
return err
}

ctx := cmd.Context()
s, err := sync.New(ctx, *opts)
if err != nil {
return err
Expand Down
49 changes: 17 additions & 32 deletions cmd/sync/sync_test.go
Original file line number Diff line number Diff line change
@@ -1,29 +1,21 @@
package sync

import (
"io"
"os"
"path/filepath"
"strings"
"testing"

"github.com/databricks/cli/bundle"
"github.com/databricks/cli/bundle/config"
"github.com/databricks/cli/cmd/root"
"github.com/databricks/cli/libs/cmdctx"
"github.com/databricks/cli/libs/flags"
"github.com/databricks/cli/libs/vfs"
"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

// newTestSyncCommand attaches the sync command to a root command so the root persistent flags resolve.
func newTestSyncCommand(t *testing.T) *cobra.Command {
syncCmd := New()
root.New(t.Context()).AddCommand(syncCmd)
return syncCmd
}

func TestSyncOptionsFromBundle(t *testing.T) {
tempDir := t.TempDir()
b := &bundle.Bundle{
Expand All @@ -43,43 +35,37 @@ func TestSyncOptionsFromBundle(t *testing.T) {
}

f := syncFlags{}
opts, err := f.syncOptionsFromBundle(New(), []string{}, b)
opts, err := f.syncOptionsFromBundle(t.Context(), []string{}, b)
require.NoError(t, err)
assert.Equal(t, tempDir, opts.LocalRoot.Native())
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
f := syncFlags{}
_, err = f.syncOptionsFromArgs(New(), []string{})
require.ErrorContains(t, err, "accepts 2 arg(s), received 0")
_, err = f.syncOptionsFromArgs(New(), []string{"foo"})
require.ErrorContains(t, err, "accepts 2 arg(s), received 1")
_, err = f.syncOptionsFromArgs(New(), []string{"foo", "bar", "qux"})
require.ErrorContains(t, err, "accepts 2 arg(s), received 3")
}

func TestSyncOptionsFromArgs(t *testing.T) {
local := t.TempDir()
remote := "/remote"

f := syncFlags{}
cmd := newTestSyncCommand(t)
cmd.SetContext(cmdctx.SetWorkspaceClient(t.Context(), nil))
opts, err := f.syncOptionsFromArgs(cmd, []string{local, remote})
ctx := cmdctx.SetWorkspaceClient(t.Context(), nil)
opts, err := f.syncOptionsFromArgs(ctx, flags.OutputText, io.Discard, []string{local, remote})
require.NoError(t, err)
assert.Equal(t, local, opts.LocalRoot.Native())
assert.Equal(t, remote, opts.RemotePath)
}

func TestSyncShorthandFlags(t *testing.T) {
cmd := newTestSyncCommand(t)
require.NoError(t, cmd.ParseFlags([]string{"-o", "json", "-p", "myprofile"}))
assert.Equal(t, flags.OutputJSON, root.OutputType(cmd))
assert.Equal(t, "myprofile", cmd.Flag("profile").Value.String())
// TestSyncDoesNotShadowRootFlags checks that sync defines no local --output or
// --profile, which would hide the root's flags, and no -o or -p shorthand,
// which would conflict with them.
func TestSyncDoesNotShadowRootFlags(t *testing.T) {
cmd := New()
for _, name := range []string{"output", "profile"} {
assert.Nil(t, cmd.LocalFlags().Lookup(name), name)
}
for _, shorthand := range []string{"o", "p"} {
assert.Nil(t, cmd.LocalFlags().ShorthandLookup(shorthand), shorthand)
}
}

func TestReadPatternsFileErrorNamesFlag(t *testing.T) {
Expand Down Expand Up @@ -108,12 +94,11 @@ func TestExcludeFromFlag(t *testing.T) {

// Set up the flags
f := syncFlags{excludeFrom: excludeFromPath}
cmd := newTestSyncCommand(t)
cmd.SetContext(cmdctx.SetWorkspaceClient(t.Context(), nil))
ctx := cmdctx.SetWorkspaceClient(t.Context(), nil)

// Test with both exclude flag and exclude-from flag
f.exclude = []string{"node_modules/"}
opts, err := f.syncOptionsFromArgs(cmd, []string{local, remote})
opts, err := f.syncOptionsFromArgs(ctx, flags.OutputText, io.Discard, []string{local, remote})
require.NoError(t, err)

// Should include both exclude flag and exclude-from patterns
Expand Down
Loading