88# NODE_VERSION=18 ./test/docker-parallel.sh # Specify Node version
99# RUNTIME=bun ./test/docker-parallel.sh # Test with Bun
1010# MAX_JOBS=8 ./test/docker-parallel.sh # Override parallelism
11+ # COVERAGE=true ./test/docker-parallel.sh # Collect code coverage
1112#
1213
1314cd " $( dirname " $0 " ) /.."
@@ -18,6 +19,18 @@ RUNTIME=${RUNTIME:-node}
1819# Node.js version (default: 20, ignored if RUNTIME=bun)
1920NODE_VERSION=${NODE_VERSION:- 20}
2021
22+ # Coverage mode
23+ COVERAGE=${COVERAGE:- false}
24+ COVERAGE_DIR=" $PWD /coverage/.nyc_output"
25+
26+ # Test limit (for debugging - 0 means no limit)
27+ TEST_LIMIT=${TEST_LIMIT:- 0}
28+
29+ if [[ " $COVERAGE " == " true" ]]; then
30+ rm -rf " $COVERAGE_DIR "
31+ mkdir -p " $COVERAGE_DIR "
32+ fi
33+
2134# Auto-detect parallelism
2235if [[ " $OSTYPE " == " darwin" * ]]; then
2336 MAX_JOBS=${MAX_JOBS:- $(sysctl -n hw.ncpu)}
@@ -175,6 +188,11 @@ while IFS= read -r -d '' f; do
175188 fi
176189done < <( find test/programmatic test/interface -name " *.mocha.js" -type f -print0 2> /dev/null)
177190
191+ # Apply test limit if set
192+ if [[ " $TEST_LIMIT " -gt 0 ]]; then
193+ TESTS=(" ${TESTS[@]: 0: $TEST_LIMIT } " )
194+ fi
195+
178196TOTAL=${# TESTS[@]}
179197GLOBAL_START=$( date +%s)
180198echo " [*] Found $TOTAL tests, running with $MAX_JOBS parallel jobs"
@@ -189,19 +207,22 @@ run_test() {
189207 local test_name=$( basename " $test_path " )
190208 local log_file=" $RESULTS_DIR /${test_name} .log"
191209
210+ # Build docker run options
211+ local docker_opts=" --rm -i --mount type=tmpfs,destination=/root/.pm2"
212+ if [[ " $COVERAGE " == " true" ]]; then
213+ # Mount coverage dir and set NODE_V8_COVERAGE to capture all Node processes (including PM2 daemon)
214+ docker_opts=" $docker_opts -v $COVERAGE_DIR :/coverage -e NODE_V8_COVERAGE=/coverage"
215+ fi
216+
192217 if [[ " $test_type " == " e2e" ]]; then
193218 # E2E: extract codebase, source include.sh, then run the script
194- # Use tmpfs for ~/.pm2 to speed up PM2 file I/O
195- cat " $CODEBASE_TAR " | docker run --rm -i \
196- --mount type=tmpfs,destination=/root/.pm2 \
219+ cat " $CODEBASE_TAR " | docker run $docker_opts \
197220 " $IMAGE_NAME " \
198221 bash -c " tar -xf - && source test/e2e/include.sh && bash $test_path " \
199222 > " $log_file " 2>&1
200223 else
201224 # Unit: extract codebase, run with mocha
202- # Use tmpfs for ~/.pm2 to speed up PM2 file I/O
203- cat " $CODEBASE_TAR " | docker run --rm -i \
204- --mount type=tmpfs,destination=/root/.pm2 \
225+ cat " $CODEBASE_TAR " | docker run $docker_opts \
205226 " $IMAGE_NAME " \
206227 bash -c " tar -xf - && mocha --exit --bail $test_path " \
207228 > " $log_file " 2>&1
@@ -315,3 +336,27 @@ echo "============================================"
315336echo " All $TOTAL tests passed in ${GLOBAL_DURATION} s"
316337[[ $SKIPPED -gt 0 ]] && echo " ($SKIPPED tests skipped - require host features)"
317338echo " ============================================"
339+
340+ # Generate coverage report if enabled
341+ if [[ " $COVERAGE " == " true" ]]; then
342+ echo " "
343+ echo " [*] Fixing coverage paths (container -> host)..."
344+ # Fix paths in coverage files: /var/pm2/ -> current directory
345+ for f in " $COVERAGE_DIR " /* .json; do
346+ sed -i.bak ' s|file:///var/pm2/|file://' " $PWD " ' /|g' " $f " 2> /dev/null || \
347+ sed -i ' ' ' s|file:///var/pm2/|file://' " $PWD " ' /|g' " $f "
348+ done
349+ rm -f " $COVERAGE_DIR " /* .bak
350+
351+ echo " [*] Generating coverage report..."
352+ npx c8 report --temp-directory=" $COVERAGE_DIR " --reporter=html --reporter=text --reporter=lcov --report-dir=./coverage --all --src=. \
353+ --include=' lib/**/*.js' --include=' bin/**/*.js' \
354+ --exclude=' test/**' --exclude=' lib/templates/**' \
355+ --exclude=' lib/binaries/DevCLI.js' --exclude=' lib/binaries/Runtime.js' --exclude=' lib/binaries/Runtime4Docker.js' \
356+ --exclude=' lib/API/Dashboard.js' --exclude=' lib/API/Monit.js' --exclude=' lib/API/Serve.js' \
357+ --exclude=' lib/HttpInterface.js' --exclude=' lib/ProcessContainerBun.js' --exclude=' lib/ProcessContainerForkBun.js' \
358+ --exclude=' lib/tools/json5.js' --exclude=' lib/tools/treeify.js' --exclude=' lib/completion.js' \
359+ --exclude=' lib/API/pm2-plus/**'
360+ echo " "
361+ echo " [*] Coverage report: coverage/index.html"
362+ fi
0 commit comments