Skip to content
Myr

Revert "Make detection for test-binary more universal (#2173)" (#2235)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

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

authored by Marc Khouzam · committed by GitHub · parent a97f9fd · Browse files · 4 files+3 added−76 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Trailers

  • Ref: https://github.com/spf13/cobra/pull/2173#issuecomment-2661544195

Diff stats

4 files+3 added−76 removed

  • other:+3 added−76 removed

4 files+3 added−76 removed

deleted:command_go120.go-33 removed
1// Copyright 2013-2024 The Cobra Authors2//3// Licensed under the Apache License, Version 2.0 (the "License");4// you may not use this file except in compliance with the License.5// You may obtain a copy of the License at6//7// http://www.apache.org/licenses/LICENSE-2.08//9// Unless required by applicable law or agreed to in writing, software10// distributed under the License is distributed on an "AS IS" BASIS,11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.12// See the License for the specific language governing permissions and13// limitations under the License.1415//go:build !go1.2116// +build !go1.211718package cobra1920import (21 "os"22 "strings"23)2425// based on golang.org/x/mod/internal/lazyregexp: https://cs.opensource.google/go/x/mod/+/refs/tags/v0.19.0:internal/lazyregexp/lazyre.go;l=6626// For a non-go-test program which still has a name ending with ".test[.exe]", it will need to either:27// 1- Use go >= 1.21, or28// 2- call "rootCmd.SetArgs(os.Args[1:])" before calling "rootCmd.Execute()"29var inTest = len(os.Args) > 0 && strings.HasSuffix(strings.TrimSuffix(os.Args[0], ".exe"), ".test")3031func isTesting() bool {32 return inTest33}
deleted:command_go121.go-25 removed
1// Copyright 2013-2024 The Cobra Authors2//3// Licensed under the Apache License, Version 2.0 (the "License");4// you may not use this file except in compliance with the License.5// You may obtain a copy of the License at6//7// http://www.apache.org/licenses/LICENSE-2.08//9// Unless required by applicable law or agreed to in writing, software10// distributed under the License is distributed on an "AS IS" BASIS,11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.12// See the License for the specific language governing permissions and13// limitations under the License.1415//go:build go1.2116// +build go1.211718package cobra1920import "testing"2122func isTesting() bool {23 // Only available starting with go 1.2124 return testing.Testing()25}
modified:command_test.go-13 removed
2919 unmodified lines
2920 checkStringContains(t, output, "unknown flag: --unknown")2921 })2922 }2923}29242925// 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 provided2927// by "go test"; instead, unit tests must set the arguments they need using2928// rootCmd.SetArgs().2929func TestNoOSArgsWhenTesting(t *testing.T) {2930 root := &Command{Use: "root", Run: emptyRun}2931 os.Args = append(os.Args, "--unknown")29322933 if _, err := root.ExecuteC(); err != nil {2934 t.Errorf("error: %v", err)2935 }2936}
modified:command.go+3 added-5 removed
21 unmodified lines
22 "errors"23 "fmt"24 "io"25 "os"26 "path/filepath"27 "sort"28 "strings"2930 flag "github.com/spf13/pflag"
1069 unmodified lines
1100 c.InitDefaultHelpCmd()11011102 args := c.args11031103 // 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.test1106 // ./foo.test -test.run TestNoArgs1107 if c.args == nil && !isTesting() {1104 // Workaround FAIL with "go test -v" or "cobra.test -test.v", see #1551105 if c.args == nil && filepath.Base(os.Args[0]) != "cobra.test" {1106 args = os.Args[1:]1107 }11081109 // initialize the __complete command to be used for shell completion
958 unmodified lines