main_test.go 615 B

12345678910111213141516171819202122232425262728293031
  1. package main
  2. import (
  3. "bytes"
  4. "errors"
  5. "testing"
  6. "github.com/spf13/cobra"
  7. )
  8. func TestRootCommandSuppressesCobraErrorAndUsage(t *testing.T) {
  9. cmd := newRootCommand()
  10. var output bytes.Buffer
  11. cmd.SetOut(&output)
  12. cmd.SetErr(&output)
  13. cmd.AddCommand(&cobra.Command{
  14. Use: "probe",
  15. RunE: func(*cobra.Command, []string) error {
  16. return errors.New("probe failed")
  17. },
  18. })
  19. cmd.SetArgs([]string{"probe"})
  20. err := cmd.Execute()
  21. if err == nil {
  22. t.Fatal("Execute() expected error, got nil")
  23. }
  24. if output.Len() != 0 {
  25. t.Errorf("Cobra output = %q, want no duplicate error or usage", output.String())
  26. }
  27. }