MegaBites-AI/Windows-powershell
0308
1# Copyright (c) Microsoft Corporation.2# Licensed under the MIT License.3 4Describe "New-Variable DRT Unit Tests" -Tags "CI" {5 It "New-Variable variable with description should works"{6 New-Variable foo bar -Description "my description"7 $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 -BeExactly "my description"12 }13 14 It "New-Variable variable with option should works"{15 New-Variable foo bar -Option Constant16 $var1=Get-Variable -Name foo17 $var1.Name | Should -BeExactly "foo"18 $var1.Value | Should -BeExactly "bar"19 $var1.Options | Should -BeExactly "Constant"20 $var1.Description | Should -BeNullOrEmpty21 }22 23 It "New-Variable variable twice should throw Exception"{24 New-Variable foo bogus25 26 $e = { New-Variable foo bar -Scope 1 -ErrorAction Stop } |27 Should -Throw -ErrorId "VariableAlreadyExists,Microsoft.PowerShell.Commands.NewVariableCommand" -PassThru28 $e.CategoryInfo | Should -Match "SessionStateException"29 30 New-Variable foo bar -Force -PassThru31 $var1=Get-Variable -Name foo32 $var1.Name | Should -BeExactly "foo"33 $var1.Value | Should -BeExactly "bar"34 $var1.Options | Should -BeExactly "None"35 $var1.Description | Should -BeNullOrEmpty36 }37 38 It "New-Variable ReadOnly variable twice should throw Exception"{39 New-Variable foo bogus -Option ReadOnly40 41 $e = { New-Variable foo bar -Scope 1 -ErrorAction Stop } |42 Should -Throw -ErrorId "VariableAlreadyExists,Microsoft.PowerShell.Commands.NewVariableCommand" -PassThru43 $e.CategoryInfo | Should -Match "SessionStateException"44 45 New-Variable foo bar -Force -PassThru46 $var1=Get-Variable -Name foo47 $var1.Name | Should -BeExactly "foo"48 $var1.Value | Should -BeExactly "bar"49 $var1.Options | Should -BeExactly "None"50 $var1.Description | Should -BeNullOrEmpty51 }52}53 54Describe "New-Variable" -Tags "CI" {55 It "Should create a new variable with no parameters" {56 { New-Variable var1 } | Should -Not -Throw57 }58 59 It "Should be able to set variable name using the Name parameter" {60 { New-Variable -Name var1 } | Should -Not -Throw61 }62 63 It "Should be able to assign a value to a variable using the value switch" {64 New-Variable var1 -Value 465 66 $var1 | Should -Be 467 }68 69 It "Should be able to assign a value to a new variable without using the value switch" {70 New-Variable var1 "test"71 72 $var1 | Should -BeExactly "test"73 }74 75 It "Should assign a description to a new variable using the description switch" {76 New-Variable var1 100 -Description "Test Description"77 78 (Get-Variable var1).Description | Should -BeExactly "Test Description"79 }80 81 It "Should not be able to set the name of a new variable to that of an old variable within same scope when the Force switch is missing" {82 New-Variable var183 { New-Variable var1 -Scope 1 -ErrorAction Stop } | Should -Throw -ErrorId "VariableAlreadyExists,Microsoft.PowerShell.Commands.NewVariableCommand"84 }85 86 It "Should change the value of an already existing variable using the Force switch" {87 New-Variable var1 -Value 188 89 $var1 | Should -Be 190 91 New-Variable var1 -Value 2 -Force92 93 $var1 | Should -Be 294 $var1 | Should -Not -Be 195 96 }97 98 It "Should be able to set the value of a variable by piped input" {99 $in = "value"100 101 $in | New-Variable -Name var1102 103 $var1 | Should -Be $in104 105 }106 107 It "Should be able to pipe object properties to output using the PassThru switch" {108 $in = Set-Variable -Name testVar -Value "test" -Description "test description" -PassThru109 110 $in.Description | Should -BeExactly "test description"111 }112 113 It "Should be able to set the value using the value switch" {114 New-Variable -Name var1 -Value 2115 116 $var1 | Should -Be 2117 }118 119 Context "Option tests" {120 It "Should be able to use the options switch without error" {121 { New-Variable -Name var1 -Value 2 -Option Unspecified } | Should -Not -Throw122 }123 124 It "Should default to none as the value for options" {125 (New-Variable -Name var2 -Value 4 -PassThru).Options | Should -BeExactly "None"126 }127 128 It "Should be able to set ReadOnly option" {129 { New-Variable -Name var1 -Value 2 -Option ReadOnly } | Should -Not -Throw130 }131 132 It "Should not be able to change variable created using the ReadOnly option when the Force switch is not used" {133 New-Variable -Name var1 -Value 1 -Option ReadOnly134 135 Set-Variable -Name var1 -Value 2 -ErrorAction SilentlyContinue136 137 $var1 | Should -Not -Be 2138 }139 140 It "Should be able to set a new variable to constant" {141 { New-Variable -Name var1 -Option Constant } | Should -Not -Throw142 }143 144 It "Should not be able to change an existing variable to constant" {145 New-Variable -Name var1 -Value 1 -PassThru146 147 Set-Variable -Name var1 -Option Constant -ErrorAction SilentlyContinue148 149 (Get-Variable var1).Options | Should -BeExactly "None"150 }151 152 It "Should not be able to delete a constant variable" {153 New-Variable -Name var1 -Value 2 -Option Constant154 155 Remove-Variable -Name var1 -ErrorAction SilentlyContinue156 157 $var1 | Should -Be 2158 }159 160 It "Should not be able to change a constant variable" {161 New-Variable -Name var1 -Value 1 -Option Constant162 163 Set-Variable -Name var1 -Value 2 -ErrorAction SilentlyContinue164 165 $var1 | Should -Not -Be 2166 }167 168 It "Should be able to create a variable as private without error" {169 { New-Variable -Name var1 -Option Private } | Should -Not -Throw170 }171 172 It "Should be able to see the value of a private variable when within scope" {173 174 New-Variable -Name var1 -Value 100 -Option Private175 176 $var1 | Should -Be 100177 178 }179 180 It "Should not be able to see the value of a private variable when out of scope" {181 {New-Variable -Name var1 -Value 1 -Option Private} | Should -Not -Throw182 183 $var1 | Should -BeNullOrEmpty184 }185 186 It "Should be able to use the AllScope switch without error" {187 { New-Variable -Name var1 -Option AllScope } | Should -Not -Throw188 }189 190 It "Should be able to see variable created using the AllScope switch in a child scope" {191 New-Variable -Name var1 -Value 1 -Option AllScope192 &{ $var1 = 2 }193 $var1 | Should -Be 2194 }195 196 }197 198 Context "Scope Tests" {199 BeforeAll {200 if ( Get-Variable -Scope global -Name globalVar1 -ErrorAction SilentlyContinue )201 {202 Remove-Variable -Scope global -Name globalVar1203 }204 if ( Get-Variable -Scope script -Name scriptvar -ErrorAction SilentlyContinue )205 {206 Remove-Variable -Scope script -Name scriptvar207 }208 # no check for local scope variable as that scope is created with test invocation209 }210 AfterAll {211 if ( Get-Variable -Scope global -Name globalVar1 )212 {213 Remove-Variable -Scope global -Name globalVar1214 }215 if ( Get-Variable -Scope script -Name scriptvar )216 {217 Remove-Variable -Scope script -Name scriptvar218 }219 }220 It "Should be able to create a global scope variable using the global switch" {221 New-Variable -Scope global -Name globalvar1 -Value 1222 Get-Variable -Scope global -Name globalVar1 -ValueOnly | Should -Be 1223 }224 It "Should be able to create a local scope variable using the local switch" {225 Get-Variable -Scope local -Name localvar -ValueOnly -ErrorAction silentlycontinue | Should -BeNullOrEmpty226 New-Variable -Scope local -Name localVar -Value 10227 Get-Variable -Scope local -Name localvar -ValueOnly | Should -Be 10228 }229 It "Should be able to create a script scope variable using the script switch" {230 New-Variable -Scope script -Name scriptvar -Value 100231 Get-Variable -Scope script -Name scriptvar -ValueOnly | Should -Be 100232 }233 }234}235 