2020-03-24 11:08:37 -07:00
|
|
|
# Copyright (c) Microsoft Corporation.
|
2018-02-13 09:23:53 -08:00
|
|
|
# Licensed under the MIT License.
|
|
|
|
|
|
2016-03-04 14:48:30 -08:00
|
|
|
# ensure the machine is in a clean state from the outset.
|
2015-09-29 10:12:07 -07:00
|
|
|
Remove-Variable -Name var1 -ErrorAction SilentlyContinue -Force
|
|
|
|
|
|
2016-07-25 14:01:41 -07:00
|
|
|
Describe "Remove-Variable" -Tags "CI" {
|
2015-09-29 10:12:07 -07:00
|
|
|
It "Should throw an error when a dollar sign is used in the variable name place" {
|
2016-03-04 14:52:27 -08:00
|
|
|
New-Variable -Name var1 -Value 4
|
2015-09-29 10:12:07 -07:00
|
|
|
|
2018-03-23 10:26:23 -07:00
|
|
|
{ Remove-Variable $var1 -ErrorAction Stop } | Should -Throw -ErrorId "VariableNotFound,Microsoft.PowerShell.Commands.RemoveVariableCommand"
|
2015-09-29 10:12:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
It "Should not throw error when used without the Name field, and named variable is properly specified and exists" {
|
2016-03-04 14:52:27 -08:00
|
|
|
New-Variable -Name var1 -Value 4
|
2015-09-29 10:12:07 -07:00
|
|
|
|
2016-03-04 14:52:27 -08:00
|
|
|
Remove-Variable var1
|
2015-09-29 10:12:07 -07:00
|
|
|
|
2018-04-01 04:05:45 -07:00
|
|
|
$var1 | Should -BeNullOrEmpty
|
|
|
|
|
{ Get-Variable var1 -ErrorAction stop } |
|
|
|
|
|
Should -Throw -ErrorId 'VariableNotFound,Microsoft.PowerShell.Commands.GetVariableCommand'
|
2015-09-29 10:12:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
It "Should not throw error when used with the Name field, and named variable is specified and exists" {
|
2016-03-04 14:52:27 -08:00
|
|
|
New-Variable -Name var1 -Value 2
|
2015-09-29 10:12:07 -07:00
|
|
|
|
2016-03-04 14:52:27 -08:00
|
|
|
Remove-Variable -Name var1
|
2015-09-29 10:12:07 -07:00
|
|
|
|
2018-04-01 04:05:45 -07:00
|
|
|
$var1 | Should -BeNullOrEmpty
|
|
|
|
|
{ Get-Variable var1 -ErrorAction stop } |
|
|
|
|
|
Should -Throw -ErrorId 'VariableNotFound,Microsoft.PowerShell.Commands.GetVariableCommand'
|
2015-09-29 10:12:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
It "Should throw error when used with Name field, and named variable does not exist" {
|
2018-03-23 10:26:23 -07:00
|
|
|
{ Remove-Variable -Name nonexistentVariable -ErrorAction Stop } | Should -Throw
|
2015-09-29 10:12:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
It "Should be able to remove a set of variables using wildcard characters" {
|
2016-03-04 14:52:27 -08:00
|
|
|
New-Variable tmpvar1 -Value "tempvalue"
|
|
|
|
|
New-Variable tmpvar2 -Value 2
|
|
|
|
|
New-Variable tmpmyvar1 -Value 234
|
2015-09-29 10:12:07 -07:00
|
|
|
|
2018-03-23 10:26:23 -07:00
|
|
|
$tmpvar1 | Should -BeExactly "tempvalue"
|
|
|
|
|
$tmpvar2 | Should -Be 2
|
|
|
|
|
$tmpmyvar1 | Should -Be 234
|
2015-09-29 10:12:07 -07:00
|
|
|
|
2016-03-04 14:52:27 -08:00
|
|
|
Remove-Variable -Name tmp*
|
2015-09-29 10:12:07 -07:00
|
|
|
|
2018-04-01 04:05:45 -07:00
|
|
|
$tmpvar1 | Should -BeNullOrEmpty
|
|
|
|
|
$tmpvar2 | Should -BeNullOrEmpty
|
|
|
|
|
$tmpmyvar1 | Should -BeNullOrEmpty
|
|
|
|
|
|
|
|
|
|
{ Get-Variable tmpvar1 -ErrorAction stop } |
|
|
|
|
|
Should -Throw -ErrorId 'VariableNotFound,Microsoft.PowerShell.Commands.GetVariableCommand'
|
|
|
|
|
{ Get-Variable tmpvar2 -ErrorAction stop } |
|
|
|
|
|
Should -Throw -ErrorId 'VariableNotFound,Microsoft.PowerShell.Commands.GetVariableCommand'
|
|
|
|
|
{ Get-Variable tmpmyvar1 -ErrorAction stop } |
|
|
|
|
|
Should -Throw -ErrorId 'VariableNotFound,Microsoft.PowerShell.Commands.GetVariableCommand'
|
2015-09-29 10:12:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
It "Should be able to exclude a set of variables to remove using the Exclude switch" {
|
2016-03-04 14:52:27 -08:00
|
|
|
New-Variable tmpvar1 -Value "tempvalue"
|
|
|
|
|
New-Variable tmpvar2 -Value 2
|
|
|
|
|
New-Variable tmpmyvar1 -Value 234
|
2015-09-29 10:12:07 -07:00
|
|
|
|
2018-03-23 10:26:23 -07:00
|
|
|
$tmpvar1 | Should -BeExactly "tempvalue"
|
|
|
|
|
$tmpvar2 | Should -Be 2
|
|
|
|
|
$tmpmyvar1 | Should -Be 234
|
2015-09-29 10:12:07 -07:00
|
|
|
|
2016-03-04 14:52:27 -08:00
|
|
|
Remove-Variable -Name tmp* -Exclude *my*
|
2015-09-29 10:12:07 -07:00
|
|
|
|
2018-04-01 04:05:45 -07:00
|
|
|
$tmpvar1 | Should -BeNullOrEmpty
|
|
|
|
|
$tmpvar2 | Should -BeNullOrEmpty
|
2018-03-23 10:26:23 -07:00
|
|
|
$tmpmyvar1 | Should -Be 234
|
2018-04-01 04:05:45 -07:00
|
|
|
|
|
|
|
|
{ Get-Variable tmpvar1 -ErrorAction stop } |
|
|
|
|
|
Should -Throw -ErrorId 'VariableNotFound,Microsoft.PowerShell.Commands.GetVariableCommand'
|
|
|
|
|
{ Get-Variable tmpvar2 -ErrorAction stop } |
|
|
|
|
|
Should -Throw -ErrorId 'VariableNotFound,Microsoft.PowerShell.Commands.GetVariableCommand'
|
2015-09-29 10:12:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
It "Should be able to include a set of variables to remove using the Include switch" {
|
2016-03-04 14:52:27 -08:00
|
|
|
New-Variable tmpvar1 -Value "tempvalue"
|
|
|
|
|
New-Variable tmpvar2 -Value 2
|
|
|
|
|
New-Variable tmpmyvar1 -Value 234
|
|
|
|
|
New-Variable thevar -Value 1
|
2015-09-29 10:12:07 -07:00
|
|
|
|
2018-03-23 10:26:23 -07:00
|
|
|
$tmpvar1 | Should -BeExactly "tempvalue"
|
|
|
|
|
$tmpvar2 | Should -Be 2
|
|
|
|
|
$tmpmyvar1 | Should -Be 234
|
|
|
|
|
$thevar | Should -Be 1
|
2015-09-29 10:12:07 -07:00
|
|
|
|
2016-03-04 14:52:27 -08:00
|
|
|
Remove-Variable -Name tmp* -Include *my*
|
2015-09-29 10:12:07 -07:00
|
|
|
|
2018-03-23 10:26:23 -07:00
|
|
|
$tmpvar1 | Should -BeExactly "tempvalue"
|
|
|
|
|
$tmpvar2 | Should -Be 2
|
2018-04-01 04:05:45 -07:00
|
|
|
$tmpmyvar1 | Should -BeNullOrEmpty
|
2018-03-23 10:26:23 -07:00
|
|
|
$thevar | Should -Be 1
|
2015-09-29 10:12:07 -07:00
|
|
|
|
2018-04-01 04:05:45 -07:00
|
|
|
{ Get-Variable tmpmyvar1 -ErrorAction stop } |
|
|
|
|
|
Should -Throw -ErrorId 'VariableNotFound,Microsoft.PowerShell.Commands.GetVariableCommand'
|
|
|
|
|
|
2016-03-04 14:52:27 -08:00
|
|
|
Remove-Variable tmpvar1
|
|
|
|
|
Remove-Variable tmpvar2
|
|
|
|
|
Remove-Variable thevar
|
2015-09-29 10:12:07 -07:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
It "Should throw an error when attempting to remove a read-only variable and the Force switch is not used" {
|
2016-03-04 14:52:27 -08:00
|
|
|
New-Variable -Name var1 -Value 2 -Option ReadOnly
|
2015-09-29 10:12:07 -07:00
|
|
|
|
2018-03-23 10:26:23 -07:00
|
|
|
{ Remove-Variable -Name var1 -ErrorAction Stop } | Should -Throw
|
2015-09-29 10:12:07 -07:00
|
|
|
|
2018-03-23 10:26:23 -07:00
|
|
|
$var1 | Should -Be 2
|
2015-09-29 10:12:07 -07:00
|
|
|
|
2016-03-04 14:52:27 -08:00
|
|
|
Remove-Variable -Name var1 -Force
|
2015-09-29 10:12:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
It "Should not throw an error when attempting to remove a read-only variable and the Force switch is used" {
|
2016-03-04 14:52:27 -08:00
|
|
|
New-Variable -Name var1 -Value 2 -Option ReadOnly
|
2015-09-29 10:12:07 -07:00
|
|
|
|
2016-03-04 14:52:27 -08:00
|
|
|
Remove-Variable -Name var1 -Force
|
2015-09-29 10:12:07 -07:00
|
|
|
|
2018-03-23 10:26:23 -07:00
|
|
|
$var1 | Should -BeNullOrEmpty
|
2015-09-29 10:12:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Context "Scope Tests" {
|
2016-03-04 14:52:27 -08:00
|
|
|
It "Should be able to remove a global variable using the global switch" {
|
|
|
|
|
New-Variable -Name var1 -Value "context" -Scope global
|
2015-09-29 10:12:07 -07:00
|
|
|
|
2016-03-04 14:52:27 -08:00
|
|
|
Remove-Variable -Name var1 -Scope global
|
2015-09-29 10:12:07 -07:00
|
|
|
|
2018-03-23 10:26:23 -07:00
|
|
|
$var1 | Should -BeNullOrEmpty
|
2016-03-04 14:52:27 -08:00
|
|
|
}
|
2015-09-29 10:12:07 -07:00
|
|
|
|
2016-03-04 14:52:27 -08:00
|
|
|
It "Should not be able to clear a global variable using the local switch" {
|
|
|
|
|
New-Variable -Name var1 -Value "context" -Scope global
|
2015-09-29 10:12:07 -07:00
|
|
|
|
2018-03-23 10:26:23 -07:00
|
|
|
{ Remove-Variable -Name var1 -Scope local -ErrorAction Stop } | Should -Throw
|
2015-09-29 10:12:07 -07:00
|
|
|
|
2018-03-23 10:26:23 -07:00
|
|
|
$var1 | Should -BeExactly "context"
|
2015-09-29 10:12:07 -07:00
|
|
|
|
2016-03-04 14:52:27 -08:00
|
|
|
Remove-Variable -Name var1 -Scope global
|
2018-03-23 10:26:23 -07:00
|
|
|
$var1 | Should -BeNullOrEmpty
|
2016-03-04 14:52:27 -08:00
|
|
|
}
|
2015-09-29 10:12:07 -07:00
|
|
|
|
2016-03-04 14:52:27 -08:00
|
|
|
It "Should not be able to clear a global variable using the script switch" {
|
|
|
|
|
New-Variable -Name var1 -Value "context" -Scope global
|
2015-09-29 10:12:07 -07:00
|
|
|
|
2018-03-23 10:26:23 -07:00
|
|
|
{ Remove-Variable -Name var1 -Scope local -ErrorAction Stop } | Should -Throw
|
2015-09-29 10:12:07 -07:00
|
|
|
|
2018-03-23 10:26:23 -07:00
|
|
|
$var1 | Should -BeExactly "context"
|
2015-09-29 10:12:07 -07:00
|
|
|
|
2016-03-04 14:52:27 -08:00
|
|
|
Remove-Variable -Name var1 -Scope global
|
2018-03-23 10:26:23 -07:00
|
|
|
$var1 | Should -BeNullOrEmpty
|
2016-03-04 14:52:27 -08:00
|
|
|
}
|
2015-09-29 10:12:07 -07:00
|
|
|
|
2016-03-04 14:52:27 -08:00
|
|
|
It "Should be able to remove an item locally using the local switch" {
|
|
|
|
|
New-Variable -Name var1 -Value "context" -Scope local
|
2015-09-29 10:12:07 -07:00
|
|
|
|
2018-03-23 10:26:23 -07:00
|
|
|
{ Remove-Variable -Name var1 -Scope local -ErrorAction Stop } | Should -Throw
|
2015-09-29 10:12:07 -07:00
|
|
|
|
2018-03-23 10:26:23 -07:00
|
|
|
$var1 | Should -Be context
|
2016-03-04 14:52:27 -08:00
|
|
|
}
|
2015-09-29 10:12:07 -07:00
|
|
|
|
2016-03-04 14:52:27 -08:00
|
|
|
It "Should be able to remove an item locally using the global switch" {
|
|
|
|
|
New-Variable -Name var1 -Value "context" -Scope local
|
2015-09-29 10:12:07 -07:00
|
|
|
|
2018-03-23 10:26:23 -07:00
|
|
|
{ Remove-Variable -Name var1 -Scope global -ErrorAction Stop } | Should -Throw
|
2015-09-29 10:12:07 -07:00
|
|
|
|
2018-03-23 10:26:23 -07:00
|
|
|
$var1 | Should -BeExactly "context"
|
2015-09-29 10:12:07 -07:00
|
|
|
|
2016-03-04 14:52:27 -08:00
|
|
|
Remove-Variable -Name var1 -Scope local
|
2018-03-23 10:26:23 -07:00
|
|
|
$var1 | Should -BeNullOrEmpty
|
2016-03-04 14:52:27 -08:00
|
|
|
}
|
2015-09-29 10:12:07 -07:00
|
|
|
|
2016-03-04 14:52:27 -08:00
|
|
|
It "Should be able to remove a local variable using the script scope switch" {
|
|
|
|
|
New-Variable -Name var1 -Value "context" -Scope local
|
2015-09-29 10:12:07 -07:00
|
|
|
|
2018-03-23 10:26:23 -07:00
|
|
|
{ Remove-Variable -Name var1 -Scope script -ErrorAction Stop } | Should -Throw
|
2015-09-29 10:12:07 -07:00
|
|
|
|
2018-03-23 10:26:23 -07:00
|
|
|
$var1 | Should -BeExactly "context"
|
2015-09-29 10:12:07 -07:00
|
|
|
|
2016-03-04 14:52:27 -08:00
|
|
|
Remove-Variable -Name var1 -Scope local
|
2018-03-23 10:26:23 -07:00
|
|
|
$var1 | Should -BeNullOrEmpty
|
2016-03-04 14:52:27 -08:00
|
|
|
}
|
2015-09-29 10:12:07 -07:00
|
|
|
|
2016-03-04 14:52:27 -08:00
|
|
|
It "Should be able to remove a script variable created using the script switch" {
|
|
|
|
|
New-Variable -Name var1 -Value "context" -Scope script
|
2015-09-29 10:12:07 -07:00
|
|
|
|
2018-03-23 10:26:23 -07:00
|
|
|
{ Remove-Variable -Name var1 -Scope script } | Should -Not -Throw
|
2015-09-29 10:12:07 -07:00
|
|
|
|
2018-03-23 10:26:23 -07:00
|
|
|
$var1 | Should -BeNullOrEmpty
|
2016-03-04 14:52:27 -08:00
|
|
|
}
|
2015-09-29 10:12:07 -07:00
|
|
|
|
2016-03-04 14:52:27 -08:00
|
|
|
It "Should not be able to remove a global script variable that was created using the script scope switch" {
|
|
|
|
|
New-Variable -Name var1 -Value "context" -Scope script
|
2015-09-29 10:12:07 -07:00
|
|
|
|
2018-03-23 10:26:23 -07:00
|
|
|
{ Remove-Variable -Name var1 -Scope global -ErrorAction Stop } | Should -Throw
|
2015-09-29 10:12:07 -07:00
|
|
|
|
2018-03-23 10:26:23 -07:00
|
|
|
$var1 | Should -BeExactly "context"
|
2016-03-04 14:52:27 -08:00
|
|
|
}
|
2015-09-29 10:12:07 -07:00
|
|
|
}
|
|
|
|
|
}
|
2016-04-24 22:33:29 -07:00
|
|
|
|
2016-07-25 14:01:41 -07:00
|
|
|
Describe "Remove-Variable basic functionality" -Tags "CI" {
|
2016-04-24 22:33:29 -07:00
|
|
|
It "Remove-Variable variable should works"{
|
|
|
|
|
New-Variable foo bar
|
|
|
|
|
Remove-Variable foo
|
2018-05-17 14:42:04 -07:00
|
|
|
$var1 = Get-Variable -Name foo -ErrorAction SilentlyContinue
|
2018-03-23 10:26:23 -07:00
|
|
|
$var1 | Should -BeNullOrEmpty
|
2016-04-24 22:33:29 -07:00
|
|
|
}
|
2017-01-16 13:31:14 -08:00
|
|
|
|
2016-04-24 22:33:29 -07:00
|
|
|
It "Remove-Variable Constant variable should throw SessionStateUnauthorizedAccessException"{
|
|
|
|
|
New-Variable foo bar -Option Constant
|
2018-05-17 14:42:04 -07:00
|
|
|
$e = { Remove-Variable foo -Scope 1 -ErrorAction Stop } |
|
2018-03-23 10:26:23 -07:00
|
|
|
Should -Throw -ErrorId "VariableNotRemovable,Microsoft.PowerShell.Commands.RemoveVariableCommand" -PassThru
|
|
|
|
|
$e.CategoryInfo | Should -Match "SessionStateUnauthorizedAccessException"
|
2016-04-24 22:33:29 -07:00
|
|
|
}
|
2017-01-16 13:31:14 -08:00
|
|
|
|
2016-04-24 22:33:29 -07:00
|
|
|
It "Remove-Variable ReadOnly variable should throw SessionStateUnauthorizedAccessException and force remove should work"{
|
|
|
|
|
New-Variable foo bar -Option ReadOnly
|
2018-05-17 14:42:04 -07:00
|
|
|
$e = { Remove-Variable foo -Scope 1 -ErrorAction Stop } |
|
2018-03-23 10:26:23 -07:00
|
|
|
Should -Throw -ErrorId "VariableNotRemovable,Microsoft.PowerShell.Commands.RemoveVariableCommand" -PassThru
|
|
|
|
|
$e.CategoryInfo | Should -Match "SessionStateUnauthorizedAccessException"
|
2016-04-24 22:33:29 -07:00
|
|
|
Remove-Variable foo -Force
|
2018-05-17 14:42:04 -07:00
|
|
|
$var1 = Get-Variable -Name foo -ErrorAction SilentlyContinue
|
2018-03-23 10:26:23 -07:00
|
|
|
$var1 | Should -BeNullOrEmpty
|
2016-04-24 22:33:29 -07:00
|
|
|
}
|
2017-01-16 13:31:14 -08:00
|
|
|
|
2016-04-24 22:33:29 -07:00
|
|
|
It "Remove-Variable Constant variable should throw SessionStateUnauthorizedAccessException and force remove should also throw exception"{
|
|
|
|
|
New-Variable foo bar -Option Constant
|
2018-05-17 14:42:04 -07:00
|
|
|
$e = { Remove-Variable foo -Scope 1 -ErrorAction Stop } |
|
2018-03-23 10:26:23 -07:00
|
|
|
Should -Throw -ErrorId "VariableNotRemovable,Microsoft.PowerShell.Commands.RemoveVariableCommand" -PassThru
|
|
|
|
|
$e.CategoryInfo | Should -Match "SessionStateUnauthorizedAccessException"
|
2017-01-16 13:31:14 -08:00
|
|
|
|
2018-05-17 14:42:04 -07:00
|
|
|
{ Remove-Variable foo -Force -Scope 1 -ErrorAction Stop } |
|
2018-03-23 10:26:23 -07:00
|
|
|
Should -Throw -ErrorId "VariableNotRemovable,Microsoft.PowerShell.Commands.RemoveVariableCommand" -PassThru
|
|
|
|
|
$e.CategoryInfo | Should -Match "SessionStateUnauthorizedAccessException"
|
2016-04-24 22:33:29 -07:00
|
|
|
}
|
2017-01-16 13:31:14 -08:00
|
|
|
|
2016-04-24 22:33:29 -07:00
|
|
|
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
|
2018-05-17 14:42:04 -07:00
|
|
|
$e = { Get-Variable -Name foo -Scope local -ErrorAction Stop } |
|
2018-03-23 10:26:23 -07:00
|
|
|
Should -Throw -ErrorId "VariableNotFound,Microsoft.PowerShell.Commands.GetVariableCommand" -PassThru
|
|
|
|
|
$e.CategoryInfo | Should -Match "ItemNotFoundException"
|
2016-04-24 22:33:29 -07:00
|
|
|
}
|
2017-01-16 13:31:14 -08:00
|
|
|
|
2016-04-24 22:33:29 -07:00
|
|
|
$var1 = Get-Variable -Name foo
|
2018-03-23 10:26:23 -07:00
|
|
|
$var1.Name | Should -BeExactly "foo"
|
|
|
|
|
$var1.Value | Should -BeExactly "bar"
|
|
|
|
|
$var1.Options | Should -BeExactly "None"
|
|
|
|
|
$var1.Description | Should -BeExactly ""
|
2017-01-16 13:31:14 -08:00
|
|
|
|
2016-04-24 22:33:29 -07:00
|
|
|
}
|
|
|
|
|
}
|