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

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

View as plain text