diff --git a/cmd/resources/empty_response_test.go b/cmd/resources/empty_response_test.go new file mode 100644 index 000000000..d18c31761 --- /dev/null +++ b/cmd/resources/empty_response_test.go @@ -0,0 +1,60 @@ +package resources_test + +import ( + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + + "github.com/launchdarkly/ldcli/cmd" + "github.com/launchdarkly/ldcli/internal/analytics" + "github.com/launchdarkly/ldcli/internal/resources" +) + +// A GET that returns no body, such as the default release plan of an +// environment that has none, must not print the last path parameter as a key. +func TestEmptyResponse(t *testing.T) { + tests := []struct { + name string + args []string + expected string + }{ + { + name: "GET with JSON output prints an empty object", + args: []string{"projects", "get", "--project", "my-project", "--output", "json"}, + expected: "{}\n", + }, + { + name: "GET with plaintext output says no resource was returned", + args: []string{"projects", "get", "--project", "my-project", "--output", "plaintext"}, + expected: "No projects returned\n", + }, + { + name: "GET on a beta resource drops the suffix from the message", + args: []string{"flags-beta", "list-dependent", "--project", "my-project", "--flag", "my-flag", "--output", "plaintext"}, + expected: "No flags returned\n", + }, + { + name: "DELETE still prints the key of the deleted resource", + args: []string{"projects", "delete", "--project", "my-project", "--output", "json"}, + expected: `{"key": "my-project"}` + "\n", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + client := &resources.MockClient{Response: []byte("")} + args := append(tt.args, "--access-token", "abcd1234") + + output, err := cmd.CallCmd( + t, + cmd.APIClients{ResourcesClient: client}, + analytics.NoopClientFn{}.Tracker(), + args, + ) + + require.NoError(t, err) + assert.Equal(t, tt.expected, string(output)) + }) + } +} diff --git a/cmd/resources/resources.go b/cmd/resources/resources.go index 188204e14..1dde37ad3 100644 --- a/cmd/resources/resources.go +++ b/cmd/resources/resources.go @@ -344,6 +344,10 @@ func (op *OperationCmd) makeRequest(cmd *cobra.Command, args []string) error { } if string(res) == "" { + if strings.EqualFold(op.HTTPMethod, "GET") { + fmt.Fprint(cmd.OutOrStdout(), emptyGetOutput(cmd)+"\n") + return nil + } // assuming the key to be deleted/replaced is last in the list of params, // e.g. contexts delete-instances or code-refs replace-branch res = []byte(fmt.Sprintf(`{"key": %q}`, urlParms[len(urlParms)-1])) @@ -362,6 +366,17 @@ func (op *OperationCmd) makeRequest(cmd *cobra.Command, args []string) error { return nil } +// emptyGetOutput reports a GET that found no resource, such as an environment +// with no default release plan, so the response is not mistaken for a resource. +func emptyGetOutput(cmd *cobra.Command) string { + if cliflags.GetOutputKind(cmd) == output.OutputKindJSON.String() { + return "{}" + } + resourceName := strings.ReplaceAll(strings.TrimSuffix(cmd.Parent().Name(), "-beta"), "-", " ") + + return fmt.Sprintf("No %s returned", resourceName) +} + func NewOperationCmd( parentCmd *cobra.Command, client resources.Client,