github.com/letsencrypt/trillian@v1.1.2-0.20180615153820-ae375a99d36a/scripts/presubmit.sh (about) 1 #!/bin/bash 2 # 3 # Presubmit checks for Trillian. 4 # 5 # Checks for lint errors, spelling, licensing, correct builds / tests and so on. 6 # Flags may be specified to allow suppressing of checks or automatic fixes, try 7 # `scripts/presubmit.sh --help` for details. 8 # 9 # Globals: 10 # GO_TEST_PARALLELISM: max processes to use for Go tests. Optional (defaults 11 # to 10). 12 # GO_TEST_TIMEOUT: timeout for 'go test'. Optional (defaults to 5m). 13 set -eu 14 15 16 check_pkg() { 17 local cmd="$1" 18 local pkg="$2" 19 check_cmd "$cmd" "try running 'go get -u $pkg'" 20 } 21 22 check_cmd() { 23 local cmd="$1" 24 local msg="$2" 25 if ! type -p "${cmd}" > /dev/null; then 26 echo "${cmd} not found, ${msg}" 27 return 1 28 fi 29 } 30 31 usage() { 32 echo "$0 [--coverage] [--fix] [--no-build] [--no-linters] [--no-generate]" 33 } 34 35 main() { 36 local coverage=0 37 local fix=0 38 local run_build=1 39 local run_lint=1 40 local run_generate=1 41 while [[ $# -gt 0 ]]; do 42 case "$1" in 43 --coverage) 44 coverage=1 45 ;; 46 --fix) 47 fix=1 48 ;; 49 --help) 50 usage 51 exit 0 52 ;; 53 --no-build) 54 run_build=0 55 ;; 56 --no-linters) 57 run_lint=0 58 ;; 59 --no-generate) 60 run_generate=0 61 ;; 62 *) 63 usage 64 exit 1 65 ;; 66 esac 67 shift 1 68 done 69 70 cd "$(dirname "$0")" # at scripts/ 71 cd .. # at top level 72 73 if [[ "$fix" -eq 1 ]]; then 74 check_pkg goimports golang.org/x/tools/cmd/goimports || exit 1 75 76 local go_srcs="$(find . -name '*.go' | \ 77 grep -v vendor/ | \ 78 grep -v mock_ | \ 79 grep -v .pb.go | \ 80 grep -v .pb.gw.go | \ 81 grep -v _string.go | \ 82 tr '\n' ' ')" 83 84 echo 'running gofmt' 85 gofmt -s -w ${go_srcs} 86 echo 'running goimports' 87 goimports -w ${go_srcs} 88 fi 89 90 if [[ "${run_build}" -eq 1 ]]; then 91 local goflags='' 92 if [[ "${GOFLAGS:+x}" ]]; then 93 goflags="${GOFLAGS}" 94 fi 95 96 echo 'running go build' 97 go build ${goflags} ./... 98 99 echo 'running go test' 100 # Install test deps so that individual test runs below can reuse them. 101 echo 'installing test deps' 102 go test ${goflags} -i ./... 103 104 if [[ ${coverage} -eq 1 ]]; then 105 # Individual package profiles are written to "$profile.out" files under 106 # /tmp/trillian_profile. 107 # An aggregate profile is created at /tmp/coverage.txt. 108 mkdir -p /tmp/trillian_profile 109 rm -f /tmp/trillian_profile/* 110 111 for d in $(go list ./...); do 112 # Create a different -coverprofile for each test 113 # Transform $d to a smaller, valid file name. 114 # For example: 115 # * github.com/google/trillian becomes trillian.out 116 # * github.com/google/trillian/cmd/createtree/keys becomes 117 # trillian-cmd-createtree-keys.out 118 local profile="${d}.out" 119 profile="${profile#github.com/*/}" 120 profile="${profile//\//-}" 121 local coverflags="-covermode=atomic -coverprofile='/tmp/trillian_profile/${profile}'" 122 123 # Do not run go test in the loop, instead echo it so we can use xargs to 124 # add some parallelism. 125 echo go test \ 126 -short \ 127 -timeout=${GO_TEST_TIMEOUT:-5m} \ 128 ${coverflags} \ 129 ${goflags} \ 130 "$d" -alsologtostderr 131 done | xargs -I '{}' -P ${GO_TEST_PARALLELISM:-10} bash -c '{}' 132 133 cat /tmp/trillian_profile/*.out > /tmp/coverage.txt 134 else 135 go test \ 136 -short \ 137 -timeout=${GO_TEST_TIMEOUT:-5m} \ 138 ${goflags} \ 139 ./... -alsologtostderr 140 fi 141 fi 142 143 if [[ "${run_lint}" -eq 1 ]]; then 144 check_cmd gometalinter \ 145 'have you installed github.com/alecthomas/gometalinter?' || exit 1 146 147 echo 'running gometalinter' 148 gometalinter --config=gometalinter.json --deadline=60s ./... 149 fi 150 151 if [[ "${run_generate}" -eq 1 ]]; then 152 check_cmd protoc 'have you installed protoc?' 153 check_pkg mockgen github.com/golang/mock/mockgen || exit 1 154 check_pkg stringer golang.org/x/tools/cmd/stringer || exit 1 155 156 echo 'running go generate' 157 go generate -run="protoc" ./... 158 go generate -run="mockgen" ./... 159 go generate -run="stringer" ./... 160 fi 161 } 162 163 main "$@"