Skip to content

Commit 5520f4b

Browse files
committed
cov
1 parent 5b2b295 commit 5520f4b

5 files changed

Lines changed: 82 additions & 9 deletions

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,5 @@ e2e_time
2323
unit_time
2424
*.heapprofile
2525
a.out
26+
coverage/
27+
.nyc_output/

.nycrc.json

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"all": true,
3+
"include": ["lib/**/*.js", "bin/**/*.js"],
4+
"exclude": [
5+
"test/**",
6+
"**/*.mocha.js",
7+
"lib/templates/**",
8+
"lib/binaries/DevCLI.js",
9+
"lib/binaries/Runtime.js",
10+
"lib/binaries/Runtime4Docker.js",
11+
"lib/API/Dashboard.js",
12+
"lib/API/Monit.js",
13+
"lib/API/Serve.js",
14+
"lib/HttpInterface.js",
15+
"lib/ProcessContainerBun.js",
16+
"lib/ProcessContainerForkBun.js",
17+
"lib/tools/json5.js",
18+
"lib/tools/treeify.js",
19+
"lib/completion.js",
20+
"lib/API/pm2-plus/**"
21+
],
22+
"reporter": ["html", "text", "lcov"],
23+
"report-dir": "./coverage",
24+
"temp-dir": "./coverage/.nyc_output"
25+
}

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,8 @@
102102
"test:unit": "bash test/unit.sh",
103103
"test:e2e": "bash test/e2e.sh",
104104
"test": "bash test/unit.sh && bash test/e2e.sh",
105-
"test:parallel": "bash test/docker-parallel.sh"
105+
"test:parallel": "bash test/docker-parallel.sh",
106+
"test:coverage": "COVERAGE=true bash test/docker-parallel.sh"
106107
},
107108
"keywords": [
108109
"cli",

test/Dockerfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ RUN if [ "$RUNTIME" = "bun" ]; then \
3939
ln -s /root/.bun/bin/bunx /usr/local/bin/bunx; \
4040
fi
4141

42-
# Install mocha globally
43-
RUN npm install -g mocha
42+
# Install mocha and c8 globally
43+
RUN npm install -g mocha c8
4444

4545
WORKDIR /var/pm2
4646

test/docker-parallel.sh

Lines changed: 51 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
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

1314
cd "$(dirname "$0")/.."
@@ -18,6 +19,18 @@ RUNTIME=${RUNTIME:-node}
1819
# Node.js version (default: 20, ignored if RUNTIME=bun)
1920
NODE_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
2235
if [[ "$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
176189
done < <(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+
178196
TOTAL=${#TESTS[@]}
179197
GLOBAL_START=$(date +%s)
180198
echo "[*] 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 "============================================"
315336
echo "All $TOTAL tests passed in ${GLOBAL_DURATION}s"
316337
[[ $SKIPPED -gt 0 ]] && echo "($SKIPPED tests skipped - require host features)"
317338
echo "============================================"
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

Comments
 (0)