life_test.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. package life_test
  5. import (
  6. "bytes"
  7. "log"
  8. "os"
  9. "os/exec"
  10. "path/filepath"
  11. "strings"
  12. "testing"
  13. )
  14. func TestMain(m *testing.M) {
  15. log.SetFlags(log.Lshortfile)
  16. os.Exit(testMain(m))
  17. }
  18. func testMain(m *testing.M) int {
  19. GOPATH, err := os.MkdirTemp("", "cgolife")
  20. if err != nil {
  21. log.Panic(err)
  22. }
  23. defer os.RemoveAll(GOPATH)
  24. os.Setenv("GOPATH", GOPATH)
  25. // Copy testdata into GOPATH/src/cgolife, along with a go.mod file
  26. // declaring the same path.
  27. modRoot := filepath.Join(GOPATH, "src", "cgolife")
  28. if err := overlayDir(modRoot, "testdata"); err != nil {
  29. log.Panic(err)
  30. }
  31. if err := os.Chdir(modRoot); err != nil {
  32. log.Panic(err)
  33. }
  34. os.Setenv("PWD", modRoot)
  35. if err := os.WriteFile("go.mod", []byte("module cgolife\n"), 0666); err != nil {
  36. log.Panic(err)
  37. }
  38. return m.Run()
  39. }
  40. func TestTestRun(t *testing.T) {
  41. if os.Getenv("GOOS") == "android" {
  42. t.Skip("the go tool runs with CGO_ENABLED=0 on the android device")
  43. }
  44. out, err := exec.Command("go", "env", "GOROOT").Output()
  45. if err != nil {
  46. t.Fatal(err)
  47. }
  48. GOROOT := string(bytes.TrimSpace(out))
  49. cmd := exec.Command("go", "run", filepath.Join(GOROOT, "test", "run.go"), "-", ".")
  50. out, err = cmd.CombinedOutput()
  51. if err != nil {
  52. t.Fatalf("%s: %s\n%s", strings.Join(cmd.Args, " "), err, out)
  53. }
  54. t.Logf("%s:\n%s", strings.Join(cmd.Args, " "), out)
  55. }