From e5f2afa72fd99d46d57b9a8413e365fe5f84740d Mon Sep 17 00:00:00 2001 From: "pieter.noordhuis" Date: Wed, 11 Nov 2026 13:37:39 +0000 Subject: [PATCH] Test sync option builders without a root command The sync and bundle sync option builders took the command only to read its context, output type, and stdout. Pass those instead, so their tests no longer attach the command to root.New() to resolve the root flags. The argument count check for sync moves from the builder into RunE. Co-authored-by: Isaac --- cmd/bundle/sync.go | 12 +++++----- cmd/bundle/sync_test.go | 46 ++++++++++++++------------------------ cmd/sync/sync.go | 27 ++++++++++------------- cmd/sync/sync_test.go | 49 ++++++++++++++--------------------------- 4 files changed, 53 insertions(+), 81 deletions(-) diff --git a/cmd/bundle/sync.go b/cmd/bundle/sync.go index 955d425f208..219c82f3171 100644 --- a/cmd/bundle/sync.go +++ b/cmd/bundle/sync.go @@ -23,14 +23,16 @@ 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: @@ -38,7 +40,7 @@ func (f *syncFlags) syncOptionsFromBundle(cmd *cobra.Command, b *bundle.Bundle) } if outputFunc != nil { opts.OutputHandler = func(ctx context.Context, c <-chan sync.Event) { - outputFunc(ctx, c, cmd.OutOrStdout()) + outputFunc(ctx, c, stdout) } } @@ -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 } diff --git a/cmd/bundle/sync_test.go b/cmd/bundle/sync_test.go index 579aa69865f..4f778ca565a 100644 --- a/cmd/bundle/sync_test.go +++ b/cmd/bundle/sync_test.go @@ -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) { @@ -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) + } } diff --git a/cmd/sync/sync.go b/cmd/sync/sync.go index 027ef79a4a0..7d7fc21a600 100644 --- a/cmd/sync/sync.go +++ b/cmd/sync/sync.go @@ -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) } @@ -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: @@ -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 { @@ -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 { + 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 diff --git a/cmd/sync/sync_test.go b/cmd/sync/sync_test.go index 757defc34f8..182f764df1b 100644 --- a/cmd/sync/sync_test.go +++ b/cmd/sync/sync_test.go @@ -1,6 +1,7 @@ package sync import ( + "io" "os" "path/filepath" "strings" @@ -8,22 +9,13 @@ import ( "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{ @@ -43,7 +35,7 @@ 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) @@ -51,35 +43,29 @@ func TestSyncOptionsFromBundle(t *testing.T) { 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) { @@ -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