Skip to content

Commit

Permalink
Revert "Make detection for test-binary more universal (#2173)" (#2235)
Browse files Browse the repository at this point in the history
This reverts commit d1e9d85.

Some programs set os.Args in their unit tests and this change would
break those tests.

Ref: #2173 (comment)
  • Loading branch information
marckhouzam authored Feb 16, 2025
1 parent a97f9fd commit 40b5bc1
Show file tree
Hide file tree
Showing 4 changed files with 3 additions and 76 deletions.
8 changes: 3 additions & 5 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"fmt"
"io"
"os"
"path/filepath"
"sort"
"strings"

Expand Down Expand Up @@ -1100,11 +1101,8 @@ func (c *Command) ExecuteC() (cmd *Command, err error) {

args := c.args

// If running unit tests, we don't want to take the os.Args, see #155 and #2173.
// For example, the following would fail:
// go test -c -o foo.test
// ./foo.test -test.run TestNoArgs
if c.args == nil && !isTesting() {
// Workaround FAIL with "go test -v" or "cobra.test -test.v", see #155
if c.args == nil && filepath.Base(os.Args[0]) != "cobra.test" {
args = os.Args[1:]
}

Expand Down
33 changes: 0 additions & 33 deletions command_go120.go

This file was deleted.

25 changes: 0 additions & 25 deletions command_go121.go

This file was deleted.

13 changes: 0 additions & 13 deletions command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2921,16 +2921,3 @@ func TestUnknownFlagShouldReturnSameErrorRegardlessOfArgPosition(t *testing.T) {
})
}
}

// This tests verifies that when running unit tests, os.Args are not used.
// This is because we don't want to process any arguments that are provided
// by "go test"; instead, unit tests must set the arguments they need using
// rootCmd.SetArgs().
func TestNoOSArgsWhenTesting(t *testing.T) {
root := &Command{Use: "root", Run: emptyRun}
os.Args = append(os.Args, "--unknown")

if _, err := root.ExecuteC(); err != nil {
t.Errorf("error: %v", err)
}
}

0 comments on commit 40b5bc1

Please sign in to comment.