From: Austin Clements Date: Sat, 12 Nov 2022 02:57:57 +0000 (-0500) Subject: cmd/dist: simplify old code X-Git-Tag: go1.20rc1~208 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=4d1052b0d4e8916fefb542ba8a6f9d325db214f6;p=gostls13.git cmd/dist: simplify old code Now that all uses of "go test" have been converted over to the new abstraction, we can delete the old helpers for building "go test" commands and simplify some code that's only used by the new abstraction now. For #37486. Change-Id: I770cd457e018160d694abcc0b6ac80f7dc2e8425 Reviewed-on: https://go-review.googlesource.com/c/go/+/450020 Reviewed-by: Dmitri Shuralyov Reviewed-by: Dmitri Shuralyov TryBot-Result: Gopher Robot Run-TryBot: Austin Clements --- diff --git a/src/cmd/dist/test.go b/src/cmd/dist/test.go index 33aec6ef4e..899d34dd3d 100644 --- a/src/cmd/dist/test.go +++ b/src/cmd/dist/test.go @@ -66,6 +66,7 @@ type tester struct { banner string // prefix, or "" for none lastHeading string // last dir heading printed + short bool cgoEnabled bool partial bool @@ -100,6 +101,21 @@ func (t *tester) run() { os.Setenv("PATH", fmt.Sprintf("%s%c%s", gorootBin, os.PathListSeparator, os.Getenv("PATH"))) + // Default to running tests in "short" mode, unless the environment variable + // GO_TEST_SHORT is set to a non-empty, false-ish string. + // + // This environment variable is meant to be an internal detail between the + // Go build system and cmd/dist for the purpose of longtest builders, and is + // not intended for use by users. See golang.org/issue/12508. + t.short = true + if v := os.Getenv("GO_TEST_SHORT"); v != "" { + short, err := strconv.ParseBool(v) + if err != nil { + fatalf("invalid GO_TEST_SHORT %q: %v", v, err) + } + t.short = short + } + cmd := exec.Command(gorootBinGo, "env", "CGO_ENABLED", "GOEXE", "GOTMPDIR") cmd.Stderr = new(bytes.Buffer) slurp, err := cmd.Output() @@ -295,41 +311,6 @@ func (t *tester) maybeLogMetadata() error { return t.dirCmd(filepath.Join(goroot, "src/cmd/internal/metadata"), "go", []string{"run", "main.go"}).Run() } -// short returns a -short flag value to use with 'go test' -// or a test binary for tests intended to run in short mode. -// It returns "true", unless the environment variable -// GO_TEST_SHORT is set to a non-empty, false-ish string. -// -// This environment variable is meant to be an internal -// detail between the Go build system and cmd/dist for -// the purpose of longtest builders, and is not intended -// for use by users. See golang.org/issue/12508. -// -// TODO: Simplify this once all uses of goTest() are gone. -func short() string { - if v := os.Getenv("GO_TEST_SHORT"); v != "" { - short, err := strconv.ParseBool(v) - if err != nil { - fatalf("invalid GO_TEST_SHORT %q: %v", v, err) - } - if !short { - return "false" - } - } - return "true" -} - -// goTest returns the beginning of the go test command line. -// Callers should use goTest and then pass flags overriding these -// defaults as later arguments in the command line. -// -// TODO: Convert all uses of goTest() to goTest.run and delete this. -func (t *tester) goTest() []string { - return []string{ - "go", "test", "-short=" + short(), "-count=1", t.tags(), t.runFlag(""), - } -} - // goTest represents all options to a "go test" command. The final command will // combine configuration from goTest and tester flags. type goTest struct { @@ -479,10 +460,20 @@ func (opts *goTest) buildArgs(t *tester) (goCmd string, build, run, pkgs []strin d := opts.timeout * time.Duration(t.timeoutScale) run = append(run, "-timeout="+d.String()) } - if opts.short || short() == "true" { + if opts.short || t.short { run = append(run, "-short") } - build = append(build, t.tags(opts.tags...)) + var tags []string + if t.iOS() { + tags = append(tags, "lldb") + } + if noOpt { + tags = append(tags, "noopt") + } + tags = append(tags, opts.tags...) + if len(tags) > 0 { + build = append(build, "-tags="+strings.Join(tags, ",")) + } if t.race || opts.race { build = append(build, "-race") } @@ -551,46 +542,6 @@ func (opts *goTest) buildArgs(t *tester) (goCmd string, build, run, pkgs []strin return } -func (t *tester) tags(extra ...string) string { - tags := "" - ios := t.iOS() - switch { - case ios && noOpt: - tags = "lldb,noopt" - case ios: - tags = "lldb" - case noOpt: - tags = "noopt" - } - for _, x := range extra { - if x == "" { - continue - } - if tags != "" { - tags += "," - } - tags += x - } - return "-tags=" + tags -} - -// timeoutDuration converts the provided number of seconds into a -// time.Duration, scaled by the t.timeoutScale factor. -// -// TODO: Delete in favor of goTest.run -func (t *tester) timeoutDuration(sec int) time.Duration { - return time.Duration(sec) * time.Second * time.Duration(t.timeoutScale) -} - -// timeout returns the "-timeout=" string argument to "go test" given -// the number of seconds of timeout. It scales it by the -// t.timeoutScale factor. -// -// TODO: Delete in favor of goTest.run -func (t *tester) timeout(sec int) string { - return "-timeout=" + t.timeoutDuration(sec).String() -} - // ranGoTest and stdMatches are state closed over by the stdlib // testing func in registerStdTest below. The tests are run // sequentially, so there's no need for locks. @@ -753,7 +704,7 @@ func (t *tester) registerTests() { // (with GO_TEST_SHORT=false) because the runtime test is // already quite long and mayMoreStackMove makes it about // twice as slow. - if !t.compileOnly && short() == "false" { + if !t.compileOnly && !t.short { // hooks is the set of maymorestack hooks to test with. hooks := []string{"mayMoreStackPreempt", "mayMoreStackMove"} // pkgs is the set of test packages to run. @@ -1144,32 +1095,6 @@ func flattenCmdline(cmdline []interface{}) (bin string, args []string) { } } - // The go command is too picky about duplicated flags. - // Drop all but the last of the allowed duplicated flags. - drop := make([]bool, len(list)) - have := map[string]int{} - for i := 1; i < len(list); i++ { - j := strings.Index(list[i], "=") - if j < 0 { - continue - } - flag := list[i][:j] - switch flag { - case "-run", "-tags": - if have[flag] != 0 { - drop[have[flag]] = true - } - have[flag] = i - } - } - out := list[:0] - for i, x := range list { - if !drop[i] { - out = append(out, x) - } - } - list = out - bin = list[0] if bin == "go" { bin = gorootBinGo @@ -1641,14 +1566,6 @@ func isAlpineLinux() bool { return err == nil && fi.Mode().IsRegular() } -// TODO: Delete in favor of goTest.run -func (t *tester) runFlag(rx string) string { - if t.compileOnly { - return "-run=^$" - } - return "-run=" + rx -} - func (t *tester) registerRaceTests() { hdr := "Testing race detector" t.registerTest("race:runtime/race", hdr,