When adding values to a context in a Before function that value is not present in the After function when a subcommand was run. Without the subcommand the value is present as expected.
The following test demonstrates that behavior and should pass:
func TestCommand_Run_BeforeReturnNewContextSubcommand (t * testing.T ) {
var receivedValFromAction , receivedValFromAfter string
type key string
bkey := key ("bkey" )
cmd := & Command {
Name : "bar" ,
Before : func (ctx context.Context , cmd * Command ) (context.Context , error ) {
return context .WithValue (ctx , bkey , "bval" ), nil
},
After : func (ctx context.Context , cmd * Command ) error {
if val := ctx .Value (bkey ); val == nil {
return errors .New ("bkey value not found" )
} else {
receivedValFromAfter = val .(string )
}
return nil
},
Commands : []* Command {
{
Name : "baz" ,
Action : func (ctx context.Context , cmd * Command ) error {
if val := ctx .Value (bkey ); val == nil {
return errors .New ("bkey value not found" )
} else {
receivedValFromAction = val .(string )
}
return nil
},
},
},
}
require .NoError (t , cmd .Run (buildTestContext (t ), []string {"bar" , "baz" }))
require .Equal (t , "bval" , receivedValFromAfter )
require .Equal (t , "bval" , receivedValFromAction )
}
This test is a slight variation of an already existing test:
func TestCommand_Run_BeforeReturnNewContext (t * testing.T ) {
var receivedValFromAction , receivedValFromAfter string
type key string
bkey := key ("bkey" )
cmd := & Command {
Name : "bar" ,
Before : func (ctx context.Context , cmd * Command ) (context.Context , error ) {
return context .WithValue (ctx , bkey , "bval" ), nil
},
Action : func (ctx context.Context , cmd * Command ) error {
if val := ctx .Value (bkey ); val == nil {
return errors .New ("bkey value not found" )
} else {
receivedValFromAction = val .(string )
}
return nil
},
After : func (ctx context.Context , cmd * Command ) error {
if val := ctx .Value (bkey ); val == nil {
return errors .New ("bkey value not found" )
} else {
receivedValFromAfter = val .(string )
}
return nil
},
}
require .NoError (t , cmd .Run (buildTestContext (t ), []string {"foo" , "bar" }))
require .Equal (t , "bval" , receivedValFromAfter )
require .Equal (t , "bval" , receivedValFromAction )
}
Reactions are currently unavailable
When adding values to a context in a Before function that value is not present in the After function when a subcommand was run. Without the subcommand the value is present as expected.
The following test demonstrates that behavior and should pass:
This test is a slight variation of an already existing test:
cli/command_test.go
Lines 320 to 352 in efab65a