From bab428af09e0373e1deda9dec24d30cc4c5c7026 Mon Sep 17 00:00:00 2001 From: Robert Liebowitz Date: Thu, 17 Oct 2019 05:12:17 -0400 Subject: [PATCH] Ensure command help always prints with overridden print functions --- help_test.go | 174 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 174 insertions(+) diff --git a/help_test.go b/help_test.go index b13eef4..870b32e 100644 --- a/help_test.go +++ b/help_test.go @@ -4,6 +4,7 @@ import ( "bytes" "flag" "fmt" + "io" "runtime" "strings" "testing" @@ -210,6 +211,179 @@ func TestShowAppHelp_CommandAliases(t *testing.T) { } } +func TestShowCommandHelp_HelpPrinter(t *testing.T) { + doublecho := func(text string) string { + return text + " " + text + } + + tests := []struct { + name string + template string + printer helpPrinter + command string + wantTemplate string + wantOutput string + }{ + { + name: "no-command", + template: "", + printer: func(w io.Writer, templ string, data interface{}) { + fmt.Fprint(w, "yo") + }, + command: "", + wantTemplate: SubcommandHelpTemplate, + wantOutput: "yo", + }, + { + name: "standard-command", + template: "", + printer: func(w io.Writer, templ string, data interface{}) { + fmt.Fprint(w, "yo") + }, + command: "my-command", + wantTemplate: CommandHelpTemplate, + 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} + HelpPrinterCustom(w, templ, data, fm) + }, + command: "my-command", + wantTemplate: "{{doublecho .Name}}", + wantOutput: "my-command my-command", + }, + } + + 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, + Commands: []Command{ + { + Name: "my-command", + CustomHelpTemplate: tt.template, + }, + }, + } + + err := app.Run([]string{"my-app", "help", tt.command}) + if err != nil { + t.Fatal(err) + } + + got := buf.String() + if got != tt.wantOutput { + t.Errorf("want output %q, got %q", tt.wantOutput, got) + } + }) + } +} +func TestShowCommandHelp_HelpPrinterCustom(t *testing.T) { + doublecho := func(text string) string { + return text + " " + text + } + + tests := []struct { + name string + template string + printer helpPrinterCustom + command string + wantTemplate string + wantOutput string + }{ + { + name: "no-command", + template: "", + printer: func(w io.Writer, templ string, data interface{}, fm map[string]interface{}) { + fmt.Fprint(w, "yo") + }, + command: "", + wantTemplate: SubcommandHelpTemplate, + wantOutput: "yo", + }, + { + name: "standard-command", + template: "", + printer: func(w io.Writer, templ string, data interface{}, fm map[string]interface{}) { + fmt.Fprint(w, "yo") + }, + command: "my-command", + wantTemplate: CommandHelpTemplate, + 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) + }, + command: "my-command", + wantTemplate: "{{doublecho .Name}}", + wantOutput: "my-command my-command", + }, + } + + 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, + Commands: []Command{ + { + Name: "my-command", + CustomHelpTemplate: tt.template, + }, + }, + } + + err := app.Run([]string{"my-app", "help", tt.command}) + if err != nil { + t.Fatal(err) + } + + got := buf.String() + if got != tt.wantOutput { + t.Errorf("want output %q, got %q", tt.wantOutput, got) + } + }) + } +} + func TestShowCommandHelp_CommandAliases(t *testing.T) { app := &App{ Commands: []Command{