github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/util/leaktest/add-leaktest.sh (about)

     1  #!/bin/sh
     2  #
     3  # Add leaktest.AfterTest(t) to all tests in the given files.
     4  # In addition to running this script, add a main_test.go file similar
     5  # to multiraft/main_test.go (with the package statement changed).
     6  #
     7  # This script is idempotent and should be safe to run on files containing
     8  # a mix of tests with and without AfterTest calls.
     9  #
    10  # Usage: add-leaktest.sh pkg/*_test.go
    11  
    12  # Note that go:generate does not do expansion. So "go:generate add-leakest.sh
    13  # *_test.go" will call into here with a single argument of "*_test.go"
    14  
    15  set -eu
    16  
    17  sed -i'~' -e '
    18    /^func Test.*(t \*testing.T) {/ {
    19      # Skip past the test declaration
    20      n
    21      # If the next line does not call AfterTest, insert it.
    22      /leaktest.AfterTest(t)()/! i\
    23        defer leaktest.AfterTest(t)()
    24    }
    25  ' $@
    26  
    27  for i in $@; do
    28    if ! cmp -s $i $i~ ; then
    29      # goimports will adjust indentation and add any necessary import.
    30      goimports -w $i
    31    fi
    32    rm -f $i~
    33  done