Skip to content

Commit

Permalink
Convert ShouldBeErrorId to Should -Throw -ErrorId in PowerShell t…
Browse files Browse the repository at this point in the history
…ests (#6682)

Convert ShouldBeErrorId to Should -Throw -ErrorId in PowerShell tests.
Get rid of try { } catch { } formula to assert that errors were thrown.
Small fixes in tests to obey the new Pester -Parameter syntax.
  • Loading branch information
kalgiz authored and TravisEz13 committed May 17, 2018
1 parent ed282ce commit 2df9aac
Show file tree
Hide file tree
Showing 109 changed files with 604 additions and 1,574 deletions.
51 changes: 10 additions & 41 deletions test/powershell/Host/ConsoleHost.Tests.ps1
Expand Up @@ -93,35 +93,20 @@ Describe "ConsoleHost unit tests" -tags "Feature" {

Context "ShellInterop" {
It "Verify Parsing Error Output Format Single Shell should throw exception" {
try
{
& $powershell -outp blah -comm { $input }
Throw "Test execution should not reach here!"
}
catch
{
$_.FullyQualifiedErrorId | Should -Be "IncorrectValueForFormatParameter"
}
{ & $powershell -outp blah -comm { $input } } | Should -Throw -ErrorId "IncorrectValueForFormatParameter"
}

It "Verify Validate Dollar Error Populated should throw exception" {
$origEA = $ErrorActionPreference
$ErrorActionPreference = "Stop"
try
{
$a = 1,2,3
$a = 1,2,3
$e = {
$a | & $powershell -noprofile -command { wgwg-wrwrhqwrhrh35h3h3}
Throw "Test execution should not reach here!"
}
catch
{
$_.ToString() | Should -Match "wgwg-wrwrhqwrhrh35h3h3"
$_.FullyQualifiedErrorId | Should -Be "CommandNotFoundException"
}
finally
{
$ErrorActionPreference = $origEA
}
} | Should -Throw -ErrorId "CommandNotFoundException" -PassThru

$e.ToString() | Should -Match "wgwg-wrwrhqwrhrh35h3h3"

$ErrorActionPreference = $origEA
}

It "Verify Validate Output Format As Text Explicitly Child Single Shell does not throw" {
Expand All @@ -131,15 +116,7 @@ Describe "ConsoleHost unit tests" -tags "Feature" {
}

It "Verify Parsing Error Input Format Single Shell should throw exception" {
try
{
& $powershell -input blah -comm { $input }
Throw "Test execution should not reach here!"
}
catch
{
$_.FullyQualifiedErrorId | Should -Be "IncorrectValueForFormatParameter"
}
{ & $powershell -input blah -comm { $input } } | Should -Throw -ErrorId "IncorrectValueForFormatParameter"
}
}
Context "CommandLine" {
Expand Down Expand Up @@ -496,15 +473,7 @@ foo
recurse $args
}

try
{
recurse "args"
Throw "Incorrect exception"
}
catch
{
$_.FullyQualifiedErrorId | Should -Be "CallDepthOverflow"
}
{ recurse "args" } | Should -Throw -ErrorId "CallDepthOverflow"
}
}

Expand Down
24 changes: 4 additions & 20 deletions test/powershell/Host/HostUtilities.Tests.ps1
Expand Up @@ -8,29 +8,13 @@ Describe "InvokeOnRunspace method argument error handling" -tags "Feature" {
}

It "Null argument exception should be thrown for null PSCommand argument" {

try
{
[System.Management.Automation.HostUtilities]::InvokeOnRunspace($null, $localRunspace)
throw "InvokeOnRunspace method did not throw expected PSArgumentNullException exception"
}
catch
{
$_.FullyQualifiedErrorId | Should -Be "PSArgumentNullException"
}
{ [System.Management.Automation.HostUtilities]::InvokeOnRunspace($null, $localRunspace) } |
Should -Throw -ErrorId "PSArgumentNullException"
}

It "Null argument exception should be thrown for null Runspace argument" {

try
{
[System.Management.Automation.HostUtilities]::InvokeOnRunspace($command, $null)
throw "InvokeOnRunspace method did not throw expected PSArgumentNullException exception"
}
catch
{
$_.FullyQualifiedErrorId | Should -Be "PSArgumentNullException"
}
{ [System.Management.Automation.HostUtilities]::InvokeOnRunspace($command, $null) } |
Should -Throw -ErrorId "PSArgumentNullException"
}
}

