diff --git a/client/cmd/root.go b/client/cmd/root.go index a9fc9bc60..f69979210 100644 --- a/client/cmd/root.go +++ b/client/cmd/root.go @@ -93,7 +93,7 @@ func init() { rootCmd.PersistentFlags().StringVarP(&logLevel, "log-level", "l", "info", "sets Netbird log level") rootCmd.PersistentFlags().StringVar(&logFile, "log-file", defaultLogFile, "sets Netbird log path. If console is specified the the log will be output to stdout") rootCmd.PersistentFlags().StringVarP(&setupKey, "setup-key", "k", "", "Setup key obtained from the Management Service Dashboard (used to register peer)") - rootCmd.PersistentFlags().StringVarP(&preSharedKey, "preshared-key", "p", "", "Sets Wireguard PreSharedKey property. If set, then only peers that have the same key can communicate.") + rootCmd.PersistentFlags().StringVar(&preSharedKey, "preshared-key", "", "Sets Wireguard PreSharedKey property. If set, then only peers that have the same key can communicate.") rootCmd.AddCommand(serviceCmd) rootCmd.AddCommand(upCmd) rootCmd.AddCommand(downCmd) diff --git a/client/cmd/root_test.go b/client/cmd/root_test.go new file mode 100644 index 000000000..abb7d41b2 --- /dev/null +++ b/client/cmd/root_test.go @@ -0,0 +1,36 @@ +package cmd + +import ( + "fmt" + "io" + "testing" +) + +func TestInitCommands(t *testing.T) { + helpFlag := "-h" + commandArgs := [][]string{{"root", helpFlag}} + for _, command := range rootCmd.Commands() { + commandArgs = append(commandArgs, []string{command.Name(), command.Name(), helpFlag}) + for _, subcommand := range command.Commands() { + commandArgs = append(commandArgs, []string{command.Name() + " " + subcommand.Name(), command.Name(), subcommand.Name(), helpFlag}) + } + } + + for _, args := range commandArgs { + t.Run(fmt.Sprintf("Testing Command %s", args[0]), func(t *testing.T) { + defer func() { + err := recover() + if err != nil { + t.Fatalf("got an panic error while running the command: %s -h. Error: %s", args[0], err) + } + }() + + rootCmd.SetArgs(args[1:]) + rootCmd.SetOut(io.Discard) + if err := rootCmd.Execute(); err != nil { + t.Errorf("expected no error while running %s command, got %v", args[0], err) + return + } + }) + } +}