package ghcmd import ( "bytes" "errors" "fmt" "net" "testing" "github.com/cli/cli/v2/pkg/cmdutil" "github.com/spf13/cobra" ) func Test_printError(t *testing.T) { cmd := &cobra.Command{} type args struct { err error cmd *cobra.Command debug bool } tests := []struct { name string args args wantOut string }{ { name: "generic error", args: args{ err: errors.New("the app exploded"), cmd: nil, debug: false, }, wantOut: "the app exploded\n", }, { name: "DNS error", args: args{ err: fmt.Errorf("DNS oopsie: %w", &net.DNSError{ Name: "api.github.com", }), cmd: nil, debug: false, }, wantOut: `error connecting to api.github.com check your internet connection or https://githubstatus.com `, }, { name: "Cobra flag error", args: args{ err: cmdutil.FlagErrorf("unknown flag --foo"), cmd: cmd, debug: false, }, wantOut: "unknown flag --foo\n\nUsage:\n\n", }, { name: "unknown Cobra command error", args: args{ err: errors.New("unknown command foo"), cmd: cmd, debug: false, }, wantOut: "unknown command foo\n\nUsage:\n\n", }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { out := &bytes.Buffer{} printError(out, tt.args.err, tt.args.cmd, tt.args.debug) if gotOut := out.String(); gotOut != tt.wantOut { t.Errorf("printError() = %q, want %q", gotOut, tt.wantOut) } }) } }