Skip to content

Commit 3628707

Browse files
authored
Shared/livetest (#20714)
* Modified error messages for easy troubleshooting locally and added ConfirmPreference to innter scriptblock.
1 parent 65ddc9e commit 3628707

File tree

1 file changed

+14
-11
lines changed

1 file changed

+14
-11
lines changed

tools/TestFx/Live/LiveTestUtility.psm1

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,7 @@ New-Variable -Name LiveTestRawCsvFile -Value (Join-Path -Path $script:LiveTestRa
4949

5050
function InitializeLiveTestModule {
5151
[CmdletBinding()]
52-
param (
53-
[Parameter(Mandatory, Position = 0)]
54-
[ValidateNotNullOrEmpty()]
55-
[string] $ModuleName
56-
)
52+
param ()
5753

5854
if (!(Test-Path -LiteralPath $script:LiveTestAnalysisDirectory -PathType Container)) {
5955
New-Item -Path $script:LiveTestAnalysisDirectory -ItemType Directory -Force
@@ -235,7 +231,7 @@ function Invoke-LiveTestScenario {
235231

236232
do {
237233
try {
238-
$ScenarioScript.InvokeWithContext($null, [PSVariable]::new("ErrorActionPreference", "Stop"), $snrResourceGroup)
234+
$ScenarioScript.InvokeWithContext($null, @([psvariable]::new("ErrorActionPreference", "Stop"), [psvariable]::new("ConfirmPreference", "None")), $snrResourceGroup)
239235
Write-Host "##[section]Successfully executed the live scenario `"$Name`"." -ForegroundColor Green
240236
break
241237
}
@@ -246,23 +242,30 @@ function Invoke-LiveTestScenario {
246242

247243
$snrInvocationInfo = $snrErrorRecord.InvocationInfo
248244
if ($null -ne $snrInvocationInfo) {
249-
$snrErrorDetails += " thrown at line:$($snrInvocationInfo.ScriptLineNumber) char:$($snrInvocationInfo.OffsetInLine) by cmdlet `"$($snrInvocationInfo.InvocationName)`" on `"$($snrInvocationInfo.Line.ToString().Trim())`"."
245+
$snrScriptName = Split-Path -Path $snrInvocationInfo.ScriptName -Leaf -ErrorAction SilentlyContinue
246+
if ($snrScriptName -eq "Assert.ps1") {
247+
Write-Host "##[error]Exception was thrown from the Assert.ps1. The stack trace is:" -ForegroundColor Red
248+
Write-Host "##[error]$($snrErrorRecord.ScriptStackTrace)" -ForegroundColor Red
249+
}
250+
else {
251+
$snrErrorDetails += " thrown at line:$($snrInvocationInfo.ScriptLineNumber) char:$($snrInvocationInfo.OffsetInLine) by cmdlet '$($snrInvocationInfo.MyCommand)' on '$($snrInvocationInfo.Line.ToString().Trim())'"
252+
}
250253
}
251254

252255
$snrRetryErrors += $snrErrorDetails
253256

254257
if ($snrRetryCount -lt $script:ScenarioMaxRetryCount) {
255258
$snrRetryCount++
256259
$exponentialDelay = [Math]::Min((1 -shl ($snrRetryCount - 1)) * [int](Get-Random -Minimum ($script:ScenarioDelay * 0.8) -Maximum ($script:ScenarioDelay * 1.2)), $script:ScenarioMaxDelay)
257-
Write-Host "##[warning]Error occurred when executing the live scenario `"$Name`" with error message `"$snrErrorMessage`"." -ForegroundColor Yellow
260+
Write-Host "##[warning]Error occurred when executing the live scenario `"$Name`" with error details `"$snrErrorDetails`"." -ForegroundColor Yellow
258261
Write-Host "##[warning]Live test will retry automatically in $exponentialDelay seconds." -ForegroundColor Yellow
259262
Write-Host
260263

261264
Start-Sleep -Seconds $exponentialDelay
262265
Write-Host "##[warning]Retry #$snrRetryCount to execute the live scenario `"$Name`"." -ForegroundColor Yellow
263266
}
264267
else {
265-
Write-Host "##[error]Failed to execute the live scenario `"$Name`" with error message `"$snrErrorMessage`"." -ForegroundColor Red
268+
Write-Host "##[error]Failed to execute the live scenario `"$Name`" with error details `"$snrErrorDetails`"." -ForegroundColor Red
266269
Write-Host
267270
$snrCsvData.IsSuccess = $false
268271
$snrCsvData.Errors = ConvertToLiveTestJsonErrors -Errors $snrRetryErrors
@@ -274,7 +277,7 @@ function Invoke-LiveTestScenario {
274277
}
275278
catch {
276279
$snrErrorMessage = $_.Exception.Message
277-
Write-Host "##[error]Error occurred when executing the live scenario `"$Name`" with error message `"$snrErrorMessage`"" -ForegroundColor Red
280+
Write-Host "##[error]Error occurred when executing the live scenario `"$Name`" with error details `"$snrErrorDetails`"" -ForegroundColor Red
278281
Write-Host
279282
$snrCsvData.IsSuccess = $false
280283
$snrCsvData.Errors = ConvertToLiveTestJsonErrors -Errors $snrErrorMessage
@@ -327,4 +330,4 @@ function ConvertToLiveTestJsonErrors {
327330
(ConvertTo-Json $errorsObj -Compress)
328331
}
329332

330-
InitializeLiveTestModule -ModuleName $ModuleName
333+
InitializeLiveTestModule

0 commit comments

Comments
 (0)