...

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

Documentation: cmd/go/testdata/script

     1# Regression test for https://go.dev/issue/51461 and https://go.dev/issue/51483.
     2#
     3# When built with -trimpath, runtime.GOROOT() returned the bogus string "go"
     4# if GOROOT was not set explicitly in the environment.
     5# It should instead return the empty string, since we know that we don't
     6# have a valid path to return.
     7
     8[trimpath] env GOROOT=
     9[trimpath] ! go env GOROOT
    10[trimpath] stderr '^go: cannot find GOROOT directory: ''go'' binary is trimmed and GOROOT is not set$'
    11[trimpath] env GOROOT=$TESTGO_GOROOT
    12
    13[short] stop
    14
    15# With GOROOT still set, 'go build' and 'go test -c'
    16# should cause runtime.GOROOT() to report either the correct GOROOT
    17# (without -trimpath) or no GOROOT at all (with -trimpath).
    18
    19go build -o example.exe .
    20go build -trimpath -o example-trimpath.exe .
    21go test -c -o example.test.exe .
    22go test -trimpath -c -o example.test-trimpath.exe .
    23
    24env GOROOT=
    25
    26exec ./example.exe
    27stdout '^GOROOT '$TESTGO_GOROOT'$'
    28stdout '^runtime '$TESTGO_GOROOT${/}src${/}runtime'$'
    29
    30! exec ./example-trimpath.exe
    31stdout '^GOROOT $'
    32stderr 'cannot find package "runtime" in any of:\n\t\(\$GOROOT not set\)\n\t'$WORK${/}gopath${/}src${/}runtime' \(from \$GOPATH\)\n\z'
    33
    34exec ./example.test.exe -test.v
    35stdout '^GOROOT '$TESTGO_GOROOT'$'
    36stdout '^runtime '$TESTGO_GOROOT${/}src${/}runtime'$'
    37
    38! exec ./example.test-trimpath.exe -test.v
    39stdout '^GOROOT $'
    40stderr 'cannot find package "runtime" in any of:\n\t\(\$GOROOT not set\)\n\t'$WORK${/}gopath${/}src${/}runtime' \(from \$GOPATH\)$'
    41
    42# If a correct GOROOT is baked in to the 'go' command itself, 'go run' and
    43# 'go test' should not implicitly set GOROOT in the process environment
    44# (because that could mask an unexpected production dependency on the GOROOT
    45# environment variable), but 'go generate' should (because the generator may
    46# reasonably expect to be able to locate the GOROOT for which it is generating
    47# code).
    48
    49[trimpath] stop
    50
    51! go run -trimpath .
    52stdout '^GOROOT $'
    53stderr 'cannot find package "runtime" in any of:\n\t\(\$GOROOT not set\)\n\t'$WORK${/}gopath${/}src${/}runtime' \(from \$GOPATH\)\nexit status 1\n\z'
    54
    55! go test -trimpath -v .
    56stdout '^GOROOT $'
    57stdout 'cannot find package "runtime" in any of:\n\t\(\$GOROOT not set\)\n\t'$WORK${/}gopath${/}src${/}runtime' \(from \$GOPATH\)$'
    58
    59env GOFLAGS=-trimpath
    60go generate .
    61stdout '^GOROOT '$TESTGO_GOROOT'$'
    62stdout '^runtime '$TESTGO_GOROOT${/}src${/}runtime'$'
    63
    64-- go.mod --
    65module example
    66
    67go 1.19
    68-- main.go --
    69package main
    70
    71//go:generate go run .
    72
    73import (
    74	"fmt"
    75	"go/build"
    76	"os"
    77	"runtime"
    78)
    79
    80func main() {
    81	fmt.Println("GOROOT", runtime.GOROOT())
    82
    83	p, err := build.Default.Import("runtime", "", build.FindOnly)
    84	if err != nil {
    85		fmt.Fprintln(os.Stderr, err)
    86		os.Exit(1)
    87	}
    88	fmt.Println("runtime", p.Dir)
    89}
    90-- main_test.go --
    91package main
    92
    93import "testing"
    94
    95func TestMain(*testing.M) {
    96	main()
    97}

View as plain text