Skip to content

Commit aa97a01

Browse files
committed
cmd/compile: add script testing facility for compiler use
Add support for running script tests as part of the compiler's suite of tests, hooking in the script test engine packages recently moved from cmd/go to cmd/internal. These script tests will use the test binary itself as the compile tool for Go builds, and can also run the C compiler if needed. New script test cases (*.txt files) should be added to the directory cmd/compile/testdata/script. Updates #68606. Change-Id: I9b056a07024b0a72320a89ad734e4b4a51f1c10c Reviewed-on: https://go-review.googlesource.com/c/go/+/601361 Reviewed-by: Cherry Mui <[email protected]> Reviewed-by: David Chase <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent f021221 commit aa97a01

File tree

2 files changed

+86
-0
lines changed

2 files changed

+86
-0
lines changed

src/cmd/compile/script_test.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Copyright 2024 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 main
6+
7+
import (
8+
"cmd/internal/script/scripttest"
9+
"internal/testenv"
10+
"os"
11+
"runtime"
12+
"testing"
13+
)
14+
15+
var testCompiler string
16+
17+
// TestMain allows this test binary to run as the compiler
18+
// itself, which is helpful for running script tests.
19+
// If COMPILE_TEST_EXEC_COMPILE is set, we treat the run
20+
// as a 'go tool compile' invocation, otherwise behave
21+
// as a normal test binary.
22+
func TestMain(m *testing.M) {
23+
// Are we being asked to run as the compiler?
24+
// If so then kick off main.
25+
if os.Getenv("COMPILE_TEST_EXEC_COMPILE") != "" {
26+
main()
27+
os.Exit(0)
28+
}
29+
30+
if testExe, err := os.Executable(); err == nil {
31+
// on wasm, some phones, we expect an error from os.Executable()
32+
testCompiler = testExe
33+
}
34+
35+
// Regular run, just execute tests.
36+
os.Exit(m.Run())
37+
}
38+
39+
func TestScript(t *testing.T) {
40+
testenv.MustHaveGoBuild(t)
41+
doReplacement := true
42+
switch runtime.GOOS {
43+
case "wasip1", "js":
44+
// wasm doesn't support os.Executable, so we'll skip replacing
45+
// the installed linker with our test binary.
46+
doReplacement = false
47+
}
48+
repls := []scripttest.ToolReplacement{}
49+
if doReplacement {
50+
if testCompiler == "" {
51+
t.Fatalf("testCompiler not set, can't replace")
52+
}
53+
repls = []scripttest.ToolReplacement{
54+
scripttest.ToolReplacement{
55+
ToolName: "compile",
56+
ReplacementPath: testCompiler,
57+
EnvVar: "COMPILE_TEST_EXEC_COMPILE=1",
58+
},
59+
}
60+
}
61+
scripttest.RunToolScriptTest(t, repls, "testdata/script/*.txt")
62+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
2+
# Test of the linker's script test harness.
3+
4+
go build
5+
[!cgo] skip
6+
cc -c testdata/mumble.c
7+
8+
-- go.mod --
9+
module main
10+
11+
go 1.20
12+
13+
-- main.go --
14+
package main
15+
16+
func main() {
17+
println("Hi mom!")
18+
}
19+
20+
-- testdata/mumble.c --
21+
22+
int x;
23+
24+

0 commit comments

Comments
 (0)