Files
sf-cli-wrapper/sf-test-run.ps1
Reynold Lariza f250f81753 fixed ps scripts
2025-08-28 22:30:40 +08:00

228 lines
6.2 KiB
PowerShell

#!/usr/bin/env pwsh
<#
.SYNOPSIS
Focused Apex test execution wrapper for Salesforce CLI
.DESCRIPTION
A user-friendly wrapper around 'sf apex run test' that simplifies test execution
with better formatting, intelligent defaults, and comprehensive reporting options.
.PARAMETER tc
Comma-separated list of test class names to run
.PARAMETER tm
Comma-separated list of specific test methods to run (format: ClassName.methodName)
.PARAMETER tl
Test level to run (RunLocalTests, RunAllTestsInOrg, RunSpecifiedTests)
.PARAMETER st
Test suite name to run
.PARAMETER cv
Generate code coverage report
.PARAMETER wt
Wait time in minutes for test execution (default: 10)
.PARAMETER to
Target org username or alias (uses default if not specified)
.PARAMETER od
Directory to store test results and reports
.PARAMETER ve
Enable verbose test output
.PARAMETER hp
Show this help message
.EXAMPLE
.\sf-test-run.ps1 -tc "AccountTest,ContactTest"
.\sf-test-run.ps1 -tl "RunLocalTests" -cv
.\sf-test-run.ps1 -tm "AccountTest.testCreate,ContactTest.testUpdate"
.\sf-test-run.ps1 -st "AllTests" -wt 15 -to "staging"
.NOTES
This script automatically checks for Salesforce CLI installation and runs
diagnostics if the CLI is not found.
#>
param(
[string]$tc,
[string]$tm,
[ValidateSet("RunLocalTests", "RunAllTestsInOrg", "RunSpecifiedTests")]
[string]$tl,
[string]$st,
[switch]$cv,
[int]$wt = 10,
[string]$to,
[string]$od,
[switch]$ve,
[switch]$hp
)
# Show help if no parameters provided
if (-not ($tc -or $tm -or $tl -or $st -or $hp)) {
Get-Help $MyInvocation.MyCommand.Path -Detailed
exit 0
}
# Show help if requested
if ($hp) {
Get-Help $MyInvocation.MyCommand.Path -Detailed
exit 0
}
# Function to check if Salesforce CLI is installed
function Test-SalesforceCLI {
try {
$null = Get-Command sf -ErrorAction Stop
return $true
} catch {
return $false
}
}
# Function to run sf-check diagnostics
function Invoke-SalesforceCheck {
$checkScript = if (Test-Path "sf-check.ps1") {
".\sf-check.ps1"
} elseif (Test-Path "sf-check.sh") {
"bash sf-check.sh"
} else {
$null
}
if ($checkScript) {
Write-Host "Running Salesforce CLI diagnostics..." -ForegroundColor Yellow
Invoke-Expression $checkScript
} else {
Write-Host "Salesforce CLI not found and no diagnostic script available." -ForegroundColor Red
Write-Host "Please install the Salesforce CLI: https://developer.salesforce.com/tools/salesforcecli" -ForegroundColor Red
}
}
# Silently check for Salesforce CLI
if (-not (Test-SalesforceCLI)) {
Invoke-SalesforceCheck
exit 1
}
# Validate that at least one test specification is provided
$testSpecCount = @($tc, $tm, $tl, $st | Where-Object { $_ }).Count
if ($testSpecCount -eq 0) {
Write-Host "Error: Must specify one of: -tc, -tm, -tl, or -st" -ForegroundColor Red
Write-Host ""
Write-Host "Usage examples:" -ForegroundColor Yellow
Write-Host " .\sf-test-run.ps1 -tc `"AccountTest,ContactTest`"" -ForegroundColor Gray
Write-Host " .\sf-test-run.ps1 -tl `"RunLocalTests`" -cv" -ForegroundColor Gray
Write-Host " .\sf-test-run.ps1 -tm `"AccountTest.testCreate`"" -ForegroundColor Gray
Write-Host " .\sf-test-run.ps1 -st `"AllTests`"" -ForegroundColor Gray
Write-Host ""
Write-Host "Use -hp for detailed usage information." -ForegroundColor Yellow
exit 1
}
# Build the sf command
$sfArgs = @("apex", "run", "test")
# Add test specification
if ($tc) {
$sfArgs += "--class-names"
$sfArgs += $tc
Write-Host "Running test classes: $tc" -ForegroundColor Green
} elseif ($tm) {
$sfArgs += "--tests"
$sfArgs += $tm
Write-Host "Running test methods: $tm" -ForegroundColor Green
} elseif ($tl) {
$sfArgs += "--test-level"
$sfArgs += $tl
Write-Host "Running tests at level: $tl" -ForegroundColor Green
} elseif ($st) {
$sfArgs += "--suite-names"
$sfArgs += $st
Write-Host "Running test suite: $st" -ForegroundColor Green
}
# Add optional parameters
if ($to) {
$sfArgs += "--target-org"
$sfArgs += $to
Write-Host "Target org: $to" -ForegroundColor Cyan
}
if ($wt -ne 10) {
$sfArgs += "--wait"
$sfArgs += $wt.ToString()
}
if ($cv) {
$sfArgs += "--code-coverage"
Write-Host "Code coverage: Enabled" -ForegroundColor Yellow
}
if ($od) {
if (-not (Test-Path $od)) {
Write-Host "Creating output directory: $od" -ForegroundColor Yellow
New-Item -ItemType Directory -Path $od -Force | Out-Null
}
$sfArgs += "--output-dir"
$sfArgs += $od
}
# Always use detailed output for better reporting
$sfArgs += "--detailed-coverage"
$sfArgs += "--result-format"
$sfArgs += "human"
# Add verbose flag if requested
if ($ve) {
$sfArgs += "--verbose"
}
# Display test execution info
Write-Host ""
Write-Host "🧪 Starting Apex Test Execution" -ForegroundColor Blue
Write-Host "================================" -ForegroundColor Blue
# Display the command being run
Write-Host ""
Write-Host "Executing: sf $($sfArgs -join ' ')" -ForegroundColor Gray
Write-Host ""
# Execute the command
try {
$startTime = Get-Date
& sf @sfArgs
$exitCode = $LASTEXITCODE
$endTime = Get-Date
$duration = $endTime - $startTime
Write-Host ""
Write-Host "⏱️ Test execution completed in $($duration.TotalSeconds.ToString('F1')) seconds" -ForegroundColor Gray
if ($exitCode -eq 0) {
Write-Host ""
Write-Host "✅ Tests completed successfully!" -ForegroundColor Green
if ($cv) {
Write-Host "📊 Code coverage report generated" -ForegroundColor Yellow
}
if ($od) {
Write-Host "📁 Results saved to: $od" -ForegroundColor Cyan
}
} else {
Write-Host ""
Write-Host "❌ Test execution failed with exit code: $exitCode" -ForegroundColor Red
Write-Host "💡 Check test failures above for details" -ForegroundColor Yellow
exit $exitCode
}
} catch {
Write-Host "Error executing sf command: $($_.Exception.Message)" -ForegroundColor Red
exit 1
}