From 300dd9b3fe7d336bf00b315d8ff5105a81dd944f Mon Sep 17 00:00:00 2001 From: Andrew Nester Date: Fri, 26 May 2023 15:45:17 +0200 Subject: [PATCH 1/2] Do not prompt for List methods --- .codegen/service.go.tmpl | 2 +- cmd/workspace/schemas/schemas.go | 23 ++++++++--------------- cmd/workspace/workspace/workspace.go | 23 ++++++++--------------- 3 files changed, 17 insertions(+), 31 deletions(-) diff --git a/.codegen/service.go.tmpl b/.codegen/service.go.tmpl index 5510978a23..aa6d3fd116 100644 --- a/.codegen/service.go.tmpl +++ b/.codegen/service.go.tmpl @@ -60,7 +60,7 @@ func init() { {{ $hasPosArgs := and .Request (or .Request.IsAllRequiredFieldsPrimitive (eq .PascalName "RunNow")) -}} {{- $hasSinglePosArg := and $hasPosArgs (eq 1 (len .Request.RequiredFields)) -}} -{{- $serviceHasNamedIdMap := and .Service.List .Service.List.NamedIdMap -}} +{{- $serviceHasNamedIdMap := and (and .Service.List .Service.List.NamedIdMap) (not (eq .PascalName "List")) -}} {{- $hasIdPrompt := and $hasSinglePosArg $serviceHasNamedIdMap -}} {{- $wait := and .Wait (and (not .IsCrudRead) (not (eq .SnakeName "get_run"))) -}} {{- $hasRequiredArgs := and (not $hasIdPrompt) $hasPosArgs -}} diff --git a/cmd/workspace/schemas/schemas.go b/cmd/workspace/schemas/schemas.go index 1f62d0cf45..16e0387a94 100755 --- a/cmd/workspace/schemas/schemas.go +++ b/cmd/workspace/schemas/schemas.go @@ -214,7 +214,14 @@ var listCmd = &cobra.Command{ no guarantee of a specific ordering of the elements in the array.`, Annotations: map[string]string{}, - PreRunE: root.MustWorkspaceClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(1) + if cmd.Flags().Changed("json") { + check = cobra.ExactArgs(0) + } + return check(cmd, args) + }, + PreRunE: root.MustWorkspaceClient, RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) @@ -224,20 +231,6 @@ var listCmd = &cobra.Command{ return err } } else { - if len(args) == 0 { - names, err := w.Schemas.SchemaInfoNameToFullNameMap(ctx, catalog.ListSchemasRequest{}) - if err != nil { - return err - } - id, err := cmdio.Select(ctx, names, "Parent catalog for schemas of interest") - if err != nil { - return err - } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have parent catalog for schemas of interest") - } listReq.CatalogName = args[0] } diff --git a/cmd/workspace/workspace/workspace.go b/cmd/workspace/workspace/workspace.go index 7cfe39dca4..0462342452 100755 --- a/cmd/workspace/workspace/workspace.go +++ b/cmd/workspace/workspace/workspace.go @@ -294,7 +294,14 @@ var listCmd = &cobra.Command{ RESOURCE_DOES_NOT_EXIST.`, Annotations: map[string]string{}, - PreRunE: root.MustWorkspaceClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(1) + if cmd.Flags().Changed("json") { + check = cobra.ExactArgs(0) + } + return check(cmd, args) + }, + PreRunE: root.MustWorkspaceClient, RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) @@ -304,20 +311,6 @@ var listCmd = &cobra.Command{ return err } } else { - if len(args) == 0 { - names, err := w.Workspace.ObjectInfoPathToObjectIdMap(ctx, workspace.ListWorkspaceRequest{}) - if err != nil { - return err - } - id, err := cmdio.Select(ctx, names, "The absolute path of the notebook or directory") - if err != nil { - return err - } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have the absolute path of the notebook or directory") - } listReq.Path = args[0] } From 3b6b29f30efae62f7896b06c1dc5040abca183dd Mon Sep 17 00:00:00 2001 From: Andrew Nester Date: Fri, 26 May 2023 15:54:00 +0200 Subject: [PATCH 2/2] Added integration tests --- internal/clusters_test.go | 3 --- internal/helpers.go | 2 ++ internal/secrets_test.go | 2 -- internal/workspace_test.go | 24 ++++++++++++++++++++++++ 4 files changed, 26 insertions(+), 5 deletions(-) create mode 100644 internal/workspace_test.go diff --git a/internal/clusters_test.go b/internal/clusters_test.go index 827015d2e0..742509724e 100644 --- a/internal/clusters_test.go +++ b/internal/clusters_test.go @@ -6,8 +6,6 @@ import ( "testing" "github.com/stretchr/testify/assert" - - _ "github.com/databricks/cli/cmd/workspace" ) var clusterId string @@ -24,7 +22,6 @@ func TestAccClustersList(t *testing.T) { idRegExp := regexp.MustCompile(`[0-9]{4}\-[0-9]{6}-[a-z0-9]{8}`) clusterId = idRegExp.FindString(outStr) - fmt.Println(clusterId) assert.NotEmpty(t, clusterId) } diff --git a/internal/helpers.go b/internal/helpers.go index 135ebbdbea..b51d005b27 100644 --- a/internal/helpers.go +++ b/internal/helpers.go @@ -15,6 +15,8 @@ import ( "github.com/databricks/cli/cmd/root" _ "github.com/databricks/cli/cmd/version" "github.com/stretchr/testify/require" + + _ "github.com/databricks/cli/cmd/workspace" ) const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" diff --git a/internal/secrets_test.go b/internal/secrets_test.go index 69423d1753..1fdc48bdde 100644 --- a/internal/secrets_test.go +++ b/internal/secrets_test.go @@ -4,8 +4,6 @@ import ( "testing" "github.com/stretchr/testify/assert" - - _ "github.com/databricks/cli/cmd/workspace" ) func TestSecretsCreateScopeErrWhenNoArguments(t *testing.T) { diff --git a/internal/workspace_test.go b/internal/workspace_test.go new file mode 100644 index 0000000000..83d7be22f3 --- /dev/null +++ b/internal/workspace_test.go @@ -0,0 +1,24 @@ +package internal + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestAccWorkspaceList(t *testing.T) { + t.Log(GetEnvOrSkipTest(t, "CLOUD_ENV")) + + stdout, stderr := RequireSuccessfulRun(t, "workspace", "list", "/") + outStr := stdout.String() + assert.Contains(t, outStr, "ID") + assert.Contains(t, outStr, "Type") + assert.Contains(t, outStr, "Language") + assert.Contains(t, outStr, "Path") + assert.Equal(t, "", stderr.String()) +} + +func TestWorkpaceListErrorWhenNoArguments(t *testing.T) { + _, _, err := RequireErrorRun(t, "workspace", "list") + assert.Equal(t, "accepts 1 arg(s), received 0", err.Error()) +}