8000 Move test "No Exception!" pattern to 'ShouldBeErrorId' by iSazonov · Pull Request #3161 · PowerShell/PowerShell · GitHub
[go: up one dir, main page]

Skip to content

Move test "No Exception!" pattern to 'ShouldBeErrorId' #3161

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

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion build.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -728,9 +728,11 @@ function Start-PSPester {
}
$startParams = @{binDir=$binDir}

$TestHelpersModule = "$PSScriptRoot/test/tools/Modules/Test.Helpers.psm1"

if(!$FullCLR)
{
$Command += "Import-Module '$Pester'; "
$Command += "Import-Module '$Pester','$TestHelpersModule'; "
}

if ($Unelevate)
Expand Down
43 changes: 10 additions & 33 deletions test/powershell/Host/ConsoleHost.Tests.ps1
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using namespace System.Diagnostics


# Minishell (Singleshell) is a powershell concept.
# Its primary use-case is when somebody executes a scriptblock in the new powershell process.
# The objects are automatically marshelled to the child process and
Expand Down Expand Up @@ -90,30 +91,22 @@ 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"
}
} | ShouldBeErrorId "IncorrectValueForFormatParameter"

}

It "Verify Validate Dollar Error Populated should throw exception" {
$origEA = $ErrorActionPreference
$ErrorActionPreference = "Stop"
try
{
$a = 1,2,3
$a | & $powershell -noprofile -command { wgwg-wrwrhqwrhrh35h3h3}
Throw "Test execution should not reach here!"
}
catch
{
$_.ToString() | Should Match "wgwg-wrwrhqwrhrh35h3h3"
$_.FullyQualifiedErrorId | Should Be "CommandNotFoundException"
$exc = {
$a = 1,2,3
$a | & $powershell -noprofile -command { wgwg-wrwrhqwrhrh35h3h3}
} | ShouldBeErrorId "CommandNotFoundException"
$exc.ToString() | Should Match "wgwg-wrwrhqwrhrh35h3h3"
}
finally
{
Expand All @@ -129,15 +122,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 } } | ShouldBeErrorId "IncorrectValueForFormatParameter"
}
}
Context "CommandLine" {
Expand Down Expand Up @@ -370,15 +355,7 @@ foo
recurse $args
}

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

Expand Down
21 changes: 3 additions & 18 deletions test/powershell/Host/HostUtilities.Tests.ps1
catch
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
$remotingModule = Join-Path $PSScriptRoot "../Common/TestRemoting.psm1"
Import-Module $remotingModule
Import-Module $PSScriptRoot\..\Common\TestRemoting.psm1

Describe "InvokeOnRunspace method argument error handling" -tags "Feature" {

Expand All @@ -9,29 +8,15 @@ 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"
}
} | ShouldBeErrorId "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"
}
{
$_.FullyQualifiedErrorId | Should Be "PSArgumentNullException"
}
} | ShouldBeErrorId "PSArgumentNullException"
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

Describe 'Attributes Test' -Tags "CI" {

BeforeAll {
Expand Down Expand Up @@ -56,15 +57,7 @@ namespace Dummy
[C1]::new().Ensure = "Present"

It 'Error when ValidateSet should be ExceptionWhenSetting' {
try
{
[C1]::new().Ensure = "foo"
throw "Exception expected"
}
catch
{
$_.FullyQualifiedErrorId | Should be 'ExceptionWhenSetting'
}
{ [C1]::new().Ensure = "foo" } | ShouldBeErrorId 'ExceptionWhenSetting'
}
}

Expand All @@ -73,13 +66,7 @@ namespace Dummy
# This call should not throw exception
[C1]::Ensure = "Present"
It 'Error when ValidateSet should be ExceptionWhenSetting'{
try {
[C1]::Ensure = "foo"
throw "Exception expected"
}
catch {
$_.FullyQualifiedErrorId | Should be 'ExceptionWhenSetting'
}
{ [C1]::Ensure = "foo" } | ShouldBeErrorId 'ExceptionWhenSetting'
}
}

