Skip to content
Open
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
24 changes: 0 additions & 24 deletions acceptance/cmd/sync-without-args/output.txt

This file was deleted.

1 change: 0 additions & 1 deletion acceptance/cmd/sync-without-args/script

This file was deleted.

72 changes: 72 additions & 0 deletions acceptance/cmd/sync-wrong-args/output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@

>>> musterr [CLI] sync
Error: accepts 2 arg(s), received 0

Usage:
databricks sync [flags] SRC DST

Flags:
--dry-run simulate sync execution without making actual changes
--exclude strings patterns to exclude from sync (can be specified multiple times)
--exclude-from string file containing patterns to exclude from sync (one pattern per line)
--full perform full synchronization (default is incremental)
-h, --help help for sync
--include strings patterns to include in sync (can be specified multiple times)
--include-from string file containing patterns to include to sync (one pattern per line)
--interval duration file system polling interval (for --watch) (default 1s)
--watch watch local file system for changes

Global Flags:
--debug enable debug logging
-o, --output type output type: text or json (default text)
-p, --profile string ~/.databrickscfg profile
-t, --target string bundle target to use (if applicable)


>>> musterr [CLI] sync foo
Error: accepts 2 arg(s), received 1

Usage:
databricks sync [flags] SRC DST

Flags:
--dry-run simulate sync execution without making actual changes
--exclude strings patterns to exclude from sync (can be specified multiple times)
--exclude-from string file containing patterns to exclude from sync (one pattern per line)
--full perform full synchronization (default is incremental)
-h, --help help for sync
--include strings patterns to include in sync (can be specified multiple times)
--include-from string file containing patterns to include to sync (one pattern per line)
--interval duration file system polling interval (for --watch) (default 1s)
--watch watch local file system for changes

Global Flags:
--debug enable debug logging
-o, --output type output type: text or json (default text)
-p, --profile string ~/.databrickscfg profile
-t, --target string bundle target to use (if applicable)


>>> musterr [CLI] sync foo bar qux
Error: accepts at most 2 arg(s), received 3

Usage:
databricks sync [flags] SRC DST

Flags:
--dry-run simulate sync execution without making actual changes
--exclude strings patterns to exclude from sync (can be specified multiple times)
--exclude-from string file containing patterns to exclude from sync (one pattern per line)
--full perform full synchronization (default is incremental)
-h, --help help for sync
--include strings patterns to include in sync (can be specified multiple times)
--include-from string file containing patterns to include to sync (one pattern per line)
--interval duration file system polling interval (for --watch) (default 1s)
--watch watch local file system for changes

Global Flags:
--debug enable debug logging
-o, --output type output type: text or json (default text)
-p, --profile string ~/.databrickscfg profile
-t, --target string bundle target to use (if applicable)

3 changes: 3 additions & 0 deletions acceptance/cmd/sync-wrong-args/script
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
trace musterr $CLI sync
trace musterr $CLI sync foo
trace musterr $CLI sync foo bar qux
7 changes: 7 additions & 0 deletions acceptance/cmd/sync/dryrun/output.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,10 @@ Uploaded project-folder
Uploaded project-folder/app.py
Uploaded project-folder/app.yaml
Uploaded project-folder/query.sql

>>> [CLI] sync . /Users/[USERNAME] --dry-run -o json --exclude project-folder/**
Warn: Running in dry-run mode. No actual changes will be made.
{"type":"start","dry_run":true,"put":[".gitignore"]}
{"type":"progress","dry_run":true,"action":"put","path":".gitignore","progress":0}
{"type":"progress","dry_run":true,"action":"put","path":".gitignore","progress":1}
{"type":"complete","dry_run":true,"put":[".gitignore"]}
3 changes: 3 additions & 0 deletions acceptance/cmd/sync/dryrun/script
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,6 @@ trace $CLI sync . /Users/$CURRENT_USER_NAME --dry-run --exclude 'project-folder/

# use negated include to exclude files from syncing:
trace $CLI sync . /Users/$CURRENT_USER_NAME --dry-run --include 'ignored-folder/**' --include '!ignored-folder/folder1/big-blob' | grep -v "^Action" | sort

# the root's -o flag selects the output format:
trace $CLI sync . /Users/$CURRENT_USER_NAME --dry-run -o json --exclude 'project-folder/**' | jq -c 'del(.timestamp, .seq)'
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
45 changes: 16 additions & 29 deletions cmd/bundle/sync_test.go
Original file line number Diff line number Diff line change
@@ -1,30 +1,27 @@
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())
// Flags that bundle sync defines with the names or shorthands of the root's
// --output and --profile flags would shadow 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 +43,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)
}
}
22 changes: 13 additions & 9 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,15 @@ 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) {
if len(args) != 2 {
return nil, fmt.Errorf("expected SRC and DST, got %d arguments", len(args))
}

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 +109,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 @@ -195,7 +196,10 @@ func New() *cobra.Command {
var opts *sync.SyncOptions
var err error

opts, err = f.syncOptionsFromArgs(cmd, args)
if err := root.ExactArgs(2)(cmd, args); err != nil {
return err
}
opts, err = f.syncOptionsFromArgs(cmd.Context(), root.OutputType(cmd), cmd.OutOrStdout(), args)
if err != nil {
return err
}
Expand Down
54 changes: 22 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,42 @@ 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())
func TestSyncOptionsFromArgsRequiresSrcAndDst(t *testing.T) {
f := syncFlags{}
_, err := f.syncOptionsFromArgs(t.Context(), flags.OutputText, io.Discard, []string{"src"})
assert.EqualError(t, err, "expected SRC and DST, got 1 arguments")
}

// Flags that sync defines with the names or shorthands of the root's
// --output and --profile flags would shadow 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 +99,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