Skip to content

[go1.20] Updated gorepo test flag handling #1304

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 73 additions & 10 deletions tests/gorepo/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,8 @@ func (ctxt *context) match(name string) bool {

func init() { checkShouldTest() }

var errTimeout = errors.New("command exceeded time limit")

// run runs a test.
func (t *test) run() {
start := time.Now()
Expand Down Expand Up @@ -619,6 +621,7 @@ func (t *test) run() {
}

var args, flags []string
var tim int
wantError := false
f, err := splitQuoted(action)
if err != nil {
Expand Down Expand Up @@ -652,14 +655,6 @@ func (t *test) run() {
case "errorcheck", "errorcheckdir", "errorcheckoutput":
t.action = action
wantError = true
for len(args) > 0 && strings.HasPrefix(args[0], "-") {
if args[0] == "-0" {
wantError = false
} else {
flags = append(flags, args[0])
}
args = args[1:]
}
case "skip":
t.action = "skip"
return
Expand All @@ -669,6 +664,38 @@ func (t *test) run() {
return
}

// collect flags
for len(args) > 0 && strings.HasPrefix(args[0], "-") {
switch args[0] {
case "-1":
wantError = true
case "-0":
wantError = false
case "-s":
// GOPHERJS: Doesn't use singlefilepkgs in test yet.
case "-t": // timeout in seconds
args = args[1:]
var err error
tim, err = strconv.Atoi(args[0])
if err != nil {
t.err = fmt.Errorf("need number of seconds for -t timeout, got %s instead", args[0])
}
if s := os.Getenv("GO_TEST_TIMEOUT_SCALE"); s != "" {
timeoutScale, err := strconv.Atoi(s)
if err != nil {
log.Fatalf("failed to parse $GO_TEST_TIMEOUT_SCALE = %q as integer: %v", s, err)
}
tim *= timeoutScale
}
case "-goexperiment": // set GOEXPERIMENT environment
args = args[1:]
// GOPHERJS: Ignore GOEXPERIMENT for now
default:
flags = append(flags, args[0])
}
args = args[1:]
}

t.makeTempDir()
defer os.RemoveAll(t.tempDir)

Expand Down Expand Up @@ -706,8 +733,40 @@ func (t *test) run() {
cmd.Dir = t.tempDir
cmd.Env = envForDir(cmd.Dir)
}
err := cmd.Run()
if err != nil {

var err error
if tim != 0 {
err = cmd.Start()
// This command-timeout code adapted from cmd/go/test.go
// Note: the Go command uses a more sophisticated timeout
// strategy, first sending SIGQUIT (if appropriate for the
// OS in question) to try to trigger a stack trace, then
// finally much later SIGKILL. If timeouts prove to be a
// common problem here, it would be worth porting over
// that code as well. See https://do.dev/issue/50973
// for more discussion.
if err == nil {
tick := time.NewTimer(time.Duration(tim) * time.Second)
done := make(chan error)
go func() {
done <- cmd.Wait()
}()
select {
case err = <-done:
// ok
case <-tick.C:
cmd.Process.Signal(os.Interrupt)
time.Sleep(1 * time.Second)
cmd.Process.Kill()
<-done
err = errTimeout
}
tick.Stop()
}
} else {
err = cmd.Run()
}
if err != nil && err != errTimeout {
err = fmt.Errorf("%s\n%s", err, buf.Bytes())
}
return buf.Bytes(), err
Expand All @@ -728,6 +787,10 @@ func (t *test) run() {
t.err = fmt.Errorf("compilation succeeded unexpectedly\n%s", out)
return
}
if err == errTimeout {
t.err = fmt.Errorf("compilation timed out")
return
}
} else {
if err != nil {
t.err = err
Expand Down
Loading