diff --git a/internal/cmd/base/delete.go b/internal/cmd/base/delete.go index 5e613ce6..d1fb37cd 100644 --- a/internal/cmd/base/delete.go +++ b/internal/cmd/base/delete.go @@ -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 - 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) } } } diff --git a/internal/cmd/base/delete_test.go b/internal/cmd/base/delete_test.go index 1b2194c4..42fc832f 100644 --- a/internal/cmd/base/delete_test.go +++ b/internal/cmd/base/delete_test.go @@ -1,6 +1,7 @@ package base_test import ( + "errors" "sync" "testing" @@ -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", @@ -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"}, }, diff --git a/internal/testutil/cmd.go b/internal/testutil/cmd.go index ad31a459..271d649c 100644 --- a/internal/testutil/cmd.go +++ b/internal/testutil/cmd.go @@ -22,6 +22,7 @@ type TestCase struct { ExpOutType DataType ExpErrOut string ExpErrOutType DataType + ExpErr string } type DataType string @@ -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) })