github.com/miolini/go@v0.0.0-20160405192216-fca68c8cb408/src/internal/testenv/testenv.go (about)

     1  // Copyright 2015 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 provides information about what functionality
     6  // is available in different testing environments run by the Go team.
     7  //
     8  // It is an internal package because these details are specific
     9  // to the Go team's test setup (on build.golang.org) and not
    10  // fundamental to tests in general.
    11  package testenv
    12  
    13  import (
    14  	"os"
    15  	"os/exec"
    16  	"path/filepath"
    17  	"runtime"
    18  	"strings"
    19  	"testing"
    20  )
    21  
    22  // Builder reports the name of the builder running this test
    23  // (for example, "linux-amd64" or "windows-386-gce").
    24  // If the test is not running on the build infrastructure,
    25  // Builder returns the empty string.
    26  func Builder() string {
    27  	return os.Getenv("GO_BUILDER_NAME")
    28  }
    29  
    30  // HasGoBuild reports whether the current system can build programs with ``go build''
    31  // and then run them with os.StartProcess or exec.Command.
    32  func HasGoBuild() bool {
    33  	switch runtime.GOOS {
    34  	case "android", "nacl":
    35  		return false
    36  	case "darwin":
    37  		if strings.HasPrefix(runtime.GOARCH, "arm") {
    38  			return false
    39  		}
    40  	}
    41  	return true
    42  }
    43  
    44  // MustHaveGoBuild checks that the current system can build programs with ``go build''
    45  // and then run them with os.StartProcess or exec.Command.
    46  // If not, MustHaveGoBuild calls t.Skip with an explanation.
    47  func MustHaveGoBuild(t *testing.T) {
    48  	if !HasGoBuild() {
    49  		t.Skipf("skipping test: 'go build' not available on %s/%s", runtime.GOOS, runtime.GOARCH)
    50  	}
    51  }
    52  
    53  // HasGoRun reports whether the current system can run programs with ``go run.''
    54  func HasGoRun() bool {
    55  	// For now, having go run and having go build are the same.
    56  	return HasGoBuild()
    57  }
    58  
    59  // MustHaveGoRun checks that the current system can run programs with ``go run.''
    60  // If not, MustHaveGoRun calls t.Skip with an explanation.
    61  func MustHaveGoRun(t *testing.T) {
    62  	if !HasGoRun() {
    63  		t.Skipf("skipping test: 'go run' not available on %s/%s", runtime.GOOS, runtime.GOARCH)
    64  	}
    65  }
    66  
    67  // GoToolPath reports the path to the Go tool.
    68  // If the tool is unavailable GoToolPath calls t.Skip.
    69  // If the tool should be available and isn't, GoToolPath calls t.Fatal.
    70  func GoToolPath(t *testing.T) string {
    71  	MustHaveGoBuild(t)
    72  
    73  	var exeSuffix string
    74  	if runtime.GOOS == "windows" {
    75  		exeSuffix = ".exe"
    76  	}
    77  
    78  	path := filepath.Join(runtime.GOROOT(), "bin", "go"+exeSuffix)
    79  	if _, err := os.Stat(path); err == nil {
    80  		return path
    81  	}
    82  
    83  	goBin, err := exec.LookPath("go" + exeSuffix)
    84  	if err != nil {
    85  		t.Fatalf("cannot find go tool: %v", err)
    86  	}
    87  	return goBin
    88  }
    89  
    90  // HasExec reports whether the current system can start new processes
    91  // using os.StartProcess or (more commonly) exec.Command.
    92  func HasExec() bool {
    93  	switch runtime.GOOS {
    94  	case "nacl":
    95  		return false
    96  	case "darwin":
    97  		if strings.HasPrefix(runtime.GOARCH, "arm") {
    98  			return false
    99  		}
   100  	}
   101  	return true
   102  }
   103  
   104  // MustHaveExec checks that the current system can start new processes
   105  // using os.StartProcess or (more commonly) exec.Command.
   106  // If not, MustHaveExec calls t.Skip with an explanation.
   107  func MustHaveExec(t *testing.T) {
   108  	if !HasExec() {
   109  		t.Skipf("skipping test: cannot exec subprocess on %s/%s", runtime.GOOS, runtime.GOARCH)
   110  	}
   111  }
   112  
   113  // HasExternalNetwork reports whether the current system can use
   114  // external (non-localhost) networks.
   115  func HasExternalNetwork() bool {
   116  	return !testing.Short()
   117  }
   118  
   119  // MustHaveExternalNetwork checks that the current system can use
   120  // external (non-localhost) networks.
   121  // If not, MustHaveExternalNetwork calls t.Skip with an explanation.
   122  func MustHaveExternalNetwork(t *testing.T) {
   123  	if testing.Short() {
   124  		t.Skipf("skipping test: no external network in -short mode")
   125  	}
   126  }