MegaBites-AI/Windows-powershell
0308
1# Copyright (c) Microsoft Corporation.2# Licensed under the MIT License.3 4# ensure the machine is in a clean state from the outset.5Remove-Variable -Name var1 -ErrorAction SilentlyContinue -Force6 7Describe "Remove-Variable" -Tags "CI" {8 It "Should throw an error when a dollar sign is used in the variable name place" {9 New-Variable -Name var1 -Value 410 11 { Remove-Variable $var1 -ErrorAction Stop } | Should -Throw -ErrorId "VariableNotFound,Microsoft.PowerShell.Commands.RemoveVariableCommand"12 }13 14 It "Should not throw error when used without the Name field, and named variable is properly specified and exists" {15 New-Variable -Name var1 -Value 416 17 Remove-Variable var118 19 $var1 | Should -BeNullOrEmpty20 { Get-Variable var1 -ErrorAction stop } |21 Should -Throw -ErrorId 'VariableNotFound,Microsoft.PowerShell.Commands.GetVariableCommand'22 }23 24 It "Should not throw error when used with the Name field, and named variable is specified and exists" {25 New-Variable -Name var1 -Value 226 27 Remove-Variable -Name var128 29 $var1 | Should -BeNullOrEmpty30 { Get-Variable var1 -ErrorAction stop } |31 Should -Throw -ErrorId 'VariableNotFound,Microsoft.PowerShell.Commands.GetVariableCommand'32 }33 34 It "Should throw error when used with Name field, and named variable does not exist" {35 { Remove-Variable -Name nonexistentVariable -ErrorAction Stop } | Should -Throw36 }37 38 It "Should be able to remove a set of variables using wildcard characters" {39 New-Variable tmpvar1 -Value "tempvalue"40 New-Variable tmpvar2 -Value 241 New-Variable tmpmyvar1 -Value 23442 43 $tmpvar1 | Should -BeExactly "tempvalue"44 $tmpvar2 | Should -Be 245 $tmpmyvar1 | Should -Be 23446 47 Remove-Variable -Name tmp*48 49 $tmpvar1 | Should -BeNullOrEmpty50 $tmpvar2 | Should -BeNullOrEmpty51 $tmpmyvar1 | Should -BeNullOrEmpty52 53 { Get-Variable tmpvar1 -ErrorAction stop } |54 Should -Throw -ErrorId 'VariableNotFound,Microsoft.PowerShell.Commands.GetVariableCommand'55 { Get-Variable tmpvar2 -ErrorAction stop } |56 Should -Throw -ErrorId 'VariableNotFound,Microsoft.PowerShell.Commands.GetVariableCommand'57 { Get-Variable tmpmyvar1 -ErrorAction stop } |58 Should -Throw -ErrorId 'VariableNotFound,Microsoft.PowerShell.Commands.GetVariableCommand'59 }60 61 It "Should be able to exclude a set of variables to remove using the Exclude switch" {62 New-Variable tmpvar1 -Value "tempvalue"63 New-Variable tmpvar2 -Value 264 New-Variable tmpmyvar1 -Value 23465 66 $tmpvar1 | Should -BeExactly "tempvalue"67 $tmpvar2 | Should -Be 268 $tmpmyvar1 | Should -Be 23469 70 Remove-Variable -Name tmp* -Exclude *my*71 72 $tmpvar1 | Should -BeNullOrEmpty73 $tmpvar2 | Should -BeNullOrEmpty74 $tmpmyvar1 | Should -Be 23475 76 { Get-Variable tmpvar1 -ErrorAction stop } |77 Should -Throw -ErrorId 'VariableNotFound,Microsoft.PowerShell.Commands.GetVariableCommand'78 { Get-Variable tmpvar2 -ErrorAction stop } |79 Should -Throw -ErrorId 'VariableNotFound,Microsoft.PowerShell.Commands.GetVariableCommand'80 }81 82 It "Should be able to include a set of variables to remove using the Include switch" {83 New-Variable tmpvar1 -Value "tempvalue"84 New-Variable tmpvar2 -Value 285 New-Variable tmpmyvar1 -Value 23486 New-Variable thevar -Value 187 88 $tmpvar1 | Should -BeExactly "tempvalue"89 $tmpvar2 | Should -Be 290 $tmpmyvar1 | Should -Be 23491 $thevar | Should -Be 192 93 Remove-Variable -Name tmp* -Include *my*94 95 $tmpvar1 | Should -BeExactly "tempvalue"96 $tmpvar2 | Should -Be 297 $tmpmyvar1 | Should -BeNullOrEmpty98 $thevar | Should -Be 199 100 { Get-Variable tmpmyvar1 -ErrorAction stop } |101 Should -Throw -ErrorId 'VariableNotFound,Microsoft.PowerShell.Commands.GetVariableCommand'102 103 Remove-Variable tmpvar1104 Remove-Variable tmpvar2105 Remove-Variable thevar106 107 }108 109 It "Should throw an error when attempting to remove a read-only variable and the Force switch is not used" {110 New-Variable -Name var1 -Value 2 -Option ReadOnly111 112 { Remove-Variable -Name var1 -ErrorAction Stop } | Should -Throw113 114 $var1 | Should -Be 2115 116 Remove-Variable -Name var1 -Force117 }118 119 It "Should not throw an error when attempting to remove a read-only variable and the Force switch is used" {120 New-Variable -Name var1 -Value 2 -Option ReadOnly121 122 Remove-Variable -Name var1 -Force123 124 $var1 | Should -BeNullOrEmpty125 }126 127 Context "Scope Tests" {128 It "Should be able to remove a global variable using the global switch" {129 New-Variable -Name var1 -Value "context" -Scope global130 131 Remove-Variable -Name var1 -Scope global132 133 $var1 | Should -BeNullOrEmpty134 }135 136 It "Should not be able to clear a global variable using the local switch" {137 New-Variable -Name var1 -Value "context" -Scope global138 139 { Remove-Variable -Name var1 -Scope local -ErrorAction Stop } | Should -Throw140 141 $var1 | Should -BeExactly "context"142 143 Remove-Variable -Name var1 -Scope global144 $var1 | Should -BeNullOrEmpty145 }146 147 It "Should not be able to clear a global variable using the script switch" {148 New-Variable -Name var1 -Value "context" -Scope global149 150 { Remove-Variable -Name var1 -Scope local -ErrorAction Stop } | Should -Throw151 152 $var1 | Should -BeExactly "context"153 154 Remove-Variable -Name var1 -Scope global155 $var1 | Should -BeNullOrEmpty156 }157 158 It "Should be able to remove an item locally using the local switch" {159 New-Variable -Name var1 -Value "context" -Scope local160 161 { Remove-Variable -Name var1 -Scope local -ErrorAction Stop } | Should -Throw162 163 $var1 | Should -Be context164 }165 166 It "Should be able to remove an item locally using the global switch" {167 New-Variable -Name var1 -Value "context" -Scope local168 169 { Remove-Variable -Name var1 -Scope global -ErrorAction Stop } | Should -Throw170 171 $var1 | Should -BeExactly "context"172 173 Remove-Variable -Name var1 -Scope local174 $var1 | Should -BeNullOrEmpty175 }176 177 It "Should be able to remove a local variable using the script scope switch" {178 New-Variable -Name var1 -Value "context" -Scope local179 180 { Remove-Variable -Name var1 -Scope script -ErrorAction Stop } | Should -Throw181 182 $var1 | Should -BeExactly "context"183 184 Remove-Variable -Name var1 -Scope local185 $var1 | Should -BeNullOrEmpty186 }187 188 It "Should be able to remove a script variable created using the script switch" {189 New-Variable -Name var1 -Value "context" -Scope script190 191 { Remove-Variable -Name var1 -Scope script } | Should -Not -Throw192 193 $var1 | Should -BeNullOrEmpty194 }195 196 It "Should not be able to remove a global script variable that was created using the script scope switch" {197 New-Variable -Name var1 -Value "context" -Scope script198 199 { Remove-Variable -Name var1 -Scope global -ErrorAction Stop } | Should -Throw200 201 $var1 | Should -BeExactly "context"202 }203 }204}205 206Describe "Remove-Variable basic functionality" -Tags "CI" {207 It "Remove-Variable variable should works"{208 New-Variable foo bar209 Remove-Variable foo210 $var1 = Get-Variable -Name foo -ErrorAction SilentlyContinue211 $var1 | Should -BeNullOrEmpty212 }213 214 It "Remove-Variable Constant variable should throw SessionStateUnauthorizedAccessException"{215 New-Variable foo bar -Option Constant216 $e = { Remove-Variable foo -Scope 1 -ErrorAction Stop } |217 Should -Throw -ErrorId "VariableNotRemovable,Microsoft.PowerShell.Commands.RemoveVariableCommand" -PassThru218 $e.CategoryInfo | Should -Match "SessionStateUnauthorizedAccessException"219 }220 221 It "Remove-Variable ReadOnly variable should throw SessionStateUnauthorizedAccessException and force remove should work"{222 New-Variable foo bar -Option ReadOnly223 $e = { Remove-Variable foo -Scope 1 -ErrorAction Stop } |224 Should -Throw -ErrorId "VariableNotRemovable,Microsoft.PowerShell.Commands.RemoveVariableCommand" -PassThru225 $e.CategoryInfo | Should -Match "SessionStateUnauthorizedAccessException"226 Remove-Variable foo -Force227 $var1 = Get-Variable -Name foo -ErrorAction SilentlyContinue228 $var1 | Should -BeNullOrEmpty229 }230 231 It "Remove-Variable Constant variable should throw SessionStateUnauthorizedAccessException and force remove should also throw exception"{232 New-Variable foo bar -Option Constant233 $e = { Remove-Variable foo -Scope 1 -ErrorAction Stop } |234 Should -Throw -ErrorId "VariableNotRemovable,Microsoft.PowerShell.Commands.RemoveVariableCommand" -PassThru235 $e.CategoryInfo | Should -Match "SessionStateUnauthorizedAccessException"236 237 { Remove-Variable foo -Force -Scope 1 -ErrorAction Stop } |238 Should -Throw -ErrorId "VariableNotRemovable,Microsoft.PowerShell.Commands.RemoveVariableCommand" -PassThru239 $e.CategoryInfo | Should -Match "SessionStateUnauthorizedAccessException"240 }241 242 It "Remove-Variable variable in new scope should works and Get-Variable with different scope should have different result"{243 New-Variable foo bar244 &{245 Clear-Variable foo246 Remove-Variable foo247 $e = { Get-Variable -Name foo -Scope local -ErrorAction Stop } |248 Should -Throw -ErrorId "VariableNotFound,Microsoft.PowerShell.Commands.GetVariableCommand" -PassThru249 $e.CategoryInfo | Should -Match "ItemNotFoundException"250 }251 252 $var1 = Get-Variable -Name foo253 $var1.Name | Should -BeExactly "foo"254 $var1.Value | Should -BeExactly "bar"255 $var1.Options | Should -BeExactly "None"256 $var1.Description | Should -BeExactly ""257 258 }259}260 