Skip to content

Commit 209cea9

Browse files
Copilothotlong
andcommitted
Improve test script robustness and cross-platform compatibility
Co-authored-by: hotlong <50353452+hotlong@users.noreply.github.com>
1 parent 6fea0ba commit 209cea9

File tree

3 files changed

+47
-10
lines changed

3 files changed

+47
-10
lines changed

TESTING.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ ObjectDocs provides multiple test scripts to validate the complete lifecycle of
3333
- pnpm
3434
- curl (for HTTP testing)
3535
- Available port 7777
36+
- `timeout` command (Linux) or `gtimeout` (macOS via homebrew: `brew install coreutils`)
37+
- Note: Test will run without timeout protection if command is not available
3638

3739
**Output**: Detailed step-by-step progress with color-coded success/failure indicators.
3840

TESTING.zh-CN.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ ObjectDocs 提供了多个测试脚本来验证文档站点创建的完整生命
3333
- pnpm
3434
- curl(用于 HTTP 测试)
3535
- 可用端口 7777
36+
- `timeout` 命令(Linux)或 `gtimeout`(macOS 通过 homebrew 安装:`brew install coreutils`
37+
- 注意:如果命令不可用,测试将在没有超时保护的情况下运行
3638

3739
**输出**:详细的分步进度,带有彩色成功/失败指示器。
3840

test-site.sh

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ NC='\033[0m' # No Color
1414

1515
# Configuration
1616
TEST_DIR="/tmp/objectdocs-test-$(date +%s)"
17+
LOG_DIR="/tmp/objectdocs-logs-$(date +%s)"
1718
PORT=7777
1819
BUILD_TIMEOUT=300 # 5 minutes for build
1920
DEV_TIMEOUT=30 # 30 seconds for dev server to start
@@ -23,22 +24,33 @@ cleanup() {
2324
echo ""
2425
echo -e "${BLUE}Cleaning up...${NC}"
2526

26-
# Kill any running dev/start servers
27+
# Kill any running dev/start servers (gracefully first, then forcefully)
2728
if [ ! -z "$DEV_PID" ]; then
2829
kill $DEV_PID 2>/dev/null || true
30+
sleep 2
31+
kill -9 $DEV_PID 2>/dev/null || true
2932
fi
3033
if [ ! -z "$START_PID" ]; then
3134
kill $START_PID 2>/dev/null || true
35+
sleep 2
36+
kill -9 $START_PID 2>/dev/null || true
3237
fi
3338

34-
# Kill any process using the test port
39+
# Kill any process using the test port (gracefully)
40+
lsof -ti:$PORT | xargs kill 2>/dev/null || true
41+
sleep 1
3542
lsof -ti:$PORT | xargs kill -9 2>/dev/null || true
3643

3744
# Remove test directory (force recursive delete)
3845
if [ -d "$TEST_DIR" ]; then
3946
rm -rf "$TEST_DIR" 2>/dev/null || true
4047
fi
4148

49+
# Remove log directory
50+
if [ -d "$LOG_DIR" ]; then
51+
rm -rf "$LOG_DIR" 2>/dev/null || true
52+
fi
53+
4254
echo -e "${BLUE}Cleanup complete${NC}"
4355
}
4456

@@ -101,9 +113,13 @@ wait_for_server() {
101113
main() {
102114
print_section "ObjectDocs Complete Lifecycle Test"
103115
echo "Test directory: $TEST_DIR"
116+
echo "Log directory: $LOG_DIR"
104117
echo "Port: $PORT"
105118
echo ""
106119

120+
# Create log directory
121+
mkdir -p "$LOG_DIR"
122+
107123
# Check prerequisites
108124
print_section "Step 0: Checking Prerequisites"
109125

@@ -280,7 +296,7 @@ EOF
280296
print_section "Step 5: Testing Development Server"
281297

282298
print_info "Starting dev server..."
283-
pnpm dev > /tmp/dev-server.log 2>&1 &
299+
pnpm dev > "$LOG_DIR/dev-server.log" 2>&1 &
284300
DEV_PID=$!
285301

286302
if wait_for_server $PORT $DEV_TIMEOUT; then
@@ -302,13 +318,15 @@ EOF
302318
else
303319
print_error "Dev server failed to start within ${DEV_TIMEOUT}s"
304320
print_info "Server log output:"
305-
cat /tmp/dev-server.log || true
321+
cat "$LOG_DIR/dev-server.log" || true
306322
exit 1
307323
fi
308324

309325
# Stop dev server
310326
print_info "Stopping dev server..."
311327
kill $DEV_PID 2>/dev/null || true
328+
sleep 2
329+
kill -9 $DEV_PID 2>/dev/null || true
312330
wait $DEV_PID 2>/dev/null || true
313331
DEV_PID=""
314332
sleep 2
@@ -318,11 +336,24 @@ EOF
318336
print_section "Step 6: Testing Build Process"
319337

320338
print_info "Running build (this may take a few minutes)..."
321-
if timeout $BUILD_TIMEOUT pnpm build; then
322-
print_success "Build completed successfully"
339+
340+
# Check if timeout command is available
341+
if command_exists timeout; then
342+
if timeout $BUILD_TIMEOUT pnpm build; then
343+
print_success "Build completed successfully"
344+
else
345+
print_error "Build failed or timed out"
346+
exit 1
347+
fi
323348
else
324-
print_error "Build failed or timed out"
325-
exit 1
349+
# Fallback without timeout (macOS compatibility)
350+
print_warning "timeout command not available, running without timeout protection"
351+
if pnpm build; then
352+
print_success "Build completed successfully"
353+
else
354+
print_error "Build failed"
355+
exit 1
356+
fi
326357
fi
327358

328359
# Check if build output exists
@@ -336,7 +367,7 @@ EOF
336367
print_section "Step 7: Testing Production Server"
337368

338369
print_info "Starting production server..."
339-
pnpm start > /tmp/start-server.log 2>&1 &
370+
pnpm start > "$LOG_DIR/start-server.log" 2>&1 &
340371
START_PID=$!
341372

342373
if wait_for_server $PORT $DEV_TIMEOUT; then
@@ -358,13 +389,15 @@ EOF
358389
else
359390
print_error "Production server failed to start within ${DEV_TIMEOUT}s"
360391
print_info "Server log output:"
361-
cat /tmp/start-server.log || true
392+
cat "$LOG_DIR/start-server.log" || true
362393
exit 1
363394
fi
364395

365396
# Stop production server
366397
print_info "Stopping production server..."
367398
kill $START_PID 2>/dev/null || true
399+
sleep 2
400+
kill -9 $START_PID 2>/dev/null || true
368401
wait $START_PID 2>/dev/null || true
369402
START_PID=""
370403
sleep 2

0 commit comments

Comments
 (0)