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

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

View as plain text