Skip to content

Commit 40b5bc1

Browse files
authored
Revert "Make detection for test-binary more universal (#2173)" (#2235)
This reverts commit d1e9d85. Some programs set os.Args in their unit tests and this change would break those tests. Ref: #2173 (comment)
1 parent a97f9fd commit 40b5bc1

File tree

4 files changed

+3
-76
lines changed

4 files changed

+3
-76
lines changed

command.go

+3-5
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"fmt"
2424
"io"
2525
"os"
26+
"path/filepath"
2627
"sort"
2728
"strings"
2829

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

11011102
args := c.args
11021103

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

command_go120.go

-33
This file was deleted.

command_go121.go

-25
This file was deleted.

command_test.go

-13
Original file line numberDiff line numberDiff line change
@@ -2921,16 +2921,3 @@ func TestUnknownFlagShouldReturnSameErrorRegardlessOfArgPosition(t *testing.T) {
29212921
})
29222922
}
29232923
}
2924-
2925-
// This tests verifies that when running unit tests, os.Args are not used.
2926-
// This is because we don't want to process any arguments that are provided
2927-
// by "go test"; instead, unit tests must set the arguments they need using
2928-
// rootCmd.SetArgs().
2929-
func TestNoOSArgsWhenTesting(t *testing.T) {
2930-
root := &Command{Use: "root", Run: emptyRun}
2931-
os.Args = append(os.Args, "--unknown")
2932-
2933-
if _, err := root.ExecuteC(); err != nil {
2934-
t.Errorf("error: %v", err)
2935-
}
2936-
}

0 commit comments

Comments
 (0)