Black Lives Matter. Support the Equal Justice Initiative.

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

Documentation: cmd/go/internal/work

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

View as plain text