From 95eade130597af48f880bb7a35236838625def19 Mon Sep 17 00:00:00 2001 From: JumpingYang001 Date: Mon, 11 Apr 2016 00:12:28 -0700 Subject: [PATCH 1/4] Add Get-Variable Pester Unit Test --- test/powershell/Get-Variable.Tests.ps1 | 333 ++++++++++++++----------- 1 file changed, 193 insertions(+), 140 deletions(-) diff --git a/test/powershell/Get-Variable.Tests.ps1 b/test/powershell/Get-Variable.Tests.ps1 index 71c4d367b..6721b3366 100644 --- a/test/powershell/Get-Variable.Tests.ps1 +++ b/test/powershell/Get-Variable.Tests.ps1 @@ -1,140 +1,193 @@ -Describe "Get-Variable" { - It "Should be able to call with no parameters without error" { - { Get-Variable } | Should Not Throw - } - - It "Should return environment variables when called with no parameters" { - (Get-Variable).Name -contains "$" | Should Be $true - (Get-Variable).Name -contains "?" | Should Be $true - (Get-Variable).Name -contains "HOST" | Should Be $true - (Get-Variable).Name -contains "PWD" | Should Be $true - (Get-Variable).Name -contains "PID" | Should Be $true - (Get-Variable).Name -contains "^" | Should Be $true - } - - It "Should return the value of an object" { - New-Variable -Name tempVar -Value 1 - (Get-Variable tempVar).Value | Should Be (1) - } - - It "Should be able to call using the gv alias" { - { gv } | Should Not Throw - } - - It "Should be able to call using the Name switch" { - New-Variable -Name var1 -Value 4 - - { Get-Variable -Name var1 } | Should Not Throw - - (Get-Variable -Name var1).Value | Should Be 4 - - Remove-Variable var1 - } - - It "Should be able to use wildcard characters in the Name field" { - New-Variable -Name var1 -Value 4 - New-Variable -Name var2 -Value "test" - - (Get-Variable -Name var*).Value[0] | Should be 4 - (Get-Variable -Name var*).Value[1] | Should be "test" - - Remove-Variable var1 - Remove-Variable var2 - } - - It "Should return only the value if the value switch is used" { - New-Variable -Name var1 -Value 4 - - Get-Variable -Name var1 -ValueOnly | Should be 4 - - Remove-Variable var1 - } - - It "Should pipe string to the name field without the Name field being specified"{ - New-Variable -Name var1 -Value 3 - - ("var1" | Get-Variable ).Value | Should Be 3 - - Remove-Variable var1 - } - - It "Should be able to include a set of variables to get" { - New-Variable -Name var1 -Value 4 - New-Variable -Name var2 -Value 2 - - $actual = Get-Variable -Include var1, var2 - - $actual[0].Name | Should Be var1 - $actual[1].Name | Should Be var2 - - $actual[0].Value | Should Be 4 - $actual[1].Value | Should Be 2 - - Remove-Variable var1 - Remove-Variable var2 - } - - It "Should be able to exclude a set of variables to get" { - New-Variable -Name var1 -Value 4 - New-Variable -Name var2 -Value 2 - New-Variable -Name var3 -Value "test" - - $actual = Get-Variable -Exclude var1, var2 - - $actual | Where-Object { $_.Name -eq "var3" } | Should Not BeNullOrEmpty - } - - Context "Scope Tests" { - # This will violate the DRY principle. Tread softly. - It "Should be able to get a global scope variable using the global switch" { - New-Variable globalVar -Value 1 -Scope global -Force - - (Get-Variable -Name globalVar -Scope global)[0].Value | Should Be 1 - } - - It "Should not be able to clear a global scope variable using the local switch" { - New-Variable globalVar -Value 1 -Scope global -Force - - Get-Variable -Name globalVar -Scope local -ErrorAction SilentlyContinue | Should Throw - } - - It "Should be able to get a global variable when there's one in the script scope" { - New-Variable globalVar -Value 1 -Scope global -Force - { New-Variable globalVar -Value 2 -Scope script -Force } - - $(Get-Variable -Name globalVar).Value | Should Be 1 - } - - It "Should be able to get an item locally using the local switch" { - { - New-Variable localVar -Value 1 -Scope local -Force - - Get-Variable -Name localVar -Scope local - } | Should Not Throw - } - - It "Should be able to get a variable created in the global scope when there's one in local scope" { - New-Variable localVar -Value 1 -Scope local -Force - - New-Variable localVar -Value 2 -Scope global -Force - - $(Get-Variable -Name localVar -Scope global).Value | Should Be 2 - } - - It "Should be able to get a script variable created using the script switch" { - { - New-Variable scriptVar -Value 1 -Scope script -Force - - Get-Variable -Name scriptVar -Scope script - } | Should Not Throw - } - - It "Should be able to clear a global script variable that was created using the script scope switch" { - { - New-Variable scriptVar -Value 1 -Scope script -Force - - Get-Variable -Name scriptVar -Scope script - } | Should Not Throw - } - } -} + +Describe "Get-Variable DRT Unit Tests" -Tags DRT{ + It "Get-Variable not exist variable Name should throw ItemNotFoundException skip now as bug#777" -Skip:$true{ + try { + Get-Variable -Name nonexistingVariableName + Throw "Execution OK" + } + catch { + $_.FullyQualifiedErrorId | Should be "VariableNotFound,Microsoft.PowerShell.Commands.GetVariableCommand" + } + } + + It "Get-Variable exist variable Name with include and bogus exclude should work"{ + Set-Variable newVar testing + $var1=get-variable -Name newVar -Include newVar -Exclude bogus + $var1.Name|Should Be "newVar" + $var1.Value|Should Be "testing" + } + + It "Get-Variable exist variable Name with Description and Option should work"{ + Set-Variable newVar testing -Option ReadOnly -Description "testing description" + $var1=get-variable -Name newVar + $var1.Name|Should Be "newVar" + $var1.Value|Should Be "testing" + $var1.Options|Should Be "ReadOnly" + $var1.Description|Should Be "testing description" + } + + It "Get-Variable exist variable Globbing Name should work"{ + Set-Variable abcaVar testing + Set-Variable bcdaVar "another test" + Set-Variable aVarfoo wow + $var1=get-variable -Name *aVar* -Scope local + $var1[0].Name|Should Be "abcaVar" + $var1[0].Value|Should Be "testing" + $var1[1].Name|Should Be "aVarfoo" + $var1[1].Value|Should Be "wow" + $var1[2].Name|Should Be "bcdaVar" + $var1[2].Value|Should Be "another test" + } + + It "Get-Variable an exist private variable Name should throw ItemNotFoundException skip now as bug#777" -Skip:$true{ + try { + Set-Variable newVar testing -Option Private + Get-Variable -Name newVar + Throw "Execution OK" + } + catch { + $_.FullyQualifiedErrorId | Should be "VariableNotFound,Microsoft.PowerShell.Commands.GetVariableCommand" + } + } +} + +Describe "Get-Variable" { + It "Should be able to call with no parameters without error" { + { Get-Variable } | Should Not Throw + } + + It "Should return environment variables when called with no parameters" { + (Get-Variable).Name -contains "$" | Should Be $true + (Get-Variable).Name -contains "?" | Should Be $true + (Get-Variable).Name -contains "HOST" | Should Be $true + (Get-Variable).Name -contains "PWD" | Should Be $true + (Get-Variable).Name -contains "PID" | Should Be $true + (Get-Variable).Name -contains "^" | Should Be $true + } + + It "Should return the value of an object" { + New-Variable -Name tempVar -Value 1 + (Get-Variable tempVar).Value | Should Be (1) + } + + It "Should be able to call using the gv alias" { + { gv } | Should Not Throw + } + + It "Should be able to call using the Name switch" { + New-Variable -Name var1 -Value 4 + + { Get-Variable -Name var1 } | Should Not Throw + + (Get-Variable -Name var1).Value | Should Be 4 + + Remove-Variable var1 + } + + It "Should be able to use wildcard characters in the Name field" { + New-Variable -Name var1 -Value 4 + New-Variable -Name var2 -Value "test" + + (Get-Variable -Name var*).Value[0] | Should be 4 + (Get-Variable -Name var*).Value[1] | Should be "test" + + Remove-Variable var1 + Remove-Variable var2 + } + + It "Should return only the value if the value switch is used" { + New-Variable -Name var1 -Value 4 + + Get-Variable -Name var1 -ValueOnly | Should be 4 + + Remove-Variable var1 + } + + It "Should pipe string to the name field without the Name field being specified"{ + New-Variable -Name var1 -Value 3 + + ("var1" | Get-Variable ).Value | Should Be 3 + + Remove-Variable var1 + } + + It "Should be able to include a set of variables to get" { + New-Variable -Name var1 -Value 4 + New-Variable -Name var2 -Value 2 + + $actual = Get-Variable -Include var1, var2 + + $actual[0].Name | Should Be var1 + $actual[1].Name | Should Be var2 + + $actual[0].Value | Should Be 4 + $actual[1].Value | Should Be 2 + + Remove-Variable var1 + Remove-Variable var2 + } + + It "Should be able to exclude a set of variables to get" { + New-Variable -Name var1 -Value 4 + New-Variable -Name var2 -Value 2 + New-Variable -Name var3 -Value "test" + + $actual = Get-Variable -Exclude var1, var2 + + $actual | Where-Object { $_.Name -eq "var3" } | Should Not BeNullOrEmpty + } + + Context "Scope Tests" { + # This will violate the DRY principle. Tread softly. + It "Should be able to get a global scope variable using the global switch" { + New-Variable globalVar -Value 1 -Scope global -Force + + (Get-Variable -Name globalVar -Scope global)[0].Value | Should Be 1 + } + + It "Should not be able to clear a global scope variable using the local switch" { + New-Variable globalVar -Value 1 -Scope global -Force + + Get-Variable -Name globalVar -Scope local -ErrorAction SilentlyContinue | Should Throw + } + + It "Should be able to get a global variable when there's one in the script scope" { + New-Variable globalVar -Value 1 -Scope global -Force + { New-Variable globalVar -Value 2 -Scope script -Force } + + $(Get-Variable -Name globalVar).Value | Should Be 1 + } + + It "Should be able to get an item locally using the local switch" { + { + New-Variable localVar -Value 1 -Scope local -Force + + Get-Variable -Name localVar -Scope local + } | Should Not Throw + } + + It "Should be able to get a variable created in the global scope when there's one in local scope" { + New-Variable localVar -Value 1 -Scope local -Force + + New-Variable localVar -Value 2 -Scope global -Force + + $(Get-Variable -Name localVar -Scope global).Value | Should Be 2 + } + + It "Should be able to get a script variable created using the script switch" { + { + New-Variable scriptVar -Value 1 -Scope script -Force + + Get-Variable -Name scriptVar -Scope script + } | Should Not Throw + } + + It "Should be able to clear a global script variable that was created using the script scope switch" { + { + New-Variable scriptVar -Value 1 -Scope script -Force + + Get-Variable -Name scriptVar -Scope script + } | Should Not Throw + } + } +} From 053191437763abaeb2d609f08ae55ad20f7d853f Mon Sep 17 00:00:00 2001 From: JumpingYang001 Date: Tue, 12 Apr 2016 00:46:50 -0700 Subject: [PATCH 2/4] Enable bug777 skip for Get-Variable --- test/powershell/Get-Variable.Tests.ps1 | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/test/powershell/Get-Variable.Tests.ps1 b/test/powershell/Get-Variable.Tests.ps1 index 6721b3366..0d6d2fb82 100644 --- a/test/powershell/Get-Variable.Tests.ps1 +++ b/test/powershell/Get-Variable.Tests.ps1 @@ -1,6 +1,7 @@ Describe "Get-Variable DRT Unit Tests" -Tags DRT{ - It "Get-Variable not exist variable Name should throw ItemNotFoundException skip now as bug#777" -Skip:$true{ + It "Get-Variable not exist variable Name should throw ItemNotFoundException"{ + $ErrorActionPreference = "Stop" try { Get-Variable -Name nonexistingVariableName Throw "Execution OK" @@ -8,6 +9,7 @@ Describe "Get-Variable DRT Unit Tests" -Tags DRT{ catch { $_.FullyQualifiedErrorId | Should be "VariableNotFound,Microsoft.PowerShell.Commands.GetVariableCommand" } + $ErrorActionPreference = "SilentlyContinue" } It "Get-Variable exist variable Name with include and bogus exclude should work"{ @@ -39,7 +41,7 @@ Describe "Get-Variable DRT Unit Tests" -Tags DRT{ $var1[2].Value|Should Be "another test" } - It "Get-Variable an exist private variable Name should throw ItemNotFoundException skip now as bug#777" -Skip:$true{ + It "Get-Variable an exist private variable Name should throw ItemNotFoundException skip now as bug#818" -Skip:$true{ try { Set-Variable newVar testing -Option Private Get-Variable -Name newVar From 191f232519ac3d9b8f7a5142ec2e95cc014a003e Mon Sep 17 00:00:00 2001 From: JumpingYang001 Date: Wed, 13 Apr 2016 00:01:49 -0700 Subject: [PATCH 3/4] Update fixed comments from PR for Get-Variable Pester unit test --- test/powershell/Get-Variable.Tests.ps1 | 29 +++++++++++++------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/test/powershell/Get-Variable.Tests.ps1 b/test/powershell/Get-Variable.Tests.ps1 index 0d6d2fb82..6b26ff4a2 100644 --- a/test/powershell/Get-Variable.Tests.ps1 +++ b/test/powershell/Get-Variable.Tests.ps1 @@ -1,25 +1,23 @@ Describe "Get-Variable DRT Unit Tests" -Tags DRT{ - It "Get-Variable not exist variable Name should throw ItemNotFoundException"{ - $ErrorActionPreference = "Stop" + It "Get-Variable of not existing variable Name should throw ItemNotFoundException"{ try { - Get-Variable -Name nonexistingVariableName + Get-Variable -EA Stop -Name nonexistingVariableName Throw "Execution OK" } catch { $_.FullyQualifiedErrorId | Should be "VariableNotFound,Microsoft.PowerShell.Commands.GetVariableCommand" } - $ErrorActionPreference = "SilentlyContinue" } - It "Get-Variable exist variable Name with include and bogus exclude should work"{ + It "Get-Variable of existing variable Name with include and bogus exclude should work"{ Set-Variable newVar testing $var1=get-variable -Name newVar -Include newVar -Exclude bogus $var1.Name|Should Be "newVar" $var1.Value|Should Be "testing" } - It "Get-Variable exist variable Name with Description and Option should work"{ + It "Get-Variable of existing variable Name with Description and Option should work"{ Set-Variable newVar testing -Option ReadOnly -Description "testing description" $var1=get-variable -Name newVar $var1.Name|Should Be "newVar" @@ -28,11 +26,12 @@ Describe "Get-Variable DRT Unit Tests" -Tags DRT{ $var1.Description|Should Be "testing description" } - It "Get-Variable exist variable Globbing Name should work"{ + It "Get-Variable of existing variable Globbing Name should work"{ Set-Variable abcaVar testing Set-Variable bcdaVar "another test" Set-Variable aVarfoo wow $var1=get-variable -Name *aVar* -Scope local + $var1.Count | Should be 3 $var1[0].Name|Should Be "abcaVar" $var1[0].Value|Should Be "testing" $var1[1].Name|Should Be "aVarfoo" @@ -41,10 +40,10 @@ Describe "Get-Variable DRT Unit Tests" -Tags DRT{ $var1[2].Value|Should Be "another test" } - It "Get-Variable an exist private variable Name should throw ItemNotFoundException skip now as bug#818" -Skip:$true{ + It "Get-Variable of existing private variable Name should throw ItemNotFoundException skip now as bug#818" -Skip:$true{ try { Set-Variable newVar testing -Option Private - Get-Variable -Name newVar + &{Get-Variable -Name newVar} Throw "Execution OK" } catch { @@ -73,7 +72,7 @@ Describe "Get-Variable" { } It "Should be able to call using the gv alias" { - { gv } | Should Not Throw + (get-alias gv).Definition | Should be "Get-Variable" } It "Should be able to call using the Name switch" { @@ -136,7 +135,7 @@ Describe "Get-Variable" { $actual = Get-Variable -Exclude var1, var2 - $actual | Where-Object { $_.Name -eq "var3" } | Should Not BeNullOrEmpty + $actual.Name -contains "var3" | Should Be $true } Context "Scope Tests" { @@ -144,7 +143,7 @@ Describe "Get-Variable" { It "Should be able to get a global scope variable using the global switch" { New-Variable globalVar -Value 1 -Scope global -Force - (Get-Variable -Name globalVar -Scope global)[0].Value | Should Be 1 + (Get-Variable -Name globalVar -Scope global).Value | Should Be 1 } It "Should not be able to clear a global scope variable using the local switch" { @@ -155,9 +154,9 @@ Describe "Get-Variable" { It "Should be able to get a global variable when there's one in the script scope" { New-Variable globalVar -Value 1 -Scope global -Force - { New-Variable globalVar -Value 2 -Scope script -Force } + { New-Variable globalVar -Value 2 -Scope script -Force} - $(Get-Variable -Name globalVar).Value | Should Be 1 + (Get-Variable -Name globalVar).Value | Should Be 1 } It "Should be able to get an item locally using the local switch" { @@ -173,7 +172,7 @@ Describe "Get-Variable" { New-Variable localVar -Value 2 -Scope global -Force - $(Get-Variable -Name localVar -Scope global).Value | Should Be 2 + (Get-Variable -Name localVar -Scope global).Value | Should Be 2 } It "Should be able to get a script variable created using the script switch" { From d0c759f10e68521b4a36dbcf9c894f3b6ffe64aa Mon Sep 17 00:00:00 2001 From: JumpingYang001 Date: Wed, 13 Apr 2016 01:43:27 -0700 Subject: [PATCH 4/4] Update indent for Get-Variable Pester unit test --- test/powershell/Get-Variable.Tests.ps1 | 80 +++++++++++++------------- 1 file changed, 40 insertions(+), 40 deletions(-) diff --git a/test/powershell/Get-Variable.Tests.ps1 b/test/powershell/Get-Variable.Tests.ps1 index 6b26ff4a2..dd25aceaa 100644 --- a/test/powershell/Get-Variable.Tests.ps1 +++ b/test/powershell/Get-Variable.Tests.ps1 @@ -54,88 +54,88 @@ Describe "Get-Variable DRT Unit Tests" -Tags DRT{ Describe "Get-Variable" { It "Should be able to call with no parameters without error" { - { Get-Variable } | Should Not Throw + { Get-Variable } | Should Not Throw } It "Should return environment variables when called with no parameters" { - (Get-Variable).Name -contains "$" | Should Be $true - (Get-Variable).Name -contains "?" | Should Be $true - (Get-Variable).Name -contains "HOST" | Should Be $true - (Get-Variable).Name -contains "PWD" | Should Be $true - (Get-Variable).Name -contains "PID" | Should Be $true - (Get-Variable).Name -contains "^" | Should Be $true + (Get-Variable).Name -contains "$" | Should Be $true + (Get-Variable).Name -contains "?" | Should Be $true + (Get-Variable).Name -contains "HOST" | Should Be $true + (Get-Variable).Name -contains "PWD" | Should Be $true + (Get-Variable).Name -contains "PID" | Should Be $true + (Get-Variable).Name -contains "^" | Should Be $true } It "Should return the value of an object" { - New-Variable -Name tempVar -Value 1 - (Get-Variable tempVar).Value | Should Be (1) + New-Variable -Name tempVar -Value 1 + (Get-Variable tempVar).Value | Should Be (1) } It "Should be able to call using the gv alias" { - (get-alias gv).Definition | Should be "Get-Variable" + (get-alias gv).Definition | Should be "Get-Variable" } It "Should be able to call using the Name switch" { - New-Variable -Name var1 -Value 4 + New-Variable -Name var1 -Value 4 - { Get-Variable -Name var1 } | Should Not Throw + { Get-Variable -Name var1 } | Should Not Throw - (Get-Variable -Name var1).Value | Should Be 4 + (Get-Variable -Name var1).Value | Should Be 4 - Remove-Variable var1 + Remove-Variable var1 } It "Should be able to use wildcard characters in the Name field" { - New-Variable -Name var1 -Value 4 - New-Variable -Name var2 -Value "test" + New-Variable -Name var1 -Value 4 + New-Variable -Name var2 -Value "test" - (Get-Variable -Name var*).Value[0] | Should be 4 - (Get-Variable -Name var*).Value[1] | Should be "test" + (Get-Variable -Name var*).Value[0] | Should be 4 + (Get-Variable -Name var*).Value[1] | Should be "test" - Remove-Variable var1 - Remove-Variable var2 + Remove-Variable var1 + Remove-Variable var2 } It "Should return only the value if the value switch is used" { - New-Variable -Name var1 -Value 4 + New-Variable -Name var1 -Value 4 - Get-Variable -Name var1 -ValueOnly | Should be 4 + Get-Variable -Name var1 -ValueOnly | Should be 4 - Remove-Variable var1 + Remove-Variable var1 } It "Should pipe string to the name field without the Name field being specified"{ - New-Variable -Name var1 -Value 3 + New-Variable -Name var1 -Value 3 - ("var1" | Get-Variable ).Value | Should Be 3 + ("var1" | Get-Variable ).Value | Should Be 3 - Remove-Variable var1 + Remove-Variable var1 } It "Should be able to include a set of variables to get" { - New-Variable -Name var1 -Value 4 - New-Variable -Name var2 -Value 2 + New-Variable -Name var1 -Value 4 + New-Variable -Name var2 -Value 2 - $actual = Get-Variable -Include var1, var2 + $actual = Get-Variable -Include var1, var2 - $actual[0].Name | Should Be var1 - $actual[1].Name | Should Be var2 + $actual[0].Name | Should Be var1 + $actual[1].Name | Should Be var2 - $actual[0].Value | Should Be 4 - $actual[1].Value | Should Be 2 + $actual[0].Value | Should Be 4 + $actual[1].Value | Should Be 2 - Remove-Variable var1 - Remove-Variable var2 + Remove-Variable var1 + Remove-Variable var2 } It "Should be able to exclude a set of variables to get" { - New-Variable -Name var1 -Value 4 - New-Variable -Name var2 -Value 2 - New-Variable -Name var3 -Value "test" + New-Variable -Name var1 -Value 4 + New-Variable -Name var2 -Value 2 + New-Variable -Name var3 -Value "test" - $actual = Get-Variable -Exclude var1, var2 + $actual = Get-Variable -Exclude var1, var2 - $actual.Name -contains "var3" | Should Be $true + $actual.Name -contains "var3" | Should Be $true } Context "Scope Tests" {