Skip to content

Commit

Permalink
test(rootCmd): add a test function to test the root command
Browse files Browse the repository at this point in the history
  • Loading branch information
danvergara committed Apr 3, 2021
1 parent 3e2758d commit c98b1b1
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 14 deletions.
34 changes: 20 additions & 14 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,22 +38,28 @@ var (
ssl string
)

// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "dblab",
Short: "Interactive databse client",
Long: `dblab is a terminal UI based interactive database client for Postgres, MySQL and SQLite.`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Printf("driver: %s\n", driver)
fmt.Printf("host: %s\n", host)
fmt.Printf("db: %s\n", db)
fmt.Printf("port: %s\n", port)
fmt.Printf("user: %s\n", user)
fmt.Printf("pass: %s\n", pass)
fmt.Printf("ssl: %s\n", ssl)
},
// NewRootCmd returns the root command
func NewRootCmd() *cobra.Command {
return &cobra.Command{
Use: "dblab",
Short: "Interactive databse client",
Long: `dblab is a terminal UI based interactive database client for Postgres, MySQL and SQLite.`,
RunE: func(cmd *cobra.Command, args []string) error {
fmt.Printf("driver: %s\n", driver)
fmt.Printf("host: %s\n", host)
fmt.Printf("db: %s\n", db)
fmt.Printf("port: %s\n", port)
fmt.Printf("user: %s\n", user)
fmt.Printf("pass: %s\n", pass)
fmt.Printf("ssl: %s\n", ssl)
return nil
},
}
}

// rootCmd represents the base command when called without any subcommands
var rootCmd = NewRootCmd()

// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
Expand Down
13 changes: 13 additions & 0 deletions cmd/root_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package cmd

import "testing"

func TestRootcmd(t *testing.T) {
cmd := NewRootCmd()

err := cmd.Execute()

if err != nil {
t.Error(err)
}
}

0 comments on commit c98b1b1

Please sign in to comment.