Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: errors silently ignored when deleting resources #792

Merged
merged 4 commits into from
Jun 26, 2024
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
16 changes: 9 additions & 7 deletions internal/cmd/base/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,16 +85,18 @@ func (dc *DeleteCmd) Run(s state.State, cmd *cobra.Command, args []string) error
}
}

if len(actions) > 0 {
// TODO: We do not check when an action fail for a specific resource
jooola marked this conversation as resolved.
Show resolved Hide resolved
if err := s.WaitForActions(cmd, s, actions...); err != nil {
errs = append(errs, err)
for _, result := range results {
if result.Error != nil {
errs = append(errs, result.Error)
} else {
deleted = append(deleted, result.IDOrName)
}
}

for _, result := range results {
if result.Error == nil {
deleted = append(deleted, result.IDOrName)
if len(actions) > 0 {
// TODO: We do not check if an action fails for a specific resource
if err := s.WaitForActions(cmd, s, actions...); err != nil {
errs = append(errs, err)
}
}
}
Expand Down
16 changes: 16 additions & 0 deletions internal/cmd/base/delete_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package base_test

import (
"errors"
"sync"
"testing"

Expand Down Expand Up @@ -28,6 +29,11 @@ var fakeDeleteCmd = &base.DeleteCmd{
mu.Lock()
cmd.Println("Fetching fake resource")

if idOrName == "fail" {
mu.Unlock()
return nil, nil, errors.New("this is an error")
}

resource := &fakeResource{
ID: 123,
Name: "test",
Expand All @@ -52,6 +58,16 @@ func TestDelete(t *testing.T) {
ExpOut: "Fetching fake resource\nDeleting fake resource\nFetching fake resource\nDeleting fake resource\n" +
"Fetching fake resource\nDeleting fake resource\nFake resources 123, 456, 789 deleted\n",
},
"error": {
Args: []string{"delete", "fail"},
ExpOut: "Fetching fake resource\n",
ExpErr: "this is an error",
},
"error multiple": {
Args: []string{"delete", "123", "fail", "789"},
ExpOut: "Fetching fake resource\nDeleting fake resource\nFetching fake resource\nFetching fake resource\nDeleting fake resource\nFake resources 123, 789 deleted\n",
ExpErr: "this is an error",
},
"quiet": {
Args: []string{"delete", "123", "--quiet"},
},
Expand Down
7 changes: 6 additions & 1 deletion internal/testutil/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type TestCase struct {
ExpOutType DataType
ExpErrOut string
ExpErrOutType DataType
ExpErr string
}

type DataType string
Expand Down Expand Up @@ -72,7 +73,11 @@ func TestCommand(t *testing.T, cmd TestableCommand, cases map[string]TestCase) {

out, errOut, err := fx.Run(rootCmd, testCase.Args)

assert.NoError(t, err)
if testCase.ExpErr != "" {
assert.EqualError(t, err, testCase.ExpErr)
} else {
assert.NoError(t, err)
}
testCase.ExpOutType.test(t, testCase.ExpOut, out)
testCase.ExpErrOutType.test(t, testCase.ExpErrOut, errOut)
})
Expand Down
Loading