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