1 // Copyright 2019 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
16 func TestMain(m *testing.M) {
17 log.SetFlags(log.Lshortfile)
21 func testMain(m *testing.M) int {
22 GOPATH, err := os.MkdirTemp("", "cgolife")
26 defer os.RemoveAll(GOPATH)
27 os.Setenv("GOPATH", GOPATH)
29 // Copy testdata into GOPATH/src/cgolife, along with a go.mod file
30 // declaring the same path.
31 modRoot := filepath.Join(GOPATH, "src", "cgolife")
32 if err := overlayDir(modRoot, "testdata"); err != nil {
35 if err := os.Chdir(modRoot); err != nil {
38 os.Setenv("PWD", modRoot)
39 if err := os.WriteFile("go.mod", []byte("module cgolife\n"), 0666); err != nil {
46 // TestTestRun runs a test case for cgo //export.
47 func TestTestRun(t *testing.T) {
48 if os.Getenv("GOOS") == "android" {
49 t.Skip("the go tool runs with CGO_ENABLED=0 on the android device")
52 cmd := exec.Command("go", "run", "main.go")
53 got, err := cmd.CombinedOutput()
55 t.Fatalf("%v: %s\n%s", cmd, err, got)
57 want, err := os.ReadFile("main.out")
59 t.Fatal("reading golden output:", err)
61 if !bytes.Equal(got, want) {
62 t.Errorf("'%v' output does not match expected in main.out. Instead saw:\n%s", cmd, got)