MegaBites-AI/Windows-powershell
0372
1# Copyright (c) Microsoft Corporation.2# Licensed under the MIT License.3 4Describe "Set-Variable DRT Unit Tests" -Tags "CI" {5 It "Set-Variable normal variable Name should works"{6 Set-Variable foo bar7 $var1=Get-Variable -Name foo8 $var1.Name | Should -BeExactly "foo"9 $var1.Value | Should -BeExactly "bar"10 $var1.Options | Should -BeExactly "None"11 $var1.Description | Should -BeNullOrEmpty12 }13 14 It "Set-Variable normal variable Name with position should works"{15 Set-Variable -Name foo bar16 $var1=Get-Variable -Name foo17 $var1.Name | Should -BeExactly "foo"18 $var1.Value | Should -BeExactly "bar"19 $var1.Options | Should -BeExactly "None"20 $var1.Description | Should -BeNullOrEmpty21 }22 23 It "Set-Variable normal variable Name with scope should works"{24 Set-Variable -Name foo -Value bar025 26 Set-Variable -Name foo -Value bar -Scope "1"27 $var1=Get-Variable -Name foo -Scope "1"28 $var1.Name | Should -BeExactly "foo"29 $var1.Value | Should -BeExactly "bar"30 $var1.Options | Should -BeExactly "None"31 $var1.Description | Should -BeNullOrEmpty32 33 Set-Variable -Name foo -Value newValue -Scope "local"34 $var1=Get-Variable -Name foo -Scope "local"35 $var1.Name | Should -BeExactly "foo"36 $var1.Value | Should -BeExactly "newValue"37 $var1.Options | Should -BeExactly "None"38 $var1.Description | Should -BeNullOrEmpty39 40 Set-Variable -Name foo -Value newValue2 -Scope "script"41 $var1=Get-Variable -Name foo -Scope "script"42 $var1.Name | Should -BeExactly "foo"43 $var1.Value | Should -BeExactly "newValue2"44 $var1.Options | Should -BeExactly "None"45 $var1.Description | Should -BeNullOrEmpty46 }47 48 It "Set-Variable normal variable Name with position should works"{49 Set-Variable abcaVar bar50 Set-Variable bcdaVar anotherVal51 Set-Variable aVarfoo bogusval52 53 Set-Variable -Name "*aV*" -Value "overwrite" -Include "*Var*" -Exclude "bcd*"54 55 $var1=Get-Variable -Name "*aVar*" -Scope "local"56 $var1[0].Name | Should -BeExactly "abcaVar"57 $var1[0].Value | Should -BeExactly "overwrite"58 $var1[0].Options | Should -BeExactly "None"59 $var1[0].Description | Should -BeNullOrEmpty60 61 $var1[1].Name | Should -BeExactly "aVarfoo"62 $var1[1].Value | Should -BeExactly "overwrite"63 $var1[1].Options | Should -BeExactly "None"64 $var1[1].Description | Should -BeNullOrEmpty65 66 $var1[2].Name | Should -BeExactly "bcdaVar"67 $var1[2].Value | Should -BeExactly "anotherVal"68 $var1[2].Options | Should -BeExactly "None"69 $var1[2].Description | Should -BeNullOrEmpty70 }71 72 It "Set-Variable normal variable Name with Description and Value should works"{73 Set-Variable foo bar74 Set-Variable -Name foo $null -Description "new description" -PassThru:$true -Scope "local"75 $var1=Get-Variable -Name foo -Scope "local"76 $var1.Name | Should -BeExactly "foo"77 $var1.Value | Should -BeNullOrEmpty78 $var1.Options | Should -BeExactly "None"79 $var1.Description | Should -BeExactly "new description"80 }81 82 It "Set-Variable normal variable Name with just Description should works"{83 Set-Variable foo bar84 Set-Variable -Name foo -Description "new description" -PassThru:$true -Scope "local"85 $var1=Get-Variable -Name foo -Scope "local"86 $var1.Name | Should -BeExactly "foo"87 $var1.Value | Should -BeExactly "bar"88 $var1.Options | Should -BeExactly "None"89 $var1.Description | Should -BeExactly "new description"90 }91 92 It "Set-Variable overwrite Constant Option should throw SessionStateUnauthorizedAccessException"{93 Set-Variable -Name abcaVar bar -Option Constant -Scope "local"94 { Set-Variable -Name abcaVar new -Scope 1 -ErrorAction Stop } | Should -Throw -ErrorId "VariableNotWritable,Microsoft.PowerShell.Commands.SetVariableCommand"95 }96 97 It "Set-Variable of existing Private variable without force should throw Exception"{98 Set-Variable abcaVar bar -Description "new description" -Option Private99 $var1=Get-Variable -Name abcaVar100 $var1.Name | Should -BeExactly "abcaVar"101 $var1.Value | Should -BeExactly "bar"102 $var1.Options | Should -BeExactly "Private"103 $var1.Description | Should -BeExactly "new description"104 105 Set-Variable abcaVar other -Description "new description"106 $var1=Get-Variable -Name abcaVar107 $var1.Name | Should -BeExactly "abcaVar"108 $var1.Value | Should -BeExactly "other"109 $var1.Options | Should -BeExactly "Private"110 $var1.Description | Should -BeExactly "new description"111 }112 113 It "Set-Variable with Exclude, then Get-Variable it should throw ItemNotFoundException"{114 Set-Variable -Name foo1,foo2 hello -Exclude foo2 -ErrorAction Stop115 { Get-Variable -Name foo2 -ErrorAction Stop } | Should -Throw -ErrorId "VariableNotFound,Microsoft.PowerShell.Commands.GetVariableCommand"116 }117 118 It "Set-Variable of existing ReadOnly variable without force should throw Exception"{119 Set-Variable abcaVar bar -Description "new description" -Option ReadOnly120 $var1=Get-Variable -Name abcaVar121 $var1.Name | Should -BeExactly "abcaVar"122 $var1.Value | Should -BeExactly "bar"123 $var1.Options | Should -BeExactly "ReadOnly"124 $var1.Description | Should -BeExactly "new description"125 { Set-Variable abcaVar -Option None -Scope 1 -ErrorAction Stop } | Should -Throw -ErrorId "VariableNotWritable,Microsoft.PowerShell.Commands.SetVariableCommand"126 }127 128 It "Set-Variable of ReadOnly variable with private scope should work"{129 Set-Variable foo bar -Description "new description" -Option ReadOnly -Scope "private"130 $var1=Get-Variable -Name foo131 $var1.Name | Should -BeExactly "foo"132 $var1.Value | Should -BeExactly "bar"133 $var1.Options | Should -BeExactly "ReadOnly, Private"134 $var1.Description | Should -BeExactly "new description"135 }136 137 It "Set-Variable pipeline with Get-Variable should work"{138 $footest1="bar"139 ${Get-Variable footest1 -valueonly|Set-Variable bootest1 -passthru}140 $var1=Get-Variable -Name footest1141 $var1.Name | Should -BeExactly "footest1"142 $var1.Value | Should -BeExactly "bar"143 $var1.Options | Should -BeExactly "None"144 $var1.Description | Should -BeNullOrEmpty145 }146}147 148Describe "Set-Variable" -Tags "CI" {149 BeforeAll {150 if ($null -ne $PSStyle) {151 $outputRendering = $PSStyle.OutputRendering152 $PSStyle.OutputRendering = 'plaintext'153 }154 155 $expectedContent = "some test text"156 $inObject = New-Object psobject -Property @{text=$expectedContent}157 $testfile = Join-Path -Path $TestDrive -ChildPath outfileTest.txt158 }159 160 AfterAll {161 if ($null -ne $PSStyle) {162 $PSStyle.OutputRendering = $outputRendering163 }164 }165 166 It "Should create a new variable with no parameters" {167 { Set-Variable testVar } | Should -Not -Throw168 }169 170 It "Should assign a value to a variable it has to create" {171 Set-Variable -Name testVar -Value 4172 173 Get-Variable testVar -ValueOnly | Should -Be 4174 }175 176 It "Should change the value of an already existing variable" {177 $testVar=1178 179 $testVar | Should -Not -Be 2180 181 Set-Variable testVar -Value 2182 183 $testVar | Should -Be 2184 }185 186 It "Should be able to be called with the set alias" {187 set testVar -Value 1188 189 $testVar | Should -Be 1190 }191 192 It "Should be able to be called with the sv alias" {193 sv testVar -Value 2194 195 $testVar | Should -Be 2196 }197 198 It "Should be able to set variable name using the Name parameter" {199 Set-Variable -Name testVar -Value 1200 201 $testVar | Should -Be 1202 }203 204 It "Should be able to set the value of a variable by piped input" {205 $testValue = "piped input"206 207 $testValue | Set-Variable -Name testVar208 209 $testVar | Should -Be $testValue210 }211 212 It "Should be able to pipe object properties to output using the PassThru switch" {213 $in = Set-Variable -Name testVar -Value "test" -Description "test description" -PassThru214 215 $output = $in | Format-List -Property Description | Out-String216 217 # This will cause errors running these tests in Windows218 $output.Trim() | Should -BeExactly "Description : test description"219 }220 221 It "Should be able to set the value using the value switch" {222 Set-Variable -Name testVar -Value 4223 224 $testVar | Should -Be 4225 226 Set-Variable -Name testVar -Value "test"227 228 $testVar | Should -BeExactly "test"229 }230 231 Context "Scope Tests" {232 It "Should be able to set a global scope variable using the global switch" {233 { Set-Variable globalVar -Value 1 -Scope global -Force } | Should -Not -Throw234 }235 236 It "Should be able to set a global variable using the script scope switch" {237 { Set-Variable globalVar -Value 1 -Scope script -Force } | Should -Not -Throw238 }239 240 It "Should be able to set an item locally using the local switch" {241 { Set-Variable globalVar -Value 1 -Scope local -Force } | Should -Not -Throw242 }243 }244 245 Context "Set-Variable -Append tests" {246 BeforeAll {247 $testCases = @{ value = 2; Count = 2 },248 @{ value = @(2,3,4); Count = 2},249 @{ value = "abc",(Get-Process -Id $PID) ; count = 2}250 }251 252 It "Can append values <value> to a variable" -testCases $testCases {253 param ($value, $count)254 255 $variableName = "testVar"256 Set-Variable -Name $variableName -Value 1257 Set-Variable -Name $variableName -Value $value -Append258 259 $observedValues = Get-Variable $variableName -Value260 261 $observedValues.Count | Should -Be $count262 $observedValues[0] | Should -Be 1263 264 $observedValues[1] | Should -Be $value265 }266 267 It "Can use set-variable via streaming and append values" {268 $testVar = 1269 4..6 | Set-Variable -Name testVar -Append270 $testVar | Should -Be @(1,4,5,6)271 }272 }273}274 