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

feat(test): delete command unit test #1888

Merged
merged 1 commit into from
Jan 11, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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)
}