Skip to content

v3: context value added in Before missing in After when subcommands are involved #2098

@bystones

Description

@bystones

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:

cli/command_test.go

Lines 320 to 352 in efab65a

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)
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions