forked from PowerShell/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSet-Variable.Tests.ps1
More file actions
243 lines (197 loc) · 7.57 KB
/
Copy pathSet-Variable.Tests.ps1
File metadata and controls
243 lines (197 loc) · 7.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
Describe "Set-Variable DRT Unit Tests" -Tags "CI" {
It "Set-Variable normal variable Name should works"{
Set-Variable foo bar
$var1=Get-Variable -Name foo
$var1.Name|Should Be "foo"
$var1.Value|Should Be "bar"
$var1.Options|Should Be "None"
$var1.Description|Should Be ""
}
It "Set-Variable normal variable Name with position should works"{
Set-Variable -Name foo bar
$var1=Get-Variable -Name foo
$var1.Name|Should Be "foo"
$var1.Value|Should Be "bar"
$var1.Options|Should Be "None"
$var1.Description|Should Be ""
}
It "Set-Variable normal variable Name with scope should works"{
Set-Variable -Name foo -Value bar0
Set-Variable -Name foo -Value bar -Scope "1"
$var1=Get-Variable -Name foo -scope "1"
$var1.Name|Should Be "foo"
$var1.Value|Should Be "bar"
$var1.Options|Should Be "None"
$var1.Description|Should Be ""
Set-Variable -Name foo -Value newValue -Scope "local"
$var1=Get-Variable -Name foo -scope "local"
$var1.Name|Should Be "foo"
$var1.Value|Should Be "newValue"
$var1.Options|Should Be "None"
$var1.Description|Should Be ""
Set-Variable -Name foo -Value newValue2 -Scope "script"
$var1=Get-Variable -Name foo -scope "script"
$var1.Name|Should Be "foo"
$var1.Value|Should Be "newValue2"
$var1.Options|Should Be "None"
$var1.Description|Should Be ""
}
It "Set-Variable normal variable Name with position should works"{
Set-Variable abcaVar bar
Set-Variable bcdaVar anotherVal
Set-Variable aVarfoo bogusval
Set-Variable -Name "*aV*" -Value "overwrite" -Include "*Var*" -Exclude "bcd*"
$var1=Get-Variable -Name "*aVar*" -Scope "local"
$var1[0].Name|Should Be "abcaVar"
$var1[0].Value|Should Be "overwrite"
$var1[0].Options|Should Be "None"
$var1[0].Description|Should Be ""
$var1[1].Name|Should Be "aVarfoo"
$var1[1].Value|Should Be "overwrite"
$var1[1].Options|Should Be "None"
$var1[1].Description|Should Be ""
$var1[2].Name|Should Be "bcdaVar"
$var1[2].Value|Should Be "anotherVal"
$var1[2].Options|Should Be "None"
$var1[2].Description|Should Be ""
}
It "Set-Variable normal variable Name with Description and Value should works"{
Set-Variable foo bar
Set-Variable -Name foo $null -Description "new description" -PassThru:$true -Scope "local"
$var1=Get-Variable -Name foo -Scope "local"
$var1.Name|Should Be "foo"
$var1.Value|Should Be $null
$var1.Options|Should Be "None"
$var1.Description|Should Be "new description"
}
It "Set-Variable normal variable Name with just Description should works"{
Set-Variable foo bar
Set-Variable -Name foo -Description "new description" -PassThru:$true -Scope "local"
$var1=Get-Variable -Name foo -Scope "local"
$var1.Name|Should Be "foo"
$var1.Value|Should Be "bar"
$var1.Options|Should Be "None"
$var1.Description|Should Be "new description"
}
It "Set-Variable overwrite Constant Option should throw SessionStateUnauthorizedAccessException"{
Set-Variable -Name abcaVar bar -Option Constant -Scope "local"
try {
Set-Variable -Name abcaVar new -Scope "local" -EA Stop
Throw "Execution OK"
}
catch {
$_.FullyQualifiedErrorId | Should be "VariableNotWritable,Microsoft.PowerShell.Commands.SetVariableCommand"
}
}
It "Set-Variable of existing Private variable without force should throw Exception"{
Set-Variable abcaVar bar -Description "new description" -Option Private
$var1=Get-Variable -Name abcaVar
$var1.Name|Should Be "abcaVar"
$var1.Value|Should Be "bar"
$var1.Options|Should Be "Private"
$var1.Description|Should Be "new description"
Set-Variable abcaVar other -Description "new description"
$var1=Get-Variable -Name abcaVar
$var1.Name|Should Be "abcaVar"
$var1.Value|Should Be "other"
$var1.Options|Should Be "Private"
$var1.Description|Should Be "new description"
}
It "Set-Variable with Exclude, then Get-Variable it should throw ItemNotFoundException"{
Set-Variable -Name foo1,foo2 hello -Exclude foo2 -EA Stop
try {
Get-Variable -Name foo2 -EA Stop
Throw "Execution OK"
}
catch {
$_.FullyQualifiedErrorId | Should be "VariableNotFound,Microsoft.PowerShell.Commands.GetVariableCommand"
}
}
It "Set-Variable of existing ReadOnly variable without force should throw Exception"{
Set-Variable abcaVar bar -Description "new description" -Option ReadOnly
$var1=Get-Variable -Name abcaVar
$var1.Name|Should Be "abcaVar"
$var1.Value|Should Be "bar"
$var1.Options|Should Be "ReadOnly"
$var1.Description|Should Be "new description"
try {
Set-Variable abcaVar -Option None -EA Stop
Throw "Execution OK"
}
catch {
$_.FullyQualifiedErrorId | Should be "VariableNotWritable,Microsoft.PowerShell.Commands.SetVariableCommand"
}
}
It "Set-Variable of ReadOnly variable with private scope should work"{
Set-Variable foo bar -Description "new description" -Option ReadOnly -scope "private"
$var1=Get-Variable -Name foo
$var1.Name|Should Be "foo"
$var1.Value|Should Be "bar"
$var1.Options|Should Be "ReadOnly, Private"
$var1.Description|Should Be "new description"
}
It "Set-Variable pipeline with Get-Variable should work"{
$footest1="bar"
${Get-Variable footest1 -valueonly|Set-Variable bootest1 -passthru}
$var1=Get-Variable -Name footest1
$var1.Name|Should Be "footest1"
$var1.Value|Should Be "bar"
$var1.Options|Should Be "None"
$var1.Description|Should Be ""
}
}
Describe "Set-Variable" -Tags "CI" {
It "Should create a new variable with no parameters" {
{ Set-Variable testVar } | Should Not Throw
}
It "Should assign a value to a variable it has to create" {
Set-Variable -Name testVar -Value 4
Get-Variable testVar -ValueOnly | Should Be 4
}
It "Should change the value of an already existing variable" {
$testVar=1
$testVar | Should Not Be 2
Set-Variable testVar -Value 2
$testVar | Should Be 2
}
It "Should be able to be called with the set alias" {
set testVar -Value 1
$testVar | Should Be 1
}
It "Should be able to be called with the sv alias" {
sv testVar -Value 2
$testVar | Should Be 2
}
It "Should be able to set variable name using the Name parameter" {
Set-Variable -Name testVar -Value 1
$testVar | Should Be 1
}
It "Should be able to set the value of a variable by piped input" {
$testValue = "piped input"
$testValue | Set-Variable -Name testVar
$testVar | Should Be $testValue
}
It "Should be able to pipe object properties to output using the PassThru switch" {
$in = Set-Variable -Name testVar -Value "test" -Description "test description" -PassThru
$output = $in | Format-List -Property Description | Out-String
# This will cause errors running these tests in Windows
$output.Trim() | Should Be "Description : test description"
}
It "Should be able to set the value using the value switch" {
Set-Variable -Name testVar -Value 4
$testVar | Should Be 4
Set-Variable -Name testVar -Value "test"
$testVar | Should Be "test"
}
Context "Scope Tests" {
It "Should be able to set a global scope variable using the global switch" {
{ Set-Variable globalVar -Value 1 -Scope global -Force } | Should Not Throw
}
It "Should be able to set a global variable using the script scope switch" {
{ Set-Variable globalVar -Value 1 -Scope script -Force } | Should Not Throw
}
It "Should be able to set an item locally using the local switch" {
{ Set-Variable globalVar -Value 1 -Scope local -Force } | Should Not Throw
}
}
}