]> Cypherpunks.ru repositories - gostls13.git/blob - src/cmd/internal/bootstrap_test/experiment_toolid_test.go
misc/reboot: move to cmd/internal/bootstrap_test
[gostls13.git] / src / cmd / internal / bootstrap_test / experiment_toolid_test.go
1 // Copyright 2019 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 //go:build explicit
6 // +build explicit
7
8 // This test verifies that GOEXPERIMENT settings built
9 // into the toolchain influence tool ids in the Go command.
10 // This test requires bootstrapping the toolchain twice, so it's very expensive.
11 // It must be run explicitly with -tags=explicit.
12 // Verifies golang.org/issue/33091.
13
14 package bootstrap_test
15
16 import (
17         "bytes"
18         "os"
19         "os/exec"
20         "path/filepath"
21         "runtime"
22         "testing"
23 )
24
25 func TestExperimentToolID(t *testing.T) {
26         // Set up GOROOT
27         goroot, err := os.MkdirTemp("", "experiment-goroot")
28         if err != nil {
29                 t.Fatal(err)
30         }
31         defer os.RemoveAll(goroot)
32
33         gorootSrc := filepath.Join(goroot, "src")
34         if err := overlayDir(gorootSrc, filepath.Join(runtime.GOROOT(), "src")); err != nil {
35                 t.Fatal(err)
36         }
37
38         if err := os.WriteFile(filepath.Join(goroot, "VERSION"), []byte("go1.999"), 0666); err != nil {
39                 t.Fatal(err)
40         }
41         env := append(os.Environ(), "GOROOT=", "GOROOT_BOOTSTRAP="+runtime.GOROOT())
42
43         // Use a clean cache.
44         gocache, err := os.MkdirTemp("", "experiment-gocache")
45         if err != nil {
46                 t.Fatal(err)
47         }
48         defer os.RemoveAll(gocache)
49         env = append(env, "GOCACHE="+gocache)
50
51         // Build the toolchain without GOEXPERIMENT.
52         var makeScript string
53         switch runtime.GOOS {
54         case "windows":
55                 makeScript = "make.bat"
56         case "plan9":
57                 makeScript = "make.rc"
58         default:
59                 makeScript = "make.bash"
60         }
61         makeScriptPath := filepath.Join(runtime.GOROOT(), "src", makeScript)
62         runCmd(t, gorootSrc, env, makeScriptPath)
63
64         // Verify compiler version string.
65         goCmdPath := filepath.Join(goroot, "bin", "go")
66         if runtime.GOOS == "windows" {
67                 goCmdPath += ".exe"
68         }
69         gotVersion := bytes.TrimSpace(runCmd(t, gorootSrc, env, goCmdPath, "tool", "compile", "-V=full"))
70         wantVersion := []byte(`compile version go1.999`)
71         if !bytes.Equal(gotVersion, wantVersion) {
72                 t.Errorf("compile version without experiment: got %q, want %q", gotVersion, wantVersion)
73         }
74
75         // Build a package in a mode not handled by the make script.
76         runCmd(t, gorootSrc, env, goCmdPath, "build", "-race", "archive/tar")
77
78         // Rebuild the toolchain with GOEXPERIMENT.
79         env = append(env, "GOEXPERIMENT=fieldtrack")
80         runCmd(t, gorootSrc, env, makeScriptPath)
81
82         // Verify compiler version string.
83         gotVersion = bytes.TrimSpace(runCmd(t, gorootSrc, env, goCmdPath, "tool", "compile", "-V=full"))
84         wantVersion = []byte(`compile version go1.999 X:fieldtrack,framepointer`)
85         if !bytes.Equal(gotVersion, wantVersion) {
86                 t.Errorf("compile version with experiment: got %q, want %q", gotVersion, wantVersion)
87         }
88
89         // Build the same package. We should not get a cache conflict.
90         runCmd(t, gorootSrc, env, goCmdPath, "build", "-race", "archive/tar")
91 }
92
93 func runCmd(t *testing.T, dir string, env []string, path string, args ...string) []byte {
94         cmd := exec.Command(path, args...)
95         cmd.Dir = dir
96         cmd.Env = env
97         out, err := cmd.Output()
98         if err != nil {
99                 t.Fatal(err)
100         }
101         return out
102 }