1
2
3
4
5 package life_test
6
7 import (
8 "bytes"
9 "cmd/cgo/internal/cgotest"
10 "internal/testenv"
11 "log"
12 "os"
13 "os/exec"
14 "path/filepath"
15 "testing"
16 )
17
18 func TestMain(m *testing.M) {
19 log.SetFlags(log.Lshortfile)
20 os.Exit(testMain(m))
21 }
22
23 func testMain(m *testing.M) int {
24 GOPATH, err := os.MkdirTemp("", "cgolife")
25 if err != nil {
26 log.Panic(err)
27 }
28 defer os.RemoveAll(GOPATH)
29 os.Setenv("GOPATH", GOPATH)
30
31
32
33 modRoot := filepath.Join(GOPATH, "src", "cgolife")
34 if err := cgotest.OverlayDir(modRoot, "testdata"); err != nil {
35 log.Panic(err)
36 }
37 if err := os.Chdir(modRoot); err != nil {
38 log.Panic(err)
39 }
40 os.Setenv("PWD", modRoot)
41 if err := os.WriteFile("go.mod", []byte("module cgolife\n"), 0666); err != nil {
42 log.Panic(err)
43 }
44
45 return m.Run()
46 }
47
48
49 func TestTestRun(t *testing.T) {
50 testenv.MustHaveGoRun(t)
51 testenv.MustHaveCGO(t)
52
53 cmd := exec.Command("go", "run", "main.go")
54 got, err := cmd.CombinedOutput()
55 if err != nil {
56 t.Fatalf("%v: %s\n%s", cmd, err, got)
57 }
58 want, err := os.ReadFile("main.out")
59 if err != nil {
60 t.Fatal("reading golden output:", err)
61 }
62 if !bytes.Equal(got, want) {
63 t.Errorf("'%v' output does not match expected in main.out. Instead saw:\n%s", cmd, got)
64 }
65 }
66
View as plain text