Expand Down
2 changes: 1 addition & 1 deletion test/powershell/Host/TabCompletion/TabCompletion.Tests.ps1
Expand Up @@ -844,7 +844,7 @@ dir -Recurse `

It "Input '<inputStr>' should throw in tab completion" -TestCases $testCases {
param($inputStr, $expected)
$inputStr | ShouldBeErrorId $expected
$inputStr | Should -Throw -ErrorId $expected
}
}

Expand Down
2 changes: 1 addition & 1 deletion test/powershell/Installer/WindowsInstaller.Tests.ps1
Expand Up @@ -33,6 +33,6 @@ Describe "Windows Installer" -Tags "Scenario" {
}
}

$result | Should Not Be $null
$result | Should -Not -Be $null
}
}
Expand Up @@ -260,7 +260,7 @@ Describe 'ValidateSet support a dynamically generated set' -Tag "CI" {
}

It 'Throw if IValidateSetValuesGenerator is not implemented' {
{ Get-TestValidateSet0 -Param1 "TestString" -ErrorAction Stop } | ShouldBeErrorId "Argument"
{ Get-TestValidateSet0 -Param1 "TestString" -ErrorAction Stop } | Should -Throw -ErrorId "Argument"
}

It 'Dynamically generated set works in C# with default (immediate) cache expire' {
Expand All @@ -270,7 +270,7 @@ Describe 'ValidateSet support a dynamically generated set' -Tag "CI" {
It 'Empty dynamically generated set throws in C#' {
$exc = {
Get-TestValidateSet5 -Param1 "TestString1" -ErrorAction Stop
} | ShouldBeErrorId "ParameterArgumentValidationError,Test.Language.TestValidateSetCommand5"
} | Should -Throw -ErrorId "ParameterArgumentValidationError,Test.Language.TestValidateSetCommand5" -PassThru
$exc.Exception.InnerException.ErrorRecord.FullyQualifiedErrorId | Should -BeExactly "ValidateSetGeneratedValidValuesListIsNull"
}
}
Expand Down Expand Up @@ -356,20 +356,20 @@ Describe 'ValidateSet support a dynamically generated set' -Tag "CI" {
}

It 'Get the appropriate error message' {
{Get-TestValidateSetPS4 -Param1 "TestStringWrong" -ErrorAction Stop} | ShouldBeErrorId "ParameterArgumentValidationError,Get-TestValidateSetPS4"
{Get-TestValidateSetPS4 -Param1 "TestStringWrong" -ErrorAction Stop} | Should -Throw -ErrorId "ParameterArgumentValidationError,Get-TestValidateSetPS4"
}

It 'Empty dynamically generated set throws in PowerShell script' {
$exc = {
Get-TestValidateSetPS5 -Param1 "TestString1" -ErrorAction Stop
} | ShouldBeErrorId "ParameterArgumentValidationError,Get-TestValidateSetPS5"
} | Should -Throw -ErrorId "ParameterArgumentValidationError,Get-TestValidateSetPS5" -PassThru
$exc.Exception.InnerException.ErrorRecord.FullyQualifiedErrorId | Should -BeExactly "ValidateSetGeneratedValidValuesListIsNull"
}

It 'Unimplemented valid values generator type throws in PowerShell script' {
{
Get-TestValidateSetPS6 -Param1 "AnyTestString" -ErrorAction Stop
} | ShouldBeErrorId "TypeNotFound"
} | Should -Throw -ErrorId "TypeNotFound"
}

It 'IValidateSetValuesGenerator works in PowerShell module' {
Expand Down
Expand Up @@ -646,7 +646,7 @@ function test-it([EE]$ee){$ee}
}
finally
{
Remove-Module -ea ignore MSFT_2081529
Remove-Module -ErrorAction ignore MSFT_2081529
}
}

Expand Down
Expand Up @@ -231,43 +231,27 @@ Describe "Exception error position" -Tags "CI" {
static f1() { [MSFT_3090412]::bar = 42 }
static f2() { throw "an error in f2" }
static f3() { "".Substring(0, 10) }
static f4() { dir nosuchfile -ea Stop }
static f4() { dir nosuchfile -ErrorAction Stop }
}

It "Setting a property that doesn't exist" {
try {
[MSFT_3090412]::f1()
throw "f1 should have thrown"
} catch {
$_.InvocationInfo.Line | Should -Match ([regex]::Escape('[MSFT_3090412]::bar = 42'))
}
$e = { [MSFT_3090412]::f1() } | Should -Throw -PassThru -ErrorId 'PropertyAssignmentException'
$e.InvocationInfo.Line | Should -Match ([regex]::Escape('[MSFT_3090412]::bar = 42'))
}

It "Throwing an exception" {
try {
[MSFT_3090412]::f2()
throw "f2 should have thrown"
} catch {
$_.InvocationInfo.Line | Should -Match ([regex]::Escape('throw "an error in f2"'))
}
$e = { [MSFT_3090412]::f2() } | Should -Throw -PassThru -ErrorId 'an error in f2'
$e.InvocationInfo.Line | Should -Match ([regex]::Escape('throw "an error in f2"'))
}

It "Calling a .Net method that throws" {
try {
[MSFT_3090412]::f3()
throw "f3 should have thrown"
} catch {
$_.InvocationInfo.Line | Should -Match ([regex]::Escape('"".Substring(0, 10)'))
}
$e = { [MSFT_3090412]::f3() } | Should -Throw -PassThru -ErrorId 'ArgumentOutOfRangeException'
$e.InvocationInfo.Line | Should -Match ([regex]::Escape('"".Substring(0, 10)'))
}

It "Terminating error" {
try {
[MSFT_3090412]::f4()
throw "f4 should have thrown"
} catch {
$_.InvocationInfo.Line | Should -Match ([regex]::Escape('dir nosuchfile -ea Stop'))
}
$e = { [MSFT_3090412]::f4() } | Should -Throw -PassThru -ErrorId 'PathNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand'
$e.InvocationInfo.Line | Should -Match ([regex]::Escape('dir nosuchfile -ErrorAction Stop'))
}
}

Expand Down Expand Up @@ -295,56 +279,26 @@ Describe "Exception from initializer" -Tags "CI" {
}

It "instance member w/ ctor" {
try {
[MSFT_6397334a]::new()
throw "[MSFT_6397334a]::new() should have thrown"
}
catch
{
$e = $_
$e.FullyQualifiedErrorId | Should -BeExactly 'InvalidCastFromStringToInteger'
$e.InvocationInfo.Line | Should -Match 'a = "zz"'
}
$e = { [MSFT_6397334a]::new() } | Should -Throw -ErrorId 'InvalidCastFromStringToInteger' -PassThru
$e.InvocationInfo.Line | Should -Match 'a = "zz"'
}

It "instance member w/o ctor" {
try {
[MSFT_6397334b]::new()
throw "[MSFT_6397334b]::new() should have thrown"
}
catch
{
$e = $_
$e.FullyQualifiedErrorId | Should -BeExactly 'InvalidCastFromStringToInteger'
$e.InvocationInfo.Line | Should -Match 'a = "zz"'
}
$e = { [MSFT_6397334b]::new() } | Should -Throw -ErrorId 'InvalidCastFromStringToInteger' -PassThru
$e.InvocationInfo.Line | Should -Match 'a = "zz"'
}

It "static member w/ ctor" {
try {
$null = [MSFT_6397334c]::a
throw "No Exception!"
}
catch
{
$_.Exception | Should -BeOfType System.TypeInitializationException
$e = $_.Exception.InnerException.InnerException.ErrorRecord
$e.FullyQualifiedErrorId | Should -BeExactly 'InvalidCastFromStringToInteger'
$e.InvocationInfo.Line | Should -Match 'a = "zz"'
}
$e = { $null = [MSFT_6397334c]::a } | Should -Throw -PassThru
$e.Exception | Should -BeOfType 'System.TypeInitializationException'
$e.Exception.InnerException.ErrorRecord.FullyQualifiedErrorId | Should -BeExactly 'InvalidCastFromStringToInteger'
$e.Exception.InnerException.InnerException.ErrorRecord.InvocationInfo.Line | Should -Match 'a = "zz"'
}

It "static member w/o ctor" {
try {
$null = [MSFT_6397334d]::a
throw "No Exception!"
}
catch
{
$_.Exception | Should -BeOfType System.TypeInitializationException
$e = $_.Exception.InnerException.InnerException.ErrorRecord
$e.FullyQualifiedErrorId | Should -BeExactly 'InvalidCastFromStringToInteger'
$e.InvocationInfo.Line | Should -Match 'a = "zz"'
}
$e = { $null = [MSFT_6397334d]::a } | Should -Throw -PassThru
$e.Exception | Should -BeOfType System.TypeInitializationException
$e.Exception.InnerException.InnerException.ErrorRecord.FullyQualifiedErrorId | Should -BeExactly 'InvalidCastFromStringToInteger'
$e.Exception.InnerException.InnerException.ErrorRecord.InvocationInfo.Line | Should -Match 'a = "zz"'
}
}
Expand Up @@ -58,7 +58,7 @@ Describe "ComparisonOperator" -tag "CI" {
@{operator = "-isnot"; type = "[foo]"; expectedError='TypeNotFound,Microsoft.PowerShell.Commands.InvokeExpressionCommand'}
) {
param($operator, $type, $expectedError)
{ Invoke-Expression "'Hello' $operator $type" } | ShouldBeErrorId $expectedError
{ Invoke-Expression "'Hello' $operator $type" } | Should -Throw -ErrorId $expectedError
}

It "Should succeed in comparing type: <lhs> <operator> <rhs>" -TestCases @(
Expand Down
2 changes: 1 addition & 1 deletion test/powershell/Language/Operators/SplitOperator.Tests.ps1
Expand Up @@ -210,7 +210,7 @@ Describe "Split Operator" -Tags CI {
}

It "Binary split operator doesn't works with RegexMatch,SimpleMatch" {
{ "abc" -split "B", 0, 'RegexMatch,SimpleMatch' } | ShouldBeErrorId "InvalidSplitOptionCombination"
{ "abc" -split "B", 0, 'RegexMatch,SimpleMatch' } | Should -Throw -ErrorId "InvalidSplitOptionCombination"
}
}

Expand Down
Expand Up @@ -172,21 +172,15 @@ Describe "Assign readonly/constant variables" -Tags "CI" {

Describe "Attribute error position" -Tags "CI" {
It "Ambiguous overloads" {
try
{
$e = {
& {
param(
[ValidateNotNull(1,2,3,4)]
$param
)
}
throw "Should have thrown"
}
catch
{
$_.InvocationInfo.Line | Should -Match ValidateNotNull
$_.FullyQualifiedErrorId | Should -Be MethodCountCouldNotFindBest
}
} | Should -Throw -PassThru -ErrorId 'MethodCountCouldNotFindBest'
$e.InvocationInfo.Line | Should -Match ValidateNotNull
}
}

Expand Down

0 comments on commit 2df9aac

Please sign in to comment.