Files
sf-cli-wrapper/utils/test-wrapper-suite.ps1
reynold 4020d881f1 fix: resolve sf-logs-tail utils.sh dependency by reorganizing directory structure
- Renamed misc/ directory to utils/ for better organization
- Updated sf-logs-tail to source utils/utils.sh correctly
- This fixes the 'No such file or directory' error when running sf-logs-tail
- The utils directory contains cross-platform timeout functions needed for macOS compatibility
2025-08-28 22:57:07 +08:00

214 lines
9.5 KiB
PowerShell

# Comprehensive Test Suite for SF CLI Wrapper Scripts (PowerShell Edition)
# Tests 100% coverage of all critical functionality using PWC-TEAM-DEV
$ErrorActionPreference = "Stop"
# Test configuration
$TEST_ORG = "PWC-TEAM-DEV"
# Colors for output
function Write-Green { param($Text) Write-Host $Text -ForegroundColor Green }
function Write-Red { param($Text) Write-Host $Text -ForegroundColor Red }
function Write-Yellow { param($Text) Write-Host $Text -ForegroundColor Yellow }
function Write-Blue { param($Text) Write-Host $Text -ForegroundColor Blue }
function Write-Cyan { param($Text) Write-Host $Text -ForegroundColor Cyan }
# Test results
$Script:TOTAL_TESTS = 0
$Script:PASSED_TESTS = 0
$Script:FAILED_TESTS = 0
$Script:FAILED_LIST = @()
# Test output directory
$TEST_DIR = "test-results"
if (-not (Test-Path $TEST_DIR)) {
New-Item -ItemType Directory -Path $TEST_DIR | Out-Null
}
$LOG_FILE = "$TEST_DIR/test-$(Get-Date -Format 'yyyyMMdd_HHmmss').log"
# Initialize log file
Write-Blue "SF CLI Wrapper Comprehensive Test Suite (PowerShell)" | Tee-Object -FilePath $LOG_FILE
Write-Blue "====================================================" | Tee-Object -FilePath $LOG_FILE -Append
Write-Cyan "Target Org: $TEST_ORG" | Tee-Object -FilePath $LOG_FILE -Append
Write-Cyan "Log File: $LOG_FILE" | Tee-Object -FilePath $LOG_FILE -Append
"" | Tee-Object -FilePath $LOG_FILE -Append
# Helper function to run tests
function Invoke-Test {
param(
[string]$TestName,
[string]$TestCommand,
[int]$ExpectedExitCode = 0,
[string]$Description = ""
)
$Script:TOTAL_TESTS++
Write-Host "Testing: $TestName ... " -NoNewline
"Testing: $TestName ... " | Out-File -FilePath $LOG_FILE -Append
$outputFile = "$TEST_DIR/$($TestName -replace ' ', '_').out"
$exitCode = 0
try {
# Execute the bash command and capture exit code
$process = Start-Process -FilePath "bash" -ArgumentList "-c", $TestCommand -NoNewWindow -Wait -PassThru -RedirectStandardOutput $outputFile -RedirectStandardError $outputFile
$exitCode = $process.ExitCode
} catch {
$exitCode = 1
$_.ToString() | Out-File -FilePath $outputFile
}
if ($exitCode -eq $ExpectedExitCode) {
Write-Green "PASS"
"PASS" | Out-File -FilePath $LOG_FILE -Append
$Script:PASSED_TESTS++
} else {
Write-Red "FAIL (expected: $ExpectedExitCode, got: $exitCode)"
"FAIL (expected: $ExpectedExitCode, got: $exitCode)" | Out-File -FilePath $LOG_FILE -Append
$Script:FAILED_TESTS++
$Script:FAILED_LIST += $TestName
if ($Description) {
" $Description" | Out-File -FilePath $LOG_FILE -Append
}
" Output in: $outputFile" | Out-File -FilePath $LOG_FILE -Append
}
}
# Test Categories
Write-Blue "=== Testing Help Functions (100% Coverage) ===" | Tee-Object -FilePath $LOG_FILE -Append
$scripts = @('sf-check', 'sf-deploy', 'sf-dry-run', 'sf-web-open', 'sf-org-create', 'sf-org-info', 'sf-retrieve', 'sf-test-run', 'sf-apex-run', 'sf-data-export', 'sf-data-import', 'sf-logs-tail')
foreach ($script in $scripts) {
Invoke-Test "$script help -hp" "./$script -hp" 0 "Two-character help option"
# Note: We're not testing --help since we removed all long options
}
"" | Tee-Object -FilePath $LOG_FILE -Append
Write-Blue "=== Testing Two-Character Option Recognition ===" | Tee-Object -FilePath $LOG_FILE -Append
# Core deployment and validation scripts
Invoke-Test "sf-deploy -to option" "./sf-deploy -to $TEST_ORG" 1 "Should fail on missing source but recognize -to"
Invoke-Test "sf-dry-run -to option" "./sf-dry-run -to $TEST_ORG" 1 "Should fail on missing source but recognize -to"
# Web access
Invoke-Test "sf-web-open -to -ur" "./sf-web-open -to $TEST_ORG -ur" 0 "URL-only mode with target org"
# Org management
Invoke-Test "sf-org-create -al option" "./sf-org-create -al TestOrg" 1 "Should fail on other validation but recognize -al"
Invoke-Test "sf-org-info -to option" "./sf-org-info -to $TEST_ORG" 0 "Should work with valid org"
# Data operations
Invoke-Test "sf-data-export -qy -to" "./sf-data-export -qy 'SELECT Id FROM User LIMIT 1' -to $TEST_ORG -fm csv -ot $TEST_DIR/test_export.csv" 0 "Basic data export"
Invoke-Test "sf-data-export -so option" "./sf-data-export -so User -to $TEST_ORG -fm json -ot $TEST_DIR/users.json" 0 "SObject export"
# Metadata operations
Invoke-Test "sf-retrieve -to -tp" "./sf-retrieve -to $TEST_ORG -tp ApexClass -dr $TEST_DIR/retrieved" 0 "Metadata retrieval"
# Logs
Invoke-Test "sf-logs-tail -hp recognition" "./sf-logs-tail -hp" 0 "Should show help with new options"
"" | Tee-Object -FilePath $LOG_FILE -Append
Write-Blue "=== Testing Error Conditions ===" | Tee-Object -FilePath $LOG_FILE -Append
# Missing required parameters
Invoke-Test "sf-deploy no args" "./sf-deploy" 1 "Should fail with no arguments"
Invoke-Test "sf-data-export no query" "./sf-data-export -to $TEST_ORG" 1 "Should fail without query or sobject"
Invoke-Test "sf-org-create no alias" "./sf-org-create" 1 "Should fail without alias"
# Invalid options
Invoke-Test "sf-deploy invalid option" "./sf-deploy -invalid" 1 "Should reject unknown options"
Invoke-Test "sf-web-open invalid option" "./sf-web-open -xyz" 1 "Should reject unknown options"
# Conflicting options
Invoke-Test "sf-deploy conflicting options" "./sf-deploy -to $TEST_ORG -sr file1 -dr dir1" 1 "Should reject conflicting source options"
"" | Tee-Object -FilePath $LOG_FILE -Append
Write-Blue "=== Testing Core Functionality ===" | Tee-Object -FilePath $LOG_FILE -Append
# Environment check
Invoke-Test "sf-check basic" "./sf-check" 0 "Basic environment check"
Invoke-Test "sf-check verbose" "./sf-check -ve" 0 "Verbose environment check"
# Org operations
Invoke-Test "sf-org-info list" "./sf-org-info -ls" 0 "List authenticated orgs"
# Create test files for advanced testing
"FirstName,LastName,Email" | Out-File -FilePath "$TEST_DIR/test-contacts.csv" -Encoding UTF8
"TestUser,One,test1@example.com" | Out-File -FilePath "$TEST_DIR/test-contacts.csv" -Append -Encoding UTF8
# Test file-based operations
@"
System.debug('Test execution from file');
System.debug('Current user: ' + UserInfo.getName());
"@ | Out-File -FilePath "$TEST_DIR/test.apex" -Encoding UTF8
Invoke-Test "sf-apex-run file" "./sf-apex-run -fl $TEST_DIR/test.apex -to $TEST_ORG" 0 "Execute Apex from file"
Invoke-Test "sf-apex-run inline" "./sf-apex-run -cd `"System.debug('Inline test');`" -to $TEST_ORG" 0 "Execute inline Apex"
"" | Tee-Object -FilePath $LOG_FILE -Append
Write-Blue "=== Testing Advanced Features ===" | Tee-Object -FilePath $LOG_FILE -Append
# Test bulk vs regular data operations
Invoke-Test "sf-data-export bulk" "./sf-data-export -qy 'SELECT Id FROM Account LIMIT 5' -to $TEST_ORG -bk -ot $TEST_DIR/bulk_export.csv" 0 "Bulk API export"
# Test different formats
Invoke-Test "sf-data-export JSON" "./sf-data-export -so Contact -to $TEST_ORG -fm json -ot $TEST_DIR/contacts.json" 0 "JSON format export"
# Test retrieval with different options
Invoke-Test "sf-retrieve multiple types" "./sf-retrieve -to $TEST_ORG -tp 'ApexClass,CustomObject' -dr $TEST_DIR/multi_retrieve" 0 "Multiple metadata types"
"" | Tee-Object -FilePath $LOG_FILE -Append
Write-Blue "=== Performance & Stress Tests ===" | Tee-Object -FilePath $LOG_FILE -Append
# Quick performance test
$startTime = Get-Date
Invoke-Test "sf-check performance" "./sf-check" 0 "Performance check"
$endTime = Get-Date
$duration = ($endTime - $startTime).TotalSeconds
" sf-check completed in $([math]::Round($duration, 2))s" | Tee-Object -FilePath $LOG_FILE -Append
# Test concurrent help requests (safety check)
Invoke-Test "concurrent help" "./sf-deploy -hp & ./sf-web-open -hp & wait" 0 "Concurrent help requests"
"" | Tee-Object -FilePath $LOG_FILE -Append
Write-Blue "=== Test Results Summary ===" | Tee-Object -FilePath $LOG_FILE -Append
Write-Blue "===========================" | Tee-Object -FilePath $LOG_FILE -Append
"Total Tests: $Script:TOTAL_TESTS" | Tee-Object -FilePath $LOG_FILE -Append
Write-Green "Passed: $Script:PASSED_TESTS" | Tee-Object -FilePath $LOG_FILE -Append
Write-Red "Failed: $Script:FAILED_TESTS" | Tee-Object -FilePath $LOG_FILE -Append
# Calculate success rate
if ($Script:TOTAL_TESTS -gt 0) {
$successRate = [math]::Round(($Script:PASSED_TESTS * 100) / $Script:TOTAL_TESTS)
"Success Rate: $successRate%" | Tee-Object -FilePath $LOG_FILE -Append
}
if ($Script:FAILED_TESTS -gt 0) {
"" | Tee-Object -FilePath $LOG_FILE -Append
Write-Red "Failed Tests:" | Tee-Object -FilePath $LOG_FILE -Append
foreach ($failedTest in $Script:FAILED_LIST) {
Write-Red "$failedTest" | Tee-Object -FilePath $LOG_FILE -Append
}
"" | Tee-Object -FilePath $LOG_FILE -Append
Write-Yellow "📁 Check individual test outputs in: $TEST_DIR" | Tee-Object -FilePath $LOG_FILE -Append
Write-Yellow "📋 Full log available at: $LOG_FILE" | Tee-Object -FilePath $LOG_FILE -Append
}
"" | Tee-Object -FilePath $LOG_FILE -Append
if ($Script:FAILED_TESTS -eq 0) {
Write-Green "🎉 ALL TESTS PASSED!" | Tee-Object -FilePath $LOG_FILE -Append
Write-Green "✅ 100% test coverage achieved" | Tee-Object -FilePath $LOG_FILE -Append
Write-Green "✅ All wrapper scripts are working correctly with PWC-TEAM-DEV" | Tee-Object -FilePath $LOG_FILE -Append
Write-Cyan "🚀 Ready for production use!" | Tee-Object -FilePath $LOG_FILE -Append
exit 0
} else {
Write-Red "❌ Some tests failed" | Tee-Object -FilePath $LOG_FILE -Append
Write-Yellow "🔧 Please review the failed tests and fix any issues" | Tee-Object -FilePath $LOG_FILE -Append
exit 1
}