Skip to content

Commit 207230a

Browse files
committed
chore: Improve GitHub Actions workflows lint and testing
Signed-off-by: Glenn Lewis <6598971+gmlewis@users.noreply.github.com>
1 parent cf3388c commit 207230a

File tree

2 files changed

+91
-10
lines changed

2 files changed

+91
-10
lines changed

.github/workflows/linter.yml

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ permissions:
55
contents: read
66

77
jobs:
8-
lint:
8+
check-generated:
99
runs-on: ubuntu-latest
1010
steps:
1111
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
@@ -15,10 +15,28 @@ jobs:
1515
cache-dependency-path: "**/go.sum"
1616
- name: Check generated code
1717
run: ./script/generate.sh --check
18+
19+
golangci-lint:
20+
runs-on: ubuntu-latest
21+
steps:
22+
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
23+
- uses: actions/setup-go@4a3601121dd01d1626a1e23e37211e3254c1c06c # v6.4.0
24+
with:
25+
go-version: stable
26+
cache-dependency-path: "**/go.sum"
1827
- uses: golangci/golangci-lint-action@1e7e51e771db61008b38414a730f564565cf7c20 # v9.2.0
1928
with:
2029
version: v2.10.1 # sync with version in .custom-gcl.yml
2130
experimental: "automatic-module-directories"
31+
32+
check-openapi:
33+
runs-on: ubuntu-latest
34+
steps:
35+
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
36+
- uses: actions/setup-go@4a3601121dd01d1626a1e23e37211e3254c1c06c # v6.4.0
37+
with:
38+
go-version: stable
39+
cache-dependency-path: "**/go.sum"
2240
- name: Check OpenAPI
2341
run: ./script/metadata.sh update-openapi --validate
2442
env:

script/test.sh

Lines changed: 72 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
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

710
set -e
811

@@ -16,16 +19,76 @@ if [ -n "$UPDATE_GOLDEN" ]; then
1619
find . -name golden -type d -exec rm -rf {} +
1720
fi
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

2169
for 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
2781
done
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"
3189
fi
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

Comments
 (0)