Skip to content

Commit

Permalink
Ensure command help always prints with overridden print functions
Browse files Browse the repository at this point in the history
  • Loading branch information
rliebz committed Oct 17, 2019
1 parent a14194a commit bab428a
Showing 1 changed file with 174 additions and 0 deletions.
174 changes: 174 additions & 0 deletions help_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"flag"
"fmt"
"io"
"runtime"
"strings"
"testing"
Expand Down Expand Up @@ -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{
Expand Down

0 comments on commit bab428a

Please sign in to comment.