github.com/ungtb10d/cli/v2@v2.0.0-20221110210412-98537dd9d6a1/cmd/gh/main_test.go (about) 1 package main 2 3 import ( 4 "bytes" 5 "errors" 6 "fmt" 7 "net" 8 "testing" 9 10 "github.com/ungtb10d/cli/v2/pkg/cmdutil" 11 "github.com/spf13/cobra" 12 ) 13 14 func Test_printError(t *testing.T) { 15 cmd := &cobra.Command{} 16 17 type args struct { 18 err error 19 cmd *cobra.Command 20 debug bool 21 } 22 tests := []struct { 23 name string 24 args args 25 wantOut string 26 }{ 27 { 28 name: "generic error", 29 args: args{ 30 err: errors.New("the app exploded"), 31 cmd: nil, 32 debug: false, 33 }, 34 wantOut: "the app exploded\n", 35 }, 36 { 37 name: "DNS error", 38 args: args{ 39 err: fmt.Errorf("DNS oopsie: %w", &net.DNSError{ 40 Name: "api.github.com", 41 }), 42 cmd: nil, 43 debug: false, 44 }, 45 wantOut: `error connecting to api.github.com 46 check your internet connection or https://githubstatus.com 47 `, 48 }, 49 { 50 name: "Cobra flag error", 51 args: args{ 52 err: cmdutil.FlagErrorf("unknown flag --foo"), 53 cmd: cmd, 54 debug: false, 55 }, 56 wantOut: "unknown flag --foo\n\nUsage:\n\n", 57 }, 58 { 59 name: "unknown Cobra command error", 60 args: args{ 61 err: errors.New("unknown command foo"), 62 cmd: cmd, 63 debug: false, 64 }, 65 wantOut: "unknown command foo\n\nUsage:\n\n", 66 }, 67 } 68 69 for _, tt := range tests { 70 t.Run(tt.name, func(t *testing.T) { 71 out := &bytes.Buffer{} 72 printError(out, tt.args.err, tt.args.cmd, tt.args.debug) 73 if gotOut := out.String(); gotOut != tt.wantOut { 74 t.Errorf("printError() = %q, want %q", gotOut, tt.wantOut) 75 } 76 }) 77 } 78 }