Skip to content

Commit 409ab74

Browse files
iSazonovdaxian-dbw
authored andcommitted
Fix GetType() bad pattern and related issues in tests (PowerShell#3134)
* Fix GetType() bad pattern and related issues in tests $var.GetType() can raise an exception in tests so we should check $var before make the call. A large part of the tests does not make this check. I start with searching ".GetType()" but discovered many related issues in tests (reduntant and unneeded tests, "throw" bad pattens, bugs, formattings (sorry!) and so on) - I had to fix them too. * Fix after code review * Second wave of migration GetType() -> BeOfType Removed 'GetType().Name' patterns.
1 parent d801b75 commit 409ab74

62 files changed

Lines changed: 383 additions & 445 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

test/powershell/Host/ConsoleHost.Tests.ps1

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,22 @@ Describe 'minishell for native executables' -Tag 'CI' {
1616

1717
It 'gets a hashtable object from minishell' {
1818
$output = & $powershell -noprofile { @{'a' = 'b'} }
19-
($output | measure).Count | Should Be 1
20-
($output.GetType().Name) | Should Be 'Hashtable'
19+
($output | Measure-Object).Count | Should Be 1
20+
$output | Should BeOfType 'Hashtable'
2121
$output['a'] | Should Be 'b'
2222
}
2323

2424
It 'gets the error stream from minishell' {
2525
$output = & $powershell -noprofile { Write-Error 'foo' } 2>&1
26-
($output | measure).Count | Should Be 1
27-
($output.GetType().Name) | Should Be 'ErrorRecord'
26+
($output | Measure-Object).Count | Should Be 1
27+
$output | Should BeOfType 'System.Management.Automation.ErrorRecord'
2828
$output.FullyQualifiedErrorId | Should Be 'Microsoft.PowerShell.Commands.WriteErrorException'
2929
}
3030

3131
It 'gets the information stream from minishell' {
3232
$output = & $powershell -noprofile { Write-Information 'foo' } 6>&1
33-
($output.GetType().Name) | Should Be 'InformationRecord'
33+
($output | Measure-Object).Count | Should Be 1
34+
$output | Should BeOfType 'System.Management.Automation.InformationRecord'
3435
$output | Should Be 'foo'
3536
}
3637
}
@@ -150,7 +151,7 @@ Describe "ConsoleHost unit tests" -tags "Feature" {
150151
}
151152
foreach ($x in "--help", "-help", "-h", "-?", "--he", "-hel", "--HELP", "-hEl") {
152153
It "Accepts '$x' as a parameter for help" {
153-
& $powershell -noprofile $x | ?{ $_ -match "PowerShell[.exe] -Help | -? | /?" } | Should Not BeNullOrEmpty
154+
& $powershell -noprofile $x | Where-Object { $_ -match "PowerShell[.exe] -Help | -? | /?" } | Should Not BeNullOrEmpty
154155
}
155156
}
156157

test/powershell/Language/Classes/Scripting.Classes.Exceptions.Tests.ps1

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -322,11 +322,11 @@ Describe "Exception from initializer" -Tags "CI" {
322322
It "static member w/ ctor" {
323323
try {
324324
$null = [MSFT_6397334c]::a
325-
throw "[MSFT_6397334c]::a should have thrown"
325+
throw "No Exception!"
326326
}
327327
catch
328328
{
329-
$_.Exception.GetType().FullName | Should Be System.TypeInitializationException
329+
$_.Exception | Should BeOfType System.TypeInitializationException
330330
$e = $_.Exception.InnerException.InnerException.ErrorRecord
331331
$e.FullyQualifiedErrorId | Should Be InvalidCastFromStringToInteger
332332
$e.InvocationInfo.Line | Should Match 'a = "zz"'
@@ -336,11 +336,11 @@ Describe "Exception from initializer" -Tags "CI" {
336336
It "static member w/o ctor" {
337337
try {
338338
$null = [MSFT_6397334d]::a
339-
throw "[MSFT_6397334d]::a should have thrown"
339+
throw "No Exception!"
340340
}
341341
catch
342342
{
343-
$_.Exception.GetType().FullName | Should Be System.TypeInitializationException
343+
$_.Exception | Should BeOfType System.TypeInitializationException
344344
$e = $_.Exception.InnerException.InnerException.ErrorRecord
345345
$e.FullyQualifiedErrorId | Should Be InvalidCastFromStringToInteger
346346
$e.InvocationInfo.Line | Should Match 'a = "zz"'

test/powershell/Language/Classes/scripting.Classes.inheritance.tests.ps1

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,10 @@ Describe 'Classes inheritance syntax' -Tags "CI" {
7272
[A]::b = [B]::new()
7373
try {
7474
[A]::b = "bla"
75+
throw "No Exception!"
7576
} catch {
76-
$_.Exception.GetType().Name | Should Be SetValueInvocationException
77-
return
77+
$_.Exception | Should BeOfType 'System.Management.Automation.SetValueInvocationException'
7878
}
79-
# Fail, if come heres
80-
'' | Should Be "Exception expected"
8179
}
8280
}
8381

@@ -419,12 +417,10 @@ Describe 'Classes inheritance ctors' -Tags "CI" {
419417

420418
try {
421419
[B]::new(101)
420+
throw "No Exception!"
422421
} catch {
423-
$_.Exception.GetType().Name | Should Be MethodException
424-
return
422+
$_.Exception | Should BeOfType "System.Management.Automation.MethodException"
425423
}
426-
# Fail
427-
'' | Should Be "Exception expected"
428424
}
429425

430426
It 'call default base ctor implicitly' {

test/powershell/Language/Interop/DotNet/DotNetAPI.Tests.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@ Describe "DotNetAPI" -Tags "CI" {
2020
It "Should be able to create a new instance of a .Net object" {
2121
[System.Guid]$guidVal = [System.Guid]::NewGuid()
2222

23-
$guidVal.GetType().Name | Should Be "Guid"
23+
$guidVal | Should BeOfType Guid
2424
}
2525
}

test/powershell/Language/LanguageTestSupport.psm1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ function Test-ErrorStmt
126126
$ast = Get-ParseResults $src -Ast
127127
$asts = @(Flatten-Ast $ast.EndBlock.Statements[0])
128128

129-
It 'Type is ErrorStatementAst' { $asts[0].GetType() | Should Be System.Management.Automation.Language.ErrorStatementAst }
129+
It 'Type is ErrorStatementAst' { $asts[0] | Should BeOfType System.Management.Automation.Language.ErrorStatementAst }
130130
It "`$asts.count" { $asts.Count | Should Be ($a.Count + 1) }
131131
It "`$asts[0].Extent.Text" { $asts[0].Extent.Text | Should Be $errorStmtExtent }
132132
for ($i = 0; $i -lt $a.Count; ++$i)
@@ -159,7 +159,7 @@ function Test-Ast
159159
$ast = Get-ParseResults $src -Ast
160160
$ast = $ast.EndBlock.Statements[0]
161161
Context "Ast Validation: <<$src>>" {
162-
$ast.GetType() | Should Be System.Management.Automation.Language.ErrorStatementAst
162+
$ast | Should BeOfType System.Management.Automation.Language.ErrorStatementAst
163163
$ast.Flags.ContainsKey($flagName) | Should be $true
164164

165165
$asts = @(Flatten-Ast $ast.Flags[$flagName].Item2)

test/powershell/Language/Parser/Ast.Tests.ps1

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,22 @@ Describe "The SafeGetValue method on AST returns safe values" -Tags "CI" {
55
$HtAst = {
66
@{ one = 1 }
77
}.ast.Find({$args[0] -is $HashtableAstType}, $true)
8-
$HtAst.SafeGetValue().GetType().Name | Should be Hashtable
8+
$HtAst | Should Not BeNullOrEmpty
9+
$HtAst.SafeGetValue() | Should BeOfType "Hashtable"
910
}
1011
It "An Array is returned from a LiteralArrayAst" {
1112
$ArrayAstType = [ArrayLiteralAst]
1213
$ArrayAst = {
1314
@( 1,2,3,4)
1415
}.ast.Find({$args[0] -is $ArrayAstType}, $true)
15-
$ArrayAst.SafeGetValue().GetType().Name | Should be "Object[]"
16+
$ArrayAst | Should Not BeNullOrEmpty
17+
,$ArrayAst.SafeGetValue() | Should BeOfType "Object[]"
1618
}
1719
It "The proper error is returned when a variable is referenced" {
1820
$ast = { $a }.Ast.Find({$args[0] -is "VariableExpressionAst"},$true)
1921
try {
2022
$ast.SafeGetValue() | out-null
21-
Throw "Execution Succeeded"
23+
throw "No Exception!"
2224
}
2325
catch {
2426
$_.FullyQualifiedErrorId | Should be "InvalidOperationException"
@@ -28,7 +30,7 @@ Describe "The SafeGetValue method on AST returns safe values" -Tags "CI" {
2830
It "A ScriptBlock AST fails with the proper error" {
2931
try {
3032
{ 1 }.Ast.SafeGetValue()
31-
Throw "Execution Succeeded"
33+
throw "No Exception!"
3234
}
3335
catch {
3436
$_.FullyQualifiedErrorId | Should be "InvalidOperationException"

test/powershell/Language/Parser/BNotOperator.Tests.ps1

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,25 +39,25 @@ Describe "bnot on enums" -Tags "CI" {
3939
It "max - 1" {
4040
$res = -bnot $enumType::MaxMinus1
4141
$res | Should Be $enumType::MinPlus1
42-
$res.GetType() | Should Be $enumType
42+
$res | Should BeOfType $enumType
4343
}
4444

4545
It "min + 1" {
4646
$res = -bnot $enumType::MinPlus1
4747
$res | Should Be $enumType::MaxMinus1
48-
$res.GetType() | Should Be $enumType
48+
$res | Should BeOfType $enumType
4949
}
5050

5151
It "Max" {
5252
$res = -bnot $enumType::Max
5353
$res | Should Be $enumType::Min
54-
$res.GetType() | Should Be $enumType
54+
$res | Should BeOfType $enumType
5555
}
5656

5757
It "Min" {
5858
$res = -bnot $enumType::Min
5959
$res | Should Be $enumType::Max
60-
$res.GetType() | Should Be $enumType
60+
$res | Should BeOfType $enumType
6161
}
6262
}
6363
}
@@ -90,51 +90,51 @@ Describe "bnot on integral types" -Tags "CI" {
9090
It "max - 1" {
9191
$res = -bnot $maxMinus1
9292
$res | Should Be (-bnot [int]$maxMinus1)
93-
$res.GetType() | Should Be $expectedResultType
93+
$res | Should BeOfType $expectedResultType
9494
}
9595

9696
It "min + 1" {
9797
$res = -bnot $minPlus1
9898
$res | Should Be (-bnot [int]$minPlus1)
99-
$res.GetType() | Should Be $expectedResultType
99+
$res | Should BeOfType $expectedResultType
100100
}
101101

102102
It "max" {
103103
$res = -bnot $max
104104
$res | Should Be (-bnot [int]$max)
105-
$res.GetType() | Should Be $expectedResultType
105+
$res | Should BeOfType $expectedResultType
106106
}
107107

108108
It "min" {
109109
$res = -bnot $min
110110
$res | Should Be (-bnot [int]$min)
111-
$res.GetType() | Should Be $expectedResultType
111+
$res | Should BeOfType $expectedResultType
112112
}
113113
return
114114
}
115115

116116
It "max - 1" {
117117
$res = -bnot $maxMinus1
118118
$res | Should Be $minPlus1
119-
$res.GetType() | Should Be $expectedResultType
119+
$res | Should BeOfType $expectedResultType
120120
}
121121

122122
It "min + 1" {
123123
$res = -bnot $minPlus1
124124
$res | Should Be $maxMinus1
125-
$res.GetType() | Should Be $expectedResultType
125+
$res | Should BeOfType $expectedResultType
126126
}
127127

128128
It "max" {
129129
$res = -bnot $max
130130
$res | Should Be $min
131-
$res.GetType() | Should Be $expectedResultType
131+
$res | Should BeOfType $expectedResultType
132132
}
133133

134134
It "min" {
135135
$res = -bnot $min
136136
$res | Should Be $max
137-
$res.GetType() | Should Be $expectedResultType
137+
$res | Should BeOfType $expectedResultType
138138
}
139139
}
140140
}

test/powershell/Language/Scripting/ActionPreference.Tests.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@
1919

2020
It '$err.Count' { $err.Count | Should Be 1 }
2121
It '$err[0] should not be $null' { $err[0] | Should Not Be $null }
22-
It '$err[0].GetType().Name' { $err[0].GetType().Name | Should Be ActionPreferenceStopException }
22+
It '$err[0].GetType().Name' { $err[0] | Should BeOfType "System.Management.Automation.ActionPreferenceStopException" }
2323
It '$err[0].ErrorRecord' { $err[0].ErrorRecord | Should not BeNullOrEmpty }
24-
It '$err[0].ErrorRecord.Exception.GetType().Name' { $err[0].ErrorRecord.Exception.GetType().Name | Should Be ItemNotFoundException }
24+
It '$err[0].ErrorRecord.Exception.GetType().Name' { $err[0].ErrorRecord.Exception | Should BeOfType "System.Management.Automation.ItemNotFoundException" }
2525
}
2626

2727
It 'ActionPreference Ignore Works' {

test/powershell/Language/Scripting/Debugging/DebuggerScriptTests.Tests.ps1

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -290,17 +290,13 @@ Describe "Unit tests for various script breakpoints" -Tags "CI" {
290290
{
291291
& $command
292292

293-
It "Script should fail to verify exception" {
294-
$false | Should Be $true
295-
}
293+
throw "No Exception!"
296294
}
297295
catch
298296
{
299-
$type = $_.Exception.GetType().Name
300-
301-
It "Script failed expected exception" {
302-
$type | Should Be $exception
303-
}
297+
It "Script failed expected exception" {
298+
$_.Exception.GetType().Name | Should Be $exception
299+
}
304300
}
305301
}
306302

test/powershell/Language/Scripting/I18n.Tests.ps1

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ Describe 'Testing of script internationalization' -Tags "CI" {
5252

5353
import-localizedData mydata -uiculture nl-NL -ea SilentlyContinue -ev ev
5454

55-
$ev[0].Exception.GetType() | Should Be System.Management.Automation.PSInvalidOperationException
55+
$ev | Should Not BeNullOrEmpty
56+
$ev[0].Exception | Should BeOfType "System.Management.Automation.PSInvalidOperationException"
5657
}
5758

5859
It 'Import different file name is done correctly' {
@@ -110,7 +111,8 @@ Describe 'Testing of script internationalization' -Tags "CI" {
110111
import-localizedData mydata -filename bad
111112
}
112113

113-
$script:exception.exception.gettype() | Should Be System.management.automation.psinvalidoperationexception
114+
$script:exception.exception | Should Not BeNullOrEmpty
115+
$script:exception.exception | Should BeOfType System.management.automation.psinvalidoperationexception
114116
}
115117

116118
It 'Import if psd1 file is done correctly' {

0 commit comments

Comments
 (0)