Set writer when running command as app

Also add test from https://github.com/codegangsta/cli/pull/202 with
slight modifications.
main
jszwedko 9 years ago
parent f952f5ac6f
commit b8104e5da7

@ -1,10 +1,12 @@
package cli_test
import (
"bytes"
"flag"
"fmt"
"io"
"os"
"strings"
"testing"
"github.com/codegangsta/cli"
@ -621,3 +623,57 @@ func TestGlobalFlagsInSubcommands(t *testing.T) {
expect(t, subcommandRun, true)
}
func TestApp_Run_CommandWithSubcommandHasHelpTopic(t *testing.T) {
var subcommandHelpTopics = [][]string{
{"command", "foo", "--help"},
{"command", "foo", "-h"},
{"command", "foo", "help"},
}
for _, flagSet := range subcommandHelpTopics {
t.Logf("==> checking with flags %v", flagSet)
app := cli.NewApp()
buf := new(bytes.Buffer)
app.Writer = buf
subCmdBar := cli.Command{
Name: "bar",
Usage: "does bar things",
}
subCmdBaz := cli.Command{
Name: "baz",
Usage: "does baz things",
}
cmd := cli.Command{
Name: "foo",
Description: "descriptive wall of text about how it does foo things",
Subcommands: []cli.Command{subCmdBar, subCmdBaz},
}
app.Commands = []cli.Command{cmd}
err := app.Run(flagSet)
if err != nil {
t.Error(err)
}
output := buf.String()
t.Logf("output: %q\n", buf.Bytes())
if strings.Contains(output, "No help topic for") {
t.Errorf("expect a help topic, got none: \n%q", output)
}
for _, shouldContain := range []string{
cmd.Name, cmd.Description,
subCmdBar.Name, subCmdBar.Usage,
subCmdBaz.Name, subCmdBaz.Usage,
} {
if !strings.Contains(output, shouldContain) {
t.Errorf("want help to contain %q, did not: \n%q", shouldContain, output)
}
}
}
}

@ -157,6 +157,7 @@ func (c Command) startApp(ctx *Context) error {
app.Commands = c.Subcommands
app.Flags = c.Flags
app.HideHelp = c.HideHelp
app.Writer = ctx.App.Writer
// bash completion
app.EnableBashCompletion = ctx.App.EnableBashCompletion

Loading…
Cancel
Save