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