Source file src/cmd/go/internal/work/testgo.go

     1  // Copyright 2017 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  // This file contains extra hooks for testing the go command.
     6  
     7  //go:build testgo
     8  
     9  package work
    10  
    11  import (
    12  	"cmd/go/internal/cfg"
    13  	"cmd/go/internal/search"
    14  	"fmt"
    15  	"os"
    16  	"path/filepath"
    17  	"runtime"
    18  )
    19  
    20  func init() {
    21  	if v := os.Getenv("TESTGO_VERSION"); v != "" {
    22  		runtimeVersion = v
    23  	}
    24  
    25  	if testGOROOT := os.Getenv("TESTGO_GOROOT"); testGOROOT != "" {
    26  		// Disallow installs to the GOROOT from which testgo was built.
    27  		// Installs to other GOROOTs — such as one set explicitly within a test — are ok.
    28  		allowInstall = func(a *Action) error {
    29  			if cfg.BuildN {
    30  				return nil
    31  			}
    32  
    33  			rel := search.InDir(a.Target, testGOROOT)
    34  			if rel == "" {
    35  				return nil
    36  			}
    37  
    38  			callerPos := ""
    39  			if _, file, line, ok := runtime.Caller(1); ok {
    40  				if shortFile := search.InDir(file, filepath.Join(testGOROOT, "src")); shortFile != "" {
    41  					file = shortFile
    42  				}
    43  				callerPos = fmt.Sprintf("%s:%d: ", file, line)
    44  			}
    45  			return fmt.Errorf("%stestgo must not write to GOROOT (installing to %s)", callerPos, filepath.Join("GOROOT", rel))
    46  		}
    47  	}
    48  }
    49  

View as plain text