79 lines
1.7 KiB
Go
79 lines
1.7 KiB
Go
package main
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
func TestRootCommand(t *testing.T) {
|
|
if rootCmd.Use != "mcias" {
|
|
t.Errorf("Expected root command Use to be 'mcias', got '%s'", rootCmd.Use)
|
|
}
|
|
|
|
if rootCmd.Short == "" {
|
|
t.Error("Expected root command Short to be set")
|
|
}
|
|
|
|
if rootCmd.Long == "" {
|
|
t.Error("Expected root command Long to be set")
|
|
}
|
|
dbFlag := rootCmd.PersistentFlags().Lookup("db")
|
|
if dbFlag == nil {
|
|
t.Error("Expected 'db' flag to be defined")
|
|
} else {
|
|
if dbFlag.DefValue != "mcias.db" {
|
|
t.Errorf("Expected 'db' flag default value to be 'mcias.db', got '%s'", dbFlag.DefValue)
|
|
}
|
|
}
|
|
|
|
addrFlag := rootCmd.PersistentFlags().Lookup("addr")
|
|
if addrFlag == nil {
|
|
t.Error("Expected 'addr' flag to be defined")
|
|
} else {
|
|
if addrFlag.DefValue != ":8080" {
|
|
t.Errorf("Expected 'addr' flag default value to be ':8080', got '%s'", addrFlag.DefValue)
|
|
}
|
|
}
|
|
hasServerCmd := false
|
|
hasInitCmd := false
|
|
hasUserCmd := false
|
|
hasTokenCmd := false
|
|
|
|
for _, cmd := range rootCmd.Commands() {
|
|
switch cmd.Use {
|
|
case "server":
|
|
hasServerCmd = true
|
|
case "init":
|
|
hasInitCmd = true
|
|
case "user":
|
|
hasUserCmd = true
|
|
case "token":
|
|
hasTokenCmd = true
|
|
}
|
|
}
|
|
|
|
if !hasServerCmd {
|
|
t.Error("Expected 'server' command to be added to root command")
|
|
}
|
|
if !hasInitCmd {
|
|
t.Error("Expected 'init' command to be added to root command")
|
|
}
|
|
if !hasUserCmd {
|
|
t.Error("Expected 'user' command to be added to root command")
|
|
}
|
|
if !hasTokenCmd {
|
|
t.Error("Expected 'token' command to be added to root command")
|
|
}
|
|
}
|
|
|
|
func TestExecute(t *testing.T) {
|
|
origCmd := rootCmd
|
|
defer func() { rootCmd = origCmd }()
|
|
|
|
rootCmd = &cobra.Command{Use: "test"}
|
|
if err := Execute(); err != nil {
|
|
t.Errorf("Execute() returned an error: %v", err)
|
|
}
|
|
}
|