Text file src/cmd/go/testdata/script/goroot_executable.txt

     1  [compiler:gccgo] skip
     2  [short] skip 'builds and links another cmd/go'
     3  
     4  mkdir $WORK/new/bin
     5  
     6  # In this test, we are specifically checking the logic for deriving
     7  # the value of GOROOT from runtime.GOROOT.
     8  # GOROOT_FINAL changes the default behavior of runtime.GOROOT,
     9  # and will thus cause the test to fail if it is set when our
    10  # new cmd/go is built.
    11  env GOROOT_FINAL=
    12  
    13  # $GOROOT/bin/go is whatever the user has already installed
    14  # (using make.bash or similar). We can't make assumptions about what
    15  # options it may have been built with, such as -trimpath or GOROOT_FINAL.
    16  # Instead, we build a fresh copy of the binary with known settings.
    17  go build -o $WORK/new/bin/go$GOEXE cmd/go &
    18  go build -trimpath -o $WORK/bin/check$GOEXE check.go &
    19  wait
    20  
    21  env TESTGOROOT=$GOROOT
    22  env GOROOT=
    23  
    24  # Relocated Executable
    25  exec $WORK/bin/check$GOEXE $WORK/new/bin/go$GOEXE $TESTGOROOT
    26  
    27  # Relocated Tree:
    28  # If the binary is sitting in a bin dir next to ../pkg/tool, that counts as a GOROOT,
    29  # so it should find the new tree.
    30  mkdir $WORK/new/pkg/tool
    31  exec $WORK/bin/check$GOEXE $WORK/new/bin/go$GOEXE $WORK/new
    32  
    33  [!symlink] stop 'The rest of the test cases require symlinks'
    34  
    35  # Symlinked Executable:
    36  # With a symlink into go tree, we should still find the go tree.
    37  mkdir $WORK/other/bin
    38  symlink $WORK/other/bin/go$GOEXE -> $WORK/new/bin/go$GOEXE
    39  exec $WORK/bin/check$GOEXE $WORK/new/bin/go$GOEXE $WORK/new
    40  
    41  rm $WORK/new/pkg
    42  
    43  # Runtime GOROOT:
    44  # Binaries built in the new tree should report the
    45  # new tree when they call runtime.GOROOT.
    46  symlink $WORK/new/src -> $TESTGOROOT/src
    47  symlink $WORK/new/pkg -> $TESTGOROOT/pkg
    48  exec $WORK/new/bin/go$GOEXE run check_runtime_goroot.go $WORK/new
    49  
    50  -- check.go --
    51  package main
    52  
    53  import (
    54  	"fmt"
    55  	"os"
    56  	"os/exec"
    57  	"path/filepath"
    58  	"strings"
    59  )
    60  
    61  func main() {
    62  	exe := os.Args[1]
    63  	want := os.Args[2]
    64  	cmd := exec.Command(exe, "env", "GOROOT")
    65  	out, err := cmd.CombinedOutput()
    66  	if err != nil {
    67  		fmt.Fprintf(os.Stderr, "%s env GOROOT: %v, %s\n", exe, err, out)
    68  		os.Exit(1)
    69  	}
    70  	goroot, err := filepath.EvalSymlinks(strings.TrimSpace(string(out)))
    71  	if err != nil {
    72  		fmt.Fprintln(os.Stderr, err)
    73  		os.Exit(1)
    74  	}
    75  	want, err = filepath.EvalSymlinks(want)
    76  	if err != nil {
    77  		fmt.Fprintln(os.Stderr, err)
    78  		os.Exit(1)
    79  	}
    80  	if !strings.EqualFold(goroot, want) {
    81  		fmt.Fprintf(os.Stderr, "go env GOROOT:\nhave %s\nwant %s\n", goroot, want)
    82  		os.Exit(1)
    83  	}
    84  	fmt.Fprintf(os.Stderr, "go env GOROOT: %s\n", goroot)
    85  
    86  }
    87  -- check_runtime_goroot.go --
    88  package main
    89  
    90  import (
    91  	"fmt"
    92  	"os"
    93  	"path/filepath"
    94  	"runtime"
    95  	"strings"
    96  )
    97  
    98  func main() {
    99  	goroot, err := filepath.EvalSymlinks(runtime.GOROOT())
   100  	if err != nil {
   101  		fmt.Fprintln(os.Stderr, err)
   102  		os.Exit(1)
   103  	}
   104  	want, err := filepath.EvalSymlinks(os.Args[1])
   105  	if err != nil {
   106  		fmt.Fprintln(os.Stderr, err)
   107  		os.Exit(1)
   108  	}
   109  	if !strings.EqualFold(goroot, want) {
   110  		fmt.Fprintf(os.Stderr, "go env GOROOT:\nhave %s\nwant %s\n", goroot, want)
   111  		os.Exit(1)
   112  	}
   113  	fmt.Fprintf(os.Stderr, "go env GOROOT: %s\n", goroot)
   114  
   115  }
   116  

View as plain text