|
|
|
@ -295,6 +295,7 @@ func TestShowCommandHelp_HelpPrinter(t *testing.T) {
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestShowCommandHelp_HelpPrinterCustom(t *testing.T) {
|
|
|
|
|
doublecho := func(text string) string {
|
|
|
|
|
return text + " " + text
|
|
|
|
@ -550,6 +551,144 @@ func TestShowAppHelp_HiddenCommand(t *testing.T) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestShowAppHelp_HelpPrinter(t *testing.T) {
|
|
|
|
|
doublecho := func(text string) string {
|
|
|
|
|
return text + " " + text
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
|
name string
|
|
|
|
|
template string
|
|
|
|
|
printer helpPrinter
|
|
|
|
|
wantTemplate string
|
|
|
|
|
wantOutput string
|
|
|
|
|
}{
|
|
|
|
|
{
|
|
|
|
|
name: "standard-command",
|
|
|
|
|
template: "",
|
|
|
|
|
printer: func(w io.Writer, templ string, data interface{}) {
|
|
|
|
|
fmt.Fprint(w, "yo")
|
|
|
|
|
},
|
|
|
|
|
wantTemplate: AppHelpTemplate,
|
|
|
|
|
wantOutput: "yo",
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "custom-template-command",
|
|
|
|
|
template: "{{doublecho .Name}}",
|
|
|
|
|
printer: func(w io.Writer, templ string, data interface{}) {
|
|
|
|
|
// Pass a custom function to ensure it gets used
|
|
|
|
|
fm := map[string]interface{}{"doublecho": doublecho}
|
|
|
|
|
printHelpCustom(w, templ, data, fm)
|
|
|
|
|
},
|
|
|
|
|
wantTemplate: "{{doublecho .Name}}",
|
|
|
|
|
wantOutput: "my-app my-app",
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
|
defer func(old helpPrinter) {
|
|
|
|
|
HelpPrinter = old
|
|
|
|
|
}(HelpPrinter)
|
|
|
|
|
HelpPrinter = func(w io.Writer, templ string, data interface{}) {
|
|
|
|
|
if templ != tt.wantTemplate {
|
|
|
|
|
t.Errorf("want template:\n%s\ngot template:\n%s", tt.wantTemplate, templ)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tt.printer(w, templ, data)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var buf bytes.Buffer
|
|
|
|
|
app := &App{
|
|
|
|
|
Name: "my-app",
|
|
|
|
|
Writer: &buf,
|
|
|
|
|
CustomAppHelpTemplate: tt.template,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err := app.Run([]string{"my-app", "help"})
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
got := buf.String()
|
|
|
|
|
if got != tt.wantOutput {
|
|
|
|
|
t.Errorf("want output %q, got %q", tt.wantOutput, got)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestShowAppHelp_HelpPrinterCustom(t *testing.T) {
|
|
|
|
|
doublecho := func(text string) string {
|
|
|
|
|
return text + " " + text
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
|
name string
|
|
|
|
|
template string
|
|
|
|
|
printer helpPrinterCustom
|
|
|
|
|
wantTemplate string
|
|
|
|
|
wantOutput string
|
|
|
|
|
}{
|
|
|
|
|
{
|
|
|
|
|
name: "standard-command",
|
|
|
|
|
template: "",
|
|
|
|
|
printer: func(w io.Writer, templ string, data interface{}, fm map[string]interface{}) {
|
|
|
|
|
fmt.Fprint(w, "yo")
|
|
|
|
|
},
|
|
|
|
|
wantTemplate: AppHelpTemplate,
|
|
|
|
|
wantOutput: "yo",
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "custom-template-command",
|
|
|
|
|
template: "{{doublecho .Name}}",
|
|
|
|
|
printer: func(w io.Writer, templ string, data interface{}, _ map[string]interface{}) {
|
|
|
|
|
// Pass a custom function to ensure it gets used
|
|
|
|
|
fm := map[string]interface{}{"doublecho": doublecho}
|
|
|
|
|
printHelpCustom(w, templ, data, fm)
|
|
|
|
|
},
|
|
|
|
|
wantTemplate: "{{doublecho .Name}}",
|
|
|
|
|
wantOutput: "my-app my-app",
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
|
defer func(old helpPrinterCustom) {
|
|
|
|
|
HelpPrinterCustom = old
|
|
|
|
|
}(HelpPrinterCustom)
|
|
|
|
|
HelpPrinterCustom = func(w io.Writer, templ string, data interface{}, fm map[string]interface{}) {
|
|
|
|
|
if fm != nil {
|
|
|
|
|
t.Error("unexpected function map passed")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if templ != tt.wantTemplate {
|
|
|
|
|
t.Errorf("want template:\n%s\ngot template:\n%s", tt.wantTemplate, templ)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tt.printer(w, templ, data, fm)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var buf bytes.Buffer
|
|
|
|
|
app := &App{
|
|
|
|
|
Name: "my-app",
|
|
|
|
|
Writer: &buf,
|
|
|
|
|
CustomAppHelpTemplate: tt.template,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err := app.Run([]string{"my-app", "help"})
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
got := buf.String()
|
|
|
|
|
if got != tt.wantOutput {
|
|
|
|
|
t.Errorf("want output %q, got %q", tt.wantOutput, got)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestShowAppHelp_CustomAppTemplate(t *testing.T) {
|
|
|
|
|
app := &App{
|
|
|
|
|
Commands: []Command{
|
|
|
|
|