From 99c197cc7d0d5a81a7b8171dd4de9b06e870c2c7 Mon Sep 17 00:00:00 2001 From: JumpingYang001 Date: Sun, 24 Apr 2016 22:33:29 -0700 Subject: [PATCH] Add Remove-Variable Pester Test --- test/powershell/Remove-Variable.Tests.ps1 | 90 +++++++++++++++++++++++ 1 file changed, 90 insertions(+) diff --git a/test/powershell/Remove-Variable.Tests.ps1 b/test/powershell/Remove-Variable.Tests.ps1 index 796fa39a4b..a6f6e50a12 100644 --- a/test/powershell/Remove-Variable.Tests.ps1 +++ b/test/powershell/Remove-Variable.Tests.ps1 @@ -191,3 +191,93 @@ Describe "Remove-Variable" { } } } + + +Describe "Remove-Variable basic functionality" -Tags DRT{ + It "Remove-Variable variable should works"{ + New-Variable foo bar + Remove-Variable foo + $var1 = Get-Variable -Name foo -EA SilentlyContinue + $var1 | Should BeNullOrEmpty + } + + It "Remove-Variable Constant variable should throw SessionStateUnauthorizedAccessException"{ + New-Variable foo bar -Option Constant + try + { + Remove-Variable foo -EA Stop + Throw "Execution OK" + } + catch + { + $_.CategoryInfo | Should Match "SessionStateUnauthorizedAccessException" + $_.FullyQualifiedErrorId | Should be "VariableNotRemovable,Microsoft.PowerShell.Commands.RemoveVariableCommand" + } + } + + It "Remove-Variable ReadOnly variable should throw SessionStateUnauthorizedAccessException and force remove should work"{ + New-Variable foo bar -Option ReadOnly + try + { + Remove-Variable foo -EA Stop + Throw "Execution OK" + } + catch + { + $_.CategoryInfo| Should Match "SessionStateUnauthorizedAccessException" + $_.FullyQualifiedErrorId | Should be "VariableNotRemovable,Microsoft.PowerShell.Commands.RemoveVariableCommand" + } + Remove-Variable foo -Force + $var1 = Get-Variable -Name foo -EA SilentlyContinue + $var1 | Should BeNullOrEmpty + } + + It "Remove-Variable Constant variable should throw SessionStateUnauthorizedAccessException and force remove should also throw exception"{ + New-Variable foo bar -Option Constant + try + { + Remove-Variable foo -EA Stop + Throw "Execution OK" + } + catch + { + $_.CategoryInfo | Should Match "SessionStateUnauthorizedAccessException" + $_.FullyQualifiedErrorId | Should be "VariableNotRemovable,Microsoft.PowerShell.Commands.RemoveVariableCommand" + } + + try + { + Remove-Variable foo -Force -EA Stop + Throw "Execution OK" + } + catch + { + $_.CategoryInfo | Should Match "SessionStateUnauthorizedAccessException" + $_.FullyQualifiedErrorId | Should be "VariableNotRemovable,Microsoft.PowerShell.Commands.RemoveVariableCommand" + } + } + + It "Remove-Variable variable in new scope should works and Get-Variable with different scope should have different result"{ + New-Variable foo bar + &{ + Clear-Variable foo + Remove-Variable foo + try{ + Get-Variable -Name foo -Scope local -EA Stop + Throw "Execution OK" + } + catch + { + $_.CategoryInfo | Should Match "ItemNotFoundException" + $_.FullyQualifiedErrorId | Should be "VariableNotFound,Microsoft.PowerShell.Commands.GetVariableCommand" + } + } + + $var1 = Get-Variable -Name foo + $var1.Name | Should Be "foo" + $var1.Value | Should Be "bar" + $var1.Options | Should Be "None" + $var1.Description | Should Be "" + + } +}