1
0
mirror of https://github.com/golang/go synced 2024-11-14 23:50:28 -07:00
go/misc/cgo/stdio/stdio_test.go
Dmitri Shuralyov 01f5a17aa3 misc/cgo/{life,stdio}: remove reliance on test/run.go
The misc/cgo/life and misc/cgo/stdio tests started out as fairly simple
test cases when they were added, but the machinery to execute them has
grown in complexity over the years.

They currently reuse the test/run.go runner and its "run" action without
needing much of the additional flexibility that said runner implements.
Given that runner isn't well documented, it makes it harder to see that
ultimately these tests just do 'go run' on a few test programs and check
that the output matches a golden file.

Maybe these test cases should move out of misc to be near similar tests,
or the machinery to execute them can made available in a package that is
easier and safer to reuse. I'd rather not block the refactor of the test
directory runner on that, so for now rewrite these to be self-contained.

Also delete misc/cgo/stdio/testdata/run.out which has no effect on the
test. It was seemingly accidentally kept behind during the refactor in
CL 6220049.

For #56844.

Change-Id: I5e2f542824925092cdddb03b44b6295a4136ccb4
Reviewed-on: https://go-review.googlesource.com/c/go/+/465755
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
Reviewed-by: Austin Clements <austin@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Auto-Submit: Dmitri Shuralyov <dmitshur@golang.org>
Run-TryBot: Dmitri Shuralyov <dmitshur@golang.org>
2023-02-28 01:11:11 +00:00

77 lines
1.8 KiB
Go

// Copyright 2019 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package stdio_test
import (
"bytes"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
)
func TestMain(m *testing.M) {
log.SetFlags(log.Lshortfile)
os.Exit(testMain(m))
}
func testMain(m *testing.M) int {
GOPATH, err := os.MkdirTemp("", "cgostdio")
if err != nil {
log.Panic(err)
}
defer os.RemoveAll(GOPATH)
os.Setenv("GOPATH", GOPATH)
// Copy testdata into GOPATH/src/cgostdio, along with a go.mod file
// declaring the same path.
modRoot := filepath.Join(GOPATH, "src", "cgostdio")
if err := overlayDir(modRoot, "testdata"); err != nil {
log.Panic(err)
}
if err := os.Chdir(modRoot); err != nil {
log.Panic(err)
}
os.Setenv("PWD", modRoot)
if err := os.WriteFile("go.mod", []byte("module cgostdio\n"), 0666); err != nil {
log.Panic(err)
}
return m.Run()
}
// TestTestRun runs a cgo test that doesn't depend on non-standard libraries.
func TestTestRun(t *testing.T) {
if os.Getenv("GOOS") == "android" {
t.Skip("subpackage stdio is not available on android")
}
for _, file := range [...]string{
"chain.go",
"fib.go",
"hello.go",
} {
file := file
wantFile := strings.Replace(file, ".go", ".out", 1)
t.Run(file, func(t *testing.T) {
cmd := exec.Command("go", "run", file)
got, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("%v: %s\n%s", cmd, err, got)
}
got = bytes.ReplaceAll(got, []byte("\r\n"), []byte("\n"))
want, err := os.ReadFile(wantFile)
if err != nil {
t.Fatal("reading golden output:", err)
}
if !bytes.Equal(got, want) {
t.Errorf("'%v' output does not match expected in %s. Instead saw:\n%s", cmd, wantFile, got)
}
})
}
}