Merge pull request #218 from codegangsta/add-another-help-test
Set writer when running command as app
This commit is contained in:
commit
4ee3fafcf8
56
app_test.go
56
app_test.go
@ -1,10 +1,12 @@
|
|||||||
package cli_test
|
package cli_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/codegangsta/cli"
|
"github.com/codegangsta/cli"
|
||||||
@ -621,3 +623,57 @@ func TestGlobalFlagsInSubcommands(t *testing.T) {
|
|||||||
|
|
||||||
expect(t, subcommandRun, true)
|
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.Commands = c.Subcommands
|
||||||
app.Flags = c.Flags
|
app.Flags = c.Flags
|
||||||
app.HideHelp = c.HideHelp
|
app.HideHelp = c.HideHelp
|
||||||
|
app.Writer = ctx.App.Writer
|
||||||
|
|
||||||
// bash completion
|
// bash completion
|
||||||
app.EnableBashCompletion = ctx.App.EnableBashCompletion
|
app.EnableBashCompletion = ctx.App.EnableBashCompletion
|
||||||
|
Loading…
Reference in New Issue
Block a user