...

Text file src/cmd/go/testdata/script/test_fuzz_setenv.txt

Documentation: cmd/go/testdata/script

     1[!fuzz] skip
     2[short] skip
     3env GOCACHE=$WORK/cache
     4
     5go test -fuzz=FuzzA -fuzztime=100x fuzz_setenv_test.go
     6
     7-- fuzz_setenv_test.go --
     8package fuzz
     9
    10import (
    11  "flag"
    12  "os"
    13  "testing"
    14)
    15
    16func FuzzA(f *testing.F) {
    17  if s := os.Getenv("TEST_FUZZ_SETENV_A"); isWorker() && s == "" {
    18    f.Fatal("environment variable not set")
    19  } else if !isWorker() && s != "" {
    20    f.Fatal("environment variable already set")
    21  }
    22  f.Setenv("TEST_FUZZ_SETENV_A", "A")
    23  if os.Getenv("TEST_FUZZ_SETENV_A") == "" {
    24    f.Fatal("Setenv did not set environment variable")
    25  }
    26  f.Fuzz(func(*testing.T, []byte) {})
    27}
    28
    29func FuzzB(f *testing.F) {
    30  if os.Getenv("TEST_FUZZ_SETENV_A") != "" {
    31    f.Fatal("environment variable not cleared after FuzzA")
    32  }
    33  f.Skip()
    34}
    35
    36func isWorker() bool {
    37	f := flag.Lookup("test.fuzzworker")
    38	if f == nil {
    39		return false
    40	}
    41	get, ok := f.Value.(flag.Getter)
    42	if !ok {
    43		return false
    44	}
    45	return get.Get() == interface{}(true)
    46}

View as plain text