(t *testing.T)
| 962 | } |
| 963 | |
| 964 | func TestShowRootCommandHelp_HelpPrinter(t *testing.T) { |
| 965 | doublecho := func(text string) string { |
| 966 | return text + " " + text |
| 967 | } |
| 968 | |
| 969 | tests := []struct { |
| 970 | name string |
| 971 | template string |
| 972 | printer HelpPrinterFunc |
| 973 | wantTemplate string |
| 974 | wantOutput string |
| 975 | }{ |
| 976 | { |
| 977 | name: "standard-command", |
| 978 | template: "", |
| 979 | printer: func(w io.Writer, _ string, _ any) { |
| 980 | fmt.Fprint(w, "yo") |
| 981 | }, |
| 982 | wantTemplate: RootCommandHelpTemplate, |
| 983 | wantOutput: "yo", |
| 984 | }, |
| 985 | { |
| 986 | name: "custom-template-command", |
| 987 | template: "{{doublecho .Name}}", |
| 988 | printer: func(w io.Writer, templ string, data any) { |
| 989 | // Pass a custom function to ensure it gets used |
| 990 | fm := map[string]any{"doublecho": doublecho} |
| 991 | DefaultPrintHelpCustom(w, templ, data, fm) |
| 992 | }, |
| 993 | wantTemplate: "{{doublecho .Name}}", |
| 994 | wantOutput: "my-app my-app", |
| 995 | }, |
| 996 | } |
| 997 | |
| 998 | for _, tt := range tests { |
| 999 | t.Run(tt.name, func(t *testing.T) { |
| 1000 | defer func(old HelpPrinterFunc) { |
| 1001 | HelpPrinter = old |
| 1002 | }(HelpPrinter) |
| 1003 | HelpPrinter = func(w io.Writer, templ string, data any) { |
| 1004 | assert.Equal(t, tt.wantTemplate, templ, "unexpected template") |
| 1005 | tt.printer(w, templ, data) |
| 1006 | } |
| 1007 | |
| 1008 | var buf bytes.Buffer |
| 1009 | cmd := &Command{ |
| 1010 | Name: "my-app", |
| 1011 | Writer: &buf, |
| 1012 | CustomRootCommandHelpTemplate: tt.template, |
| 1013 | } |
| 1014 | |
| 1015 | err := cmd.Run(buildTestContext(t), []string{"my-app", "help"}) |
| 1016 | require.NoError(t, err) |
| 1017 | |
| 1018 | assert.Equal(t, tt.wantOutput, buf.String()) |
| 1019 | }) |
| 1020 | } |
| 1021 | } |
nothing calls this directly
no test coverage detected