MegaBites-AI/Windows-powershell
0372
1# Copyright (c) Microsoft Corporation.2# Licensed under the MIT License.3Describe "Clear-Variable DRT Unit Tests" -Tags "CI" {4 It "Clear-Variable normal variable Name should works"{5 Set-Variable foo bar6 Clear-Variable -Name foo7 $var1=Get-Variable -Name foo8 $var1.Name | Should -BeExactly "foo"9 $var1.Value | Should -BeNullOrEmpty10 $var1.Options | Should -BeExactly "None"11 $var1.Description | Should -BeNullOrEmpty12 }13 14 It "Clear-Variable ReadOnly variable Name should throw exception and force Clear-Variable should works"{15 Set-Variable foo bar -Option ReadOnly16 17 $e = { Clear-Variable -Name foo -Scope 1 -ErrorAction Stop } | Should -Throw -ErrorId "VariableNotWritable,Microsoft.PowerShell.Commands.ClearVariableCommand" -PassThru18 $e.CategoryInfo | Should -Match "SessionStateUnauthorizedAccessException"19 20 Clear-Variable -Name foo -Force21 $var1=Get-Variable -Name foo22 $var1.Name | Should -BeExactly "foo"23 $var1.Value | Should -BeNullOrEmpty24 $var1.Options | Should -BeExactly "ReadOnly"25 $var1.Description | Should -BeNullOrEmpty26 }27 28 It "Clear-Variable normal variable Name with local scope should works"{29 Set-Variable foo bar30 &{31 Set-Variable foo baz32 $foo | Should -Be baz33 Clear-Variable -Name foo -Scope "local"34 35 $var1=Get-Variable -Name foo -Scope "local"36 $var1.Name | Should -BeExactly "foo"37 $var1.Value | Should -BeNullOrEmpty38 $var1.Options | Should -BeExactly "None"39 $var1.Description | Should -BeNullOrEmpty40 }41 42 $var1=Get-Variable -Name foo43 $var1.Name | Should -BeExactly "foo"44 $var1.Value | Should -BeExactly "bar"45 $var1.Options | Should -BeExactly "None"46 $var1.Description | Should -BeNullOrEmpty47 }48 49 It "Clear-Variable Private variable Name should works and Get-Variable with local scope should throw exception"{50 Set-Variable foo bar -Option Private51 &{52 $e = { Get-Variable -Name foo -Scope local -ErrorAction Stop } |53 Should -Throw -ErrorId "VariableNotFound,Microsoft.PowerShell.Commands.GetVariableCommand" -PassThru54 $e.CategoryInfo | Should -Match "ItemNotFoundException"55 }56 57 $var1=Get-Variable -Name foo58 $var1.Name | Should -BeExactly "foo"59 $var1.Value | Should -BeExactly "bar"60 $var1.Options | Should -BeExactly "Private"61 $var1.Description | Should -BeNullOrEmpty62 }63 64 It "Clear-Variable normal variable Name with local scope should works in different scope"{65 Set-Variable foo bar66 &{67 Set-Variable foo baz68 Clear-Variable -Name foo -Scope "local"69 70 $var1=Get-Variable -Name foo -Scope "local"71 $var1.Name | Should -BeExactly "foo"72 $var1.Value | Should -BeNullOrEmpty73 $var1.Options | Should -BeExactly "None"74 $var1.Description | Should -BeNullOrEmpty75 }76 77 $var1=Get-Variable -Name foo78 $var1.Name | Should -BeExactly "foo"79 $var1.Value | Should -BeExactly "bar"80 $var1.Options | Should -BeExactly "None"81 $var1.Description | Should -BeNullOrEmpty82 83 $var1=Get-Variable -Name foo -Scope "local"84 $var1.Name | Should -BeExactly "foo"85 $var1.Value | Should -BeExactly "bar"86 $var1.Options | Should -BeExactly "None"87 $var1.Description | Should -BeNullOrEmpty88 }89}90 91Describe "Clear-Variable" -Tags "CI" {92 BeforeEach {93 $var1 = 394 }95 96 It "Should be able to clear a variable using the Name switch" {97 Clear-Variable -Name var198 $var1 | Should -BeNullOrEmpty99 { Get-Variable var1 } | Should -Not -Throw100 }101 102 It "Should be able to clear a variable without using the Name switch" {103 Clear-Variable var1104 $var1 | Should -BeNullOrEmpty105 { Get-Variable var1 } | Should -Not -Throw106 }107 108 It "Should be able to include a set of variables to clear" {109 $var1 = 2110 $var2 = 3111 $var3 = 4112 $variable1 = 23113 $variable2 = 4114 $variable3 = 2115 $w = 3116 117 Clear-Variable -Name w, vari* -Include w, vari*118 119 $variable1 | Should -BeNullOrEmpty120 $variable2 | Should -BeNullOrEmpty121 $variable3 | Should -BeNullOrEmpty122 $w | Should -BeNullOrEmpty123 124 $var1 | Should -Not -BeNullOrEmpty125 $var2 | Should -Not -BeNullOrEmpty126 $var3 | Should -Not -BeNullOrEmpty127 }128 129 It "Should be able to exclude a set of variables to clear" {130 $var1 = 2131 $var2 = 3132 $var3 = 4133 $variable1 = 23134 $variable2 = 4135 $variable3 = 2136 $w = 3137 138 Clear-Variable -Name w, vari* -Exclude var*139 140 $variable1 | Should -Not -BeNullOrEmpty141 $variable2 | Should -Not -BeNullOrEmpty142 $variable3 | Should -Not -BeNullOrEmpty143 $var1 | Should -Not -BeNullOrEmpty144 $var2 | Should -Not -BeNullOrEmpty145 $var3 | Should -Not -BeNullOrEmpty146 147 $w | Should -BeNullOrEmpty148 }149 150 It "Should be able to pass the cleared object through the pipeline using the passthru switch" {151 { Clear-Variable -Name var1 -PassThru | Format-Wide -Property Value } | Should -Not -Throw152 }153 154 It "Should not clear environment variables" {155 $env:TEMPVARIABLE = "test data"156 157 { Clear-Variable -Name env:TEMPVARIABLE -ErrorAction Stop } | Should -Throw158 }159 160 It "Should clear variable even if it is read-only using the Force parameter" {161 try162 {163 New-Variable -Name var2 -Option ReadOnly -Value 100164 165 Clear-Variable -Name var1166 Clear-Variable -Name var2 -Force167 168 $var1 | Should -BeNullOrEmpty169 $var2 | Should -BeNullOrEmpty170 }171 finally172 {173 Remove-Variable -Name var2 -Force174 }175 }176 177 It "Should throw error when trying to clear variable that is read-only without using the Force parameter" {178 New-Variable -Name var2 -Option ReadOnly -Value 100179 { Clear-Variable -Name var2 -Scope 1 -ErrorAction stop } | Should -Throw -ErrorId "VariableNotWritable,Microsoft.PowerShell.Commands.ClearVariableCommand"180 181 $var2 | Should -Not -BeNullOrEmpty182 183 Remove-Variable -Name var2 -Force184 }185 186 Context "Scope Tests" {187 # This will violate the DRY principle. Tread softly.188 189 It "Should be able to clear a global scope variable using the global switch" {190 New-Variable globalVar -Value 1 -Scope global -Force191 192 Clear-Variable -Name globalVar -Scope global193 194 $globalVar | Should -BeNullOrEmpty195 }196 197 It "Should not be able to clear a global scope variable using the local switch" {198 New-Variable globalVar -Value 1 -Scope global -Force199 200 { Clear-Variable -Name globalVar -Scope local -ErrorAction Stop } |201 Should -Throw -ErrorId "VariableNotFound,Microsoft.PowerShell.Commands.ClearVariableCommand"202 }203 204 It "Should not be able to clear a global variable using the script scope switch" {205 New-Variable globalVar -Value 1 -Scope global -Force206 207 { Clear-Variable -Name localVar -Scope script -ErrorAction Stop } | Should -Throw -ErrorId "VariableNotFound,Microsoft.PowerShell.Commands.ClearVariableCommand"208 }209 210 It "Should be able to clear an item locally using the local switch" {211 New-Variable localVar -Value 2 -Scope local -Force212 213 Clear-Variable -Name localVar -Scope local214 215 $localVar | Should -BeNullOrEmpty216 217 { Clear-Variable -Name localVar -Scope script -ErrorAction Stop } | Should -Throw -ErrorId "VariableNotFound,Microsoft.PowerShell.Commands.ClearVariableCommand"218 }219 220 It "Should not be able to clear an item locally using the global switch" {221 New-Variable localVar -Value 2 -Scope local -Force222 223 { Clear-Variable -Name localVar -Scope global -ErrorAction Stop } | Should -Throw -ErrorId "VariableNotFound,Microsoft.PowerShell.Commands.ClearVariableCommand"224 }225 226 It "Should not be able to clear a local variable using the script scope switch" {227 New-Variable localVar -Value 2 -Scope local -Force228 229 { Clear-Variable -Name localVar -Scope script -ErrorAction Stop } | Should -Throw -ErrorId "VariableNotFound,Microsoft.PowerShell.Commands.ClearVariableCommand"230 }231 232 It "Should be able to clear a script variable created using the script switch" {233 {234 New-Variable -Name derp2 -Value 3 -Scope script -Force235 Clear-Variable -Name derp2 -Scope script236 } | Should -Not -Throw237 }238 239 It "Should be able to clear a global script variable that was created using the script scope switch" {240 {241 New-Variable -Name derpx -Value 4 -Scope script -Force242 Clear-Variable -Name derpx -Scope script243 } | Should -Not -Throw244 }245 }246}247 