Expand All @@ -89,13 +76,7 @@ namespace Dummy
[C1]::new().f = 10
[C1]::new().f = 1
It 'Error when ValidateSet should be ExceptionWhenSetting'{
try {
[C1]::new().f = 20
throw "Exception expected"
}
catch {
$_.FullyQualifiedErrorId | Should be 'ExceptionWhenSetting'
}
{ [C1]::new().f = 20 } | ShouldBeErrorId 'ExceptionWhenSetting'
}
}

Expand All @@ -104,13 +85,7 @@ namespace Dummy
# This call should not throw exception
[C1]::f = 5
It 'Error when ValidateSet should be ExceptionWhenSetting'{
try {
[C1]::f = 20
throw "Exception expected"
}
catch {
$_.FullyQualifiedErrorId | Should be 'ExceptionWhenSetting'
}
{ [C1]::f = 20 } | ShouldBeErrorId 'ExceptionWhenSetting'
}
}

Expand All @@ -120,13 +95,7 @@ namespace Dummy
[C1]::o = "abc"
[C1]::o = 5
It 'Error when ValidateSet should be ExceptionWhenSetting'{
try {
[C1]::o = 1
throw "Exception expected"
}
catch {
$_.FullyQualifiedErrorId | Should be 'ExceptionWhenSetting'
}
{ [C1]::o = 1 } | ShouldBeErrorId 'ExceptionWhenSetting'
}
}

Expand Down Expand Up @@ -165,13 +134,7 @@ namespace Dummy
$c.arg | should be 200
}
It 'Set to string should fail with ExceptionWhenSetting' {
try {
$c.arg = "abc"
throw "Exception expected"
}
catch {
$_.FullyQualifiedErrorId | Should be 'ExceptionWhenSetting'
}
{ $c.arg = "abc" } | ShouldBeErrorId 'ExceptionWhenSetting'
}
}

Expand Down
23 changes: 9 additions & 14 deletions test/powershell/Language/Parser/Ast.Tests.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using Namespace System.Management.Automation.Language


Describe "The SafeGetValue method on AST returns safe values" -Tags "CI" {
It "A hashtable is returned from a HashtableAst" {
$HashtableAstType = [HashtableAst]
Expand All @@ -18,23 +20,16 @@ Describe "The SafeGetValue method on AST returns safe values" -Tags "CI" {
}
It "The proper error is returned when a variable is referenced" {
$ast = { $a }.Ast.Find({$args[0] -is "VariableExpressionAst"},$true)
try {
$exc = {
$ast.SafeGetValue() | out-null
throw "No Exception!"
}
catch {
$_.FullyQualifiedErrorId | Should be "InvalidOperationException"
$_.ToString() | Should Match '\$a'
}
} | ShouldBeErrorId "InvalidOperationException"
$exc.Exception.Message | Should Match '\$a'
}
It "A ScriptBlock AST fails with the proper error" {
{ { 1 }.Ast.SafeGetValue() } | ShouldBeErrorId "InvalidOperationException"
}
It "A ScriptBlock AST fails with the proper error" {
try {
{ 1 }.Ast.SafeGetValue()
throw "No Exception!"
}
catch {
$_.FullyQualifiedErrorId | Should be "InvalidOperationException"
}
{ { 1 }.Ast.SafeGetValue() } | ShouldBeErrorId "InvalidOperationException"
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
$powershellexe = (get-process -id $PID).mainmodule.filename

$powershellexe = Join-Path -Path $PsHome -ChildPath "powershell"

Describe "Clone array" -Tags "CI" {
It "Cast in target expr" {
Expand Down Expand Up @@ -44,7 +45,7 @@ Describe "MSFT:3309783" -Tags "CI" {

It "Run in current process" {
# For good measure, do the same thing in this process
[psobject] | % FullName | Should Be System.Management.Automation.PSObject
[psobject] | ForEach-Object FullName | Should Be System.Management.Automation.PSObject
}

It "Pipe objects derived from PSObject" {
Expand All @@ -59,7 +60,7 @@ Describe "MSFT:3309783" -Tags "CI" {
}

[MyPsObj]::new("abc").psobject.ToString() | Should Be "MyObj: abc"
[MyPsObj]::new("def") | Out-String | % Trim | Should Be "MyObj: def"
[MyPsObj]::new("def") | Out-String | ForEach-Object Trim | Should Be "MyObj: def"
}
}

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

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

Expand Down
Loading
0