-
-
Notifications
You must be signed in to change notification settings - Fork 473
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Should-Throw
: How to negate the evaluation?
#2528
Comments
From #testing:
Did you find a scenario where |
Right... remember that. 🙂 Didn't thought it would apply here. 😉
Only when automating conversion of test scripts. E.g. only in SqlServerDsc there are 788 lines using the syntax |
AFAIK it's not set in stone, but by starting without it we get the discussion. 🙂 If it can be covered by a simple migration step trough docs, made even easier by your automated module, that'd be cool. The easiest conversion is probably to replace # Defining outside to be available for test in the end
$scriptBlock = { throw 'one' }
# Intentionally failing to confirm they actually executed without syntax errors
$source = {
$scriptBlock | Should -Not -Throw
{ throw 'two' } | Should -Not -Throw -Because 'why not'
{ throw 'three' } |
Should -Not -Throw -Because 'why not' -ExpectedMessage 'ignore'
Should -Not -Throw -Because 'why not' -ExpectedMessage 'ignore' -ActualValue { throw 'four' }
# I'm sure there's one out there :D
"throw 'five'" | ForEach-Object { [scriptblock]::Create($_) } | should -Throw -Not
# Fails on second block. Hope nobody use this
{ 'works' }, { throw 'six' } | Should -Throw -Not
}
$Ast = $source.Ast
$todo = $Ast.FindAll({ param($a) $a -is [System.Management.Automation.Language.CommandAst] -and $a.GetCommandName() -eq 'Should' }, $true)
$todo | ForEach-Object {
$pipelineElements = $_.Parent.PipelineElements
$actualValue = $null
if ($pipelineElements.Count -eq 1) {
# Should -Not -Throw -ActualValue { something }
# AFAIK positional doesn't work so being naive. 99% probably use pipeline input like docs
for ($i = 0; $i -lt $_.CommandElements.Count; $i++) {
$p = $_.CommandElements[$i]
if ($p -is [System.Management.Automation.Language.CommandParameterAst] -and $p.ParameterName -like 'A*') {
$actualValue =$_.CommandElements[$i + 1].Extent.Text
break
}
}
} elseif ($pipelineElements.Count -gt 1 -and $pipelineElements[-1] -eq $_) {
# something | maybe something else | Should -Not -Throw
$replaceStart = $_.Extent.StartOffset - $_.Parent.Extent.StartOffset
$actualValue = $_.Parent.Extent.Text.Remove($replaceStart) -replace '\s*\|\s*$'
} else {
throw 'What sorcery is this?'
}
# New parent extent
$newParentExtentText = "$($actualValue) | ForEach-Object { & `$_ } | Out-Null # TODO: Cleanup. Automatic conversion from Should -Not -Throw"
# Test
[System.Management.Automation.Language.Parser]::ParseInput($newParentExtentText, [ref]$null, [ref]$null).GetScriptBlock().Invoke()
} |
Thanks for the code example, and the suggestions for use of Btw. using It 'Should not throw, passing to ForEach-Object' {
{
Write-Error -Message 'MockErrorMessage' -ErrorId 'MockErrorId' -Category 'InvalidOperation' -TargetObject 'MockTargetObject' -ErrorAction 'Stop'
} | ForEach-Object -Process { & $_ } | Out-Null
}
It 'Should not throw, without any scriptblock' {
Write-Error -Message 'MockErrorMessage' -ErrorId 'MockErrorId' -Category 'InvalidOperation' -TargetObject 'MockTargetObject' -ErrorAction 'Stop'
} Outputs:
|
You can invoke a scriptblock-variable with call operator Only reason I didn't use it was the edge case of multiple script blocks, which you can probably ignore. Running the code directly will send data to StandardOutput in the Test-object. Not a big deal unless there's a lot of output, but might as well assign it to null like |
Then I go with |
Checklist
What is the issue?
In Pester 5 syntax we could say
Should -Not -Throw
. I can't find a way to negate theShould-Throw
command as it does not have aNot
parameter or a commandShould-NotThrow
.Expected Behavior
Possibility to evaluate a command to not throw.
Steps To Reproduce
not applicable
Describe your environment
Possible Solution?
Either add a
Not
parameter to the commandShould-Throw
or add a new commandShould-NotThrow
?The text was updated successfully, but these errors were encountered: