spf13/cobra

os.Args added as flags to command

kate-osborn opened this issue · 0 comments

Problem

I have unit tests that cover creating cobra commands, and when I run them using Ginkgo, I get an error about an unknown flag that I never explicitly added to the command:

  [FAILED] Unexpected error:
      <*errors.errorString | 0xc000191db0>: 
      unknown flag: --test.timeout
      {
          s: "unknown flag: --test.timeout",
      }
  occurred

The --test.timeout flag is one of the arguments that Ginkgo passes to the compiled Ginkgo test binary and has nothing to do with the command under test.

This only happens with commands that have no arguments and seems to be related to this line of code:

cobra/command.go

Line 1080 in bcfcff7

// Workaround FAIL with "go test -v" or "cobra.test -test.v", see #155

Expected behavior

Cobra does not add the os.Args to the command's args when they are testing-related.

Steps to reproduce

To reproduce, please see my gist where I have recreated the problem with minimal code: https://gist.github.com/kate-osborn/f21aec497bf7ccb81d73ffcfc5e963b0.

The gist includes instructions on how to run the tests.

Environment

cobra version: v1.8.0
ginkgo version: v2.15.0