github.com/MetalBlockchain/metalgo@v1.11.9/scripts/build_fuzz.sh (about)

     1  #!/usr/bin/env bash
     2  
     3  # First argument is the time, in seconds, to run each fuzz test for.
     4  # If not provided, defaults to 1 second.
     5  #
     6  # Second argument is the directory to run fuzz tests in.
     7  # If not provided, defaults to the current directory.
     8  
     9  set -euo pipefail
    10  
    11  # Mostly taken from https://github.com/golang/go/issues/46312#issuecomment-1153345129
    12  
    13  # Directory above this script
    14  AVALANCHE_PATH=$( cd "$( dirname "${BASH_SOURCE[0]}" )"; cd .. && pwd )
    15  # Load the constants
    16  source "$AVALANCHE_PATH"/scripts/constants.sh
    17  
    18  fuzzTime=${1:-1}
    19  fuzzDir=${2:-.}
    20  
    21  files=$(grep -r --include='**_test.go' --files-with-matches 'func Fuzz' "$fuzzDir")
    22  failed=false
    23  for file in ${files}
    24  do
    25      funcs=$(grep -oP 'func \K(Fuzz\w*)' "$file")
    26      for func in ${funcs}
    27      do
    28          echo "Fuzzing $func in $file"
    29          parentDir=$(dirname "$file")
    30          # If any of the fuzz tests fail, return exit code 1
    31          if ! go test "$parentDir" -run="$func" -fuzz="$func" -fuzztime="${fuzzTime}"s; then
    32              failed=true
    33          fi
    34      done
    35  done
    36  
    37  if $failed; then
    38      exit 1
    39  fi