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
4 changes: 1 addition & 3 deletions cmd/aitools/install_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1087,9 +1087,7 @@ func TestInstallOutputJSONThroughRoot(t *testing.T) {
var out, errOut bytes.Buffer
cli.SetOut(&out)
cli.SetErr(&errOut)
cli.SetArgs([]string{"install", "--agents", "codex", "--scope", "global", "--output", "json"})

err := root.Execute(ctx, cli)
err := root.Execute(ctx, cli, []string{"install", "--agents", "codex", "--scope", "global", "--output", "json"})
require.ErrorIs(t, err, root.ErrAlreadyPrinted)
assert.NotContains(t, errOut.String(), "Error:")

Expand Down
28 changes: 7 additions & 21 deletions cmd/configure/configure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,7 @@ func TestDefaultConfigureNoInteractive(t *testing.T) {
os.Stdin = inp

cmd := cmd.New(ctx)
cmd.SetArgs([]string{"configure", "--token", "--host", "https://host"})

err := root.Execute(ctx, cmd)
err := root.Execute(ctx, cmd, []string{"configure", "--token", "--host", "https://host"})
assert.NoError(t, err)

cfgPath := filepath.Join(tempHomeDir, ".databrickscfg")
Expand Down Expand Up @@ -90,9 +88,7 @@ func TestConfigFileFromEnvNoInteractive(t *testing.T) {
os.Stdin = inp

cmd := cmd.New(ctx)
cmd.SetArgs([]string{"configure", "--token", "--host", "https://host"})

err := root.Execute(ctx, cmd)
err := root.Execute(ctx, cmd, []string{"configure", "--token", "--host", "https://host"})
assert.NoError(t, err)

_, err = os.Stat(cfgPath)
Expand Down Expand Up @@ -130,9 +126,7 @@ func TestEnvVarsConfigureNoInteractive(t *testing.T) {
t.Setenv("DATABRICKS_METADATA_SERVICE_URL", "https://metadata")

cmd := cmd.New(ctx)
cmd.SetArgs([]string{"configure", "--token"})

err := root.Execute(ctx, cmd)
err := root.Execute(ctx, cmd, []string{"configure", "--token"})
assert.NoError(t, err)

_, err = os.Stat(cfgPath)
Expand Down Expand Up @@ -163,9 +157,7 @@ func TestEnvVarsConfigureNoArgsNoInteractive(t *testing.T) {
t.Setenv("DATABRICKS_TOKEN", "secret")

cmd := cmd.New(ctx)
cmd.SetArgs([]string{"configure"})

err := root.Execute(ctx, cmd)
err := root.Execute(ctx, cmd, []string{"configure"})
assert.NoError(t, err)

_, err = os.Stat(cfgPath)
Expand Down Expand Up @@ -194,9 +186,7 @@ func TestEnvProfileConfigureNoInteractive(t *testing.T) {
t.Setenv("DATABRICKS_CONFIG_PROFILE", "FROM-ENV")

cmd := cmd.New(ctx)
cmd.SetArgs([]string{"configure", "--token", "--host", "https://host"})

err := root.Execute(ctx, cmd)
err := root.Execute(ctx, cmd, []string{"configure", "--token", "--host", "https://host"})
assert.NoError(t, err)

cfg, err := ini.Load(cfgPath)
Expand All @@ -222,9 +212,7 @@ func TestProfileShorthandOverridesEnvConfigureNoInteractive(t *testing.T) {
t.Setenv("DATABRICKS_CONFIG_PROFILE", "FROM-ENV")

cmd := cmd.New(ctx)
cmd.SetArgs([]string{"configure", "--token", "--host", "https://host", "-p", "FROM-FLAG"})

err := root.Execute(ctx, cmd)
err := root.Execute(ctx, cmd, []string{"configure", "--token", "--host", "https://host", "-p", "FROM-FLAG"})
assert.NoError(t, err)

cfg, err := ini.Load(cfgPath)
Expand All @@ -251,9 +239,7 @@ func TestCustomProfileConfigureNoInteractive(t *testing.T) {
os.Stdin = inp

cmd := cmd.New(ctx)
cmd.SetArgs([]string{"configure", "--token", "--host", "https://host", "--profile", "CUSTOM"})

err := root.Execute(ctx, cmd)
err := root.Execute(ctx, cmd, []string{"configure", "--token", "--host", "https://host", "--profile", "CUSTOM"})
assert.NoError(t, err)

_, err = os.Stat(cfgPath)
Expand Down
9 changes: 3 additions & 6 deletions cmd/root/bundle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -387,10 +387,9 @@ workspace:
func TestTargetFlagFull(t *testing.T) {
cmd := emptyCommand(t)
initTargetFlag(cmd)
cmd.SetArgs([]string{"version", "--target", "development"})

ctx := t.Context()
err := Execute(ctx, cmd)
err := Execute(ctx, cmd, []string{"version", "--target", "development"})
assert.NoError(t, err)

assert.Equal(t, "development", getTarget(cmd))
Expand All @@ -399,10 +398,9 @@ func TestTargetFlagFull(t *testing.T) {
func TestTargetFlagShort(t *testing.T) {
cmd := emptyCommand(t)
initTargetFlag(cmd)
cmd.SetArgs([]string{"version", "-t", "production"})

ctx := t.Context()
err := Execute(ctx, cmd)
err := Execute(ctx, cmd, []string{"version", "-t", "production"})
assert.NoError(t, err)

assert.Equal(t, "production", getTarget(cmd))
Expand All @@ -413,10 +411,9 @@ func TestTargetEnvironmentFlag(t *testing.T) {
cmd := emptyCommand(t)
initTargetFlag(cmd)
initEnvironmentFlag(cmd)
cmd.SetArgs([]string{"version", "--environment", "development"})

ctx := t.Context()
err := Execute(ctx, cmd)
err := Execute(ctx, cmd, []string{"version", "--environment", "development"})
assert.NoError(t, err)

assert.Equal(t, "development", getTarget(cmd))
Expand Down
14 changes: 11 additions & 3 deletions cmd/root/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,11 @@ func flagErrorFunc(c *cobra.Command, err error) error {
return fmt.Errorf("%w\n\n%s", err, c.UsageString())
}

// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute(ctx context.Context, cmd *cobra.Command) (err error) {
// Execute runs cmd with args, which select the subcommand to run. It is called
// by main.main() with the arguments after the program name. Unlike
// [cobra.Command.SetArgs], nil args mean no arguments rather than the process
// arguments.
func Execute(ctx context.Context, cmd *cobra.Command, args []string) (err error) {
defer func() {
r := recover()

Expand Down Expand Up @@ -152,6 +154,12 @@ Stack Trace:
// Set a command execution ID value in the context
ctx = cmdctx.GenerateExecId(ctx)

// Cobra runs with the process arguments if args are nil.
if args == nil {
args = []string{}
}
cmd.SetArgs(args)

startTime := time.Now()

// Run the command
Expand Down
26 changes: 22 additions & 4 deletions cmd/root/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"errors"
"os"
"testing"

"github.com/databricks/cli/libs/cmdctx"
Expand Down Expand Up @@ -45,7 +46,7 @@ func TestExecuteEnrichesAuthErrors(t *testing.T) {
return nil
}

err := Execute(ctx, cmd)
err := Execute(ctx, cmd, nil)
require.Error(t, err)

output := stderr.String()
Expand All @@ -71,7 +72,7 @@ func TestExecuteNoEnrichmentWithoutConfigUsed(t *testing.T) {
}
cmd.SetErr(stderr)

err := Execute(ctx, cmd)
err := Execute(ctx, cmd, nil)
require.Error(t, err)

output := stderr.String()
Expand Down Expand Up @@ -144,7 +145,7 @@ func TestExecuteAppendsAccountHostHint(t *testing.T) {
return nil
}

err := Execute(t.Context(), cmd)
err := Execute(t.Context(), cmd, nil)
require.Error(t, err)

output := stderr.String()
Expand Down Expand Up @@ -173,7 +174,24 @@ func TestExecuteErrAlreadyPrintedNotEnriched(t *testing.T) {
}
cmd.SetErr(stderr)

err := Execute(ctx, cmd)
err := Execute(ctx, cmd, nil)
require.Error(t, err)
assert.Empty(t, stderr.String())
}

func TestExecuteWithNilArgsRunsWithoutArgs(t *testing.T) {
processArgs := os.Args
t.Cleanup(func() { os.Args = processArgs })
os.Args = []string{"databricks", "process-arg"}

var got []string
cmd := &cobra.Command{
Use: "test",
RunE: func(cmd *cobra.Command, args []string) error {
got = args
return nil
},
}
require.NoError(t, Execute(t.Context(), cmd, nil))
assert.Empty(t, got)
}
6 changes: 2 additions & 4 deletions internal/testcli/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ func (r *Runner) RunBackground() {
cli := cmd.New(r.ctx)
cli.SetOut(stdoutW)
cli.SetErr(stderrW)
cli.SetArgs(r.args)
if r.stdinW != nil {
cli.SetIn(r.stdinR)
}
Expand All @@ -123,7 +122,7 @@ func (r *Runner) RunBackground() {

// Run command in background.
go func() {
err := root.Execute(ctx, cli)
err := root.Execute(ctx, cli, r.args)
if err != nil {
if r.Verbose {
r.Logf("Error running command: %s", err)
Expand Down Expand Up @@ -181,13 +180,12 @@ func (r *Runner) Run() (bytes.Buffer, bytes.Buffer, error) {
cli := cmd.New(r.ctx)
cli.SetOut(&stdout)
cli.SetErr(&stderr)
cli.SetArgs(r.args)

if r.Verbose {
r.Logf(" args: %s", strings.Join(r.args, ", "))
}

err := root.Execute(r.ctx, cli)
err := root.Execute(r.ctx, cli, r.args)
if err != nil {
if r.Verbose {
r.Logf(" error: %s", err)
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func main() {
}

ctx := context.Background()
err := root.Execute(ctx, cmd.New(ctx))
err := root.Execute(ctx, cmd.New(ctx), os.Args[1:])
if err != nil {
os.Exit(1)
}
Expand Down
Loading