Skip to content

Commit 01f5a17

Browse files
dmitshurgopherbot
authored andcommitted
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>
1 parent fa9efd9 commit 01f5a17

File tree

10 files changed

+37
-187
lines changed

10 files changed

+37
-187
lines changed

misc/cgo/life/life_test.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"os"
1111
"os/exec"
1212
"path/filepath"
13-
"strings"
1413
"testing"
1514
)
1615

@@ -44,20 +43,22 @@ func testMain(m *testing.M) int {
4443
return m.Run()
4544
}
4645

46+
// TestTestRun runs a test case for cgo //export.
4747
func TestTestRun(t *testing.T) {
4848
if os.Getenv("GOOS") == "android" {
4949
t.Skip("the go tool runs with CGO_ENABLED=0 on the android device")
5050
}
51-
out, err := exec.Command("go", "env", "GOROOT").Output()
51+
52+
cmd := exec.Command("go", "run", "main.go")
53+
got, err := cmd.CombinedOutput()
5254
if err != nil {
53-
t.Fatal(err)
55+
t.Fatalf("%v: %s\n%s", cmd, err, got)
5456
}
55-
GOROOT := string(bytes.TrimSpace(out))
56-
57-
cmd := exec.Command("go", "run", filepath.Join(GOROOT, "test", "run.go"), "-", ".")
58-
out, err = cmd.CombinedOutput()
57+
want, err := os.ReadFile("main.out")
5958
if err != nil {
60-
t.Fatalf("%s: %s\n%s", strings.Join(cmd.Args, " "), err, out)
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)
6163
}
62-
t.Logf("%s:\n%s", strings.Join(cmd.Args, " "), out)
6364
}

misc/cgo/life/testdata/life.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// skip
2-
31
// Copyright 2010 The Go Authors. All rights reserved.
42
// Use of this source code is governed by a BSD-style
53
// license that can be found in the LICENSE file.

misc/cgo/life/testdata/main.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
// run -tags=use_go_run
2-
31
// Copyright 2010 The Go Authors. All rights reserved.
42
// Use of this source code is governed by a BSD-style
53
// license that can be found in the LICENSE file.
64

7-
// +build test_run
5+
//go:build test_run
86

97
// Run the game of life in C using Go for parallelization.
108

misc/cgo/stdio/stdio_test.go

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,20 +44,33 @@ func testMain(m *testing.M) int {
4444
return m.Run()
4545
}
4646

47+
// TestTestRun runs a cgo test that doesn't depend on non-standard libraries.
4748
func TestTestRun(t *testing.T) {
4849
if os.Getenv("GOOS") == "android" {
4950
t.Skip("subpackage stdio is not available on android")
5051
}
51-
out, err := exec.Command("go", "env", "GOROOT").Output()
52-
if err != nil {
53-
t.Fatal(err)
54-
}
55-
GOROOT := string(bytes.TrimSpace(out))
5652

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)
53+
for _, file := range [...]string{
54+
"chain.go",
55+
"fib.go",
56+
"hello.go",
57+
} {
58+
file := file
59+
wantFile := strings.Replace(file, ".go", ".out", 1)
60+
t.Run(file, func(t *testing.T) {
61+
cmd := exec.Command("go", "run", file)
62+
got, err := cmd.CombinedOutput()
63+
if err != nil {
64+
t.Fatalf("%v: %s\n%s", cmd, err, got)
65+
}
66+
got = bytes.ReplaceAll(got, []byte("\r\n"), []byte("\n"))
67+
want, err := os.ReadFile(wantFile)
68+
if err != nil {
69+
t.Fatal("reading golden output:", err)
70+
}
71+
if !bytes.Equal(got, want) {
72+
t.Errorf("'%v' output does not match expected in %s. Instead saw:\n%s", cmd, wantFile, got)
73+
}
74+
})
6175
}
62-
t.Logf("%s:\n%s", strings.Join(cmd.Args, " "), out)
6376
}

misc/cgo/stdio/testdata/chain.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
// run -tags=use_go_run
2-
31
// Copyright 2009 The Go Authors. All rights reserved.
42
// Use of this source code is governed by a BSD-style
53
// license that can be found in the LICENSE file.
64

7-
// +build test_run
5+
//go:build test_run
86

97
// Pass numbers along a chain of threads.
108

misc/cgo/stdio/testdata/fib.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
// run -tags=use_go_run
2-
31
// Copyright 2009 The Go Authors. All rights reserved.
42
// Use of this source code is governed by a BSD-style
53
// license that can be found in the LICENSE file.
64

7-
// +build test_run
5+
//go:build test_run
86

97
// Compute Fibonacci numbers with two goroutines
108
// that pass integers back and forth. No actual

misc/cgo/stdio/testdata/hello.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
// run -tags=use_go_run
2-
31
// Copyright 2009 The Go Authors. All rights reserved.
42
// Use of this source code is governed by a BSD-style
53
// license that can be found in the LICENSE file.
64

7-
// +build test_run
5+
//go:build test_run
86

97
package main
108

misc/cgo/stdio/testdata/run.out

Lines changed: 0 additions & 150 deletions
This file was deleted.

misc/cgo/stdio/testdata/stdio/file.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// skip
2-
31
// Copyright 2009 The Go Authors. All rights reserved.
42
// Use of this source code is governed by a BSD-style
53
// license that can be found in the LICENSE file.

misc/cgo/stdio/testdata/stdio/stdio.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// skip
2-
31
// Copyright 2009 The Go Authors. All rights reserved.
42
// Use of this source code is governed by a BSD-style
53
// license that can be found in the LICENSE file.

0 commit comments

Comments
 (0)