Debian Codegraph @

src/cmd/compile/script_test.go

raw
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 "flag"
10 "internal/testenv"
11 "os"
12 "runtime"
13 "testing"
14 )
15
16 //go:generate go test cmd/compile -v -run=TestScript/README --fixreadme
17
18 var fixReadme = flag.Bool("fixreadme", false, "if true, update README for script tests")
19
20 var testCompiler string
21
22 // TestMain allows this test binary to run as the compiler
23 // itself, which is helpful for running script tests.
24 // If COMPILE_TEST_EXEC_COMPILE is set, we treat the run
25 // as a 'go tool compile' invocation, otherwise behave
26 // as a normal test binary.
27 func TestMain(m *testing.M) {
28 // Are we being asked to run as the compiler?
29 // If so then kick off main.
30 if os.Getenv("COMPILE_TEST_EXEC_COMPILE") != "" {
31 main()
32 os.Exit(0)
33 }
34
35 if testExe, err := os.Executable(); err == nil {
36 // on wasm, some phones, we expect an error from os.Executable()
37 testCompiler = testExe
38 }
39
40 // Regular run, just execute tests.
41 os.Exit(m.Run())
42 }
43
44 func TestScript(t *testing.T) {
45 testenv.MustHaveGoBuild(t)
46 doReplacement := true
47 switch runtime.GOOS {
48 case "wasip1", "js":
49 // wasm doesn't support os.Executable, so we'll skip replacing
50 // the installed linker with our test binary.
51 doReplacement = false
52 }
53 repls := []scripttest.ToolReplacement{}
54 if doReplacement {
55 if testCompiler == "" {
56 t.Fatalf("testCompiler not set, can't replace")
57 }
58 repls = []scripttest.ToolReplacement{
59 scripttest.ToolReplacement{
60 ToolName: "compile",
61 ReplacementPath: testCompiler,
62 EnvVar: "COMPILE_TEST_EXEC_COMPILE=1",
63 },
64 }
65 }
66 scripttest.RunToolScriptTest(t, repls, "testdata/script", *fixReadme)
67 }