|
| 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 | +} |
0 commit comments