|
| 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