github.com/sbinet/go@v0.0.0-20160827155028-54d7de7dd62b/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  	"flag"
    15  	"os"
    16  	"os/exec"
    17  	"path/filepath"
    18  	"runtime"
    19  	"strconv"
    20  	"strings"
    21  	"testing"
    22  )
    23  
    24  // Builder reports the name of the builder running this test
    25  // (for example, "linux-amd64" or "windows-386-gce").
    26  // If the test is not running on the build infrastructure,
    27  // Builder returns the empty string.
    28  func Builder() string {
    29  	return os.Getenv("GO_BUILDER_NAME")
    30  }
    31  
    32  // HasGoBuild reports whether the current system can build programs with ``go build''
    33  // and then run them with os.StartProcess or exec.Command.
    34  func HasGoBuild() bool {
    35  	switch runtime.GOOS {
    36  	case "android", "nacl":
    37  		return false
    38  	case "darwin":
    39  		if strings.HasPrefix(runtime.GOARCH, "arm") {
    40  			return false
    41  		}
    42  	}
    43  	return true
    44  }
    45  
    46  // MustHaveGoBuild checks that the current system can build programs with ``go build''
    47  // and then run them with os.StartProcess or exec.Command.
    48  // If not, MustHaveGoBuild calls t.Skip with an explanation.
    49  func MustHaveGoBuild(t *testing.T) {
    50  	if !HasGoBuild() {
    51  		t.Skipf("skipping test: 'go build' not available on %s/%s", runtime.GOOS, runtime.GOARCH)
    52  	}
    53  }
    54  
    55  // HasGoRun reports whether the current system can run programs with ``go run.''
    56  func HasGoRun() bool {
    57  	// For now, having go run and having go build are the same.
    58  	return HasGoBuild()
    59  }
    60  
    61  // MustHaveGoRun checks that the current system can run programs with ``go run.''
    62  // If not, MustHaveGoRun calls t.Skip with an explanation.
    63  func MustHaveGoRun(t *testing.T) {
    64  	if !HasGoRun() {
    65  		t.Skipf("skipping test: 'go run' not available on %s/%s", runtime.GOOS, runtime.GOARCH)
    66  	}
    67  }
    68  
    69  // GoToolPath reports the path to the Go tool.
    70  // If the tool is unavailable GoToolPath calls t.Skip.
    71  // If the tool should be available and isn't, GoToolPath calls t.Fatal.
    72  func GoToolPath(t *testing.T) string {
    73  	MustHaveGoBuild(t)
    74  
    75  	var exeSuffix string
    76  	if runtime.GOOS == "windows" {
    77  		exeSuffix = ".exe"
    78  	}
    79  
    80  	path := filepath.Join(runtime.GOROOT(), "bin", "go"+exeSuffix)
    81  	if _, err := os.Stat(path); err == nil {
    82  		return path
    83  	}
    84  
    85  	goBin, err := exec.LookPath("go" + exeSuffix)
    86  	if err != nil {
    87  		t.Fatalf("cannot find go tool: %v", err)
    88  	}
    89  	return goBin
    90  }
    91  
    92  // HasExec reports whether the current system can start new processes
    93  // using os.StartProcess or (more commonly) exec.Command.
    94  func HasExec() bool {
    95  	switch runtime.GOOS {
    96  	case "nacl":
    97  		return false
    98  	case "darwin":
    99  		if strings.HasPrefix(runtime.GOARCH, "arm") {
   100  			return false
   101  		}
   102  	}
   103  	return true
   104  }
   105  
   106  // MustHaveExec checks that the current system can start new processes
   107  // using os.StartProcess or (more commonly) exec.Command.
   108  // If not, MustHaveExec calls t.Skip with an explanation.
   109  func MustHaveExec(t *testing.T) {
   110  	if !HasExec() {
   111  		t.Skipf("skipping test: cannot exec subprocess on %s/%s", runtime.GOOS, runtime.GOARCH)
   112  	}
   113  }
   114  
   115  // HasExternalNetwork reports whether the current system can use
   116  // external (non-localhost) networks.
   117  func HasExternalNetwork() bool {
   118  	return !testing.Short()
   119  }
   120  
   121  // MustHaveExternalNetwork checks that the current system can use
   122  // external (non-localhost) networks.
   123  // If not, MustHaveExternalNetwork calls t.Skip with an explanation.
   124  func MustHaveExternalNetwork(t *testing.T) {
   125  	if testing.Short() {
   126  		t.Skipf("skipping test: no external network in -short mode")
   127  	}
   128  }
   129  
   130  // HasSymlink reports whether the current system can use os.Symlink.
   131  func HasSymlink() bool {
   132  	ok, _ := hasSymlink()
   133  	return ok
   134  }
   135  
   136  // MustHaveSymlink reports whether the current system can use os.Symlink.
   137  // If not, MustHaveSymlink calls t.Skip with an explanation.
   138  func MustHaveSymlink(t *testing.T) {
   139  	ok, reason := hasSymlink()
   140  	if !ok {
   141  		t.Skipf("skipping test: cannot make symlinks on %s/%s%s", runtime.GOOS, runtime.GOARCH, reason)
   142  	}
   143  }
   144  
   145  var flaky = flag.Bool("flaky", false, "run known-flaky tests too")
   146  
   147  func SkipFlaky(t *testing.T, issue int) {
   148  	if !*flaky {
   149  		t.Skipf("skipping known flaky test without the -flaky flag; see golang.org/issue/%d", issue)
   150  	}
   151  }
   152  
   153  func SkipFlakyNet(t *testing.T) {
   154  	if v, _ := strconv.ParseBool(os.Getenv("GO_BUILDER_FLAKY_NET")); v {
   155  		t.Skip("skipping test on builder known to have frequent network failures")
   156  	}
   157  }