]> Cypherpunks.ru repositories - gostls13.git/blob - misc/cgo/life/life_test.go
misc/cgo/{life,stdio}: remove reliance on test/run.go
[gostls13.git] / misc / cgo / life / life_test.go
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.
4
5 package life_test
6
7 import (
8         "bytes"
9         "log"
10         "os"
11         "os/exec"
12         "path/filepath"
13         "testing"
14 )
15
16 func TestMain(m *testing.M) {
17         log.SetFlags(log.Lshortfile)
18         os.Exit(testMain(m))
19 }
20
21 func testMain(m *testing.M) int {
22         GOPATH, err := os.MkdirTemp("", "cgolife")
23         if err != nil {
24                 log.Panic(err)
25         }
26         defer os.RemoveAll(GOPATH)
27         os.Setenv("GOPATH", GOPATH)
28
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 {
33                 log.Panic(err)
34         }
35         if err := os.Chdir(modRoot); err != nil {
36                 log.Panic(err)
37         }
38         os.Setenv("PWD", modRoot)
39         if err := os.WriteFile("go.mod", []byte("module cgolife\n"), 0666); err != nil {
40                 log.Panic(err)
41         }
42
43         return m.Run()
44 }
45
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")
50         }
51
52         cmd := exec.Command("go", "run", "main.go")
53         got, err := cmd.CombinedOutput()
54         if err != nil {
55                 t.Fatalf("%v: %s\n%s", cmd, err, got)
56         }
57         want, err := os.ReadFile("main.out")
58         if err != nil {
59                 t.Fatal("reading golden output:", err)
60         }
61         if !bytes.Equal(got, want) {
62                 t.Errorf("'%v' output does not match expected in main.out. Instead saw:\n%s", cmd, got)
63         }
64 }