Merge pull request #1225 from Coderlane/command_reader

fix: Propagate App.Reader to subcommands
This commit is contained in:
lynn (they) 2021-01-25 10:08:46 -08:00 committed by GitHub
commit 44ec98527e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 0 deletions

View File

@ -897,6 +897,39 @@ func TestApp_SetStdin(t *testing.T) {
} }
} }
func TestApp_SetStdin_Subcommand(t *testing.T) {
buf := make([]byte, 12)
app := &App{
Name: "test",
Reader: strings.NewReader("Hello World!"),
Commands: []*Command{
{
Name: "command",
Subcommands: []*Command{
{
Name: "subcommand",
Action: func(c *Context) error {
_, err := c.App.Reader.Read(buf)
return err
},
},
},
},
},
}
err := app.Run([]string{"test", "command", "subcommand"})
if err != nil {
t.Fatalf("Run error: %s", err)
}
if string(buf) != "Hello World!" {
t.Error("App did not read input from desired reader.")
}
}
func TestApp_SetStdout(t *testing.T) { func TestApp_SetStdout(t *testing.T) {
var w bytes.Buffer var w bytes.Buffer

View File

@ -243,6 +243,7 @@ func (c *Command) startApp(ctx *Context) error {
app.Version = ctx.App.Version app.Version = ctx.App.Version
app.HideVersion = true app.HideVersion = true
app.Compiled = ctx.App.Compiled app.Compiled = ctx.App.Compiled
app.Reader = ctx.App.Reader
app.Writer = ctx.App.Writer app.Writer = ctx.App.Writer
app.ErrWriter = ctx.App.ErrWriter app.ErrWriter = ctx.App.ErrWriter
app.ExitErrHandler = ctx.App.ExitErrHandler app.ExitErrHandler = ctx.App.ExitErrHandler