Skip to content

How to test inherited flags? #2249

@abitrolly

Description

@abitrolly

When I test Cobra command, I do this:

        cmd := NewCmdStatus(cmdutils.NewFactory())
        t.Run("no repo", func(t *testing.T) {
                err := cmd.Execute()
                assert.Error(t, err)
        })
        t.Run("no repo with -R", func(t *testing.T) {
                cmd.SetArgs([]string{"-R", "forkrepo"})
                err := cmd.Execute()
                assert.NoError(t, err)
        })

The problem is that -R is defined in parent command, so the test fails.

I tried to add the flag explicitly to NewCmdStatus, but that didn't work too.

cmd.InheritedFlags().StringP("repo", "R", "", "Select another repository.")

So how to test that?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions