fixed ps scripts

This commit is contained in:
Reynold Lariza
2025-08-28 22:30:40 +08:00
parent 833eb9448a
commit f250f81753
6 changed files with 313 additions and 447 deletions

View File

@@ -8,41 +8,41 @@
A user-friendly wrapper around 'sf apex run test' that simplifies test execution
with better formatting, intelligent defaults, and comprehensive reporting options.
.PARAMETER TestClasses
.PARAMETER tc
Comma-separated list of test class names to run
.PARAMETER TestMethods
.PARAMETER tm
Comma-separated list of specific test methods to run (format: ClassName.methodName)
.PARAMETER TestLevel
.PARAMETER tl
Test level to run (RunLocalTests, RunAllTestsInOrg, RunSpecifiedTests)
.PARAMETER Suite
.PARAMETER st
Test suite name to run
.PARAMETER Coverage
.PARAMETER cv
Generate code coverage report
.PARAMETER Wait
.PARAMETER wt
Wait time in minutes for test execution (default: 10)
.PARAMETER o
.PARAMETER to
Target org username or alias (uses default if not specified)
.PARAMETER OutputDir
.PARAMETER od
Directory to store test results and reports
.PARAMETER VerboseOutput
.PARAMETER ve
Enable verbose test output
.PARAMETER Help
.PARAMETER hp
Show this help message
.EXAMPLE
.\sf-test-run.ps1 -TestClasses "AccountTest,ContactTest"
.\sf-test-run.ps1 -TestLevel "RunLocalTests" -Coverage
.\sf-test-run.ps1 -TestMethods "AccountTest.testCreate,ContactTest.testUpdate"
.\sf-test-run.ps1 -Suite "AllTests" -Wait 15 -o "staging"
.\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
@@ -50,56 +50,27 @@
#>
param(
[Parameter(ParameterSetName="Classes")]
[string]$TestClasses,
[Parameter(ParameterSetName="Methods")]
[string]$TestMethods,
[Parameter(ParameterSetName="Level")]
[string]$tc,
[string]$tm,
[ValidateSet("RunLocalTests", "RunAllTestsInOrg", "RunSpecifiedTests")]
[string]$TestLevel,
[Parameter(ParameterSetName="Suite")]
[string]$Suite,
[Parameter(ParameterSetName="Classes")]
[Parameter(ParameterSetName="Methods")]
[Parameter(ParameterSetName="Level")]
[Parameter(ParameterSetName="Suite")]
[switch]$Coverage,
[Parameter(ParameterSetName="Classes")]
[Parameter(ParameterSetName="Methods")]
[Parameter(ParameterSetName="Level")]
[Parameter(ParameterSetName="Suite")]
[int]$Wait = 10,
[Parameter(ParameterSetName="Classes")]
[Parameter(ParameterSetName="Methods")]
[Parameter(ParameterSetName="Level")]
[Parameter(ParameterSetName="Suite")]
[string]$o,
[Parameter(ParameterSetName="Classes")]
[Parameter(ParameterSetName="Methods")]
[Parameter(ParameterSetName="Level")]
[Parameter(ParameterSetName="Suite")]
[string]$OutputDir,
[Parameter(ParameterSetName="Classes")]
[Parameter(ParameterSetName="Methods")]
[Parameter(ParameterSetName="Level")]
[Parameter(ParameterSetName="Suite")]
[switch]$VerboseOutput,
[Parameter(ParameterSetName="Help", Mandatory=$true)]
[Alias("hp")]
[switch]$Help
[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 ($Help) {
if ($hp) {
Get-Help $MyInvocation.MyCommand.Path -Detailed
exit 0
}
@@ -140,17 +111,17 @@ if (-not (Test-SalesforceCLI)) {
}
# Validate that at least one test specification is provided
$testSpecCount = @($TestClasses, $TestMethods, $TestLevel, $Suite | Where-Object { $_ }).Count
$testSpecCount = @($tc, $tm, $tl, $st | Where-Object { $_ }).Count
if ($testSpecCount -eq 0) {
Write-Host "Error: Must specify one of: -TestClasses, -TestMethods, -TestLevel, or -Suite" -ForegroundColor Red
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 -TestClasses `"AccountTest,ContactTest`"" -ForegroundColor Gray
Write-Host " .\sf-test-run.ps1 -TestLevel `"RunLocalTests`" -Coverage" -ForegroundColor Gray
Write-Host " .\sf-test-run.ps1 -TestMethods `"AccountTest.testCreate`"" -ForegroundColor Gray
Write-Host " .\sf-test-run.ps1 -Suite `"AllTests`"" -ForegroundColor Gray
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 -Help for detailed usage information." -ForegroundColor Yellow
Write-Host "Use -hp for detailed usage information." -ForegroundColor Yellow
exit 1
}
@@ -158,48 +129,48 @@ if ($testSpecCount -eq 0) {
$sfArgs = @("apex", "run", "test")
# Add test specification
if ($TestClasses) {
if ($tc) {
$sfArgs += "--class-names"
$sfArgs += $TestClasses
Write-Host "Running test classes: $TestClasses" -ForegroundColor Green
} elseif ($TestMethods) {
$sfArgs += $tc
Write-Host "Running test classes: $tc" -ForegroundColor Green
} elseif ($tm) {
$sfArgs += "--tests"
$sfArgs += $TestMethods
Write-Host "Running test methods: $TestMethods" -ForegroundColor Green
} elseif ($TestLevel) {
$sfArgs += $tm
Write-Host "Running test methods: $tm" -ForegroundColor Green
} elseif ($tl) {
$sfArgs += "--test-level"
$sfArgs += $TestLevel
Write-Host "Running tests at level: $TestLevel" -ForegroundColor Green
} elseif ($Suite) {
$sfArgs += $tl
Write-Host "Running tests at level: $tl" -ForegroundColor Green
} elseif ($st) {
$sfArgs += "--suite-names"
$sfArgs += $Suite
Write-Host "Running test suite: $Suite" -ForegroundColor Green
$sfArgs += $st
Write-Host "Running test suite: $st" -ForegroundColor Green
}
# Add optional parameters
if ($o) {
if ($to) {
$sfArgs += "--target-org"
$sfArgs += $o
Write-Host "Target org: $o" -ForegroundColor Cyan
$sfArgs += $to
Write-Host "Target org: $to" -ForegroundColor Cyan
}
if ($Wait -ne 10) {
if ($wt -ne 10) {
$sfArgs += "--wait"
$sfArgs += $Wait.ToString()
$sfArgs += $wt.ToString()
}
if ($Coverage) {
if ($cv) {
$sfArgs += "--code-coverage"
Write-Host "Code coverage: Enabled" -ForegroundColor Yellow
}
if ($OutputDir) {
if (-not (Test-Path $OutputDir)) {
Write-Host "Creating output directory: $OutputDir" -ForegroundColor Yellow
New-Item -ItemType Directory -Path $OutputDir -Force | Out-Null
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 += $OutputDir
$sfArgs += $od
}
# Always use detailed output for better reporting
@@ -208,7 +179,7 @@ $sfArgs += "--result-format"
$sfArgs += "human"
# Add verbose flag if requested
if ($VerboseOutput) {
if ($ve) {
$sfArgs += "--verbose"
}
@@ -237,12 +208,12 @@ try {
Write-Host ""
Write-Host "✅ Tests completed successfully!" -ForegroundColor Green
if ($Coverage) {
if ($cv) {
Write-Host "📊 Code coverage report generated" -ForegroundColor Yellow
}
if ($OutputDir) {
Write-Host "📁 Results saved to: $OutputDir" -ForegroundColor Cyan
if ($od) {
Write-Host "📁 Results saved to: $od" -ForegroundColor Cyan
}
} else {
Write-Host ""