Skip to content
Merged
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
60 changes: 60 additions & 0 deletions cmd/resources/empty_response_test.go
Original file line number Diff line number Diff line change
@@ -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))
})
}
}
15 changes: 15 additions & 0 deletions cmd/resources/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]))
Expand All @@ -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"), "-", " ")

Comment thread
katc1004 marked this conversation as resolved.
Resolved
return fmt.Sprintf("No %s returned", resourceName)
}

func NewOperationCmd(
parentCmd *cobra.Command,
client resources.Client,
Expand Down
Loading