Skip to content

Commit bd1bff4

Browse files
Bryan C. Millsgopherbot
Bryan C. Mills
authored andcommitted
internal/testenv: add a test for the GoTool function
GoTool was added in CL 20967, and revised in CL 21292, for #14901. I don't fully understand what problem the GoTool function was added to solve: the discussion on that issue was pretty sparse, but it seems like when we run tests of GOROOT packages they always know their own location relative to GOROOT (and thus always know where to find the 'go' tool). Lacking that understanding, I don't want to change its behavior, but I do at least want to verify that it resolves to the real 'go' tool in the common case (running 'go test' on a package in GOROOT/src). For #50892 For #50893 Updates #14901 Change-Id: I06d831e6765be631dfc4854d7fddc3d27fc1de34 Reviewed-on: https://go-review.googlesource.com/c/go/+/381834 Reviewed-by: Dmitri Shuralyov <[email protected]> Reviewed-by: David Chase <[email protected]> Auto-Submit: Bryan Mills <[email protected]> Run-TryBot: Bryan Mills <[email protected]> TryBot-Result: Gopher Robot <[email protected]>
1 parent d09c6ac commit bd1bff4

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

src/internal/testenv/testenv_test.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Copyright 2022 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package testenv_test
6+
7+
import (
8+
"internal/testenv"
9+
"os"
10+
"path/filepath"
11+
"runtime"
12+
"testing"
13+
)
14+
15+
func TestGoToolLocation(t *testing.T) {
16+
testenv.MustHaveGoBuild(t)
17+
18+
var exeSuffix string
19+
if runtime.GOOS == "windows" {
20+
exeSuffix = ".exe"
21+
}
22+
23+
// Tests are defined to run within their package source directory,
24+
// and this package's source directory is $GOROOT/src/internal/testenv.
25+
// The 'go' command is installed at $GOROOT/bin/go, so if the environment
26+
// is correct then testenv.GoTool() should be identical to ../../../bin/go.
27+
28+
relWant := "../../../bin/go" + exeSuffix
29+
absWant, err := filepath.Abs(relWant)
30+
if err != nil {
31+
t.Fatal(err)
32+
}
33+
34+
wantInfo, err := os.Stat(absWant)
35+
if err != nil {
36+
t.Fatal(err)
37+
}
38+
t.Logf("found go tool at %q (%q)", relWant, absWant)
39+
40+
goTool, err := testenv.GoTool()
41+
if err != nil {
42+
t.Fatalf("testenv.GoTool(): %v", err)
43+
}
44+
t.Logf("testenv.GoTool() = %q", goTool)
45+
46+
gotInfo, err := os.Stat(goTool)
47+
if err != nil {
48+
t.Fatal(err)
49+
}
50+
if !os.SameFile(wantInfo, gotInfo) {
51+
t.Fatalf("%q is not the same file as %q", absWant, goTool)
52+
}
53+
}

0 commit comments

Comments
 (0)