...

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

Documentation: cmd/go/testdata/script

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

View as plain text