mirror of
https://github.com/PowerShell/PowerShell
synced 2026-06-08 12:12:50 +00:00
Add Remove-Variable Pester Test
This commit is contained in:
@@ -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 ""
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user