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

View as plain text