aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/go/testdata/script/goroot_executable.txt
blob: fdbcde06cbcee181b1d832d4bd6463e269167145 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
[gccgo] skip

mkdir $WORK/new/bin

# In this test, we are specifically checking the logic for deriving
# the value of GOROOT from runtime.GOROOT.
# GOROOT_FINAL changes the default behavior of runtime.GOROOT,
# and will thus cause the test to fail if it is set when our
# new cmd/go is built.
env GOROOT_FINAL=

go build -o $WORK/new/bin/go$GOEXE cmd/go &
go build -o $WORK/bin/check$GOEXE check.go &
wait

env TESTGOROOT=$GOROOT
env GOROOT=

# Relocated Executable
# cp $TESTGOROOT/bin/go$GOEXE $WORK/new/bin/go$GOEXE
exec $WORK/bin/check$GOEXE $WORK/new/bin/go$GOEXE $TESTGOROOT

# Relocated Tree:
# If the binary is sitting in a bin dir next to ../pkg/tool, that counts as a GOROOT,
# so it should find the new tree.
mkdir $WORK/new/pkg/tool
exec $WORK/bin/check$GOEXE $WORK/new/bin/go$GOEXE $WORK/new

[!symlink] stop 'The rest of the test cases require symlinks'

# Symlinked Executable:
# With a symlink into go tree, we should still find the go tree.
mkdir $WORK/other/bin
symlink $WORK/other/bin/go$GOEXE -> $WORK/new/bin/go$GOEXE
exec $WORK/bin/check$GOEXE $WORK/new/bin/go$GOEXE $WORK/new

rm $WORK/new/pkg

# Runtime GOROOT:
# Binaries built in the new tree should report the
# new tree when they call runtime.GOROOT.
symlink $WORK/new/src -> $TESTGOROOT/src
symlink $WORK/new/pkg -> $TESTGOROOT/pkg
exec $WORK/new/bin/go$GOEXE run check_runtime_goroot.go $WORK/new

-- check.go --
package main

import (
	"fmt"
	"os"
	"os/exec"
	"path/filepath"
	"strings"
)

func main() {
	exe := os.Args[1]
	want := os.Args[2]
	cmd := exec.Command(exe, "env", "GOROOT")
	out, err := cmd.CombinedOutput()
	if err != nil {
		fmt.Fprintf(os.Stderr, "%s env GOROOT: %v, %s\n", exe, err, out)
		os.Exit(1)
	}
	goroot, err := filepath.EvalSymlinks(strings.TrimSpace(string(out)))
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(1)
	}
	want, err = filepath.EvalSymlinks(want)
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(1)
	}
	if !strings.EqualFold(goroot, want) {
		fmt.Fprintf(os.Stderr, "go env GOROOT:\nhave %s\nwant %s\n", goroot, want)
		os.Exit(1)
	}
	fmt.Fprintf(os.Stderr, "go env GOROOT: %s\n", goroot)

}
-- check_runtime_goroot.go --
package main

import (
	"fmt"
	"os"
	"path/filepath"
	"runtime"
	"strings"
)

func main() {
	goroot, err := filepath.EvalSymlinks(runtime.GOROOT())
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(1)
	}
	want, err := filepath.EvalSymlinks(os.Args[1])
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(1)
	}
	if !strings.EqualFold(goroot, want) {
		fmt.Fprintf(os.Stderr, "go env GOROOT:\nhave %s\nwant %s\n", goroot, want)
		os.Exit(1)
	}
	fmt.Fprintf(os.Stderr, "go env GOROOT: %s\n", goroot)

}