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