Skip to content

Commit fde86ac

Browse files
committed
CI: Enhance common script with strict mode
This adds bash strict mode, color output helpers, ASSERT function, and cleanup mechanism to improve CI script reliability and debugging. - Enable bash strict mode (set -euo pipefail) - Add color output functions (print_success, print_error, print_warning) - Add ASSERT function for test validation - Add cleanup function registry with trap handler - Fix POSIX compliance (use = instead of ==)
1 parent c9f1882 commit fde86ac

File tree

1 file changed

+65
-7
lines changed

1 file changed

+65
-7
lines changed

.ci/common.sh

Lines changed: 65 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# Bash strict mode
2+
set -euo pipefail
3+
14
# Expect host is Linux/x86_64, Linux/aarch64, macOS/arm64
25

36
MACHINE_TYPE=$(uname -m)
@@ -15,12 +18,67 @@ check_platform()
1518
esac
1619
}
1720

18-
if [[ "${OS_TYPE}" == "Linux" ]]; then
21+
if [ "${OS_TYPE}" = "Linux" ]; then
1922
PARALLEL=-j$(nproc)
2023
else
2124
PARALLEL=-j$(sysctl -n hw.logicalcpu)
2225
fi
2326

27+
# Color output helpers
28+
RED='\033[0;31m'
29+
GREEN='\033[0;32m'
30+
YELLOW='\033[1;33m'
31+
NC='\033[0m' # No Color
32+
33+
print_success()
34+
{
35+
echo -e "${GREEN}[SUCCESS]${NC} $1"
36+
}
37+
38+
print_error()
39+
{
40+
echo -e "${RED}[ERROR]${NC} $1" >&2
41+
}
42+
43+
print_warning()
44+
{
45+
echo -e "${YELLOW}[WARNING]${NC} $1"
46+
}
47+
48+
# Assertion function for tests
49+
# Usage: ASSERT <condition> <error_message>
50+
ASSERT()
51+
{
52+
local condition=$1
53+
shift
54+
local message="$*"
55+
56+
if ! eval "${condition}"; then
57+
print_error "Assertion failed: ${message}"
58+
print_error "Condition: ${condition}"
59+
return 1
60+
fi
61+
}
62+
63+
# Cleanup function registry
64+
CLEANUP_FUNCS=()
65+
66+
register_cleanup()
67+
{
68+
CLEANUP_FUNCS+=("$1")
69+
}
70+
71+
cleanup()
72+
{
73+
local func
74+
for func in "${CLEANUP_FUNCS[@]-}"; do
75+
[ -n "${func}" ] || continue
76+
eval "${func}" || true
77+
done
78+
}
79+
80+
trap cleanup EXIT
81+
2482
# Universal download utility with curl/wget compatibility
2583
# Provides consistent interface regardless of which tool is available
2684

@@ -49,7 +107,7 @@ download_to_stdout()
49107
local url="$1"
50108
case "$DOWNLOAD_TOOL" in
51109
curl)
52-
curl -fsSL "$url"
110+
curl -fS --retry 5 --retry-delay 2 --retry-max-time 60 -sL "$url"
53111
;;
54112
wget)
55113
wget -qO- "$url"
@@ -66,7 +124,7 @@ download_to_file()
66124
local output="$2"
67125
case "$DOWNLOAD_TOOL" in
68126
curl)
69-
curl -fsSL -o "$output" "$url"
127+
curl -fS --retry 5 --retry-delay 2 --retry-max-time 60 -sL -o "$output" "$url"
70128
;;
71129
wget)
72130
wget -q -O "$output" "$url"
@@ -88,7 +146,7 @@ download_with_headers()
88146
for header in "$@"; do
89147
headers+=(-H "$header")
90148
done
91-
curl -fsSL "${headers[@]}" "$url"
149+
curl -fS --retry 5 --retry-delay 2 --retry-max-time 60 -sL "${headers[@]}" "$url"
92150
;;
93151
wget)
94152
for header in "$@"; do
@@ -107,7 +165,7 @@ download_silent()
107165
local url="$1"
108166
case "$DOWNLOAD_TOOL" in
109167
curl)
110-
curl -fsSL "$url"
168+
curl -fS --retry 5 --retry-delay 2 --retry-max-time 60 -sL "$url"
111169
;;
112170
wget)
113171
wget -qO- "$url"
@@ -124,7 +182,7 @@ download_with_progress()
124182
local output="$2"
125183
case "$DOWNLOAD_TOOL" in
126184
curl)
127-
curl -fL -# -o "$output" "$url"
185+
curl -fS --retry 5 --retry-delay 2 --retry-max-time 60 -L -# -o "$output" "$url"
128186
;;
129187
wget)
130188
wget -O "$output" "$url"
@@ -141,7 +199,7 @@ check_url()
141199
local url="$1"
142200
case "$DOWNLOAD_TOOL" in
143201
curl)
144-
curl -fsSL --head "$url" > /dev/null 2>&1
202+
curl -fS --retry 5 --retry-delay 2 --retry-max-time 60 -sL --head "$url" > /dev/null 2>&1
145203
;;
146204
wget)
147205
wget --spider -q "$url" 2> /dev/null

0 commit comments

Comments
 (0)