github.com/hechain20/hechain@v0.0.0-20220316014945-b544036ba106/scripts/run-unit-tests.sh (about) 1 #!/bin/bash 2 3 # Copyright hechain. All Rights Reserved. 4 # 5 # SPDX-License-Identifier: Apache-2.0 6 7 set -eo pipefail 8 9 base_dir="$(cd "$(dirname "$0")/.." && pwd)" 10 11 # regexes for packages to exclude from unit test 12 excluded_packages=( 13 "/integration(/|$)" 14 ) 15 16 # packages that must be run serially 17 serial_packages=( 18 "github.com/hechain20/hechain/gossip/..." 19 ) 20 21 # packages which need to be tested with build tag pkcs11 22 pkcs11_packages=( 23 "github.com/hechain20/hechain/bccsp/factory" 24 "github.com/hechain20/hechain/bccsp/pkcs11" 25 ) 26 27 # packages that are only tested when they (or their deps) change 28 conditional_packages=( 29 "github.com/hechain20/hechain/gossip/..." 30 ) 31 32 # join array elements by the specified string 33 join_by() { 34 local IFS="$1"; shift 35 [ "$#" -eq 0 ] && return 0 36 echo "$*" 37 } 38 39 contains_element() { 40 local key="$1"; shift 41 42 for e in "$@"; do [ "$e" == "$key" ] && return 0; done 43 return 1 44 } 45 46 # create a grep regex from the provide package spec 47 package_filter() { 48 local -a filter 49 if [ "${#@}" -ne 0 ]; then 50 while IFS= read -r pkg; do [ -n "$pkg" ] && filter+=("$pkg"); done < <(go list -f '^{{ .ImportPath }}$' "${@}") 51 fi 52 53 join_by '|' "${filter[@]}" 54 } 55 56 # obtain packages changed since some git refspec 57 packages_diff() { 58 git -C "${base_dir}" diff --no-commit-id --name-only -r "${1:-HEAD}" | 59 (grep '.go$' || true) | \ 60 sed 's%/[^/]*$%%' | sort -u | \ 61 awk '{print "github.com/hechain20/hechain/"$1}' 62 } 63 64 # obtain list of changed packages for verification 65 changed_packages() { 66 local -a changed 67 68 # first check for uncommitted changes 69 while IFS= read -r pkg; do changed+=("$pkg"); done < <(packages_diff HEAD) 70 if [ "${#changed[@]}" -eq 0 ]; then 71 # next check for changes in the latest commit 72 while IFS= read -r pkg; do changed+=("$pkg"); done < <(packages_diff HEAD^) 73 fi 74 75 join_by $'\n' "${changed[@]}" 76 } 77 78 # "go list" packages and filter out excluded packages 79 list_and_filter() { 80 local excluded conditional filter 81 82 excluded=("${excluded_packages[@]}") 83 conditional=$(package_filter "${conditional_packages[@]}") 84 if [ -n "$conditional" ]; then 85 excluded+=("$conditional") 86 fi 87 88 filter=$(join_by '|' "${excluded[@]}") 89 if [ -n "$filter" ]; then 90 go list "$@" 2>/dev/null | grep -Ev "${filter}" || true 91 else 92 go list "$@" 2>/dev/null 93 fi 94 } 95 96 # list conditional packages that have been changed 97 list_changed_conditional() { 98 [ "${#conditional_packages[@]}" -eq 0 ] && return 0 99 100 local changed 101 changed=$(changed_packages) 102 103 local -a additional_packages 104 for pkg in $(go list "${conditional_packages[@]}"); do 105 local dep_regexp 106 dep_regexp=$(go list -f '{{ join .Deps "$|" }}' "$pkg") 107 echo "${changed}" | grep -qE "$dep_regexp" && additional_packages+=("$pkg") 108 echo "${changed}" | grep -qE "$pkg\$" && additional_packages+=("$pkg") 109 done 110 111 join_by $'\n' "${additional_packages[@]}" 112 } 113 114 # remove packages that must be tested serially 115 parallel_test_packages() { 116 local filter 117 filter=$(package_filter "${serial_packages[@]}") 118 if [ -n "$filter" ]; then 119 join_by $'\n' "$@" | grep -Ev "$filter" || true 120 else 121 join_by $'\n' "$@" 122 fi 123 } 124 125 # get packages that must be tested serially 126 serial_test_packages() { 127 local filter 128 filter=$(package_filter "${serial_packages[@]}") 129 if [ -n "$filter" ]; then 130 join_by $'\n' "$@" | grep -E "$filter" || true 131 fi 132 } 133 134 # "go test" the provided packages. Packages that are not present in the serial package list 135 # will be tested in parallel 136 run_tests() { 137 local -a flags=("-cover") 138 if [ -n "${VERBOSE}" ]; then 139 flags+=("-v") 140 fi 141 142 local -a race_flags=() 143 if [ "$(uname -m)" == "x86_64" ]; then 144 export GORACE=atexit_sleep_ms=0 # reduce overhead of race 145 race_flags+=("-race") 146 fi 147 148 GO_TAGS=${GO_TAGS## } 149 [ -n "$GO_TAGS" ] && echo "Testing with $GO_TAGS..." 150 151 time { 152 local -a serial 153 while IFS= read -r pkg; do serial+=("$pkg"); done < <(serial_test_packages "$@") 154 if [ "${#serial[@]}" -ne 0 ]; then 155 go test "${flags[@]}" -failfast -tags "$GO_TAGS" "${serial[@]}" -short -p 1 -timeout=20m 156 fi 157 158 local -a parallel 159 while IFS= read -r pkg; do parallel+=("$pkg"); done < <(parallel_test_packages "$@") 160 if [ "${#parallel[@]}" -ne 0 ]; then 161 go test "${flags[@]}" "${race_flags[@]}" -tags "$GO_TAGS" "${parallel[@]}" -short -timeout=20m 162 fi 163 } 164 } 165 166 # "go test" the provided packages and generate code coverage reports. 167 run_tests_with_coverage() { 168 # run the tests serially 169 time go test -p 1 -cover -coverprofile=profile_tmp.cov -tags "$GO_TAGS" "$@" -timeout=20m 170 tail -n +2 profile_tmp.cov >> profile.cov && rm profile_tmp.cov 171 } 172 173 main() { 174 # default behavior is to run all tests 175 local -a package_spec=("${TEST_PKGS:-github.com/hechain20/hechain/...}") 176 177 # when running a "verify" job, only test packages that have changed 178 if [ "${JOB_TYPE}" = "VERIFY" ]; then 179 package_spec=() 180 while IFS= read -r pkg; do package_spec+=("$pkg"); done < <(changed_packages) 181 fi 182 183 # run everything when profiling 184 if [ "${JOB_TYPE}" = "PROFILE" ]; then 185 conditional_packages=() 186 fi 187 188 # expand the package specs into arrays of packages 189 local -a candidates packages packages_with_pkcs11 190 while IFS= read -r pkg; do candidates+=("$pkg"); done < <(go list "${package_spec[@]}") 191 while IFS= read -r pkg; do packages+=("$pkg"); done < <(list_and_filter "${package_spec[@]}") 192 while IFS= read -r pkg; do contains_element "$pkg" "${candidates[@]}" && packages+=("$pkg"); done < <(list_changed_conditional) 193 while IFS= read -r pkg; do contains_element "$pkg" "${packages[@]}" && packages_with_pkcs11+=("$pkg"); done < <(list_and_filter "${pkcs11_packages[@]}") 194 195 local all_packages=( "${packages[@]}" "${packages_with_pkcs11[@]}" "${packages_with_pkcs11[@]}" ) 196 if [ "${#all_packages[@]}" -eq 0 ]; then 197 echo "Nothing to test!!!" 198 elif [ "${JOB_TYPE}" = "PROFILE" ]; then 199 echo "mode: set" > profile.cov 200 [ "${#packages}" -eq 0 ] || run_tests_with_coverage "${packages[@]}" 201 [ "${#packages_with_pkcs11}" -eq 0 ] || GO_TAGS="${GO_TAGS} pkcs11" run_tests_with_coverage "${packages_with_pkcs11[@]}" 202 gocov convert profile.cov | gocov-xml > report.xml 203 else 204 [ "${#packages}" -eq 0 ] || run_tests "${packages[@]}" 205 [ "${#packages_with_pkcs11}" -eq 0 ] || GO_TAGS="${GO_TAGS} pkcs11" run_tests "${packages_with_pkcs11[@]}" 206 fi 207 } 208 209 main