(t *testing.T)
| 1021 | } |
| 1022 | |
| 1023 | func TestShowRootCommandHelp_HelpPrinterCustom(t *testing.T) { |
| 1024 | doublecho := func(text string) string { |
| 1025 | return text + " " + text |
| 1026 | } |
| 1027 | |
| 1028 | tests := []struct { |
| 1029 | name string |
| 1030 | template string |
| 1031 | printer HelpPrinterCustomFunc |
| 1032 | wantTemplate string |
| 1033 | wantOutput string |
| 1034 | }{ |
| 1035 | { |
| 1036 | name: "standard-command", |
| 1037 | template: "", |
| 1038 | printer: func(w io.Writer, _ string, _ any, _ map[string]any) { |
| 1039 | fmt.Fprint(w, "yo") |
| 1040 | }, |
| 1041 | wantTemplate: RootCommandHelpTemplate, |
| 1042 | wantOutput: "yo", |
| 1043 | }, |
| 1044 | { |
| 1045 | name: "custom-template-command", |
| 1046 | template: "{{doublecho .Name}}", |
| 1047 | printer: func(w io.Writer, templ string, data any, _ map[string]any) { |
| 1048 | // Pass a custom function to ensure it gets used |
| 1049 | fm := map[string]any{"doublecho": doublecho} |
| 1050 | DefaultPrintHelpCustom(w, templ, data, fm) |
| 1051 | }, |
| 1052 | wantTemplate: "{{doublecho .Name}}", |
| 1053 | wantOutput: "my-app my-app", |
| 1054 | }, |
| 1055 | } |
| 1056 | |
| 1057 | for _, tt := range tests { |
| 1058 | t.Run(tt.name, func(t *testing.T) { |
| 1059 | defer func(old HelpPrinterCustomFunc) { |
| 1060 | HelpPrinterCustom = old |
| 1061 | }(HelpPrinterCustom) |
| 1062 | HelpPrinterCustom = func(w io.Writer, templ string, data any, fm map[string]any) { |
| 1063 | assert.Nil(t, fm, "unexpected function map passed") |
| 1064 | assert.Equal(t, tt.wantTemplate, templ, "unexpected template") |
| 1065 | tt.printer(w, templ, data, fm) |
| 1066 | } |
| 1067 | |
| 1068 | var buf bytes.Buffer |
| 1069 | cmd := &Command{ |
| 1070 | Name: "my-app", |
| 1071 | Writer: &buf, |
| 1072 | CustomRootCommandHelpTemplate: tt.template, |
| 1073 | } |
| 1074 | |
| 1075 | err := cmd.Run(buildTestContext(t), []string{"my-app", "help"}) |
| 1076 | require.NoError(t, err) |
| 1077 | assert.Equal(t, tt.wantOutput, buf.String()) |
| 1078 | }) |
| 1079 | } |
| 1080 | } |
nothing calls this directly
no test coverage detected