-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathTaskfile.yml
More file actions
322 lines (285 loc) · 11 KB
/
Taskfile.yml
File metadata and controls
322 lines (285 loc) · 11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
version: "3"
silent: true
tasks:
configure:
desc: Configure CMake with test support
cmds:
- cmd: cmake --preset macosx-universal -DBUILD_TESTING=ON
platforms: [darwin]
- cmd: cmake --preset win-x64 -DBUILD_TESTING=ON
platforms: [windows]
- cmd: cmake --preset linux-amd64 -DBUILD_TESTING=ON
platforms: [linux]
build:
desc: Build the test binary
deps: [configure]
cmds:
- cmd: >-
cmake --build build/macosx-universal
--target nakama-sdk-test
--config MinSizeRel
-- CODE_SIGN_IDENTITY="" CODE_SIGNING_REQUIRED=NO CODE_SIGNING_ALLOWED=NO
platforms: [darwin]
- cmd: >-
cmake --build build/win-x64
--target nakama-sdk-test
--config MinSizeRel
platforms: [windows]
- cmd: >-
cmake --build build/linux-amd64
--target nakama-sdk-test
--config MinSizeRel
platforms: [linux]
server:up:
desc: Start the Nakama test server
cmds:
- docker compose -f integrationtests/server/docker-compose.yml up -d --build --wait
server:down:
desc: Stop the Nakama test server
cmds:
- docker compose -f integrationtests/server/docker-compose.yml down
server:logs:
desc: Tail Nakama server logs
cmds:
- docker compose -f integrationtests/server/docker-compose.yml logs -f nakama
test:
desc: Build and run integration tests (starts server automatically)
deps: [build]
cmds:
- docker compose -f integrationtests/server/docker-compose.yml up -d --build --wait
- cmd: cp build/win-x64/MinSizeRel/*.dll build/win-x64/integrationtests/MinSizeRel/ 2>/dev/null || true
platforms: [windows]
- cmd: build/macosx-universal/integrationtests/MinSizeRel/nakama-sdk-test.app/Contents/MacOS/nakama-sdk-test
platforms: [darwin]
- cmd: build/win-x64/integrationtests/MinSizeRel/nakama-sdk-test.exe
platforms: [windows]
- cmd: build/linux-amd64/integrationtests/MinSizeRel/nakama-sdk-test
platforms: [linux]
test-android:
desc: Build and run Android integration tests (optional DEVICE=<adb-serial>, ABI=arm64-v8a|armeabi-v7a)
vars:
ABI: '{{default "arm64-v8a" .ABI}}'
DEVICE: '{{default "" .DEVICE}}'
CMAKE_PRESET: "android-{{.ABI}}"
cmds:
- docker compose -f integrationtests/server/docker-compose.yml up -d --build --wait
- |
set -euo pipefail
HOST_OS="{{OS}}"
# Portable sleep (Taskfile's Windows shell lacks sleep/read -t)
_sleep() {
{{if eq OS "windows"}}powershell.exe -Command "Start-Sleep -Seconds $1"{{else}}sleep "$1"{{end}}
}
# --- Resolve ANDROID_HOME ---
if [ -z "${ANDROID_HOME:-}" ]; then
case "$HOST_OS" in
windows)
_localappdata="${LOCALAPPDATA:-}"
if [ -n "$_localappdata" ] && [ -d "$_localappdata/Android/Sdk" ]; then
export ANDROID_HOME="$_localappdata/Android/Sdk"
fi;;
darwin)
if [ -d "$HOME/Library/Android/sdk" ]; then
export ANDROID_HOME="$HOME/Library/Android/sdk"
fi;;
esac
fi
if [ -z "${ANDROID_HOME:-}" ]; then
echo "Error: ANDROID_HOME not set and could not be auto-detected."
exit 1
fi
ANDROID_HOME="${ANDROID_HOME//\\//}"
# --- Resolve adb ---
if command -v adb &>/dev/null; then
ADB="adb"
else
ADB="$ANDROID_HOME/platform-tools/adb"
if [ ! -x "$ADB" ] && [ -x "${ADB}.exe" ]; then
ADB="${ADB}.exe"
fi
fi
# --- Resolve ANDROID_NDK_HOME ---
if [ -z "${ANDROID_NDK_HOME:-}" ]; then
if [ -d "$ANDROID_HOME/ndk" ]; then
for _d in "$ANDROID_HOME/ndk"/*/; do [ -d "$_d" ] && ANDROID_NDK_HOME="${_d%/}"; done
fi
if [ -z "${ANDROID_NDK_HOME:-}" ] || [ ! -d "${ANDROID_NDK_HOME}" ]; then
echo "Error: Could not find Android NDK. Set ANDROID_NDK_HOME."
exit 1
fi
echo "Auto-detected NDK: $ANDROID_NDK_HOME"
export ANDROID_NDK_HOME
fi
ANDROID_NDK_HOME="${ANDROID_NDK_HOME//\\//}"
# --- JDK 17 detection (Gradle 7.x requires it) ---
_need_jdk17=false
if [ -n "${JAVA_HOME:-}" ]; then
_jv=$("$JAVA_HOME/bin/java" -version 2>&1); _jv=${_jv#*\"}; _jv=${_jv%%.*}
[ "${_jv:-0}" -gt 17 ] 2>/dev/null && _need_jdk17=true || true
elif command -v java &>/dev/null; then
_jv=$(java -version 2>&1); _jv=${_jv#*\"}; _jv=${_jv%%.*}
[ "${_jv:-0}" -gt 17 ] 2>/dev/null && _need_jdk17=true || true
fi
if [ "$_need_jdk17" = true ]; then
_found=""
case "$HOST_OS" in
windows)
for _d in "/c/Program Files/Eclipse Adoptium"/jdk-17.*; do
[ -d "$_d" ] && _found="$_d" && break
done;;
darwin)
for _d in /Library/Java/JavaVirtualMachines/temurin-17.*/Contents/Home; do
[ -d "$_d" ] && _found="$_d" && break
done;;
linux)
for _d in /usr/lib/jvm/temurin-17-* /usr/lib/jvm/java-17-*; do
[ -d "$_d" ] && _found="$_d" && break
done;;
esac
if [ -n "$_found" ]; then
echo "Auto-detected JDK 17: $_found"
export JAVA_HOME="$_found"
else
echo "Warning: Java ${_jv} detected but Gradle 7.x needs JDK 17. Set JAVA_HOME to JDK 17."
fi
fi
# --- Build native libraries ---
echo "=== Building native libraries (preset: {{.CMAKE_PRESET}}) ==="
cmake --preset "{{.CMAKE_PRESET}}" \
-DBUILD_TESTING=ON \
-DCMAKE_ANDROID_NDK="$ANDROID_NDK_HOME" \
-DCMAKE_ANDROID_ARCH_ABI="{{.ABI}}" \
-DCMAKE_MAKE_PROGRAM="$(command -v ninja)"
cmake --build "build/{{.CMAKE_PRESET}}" --config Debug \
--target nakama-sdk nakama-test
# --- Stage native libraries for Gradle ---
echo "=== Staging native libraries ==="
jni_dir="integrationtests/android/jniLibs/{{.ABI}}"
mkdir -p "$jni_dir"
cp "build/{{.CMAKE_PRESET}}/Debug/libnakama-sdk.so" "$jni_dir/"
cp "build/{{.CMAKE_PRESET}}/integrationtests/Debug/libnakama-test.so" "$jni_dir/"
case "{{.ABI}}" in
arm64-v8a) triple="aarch64-linux-android";;
armeabi-v7a) triple="arm-linux-androideabi";;
esac
stl_lib="$ANDROID_NDK_HOME/toolchains/llvm/prebuilt/${HOST_OS}-x86_64/sysroot/usr/lib/${triple}/libc++_shared.so"
if [ -f "$stl_lib" ]; then
cp "$stl_lib" "$jni_dir/"
else
echo "Warning: libc++_shared.so not found at $stl_lib"
fi
# --- Build APK ---
echo "=== Building APK ==="
cd integrationtests/android
{{if eq OS "windows"}}cmd.exe /c gradlew.bat{{else}}./gradlew{{end}} assembleCustomDebugType -Pabi="{{.ABI}}"
# --- Resolve device serial ---
PACKAGE="com.heroiclabs.nakamatest"
ACTIVITY="${PACKAGE}/.MainActivity"
TIMEOUT=300
LOG_TAG="nakama"
APK_PATH="build/outputs/apk/customDebugType/nakamatest-customDebugType.apk"
parse_devices() {
devices=()
local output
output=$("$ADB" devices)
while IFS= read -r line; do
if [[ "$line" == *$'\t'device ]]; then
devices+=("${line%%$'\t'*}")
fi
done <<< "$output"
}
serial="{{.DEVICE}}"
if [ -z "$serial" ]; then
parse_devices
if [ ${#devices[@]} -eq 0 ]; then
echo "No connected devices/emulators found. Starting emulator..."
EMULATOR="$ANDROID_HOME/emulator/emulator"
if [ ! -x "$EMULATOR" ] && [ -x "${EMULATOR}.exe" ]; then
EMULATOR="${EMULATOR}.exe"
fi
avd=$("$EMULATOR" -list-avds)
avd="${avd%%$'\n'*}"
avd="${avd//$'\r'/}"
if [ -z "$avd" ]; then
echo "Error: No AVDs found. Create one in Android Studio first."
exit 1
fi
echo "Starting AVD: $avd"
{{if eq OS "windows"}}powershell.exe -Command "Start-Process -FilePath '${EMULATOR}' -ArgumentList '-avd','${avd}' -WindowStyle Hidden"{{else}}"$EMULATOR" -avd "$avd" &>/dev/null &{{end}}
echo "Waiting for device to come online..."
"$ADB" wait-for-device
while [[ "$("$ADB" shell getprop sys.boot_completed 2>/dev/null)" != *"1"* ]]; do
_sleep 2
done
echo "Emulator booted."
parse_devices
fi
if [ ${#devices[@]} -gt 1 ]; then
echo "Error: Multiple devices connected. Specify DEVICE=<serial>:"
"$ADB" devices
exit 1
fi
serial="${devices[0]}"
echo "Auto-detected device: $serial"
fi
adb_cmd() {
"$ADB" -s "$serial" "$@"
}
# --- Port forwarding ---
echo "Setting up port forwarding..."
for port in 7349 7350 7351; do
adb_cmd reverse tcp:$port tcp:$port
done
# --- Install APK ---
if [ ! -f "$APK_PATH" ]; then
echo "Error: APK not found at $APK_PATH"
exit 1
fi
echo "Installing APK..."
adb_cmd uninstall "$PACKAGE" 2>/dev/null || true
adb_cmd install -r "$APK_PATH"
# --- Clear logcat and launch ---
adb_cmd logcat -c
echo "Launching $ACTIVITY..."
adb_cmd shell am start -n "$ACTIVITY"
# --- Monitor logcat for test results ---
echo "Waiting for test results (timeout: ${TIMEOUT}s)..."
echo "---"
logfile="logcat-output.tmp"
> "$logfile"
"$ADB" -s "$serial" logcat -s "${LOG_TAG}:V" -v raw > "$logfile" 2>/dev/null &
logcat_pid=$!
trap 'rm -f "$logfile"; kill $logcat_pid 2>/dev/null || true' EXIT
result=""
SECONDS=0
last_line=0
while [ "$SECONDS" -lt "$TIMEOUT" ]; do
line_num=0
while IFS= read -r line; do
line_num=$((line_num + 1))
if [ "$line_num" -le "$last_line" ]; then
continue
fi
echo "$line"
if [[ "$line" == *"Tests failed: 0"* ]]; then
result="passed"
elif [[ "$line" == *"Tests failed:"* ]]; then
result="failed"
fi
done < "$logfile"
last_line=$line_num
[ -n "$result" ] && break
_sleep 1
done
kill $logcat_pid 2>/dev/null || true
echo "---"
if [ -z "$result" ]; then
echo "TIMEOUT: Tests did not complete within ${TIMEOUT}s."
exit 1
elif [ "$result" = "passed" ]; then
echo "ALL TESTS PASSED"
exit 0
else
echo "TESTS FAILED"
exit 1
fi