I've got a build script (PowerShell 4 on Windows 2012 R2) that runs NUnit in a background job and returns NUnit's output. This output is collected in a Collections.Generic.List[string]
.
$nunitJob = Start-Job -ScriptBlock {
param(
[string]
$BinRoot,
[string]
$NUnitConsolePath,
[string[]]
$NUnitParams,
[string]
$Verbose
)
Set-Location -Path $BinRoot
$VerbosePreference = $Verbose
Write-Verbose -Message ('{0} {1}' -f $NUnitConsolePath,($NUnitParams -join ' '))
& $NUnitConsolePath $NUnitParams 2>&1
$LASTEXITCODE
} -ArgumentList $binRoot,$nunitConsolePath,$nunitParams,$VerbosePreference
$nunitJob | Wait-Job -Timeout ($timeoutMinutes * 60) | Out-Null
$jobKilled = $false
if( $nunitJob.JobStateInfo.State -eq [Management.Automation.JobState]::Running )
{
$jobKilled = $true
$errMsg = 'Killing {0} tests: exceeded {1} minute timeout.' -f $assembly.Name,$timeoutMinutes
Write-Error -Message $errMsg
}
$output = New-Object 'Collections.Generic.List[string]'
$nunitJob |
Stop-Job -PassThru |
Receive-Job |
ForEach-Object {
if( -not $_ )
{
[void]$output.Add( '' )
return
}
switch -Regex ( $_ )
{
'^Tests run: (\d+), Errors: (\d+), Failures: (\d+), Inconclusive: (\d+), Time: ([\d\.]+) seconds$'
{
$testsRun = $Matches[1]
$errors = $Matches[2]
$failures = $Matches[3]
$inconclusive = $Matches[4]
$duration = New-Object 'TimeSpan' 0,0,$Matches[5]
break
}
'^ Not run: (\d+), Invalid: (\d+), Ignored: (\d+), Skipped: (\d+)$'
{
$notRun = $Matches[1]
$invalid = $Matches[2]
$ignored = $Matches[3]
$skipped = $Matches[4]
break
}
}
# Error happens here:
[void] $output.Add( $_ )
}
Intermittently, our build will fail with this error:
Cannot find an overload for "Add" and the argument count: "1".
At line:XXXXX char:XXXXX
+ [void] $output.Add( $_ )
+ ~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodException
+ FullyQualifiedErrorId : MethodCountCouldNotFindBest
Any idea why PowerShell would not be able to find List[string]
's Add
method?
I've opened a console Window and played around with passing different typed objects to Add
without getting an error.
> $o = New-Object 'Collections.Generic.List[string]'
> $o.Add( '' )
> $o.Add( $null )
> $o.Add( 1 )
> $o
1