Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

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

Copy link
Copy link

Description

@bystones
Issue body actions

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

Reactions are currently unavailable

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

    Morty Proxy This is a proxified and sanitized view of the page, visit original site.