github.com/graybobo/golang.org-package-offline-cache@v0.0.0-20200626051047-6608995c132f/x/talks/2014/testing/subprocess/subprocess_test.go (about)

     1  package subprocess
     2  
     3  import (
     4  	"os"
     5  	"os/exec"
     6  	"testing"
     7  )
     8  
     9  func TestCrasher(t *testing.T) {
    10  	if os.Getenv("BE_CRASHER") == "1" {
    11  		Crasher()
    12  		return
    13  	}
    14  	cmd := exec.Command(os.Args[0], "-test.run=TestCrasher")
    15  	cmd.Env = append(os.Environ(), "BE_CRASHER=1")
    16  	err := cmd.Run()
    17  	if e, ok := err.(*exec.ExitError); ok && !e.Success() {
    18  		return
    19  	}
    20  	t.Fatalf("process ran with err %v, want exit status 1", err)
    21  }