11#! /bin/sh
2- # / script/test.sh runs tests on each go module in go-github. Arguments are passed to each go test invocation.
2+ # / script/test.sh runs tests on each go module in go-github in parallel.
3+ # / Arguments are passed to each go test invocation.
34# / "-race -covermode atomic ./..." is used when no arguments are given.
45# /
56# / When UPDATE_GOLDEN is set, all directories named "golden" are removed before running tests.
7+ # /
8+ # / TEST_JOBS can be set to control parallelism (defaults to detected CPU count).
69
710set -e
811
@@ -16,16 +19,76 @@ if [ -n "$UPDATE_GOLDEN" ]; then
1619 find . -name golden -type d -exec rm -rf {} +
1720fi
1821
19- MOD_DIRS=" $( git ls-files ' *go.mod' | xargs dirname | sort) "
22+ MOD_DIRS=" $( git ls-files ' *go.mod' | xargs dirname | sort -u) "
23+
24+ GREEN=' \033[0;32m'
25+ RED=' \033[0;31m'
26+ BOLD=' \033[1m'
27+ NC=' \033[0m'
28+
29+ EXIT_CODE=0
30+ FAILED_COUNT=0
31+ RUNNING=0
32+ PIDS=" "
33+ DIRS_IN_FLIGHT=" "
34+
35+ LOG_DIR=" $( mktemp -d) "
36+ trap ' rm -rf "$LOG_DIR"' EXIT
37+
38+ : " ${TEST_JOBS:= $(getconf _NPROCESSORS_ONLN 2>/ dev/ null || sysctl -n hw.ncpu 2>/ dev/ null || echo 4)} "
39+
40+ print_header () {
41+ printf " ${BOLD} %s${NC} \n\n" " $1 "
42+ }
43+
44+ wait_pids () {
45+ i=1
46+ for pid in $PIDS ; do
47+ dir=$( echo " $DIRS_IN_FLIGHT " | awk -v i=" $i " ' {print $i}' )
48+ log_file=" $LOG_DIR /$( echo " $dir " | tr ' /' ' _' ) .log"
49+
50+ if wait " $pid " ; then
51+ printf " ${GREEN} ✔ %-40s [ PASS ]${NC} \n" " $dir "
52+ else
53+ printf " ${RED} ✘ %-40s [ FAIL ]${NC} \n" " $dir "
54+ if [ -f " $log_file " ]; then
55+ sed ' s/^/ /' " $log_file "
56+ fi
57+ FAILED_COUNT=$(( FAILED_COUNT + 1 ))
58+ EXIT_CODE=1
59+ fi
60+ i=$(( i + 1 ))
61+ done
62+ PIDS=" "
63+ DIRS_IN_FLIGHT=" "
64+ RUNNING=0
65+ }
66+
67+ print_header " Testing modules"
2068
2169for dir in $MOD_DIRS ; do
22- echo " testing $dir "
23- (
24- cd " $dir "
25- go test " $@ "
26- ) || FAILED=1
70+ log_file=" $LOG_DIR /$( echo " $dir " | tr ' /' ' _' ) .log"
71+
72+ (cd " $dir " && go test " $@ " > " $log_file " 2>&1 ) &
73+
74+ PIDS=" $PIDS $! "
75+ DIRS_IN_FLIGHT=" $DIRS_IN_FLIGHT $dir "
76+ RUNNING=$(( RUNNING + 1 ))
77+
78+ if [ " $RUNNING " -ge " $TEST_JOBS " ]; then
79+ wait_pids
80+ fi
2781done
2882
29- if [ -n " $FAILED " ]; then
30- exit 1
83+ wait_pids
84+
85+ printf -- " ----------------------------\n"
86+ SUMMARY_COLOR=" $GREEN "
87+ if [ " $FAILED_COUNT " -gt 0 ]; then
88+ SUMMARY_COLOR=" $RED "
3189fi
90+
91+ printf " %bTesting: issues found in %d module directories.%b\n" " $SUMMARY_COLOR " " $FAILED_COUNT " " $NC "
92+ printf -- " --------------------------------------------\n"
93+
94+ exit " $EXIT_CODE "
0 commit comments