2019-02-22 10:22:10 -07:00
|
|
|
// 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 {
|
2021-04-03 02:10:47 -06:00
|
|
|
GOPATH, err := os.MkdirTemp("", "cgolife")
|
2019-02-22 10:22:10 -07:00
|
|
|
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)
|
|
|
|
}
|
2019-02-25 20:09:46 -07:00
|
|
|
os.Setenv("PWD", modRoot)
|
2021-04-03 02:10:47 -06:00
|
|
|
if err := os.WriteFile("go.mod", []byte("module cgolife\n"), 0666); err != nil {
|
2019-02-22 10:22:10 -07:00
|
|
|
log.Panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return m.Run()
|
|
|
|
}
|
|
|
|
|
2023-02-06 11:44:32 -07:00
|
|
|
// TestTestRun runs a test case for cgo //export.
|
2019-02-22 10:22:10 -07:00
|
|
|
func TestTestRun(t *testing.T) {
|
2019-03-06 04:53:56 -07:00
|
|
|
if os.Getenv("GOOS") == "android" {
|
|
|
|
t.Skip("the go tool runs with CGO_ENABLED=0 on the android device")
|
|
|
|
}
|
2023-02-06 11:44:32 -07:00
|
|
|
|
|
|
|
cmd := exec.Command("go", "run", "main.go")
|
|
|
|
got, err := cmd.CombinedOutput()
|
2019-02-22 10:22:10 -07:00
|
|
|
if err != nil {
|
2023-02-06 11:44:32 -07:00
|
|
|
t.Fatalf("%v: %s\n%s", cmd, err, got)
|
2019-02-22 10:22:10 -07:00
|
|
|
}
|
2023-02-06 11:44:32 -07:00
|
|
|
want, err := os.ReadFile("main.out")
|
2019-02-22 10:22:10 -07:00
|
|
|
if err != nil {
|
2023-02-06 11:44:32 -07:00
|
|
|
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)
|
2019-02-22 10:22:10 -07:00
|
|
|
}
|
|
|
|
}
|