1
0
mirror of https://github.com/golang/go synced 2024-11-07 16:26:11 -07:00
go/misc/cgo/life/life_test.go
Dmitri Shuralyov 01f5a17aa3 misc/cgo/{life,stdio}: remove reliance on test/run.go
The misc/cgo/life and misc/cgo/stdio tests started out as fairly simple
test cases when they were added, but the machinery to execute them has
grown in complexity over the years.

They currently reuse the test/run.go runner and its "run" action without
needing much of the additional flexibility that said runner implements.
Given that runner isn't well documented, it makes it harder to see that
ultimately these tests just do 'go run' on a few test programs and check
that the output matches a golden file.

Maybe these test cases should move out of misc to be near similar tests,
or the machinery to execute them can made available in a package that is
easier and safer to reuse. I'd rather not block the refactor of the test
directory runner on that, so for now rewrite these to be self-contained.

Also delete misc/cgo/stdio/testdata/run.out which has no effect on the
test. It was seemingly accidentally kept behind during the refactor in
CL 6220049.

For #56844.

Change-Id: I5e2f542824925092cdddb03b44b6295a4136ccb4
Reviewed-on: https://go-review.googlesource.com/c/go/+/465755
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
Reviewed-by: Austin Clements <austin@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Auto-Submit: Dmitri Shuralyov <dmitshur@golang.org>
Run-TryBot: Dmitri Shuralyov <dmitshur@golang.org>
2023-02-28 01:11:11 +00:00

65 lines
1.5 KiB
Go

// Copyright 2019 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package life_test
import (
"bytes"
"log"
"os"
"os/exec"
"path/filepath"
"testing"
)
func TestMain(m *testing.M) {
log.SetFlags(log.Lshortfile)
os.Exit(testMain(m))
}
func testMain(m *testing.M) int {
GOPATH, err := os.MkdirTemp("", "cgolife")
if err != nil {
log.Panic(err)
}
defer os.RemoveAll(GOPATH)
os.Setenv("GOPATH", GOPATH)
// Copy testdata into GOPATH/src/cgolife, along with a go.mod file
// declaring the same path.
modRoot := filepath.Join(GOPATH, "src", "cgolife")
if err := overlayDir(modRoot, "testdata"); err != nil {
log.Panic(err)
}
if err := os.Chdir(modRoot); err != nil {
log.Panic(err)
}
os.Setenv("PWD", modRoot)
if err := os.WriteFile("go.mod", []byte("module cgolife\n"), 0666); err != nil {
log.Panic(err)
}
return m.Run()
}
// TestTestRun runs a test case for cgo //export.
func TestTestRun(t *testing.T) {
if os.Getenv("GOOS") == "android" {
t.Skip("the go tool runs with CGO_ENABLED=0 on the android device")
}
cmd := exec.Command("go", "run", "main.go")
got, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("%v: %s\n%s", cmd, err, got)
}
want, err := os.ReadFile("main.out")
if err != nil {
t.Fatal("reading golden output:", err)
}
if !bytes.Equal(got, want) {
t.Errorf("'%v' output does not match expected in main.out. Instead saw:\n%s", cmd, got)
}
}