2016-03-04 23:48:30 +01:00
|
|
|
# ensure the machine is in a clean state from the outset.
|
2015-09-29 19:12:07 +02:00
|
|
|
Remove-Variable -Name var1 -ErrorAction SilentlyContinue -Force
|
|
|
|
|
2015-10-23 01:35:31 +02:00
|
|
|
Describe "Remove-Variable" {
|
2015-09-29 19:12:07 +02:00
|
|
|
It "Should throw an error when a dollar sign is used in the variable name place" {
|
2016-03-04 23:52:27 +01:00
|
|
|
New-Variable -Name var1 -Value 4
|
2015-09-29 19:12:07 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
Remove-Variable $var1 -ErrorAction SilentlyContinue | Should Throw
|
2015-09-29 19:12:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
It "Should not throw error when used without the Name field, and named variable is properly specified and exists" {
|
2016-03-04 23:52:27 +01:00
|
|
|
New-Variable -Name var1 -Value 4
|
2015-09-29 19:12:07 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
Remove-Variable var1
|
2015-09-29 19:12:07 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
$var1 | Should Be #nothing. it should be Nothing at all.
|
2015-09-29 19:12:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
It "Should not throw error when used with the Name field, and named variable is specified and exists" {
|
2016-03-04 23:52:27 +01:00
|
|
|
New-Variable -Name var1 -Value 2
|
2015-09-29 19:12:07 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
Remove-Variable -Name var1
|
2015-09-29 19:12:07 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
$var1 | Should Be #nothing. it should be Nothing at all.
|
2015-09-29 19:12:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
It "Should throw error when used with Name field, and named variable does not exist" {
|
2016-03-04 23:52:27 +01:00
|
|
|
Remove-Variable -Name nonexistantVariable -ErrorAction SilentlyContinue | Should Throw
|
2015-09-29 19:12:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
It "Should be able to remove a variable using the rv alias" {
|
2016-03-04 23:52:27 +01:00
|
|
|
New-Variable var1 -Value 2
|
2015-09-29 19:12:07 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
$var1 | Should Be 2
|
2015-09-29 19:12:07 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
rv -Name var1
|
2015-09-29 19:12:07 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
$var1 | Should Be #nothing. it should be Nothing at all.
|
2015-09-29 19:12:07 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
It "Should be able to remove a set of variables using wildcard characters" {
|
2016-03-04 23:52:27 +01:00
|
|
|
New-Variable tmpvar1 -Value "tempvalue"
|
|
|
|
New-Variable tmpvar2 -Value 2
|
|
|
|
New-Variable tmpmyvar1 -Value 234
|
2015-09-29 19:12:07 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
$tmpvar1 | Should Be "tempvalue"
|
|
|
|
$tmpvar2 | Should Be 2
|
|
|
|
$tmpmyvar1 | Should Be 234
|
2015-09-29 19:12:07 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
Remove-Variable -Name tmp*
|
2015-09-29 19:12:07 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
$tmpvar1 | Should Be #nothing. it should be Nothing at all.
|
|
|
|
$tmpvar2 | Should Be #nothing. it should be Nothing at all.
|
|
|
|
$tmpmyvar1 | Should Be #nothing. it should be Nothing at all.
|
2015-09-29 19:12:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
It "Should be able to exclude a set of variables to remove using the Exclude switch" {
|
2016-03-04 23:52:27 +01:00
|
|
|
New-Variable tmpvar1 -Value "tempvalue"
|
|
|
|
New-Variable tmpvar2 -Value 2
|
|
|
|
New-Variable tmpmyvar1 -Value 234
|
2015-09-29 19:12:07 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
$tmpvar1 | Should Be "tempvalue"
|
|
|
|
$tmpvar2 | Should Be 2
|
|
|
|
$tmpmyvar1 | Should Be 234
|
2015-09-29 19:12:07 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
Remove-Variable -Name tmp* -Exclude *my*
|
2015-09-29 19:12:07 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
$tmpvar1 | Should Be #nothing. it should be Nothing at all.
|
|
|
|
$tmpvar2 | Should Be #nothing. it should be Nothing at all.
|
|
|
|
$tmpmyvar1 | Should Be 234
|
2015-09-29 19:12:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
It "Should be able to include a set of variables to remove using the Include switch" {
|
2016-03-04 23:52:27 +01:00
|
|
|
New-Variable tmpvar1 -Value "tempvalue"
|
|
|
|
New-Variable tmpvar2 -Value 2
|
|
|
|
New-Variable tmpmyvar1 -Value 234
|
|
|
|
New-Variable thevar -Value 1
|
2015-09-29 19:12:07 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
$tmpvar1 | Should Be "tempvalue"
|
|
|
|
$tmpvar2 | Should Be 2
|
|
|
|
$tmpmyvar1 | Should Be 234
|
|
|
|
$thevar | Should Be 1
|
2015-09-29 19:12:07 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
Remove-Variable -Name tmp* -Include *my*
|
2015-09-29 19:12:07 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
$tmpvar1 | Should Be "tempvalue"
|
|
|
|
$tmpvar2 | Should Be 2
|
|
|
|
$tmpmyvar1 | Should Be #nothing. it should be Nothing at all.
|
|
|
|
$thevar | should Be 1
|
2015-09-29 19:12:07 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
Remove-Variable tmpvar1
|
|
|
|
Remove-Variable tmpvar2
|
|
|
|
Remove-Variable thevar
|
2015-09-29 19:12:07 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
It "Should throw an error when attempting to remove a read-only variable and the Force switch is not used" {
|
2016-03-04 23:52:27 +01:00
|
|
|
New-Variable -Name var1 -Value 2 -Option ReadOnly
|
2015-09-29 19:12:07 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
Remove-Variable -Name var1 -ErrorAction SilentlyContinue | Should Throw
|
2015-09-29 19:12:07 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
$var1 | Should Be 2
|
2015-09-29 19:12:07 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
Remove-Variable -Name var1 -Force
|
2015-09-29 19:12:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
It "Should not throw an error when attempting to remove a read-only variable and the Force switch is used" {
|
2016-03-04 23:52:27 +01:00
|
|
|
New-Variable -Name var1 -Value 2 -Option ReadOnly
|
2015-09-29 19:12:07 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
Remove-Variable -Name var1 -Force
|
2015-09-29 19:12:07 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
$var1 | Should Be # Nothing. It should be nothing at all.
|
2015-09-29 19:12:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Context "Scope Tests" {
|
2016-03-04 23:52:27 +01: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 19:12:07 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
Remove-Variable -Name var1 -Scope global
|
2015-09-29 19:12:07 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
$var1 | Should Be #Nothing.
|
|
|
|
}
|
2015-09-29 19:12:07 +02:00
|
|
|
|
2016-03-04 23:52:27 +01: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 19:12:07 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
Remove-Variable -Name var1 -Scope local -ErrorAction SilentlyContinue | Should Throw
|
2015-09-29 19:12:07 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
$var1 | Should Be "context"
|
2015-09-29 19:12:07 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
Remove-Variable -Name var1 -Scope global
|
|
|
|
$var1 | Should Be # Nothing
|
|
|
|
}
|
2015-09-29 19:12:07 +02:00
|
|
|
|
2016-03-04 23:52:27 +01: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 19:12:07 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
Remove-Variable -Name var1 -Scope local -ErrorAction SilentlyContinue | Should Throw
|
2015-09-29 19:12:07 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
$var1 | Should Be "context"
|
2015-09-29 19:12:07 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
Remove-Variable -Name var1 -Scope global
|
|
|
|
$var1 | Should Be # Nothing
|
|
|
|
}
|
2015-09-29 19:12:07 +02:00
|
|
|
|
2016-03-04 23:52:27 +01: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 19:12:07 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
Remove-Variable -Name var1 -Scope local -ErrorAction SilentlyContinue | Should Throw
|
2015-09-29 19:12:07 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
$var1 | Should Be # Nothing
|
|
|
|
}
|
2015-09-29 19:12:07 +02:00
|
|
|
|
2016-03-04 23:52:27 +01: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 19:12:07 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
Remove-Variable -Name var1 -Scope global -ErrorAction SilentlyContinue | Should Throw
|
2015-09-29 19:12:07 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
$var1 | Should Be "context"
|
2015-09-29 19:12:07 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
Remove-Variable -Name var1 -Scope local
|
|
|
|
$var1 | Should Be # Nothing
|
|
|
|
}
|
2015-09-29 19:12:07 +02:00
|
|
|
|
2016-03-04 23:52:27 +01: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 19:12:07 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
Remove-Variable -Name var1 -Scope script -ErrorAction SilentlyContinue | Should Throw
|
2015-09-29 19:12:07 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
$var1 | Should Be "context"
|
2015-09-29 19:12:07 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
Remove-Variable -Name var1 -Scope local
|
|
|
|
$var1 | Should Be # Nothing
|
|
|
|
}
|
2015-09-29 19:12:07 +02:00
|
|
|
|
2016-03-04 23:52:27 +01: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 19:12:07 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
Remove-Variable -Name var1 -Scope script | Should Throw
|
2015-09-29 19:12:07 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
$var1 | Should Be # Nothing
|
|
|
|
}
|
2015-09-29 19:12:07 +02:00
|
|
|
|
2016-03-04 23:52:27 +01: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 19:12:07 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
Remove-Variable -Name var1 -Scope global -ErrorAction SilentlyContinue | Should Throw
|
2015-09-29 19:12:07 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
$var1 | Should Be "context"
|
|
|
|
}
|
2015-09-29 19:12:07 +02:00
|
|
|
}
|
|
|
|
}
|