Skip to content

Commit

Permalink
feat(test): delete command unit test
Browse files Browse the repository at this point in the history
* Added a suite of test to check flags available for delete command
* Refactored install_test.go to avoid clash with delete method names

Closes #1157
  • Loading branch information
squakez authored and nicolaferraro committed Jan 11, 2021
1 parent 0e3fa94 commit 59ba8ac
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 103 deletions.
34 changes: 30 additions & 4 deletions pkg/cmd/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,46 @@ limitations under the License.
package cmd

import (
"testing"

"github.com/apache/camel-k/pkg/util/test"
"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
)

//nolint:deadcode,unused
const cmdDelete = "delete"

func initializeDeleteCmdOptions(t *testing.T) (*deleteCmdOptions, *cobra.Command, RootCmdOptions) {
options, rootCmd := kamelTestPreAddCommandInit()
deleteCmdOptions := addTestDeleteCmd(*options, rootCmd)
kamelTestPostAddCommandInit(t, rootCmd)

return deleteCmdOptions, rootCmd, *options
}

func addTestDeleteCmd(options RootCmdOptions, rootCmd *cobra.Command) *deleteCmdOptions {
//add a testing version of delete Command
deleteCmd, deleteCmdOptions := newCmdDelete(&options)
deleteCmd, deleteOptions := newCmdDelete(&options)
deleteCmd.RunE = func(c *cobra.Command, args []string) error {
return nil
}
deleteCmd.PostRunE = func(c *cobra.Command, args []string) error {
return nil
}
deleteCmd.Args = test.ArbitraryArgs
rootCmd.AddCommand(deleteCmd)
return deleteCmdOptions
return deleteOptions
}

//TODO: add a proper test, take inspiration by run_test.go
func TestDeleteNonExistingFlag(t *testing.T) {
_, rootCmd, _ := initializeDeleteCmdOptions(t)
_, err := test.ExecuteCommand(rootCmd, cmdDelete, "--nonExistingFlag")
assert.NotNil(t, err)
}

func TestDeleteAllFlag(t *testing.T) {
deleteCmdOptions, rootCmd, _ := initializeDeleteCmdOptions(t)
_, err := test.ExecuteCommand(rootCmd, cmdDelete, "--all")
assert.Nil(t, err)
assert.Equal(t, true, deleteCmdOptions.DeleteAll)
}

0 comments on commit 59ba8ac

Please sign in to comment.