145
|
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 "io/ioutil"
|
|
10 "log"
|
|
11 "os"
|
|
12 "os/exec"
|
|
13 "path/filepath"
|
|
14 "strings"
|
|
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 := ioutil.TempDir("", "cgolife")
|
|
25 if err != nil {
|
|
26 log.Panic(err)
|
|
27 }
|
|
28 defer os.RemoveAll(GOPATH)
|
|
29 os.Setenv("GOPATH", GOPATH)
|
|
30
|
|
31 // Copy testdata into GOPATH/src/cgolife, along with a go.mod file
|
|
32 // declaring the same path.
|
|
33 modRoot := filepath.Join(GOPATH, "src", "cgolife")
|
|
34 if err := 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 := ioutil.WriteFile("go.mod", []byte("module cgolife\n"), 0666); err != nil {
|
|
42 log.Panic(err)
|
|
43 }
|
|
44
|
|
45 return m.Run()
|
|
46 }
|
|
47
|
|
48 func TestTestRun(t *testing.T) {
|
|
49 if os.Getenv("GOOS") == "android" {
|
|
50 t.Skip("the go tool runs with CGO_ENABLED=0 on the android device")
|
|
51 }
|
|
52 out, err := exec.Command("go", "env", "GOROOT").Output()
|
|
53 if err != nil {
|
|
54 t.Fatal(err)
|
|
55 }
|
|
56 GOROOT := string(bytes.TrimSpace(out))
|
|
57
|
|
58 cmd := exec.Command("go", "run", filepath.Join(GOROOT, "test", "run.go"), "-", ".")
|
|
59 out, err = cmd.CombinedOutput()
|
|
60 if err != nil {
|
|
61 t.Fatalf("%s: %s\n%s", strings.Join(cmd.Args, " "), err, out)
|
|
62 }
|
|
63 t.Logf("%s:\n%s", strings.Join(cmd.Args, " "), out)
|
|
64 }
|