(t *testing.T)
| 22 | ) |
| 23 | |
| 24 | func Test_printError(t *testing.T) { |
| 25 | cmd := &cobra.Command{} |
| 26 | |
| 27 | type args struct { |
| 28 | err error |
| 29 | cmd *cobra.Command |
| 30 | debug bool |
| 31 | } |
| 32 | tests := []struct { |
| 33 | name string |
| 34 | args args |
| 35 | wantOut string |
| 36 | }{ |
| 37 | { |
| 38 | name: "generic error", |
| 39 | args: args{ |
| 40 | err: errors.New("the app exploded"), |
| 41 | cmd: nil, |
| 42 | debug: false, |
| 43 | }, |
| 44 | wantOut: "the app exploded\n", |
| 45 | }, |
| 46 | { |
| 47 | name: "DNS error", |
| 48 | args: args{ |
| 49 | err: fmt.Errorf("DNS oopsie: %w", &net.DNSError{ |
| 50 | Name: "api.github.com", |
| 51 | }), |
| 52 | cmd: nil, |
| 53 | debug: false, |
| 54 | }, |
| 55 | wantOut: `error connecting to api.github.com |
| 56 | check your internet connection or https://githubstatus.com |
| 57 | `, |
| 58 | }, |
| 59 | { |
| 60 | name: "Cobra flag error", |
| 61 | args: args{ |
| 62 | err: cmdutil.FlagErrorf("unknown flag --foo"), |
| 63 | cmd: cmd, |
| 64 | debug: false, |
| 65 | }, |
| 66 | wantOut: "unknown flag --foo\n\nUsage:\n\n", |
| 67 | }, |
| 68 | { |
| 69 | name: "unknown Cobra command error", |
| 70 | args: args{ |
| 71 | err: errors.New("unknown command foo"), |
| 72 | cmd: cmd, |
| 73 | debug: false, |
| 74 | }, |
| 75 | wantOut: "unknown command foo\n\nUsage:\n\n", |
| 76 | }, |
| 77 | } |
| 78 | |
| 79 | for _, tt := range tests { |
| 80 | t.Run(tt.name, func(t *testing.T) { |
| 81 | out := &bytes.Buffer{} |
nothing calls this directly
no test coverage detected