@@ -8,12 +8,10 @@ Describe 'Mock an environment variable' {
88 Describe ' Code is called' {
99 BeforeAll {
1010 $script :EnvironmentVariableName = " test$ ( New-Guid ) "
11- $script :EnvironmentVariable = " env:${environmentVariableName} "
1211 $script :InitialValue = ' Some value here and there'
1312 }
1413
1514 It ' Code is called' {
16- $script :InitialValue = ' Some value here and there'
1715 $script :called = $false
1816 Mock- EnvironmentVariable - Variable $environmentVariableName - Value $InitialValue {
1917 $script :called = $true
@@ -22,80 +20,63 @@ Describe 'Mock an environment variable' {
2220 }
2321
2422 AfterAll {
25- Remove-Item `
26- - Path $EnvironmentVariable `
27- - Recurse `
28- - Force `
29- - ErrorAction Ignore
23+ [Environment ]::SetEnvironmentVariable($EnvironmentVariableName , $null )
3024 }
3125 }
26+
3227 Describe ' Environment variable is set up' {
3328 BeforeAll {
34- $environmentVariableName = " test$ ( New-Guid ) "
35- $environmentVariable = " env:${environmentVariableName} "
36- $InitialValue = ' Some value here and there'
29+ $script :environmentVariableName = " test$ ( New-Guid ) "
30+ $script :InitialValue = ' Some value here and there'
3731 }
3832
3933 It ' Environment variable is set up' {
40- Test-Path - Path $environmentVariable | Should - BeFalse
41- Mock- EnvironmentVariable - Variable $environmentVariable - Value $InitialValue {
42- ( Get-ChildItem - Path $environmentVariable ).Value | Should - Be $InitialValue
34+ [ Environment ]::GetEnvironmentVariable( $EnvironmentVariableName ) | Should - Be $null - Because ' environment variable should be reset '
35+ Mock- EnvironmentVariable - Variable $environmentVariableName - Value $InitialValue {
36+ [ Environment ]::GetEnvironmentVariable( $EnvironmentVariableName ) | Should - Be $InitialValue
4337 }
44- Test-Path - Path $environmentVariable | Should - BeFalse
38+ [ Environment ]::GetEnvironmentVariable( $EnvironmentVariableName ) | Should - Be $null - Because ' environment variable should be reset '
4539 }
4640
4741 AfterAll {
48- Remove-Item `
49- - Path $environmentVariable `
50- - Recurse `
51- - Force `
52- - ErrorAction Ignore
42+ [Environment ]::SetEnvironmentVariable($EnvironmentVariableName , $null )
5343 }
5444 }
5545
5646 Describe ' Environment variable is set' {
5747 BeforeAll {
5848 $script :environmentVariableName = " test$ ( New-Guid ) "
59- $script :environmentVariable = " env:${environmentVariableName} "
6049 $script :InitialValue = ' Some value here and there'
6150 $script :UpdatedValue = ' Some updated value'
62- New-Item - Path $environmentVariable - Value $InitialValue
51+ [ Environment ]::SetEnvironmentVariable( $environmentVariableName , $InitialValue )
6352 }
6453 It ' Environment variable is set up' {
6554 Mock- EnvironmentVariable - Variable $environmentVariableName - Value $UpdatedValue {
66- ( Get-ChildItem - Path $environmentVariable ).Value | Should - Be $UpdatedValue
55+ [ Environment ]::GetEnvironmentVariable( $environmentVariableName ) | Should - Be $UpdatedValue
6756 }
68- ( Get-ChildItem - Path $environmentVariable ).Value | Should - Be $InitialValue
57+ [ Environment ]::GetEnvironmentVariable( $environmentVariableName ) | Should - Be $InitialValue
6958 }
7059 AfterAll {
71- Remove-Item `
72- - Path $environmentVariable `
73- - Force `
74- - Recurse `
75- - ErrorAction Ignore
60+ [Environment ]::SetEnvironmentVariable($EnvironmentVariableName , $null )
7661 }
7762 }
7863
7964 Describe ' Restore variable that got destroyed' {
8065 BeforeAll {
8166 $script :environmentVariableName = " test$ ( New-Guid ) "
82- $script :environmentVariable = " env:${environmentVariableName} "
8367 $script :InitialValue = ' Some value here and there'
8468 $script :UpdatedValue = ' Some updated value'
85- New-Item - Path $environmentVariable - Value $InitialValue
69+
70+ [Environment ]::SetEnvironmentVariable($environmentVariableName , $InitialValue )
8671 Mock- EnvironmentVariable - Variable $environmentVariableName - Value $UpdatedValue {
87- Remove-Item - Path $environmentVariable - Recurse - Force - ErrorAction Ignore
72+ [ Environment ]::SetEnvironmentVariable( $environmentVariableName , $null )
8873 }
8974 }
9075 It ' Environment variable is set up' {
91- ( Get-ChildItem - Path $environmentVariable ).Value | Should - Be $InitialValue
76+ [ Environment ]::GetEnvironmentVariable( $environmentVariableName ) | Should - Be $InitialValue
9277 }
9378 AfterAll {
94- Remove-Item `
95- - Path $environmentVariable `
96- - Force `
97- - Recurse `
98- - ErrorAction Ignore
79+ [Environment ]::SetEnvironmentVariable($EnvironmentVariableName , $null )
9980 }
10081 }
10182
@@ -104,65 +85,51 @@ Describe 'Mock an environment variable' {
10485 $script :environmentVariableName = " test$ ( New-Guid ) "
10586 $script :environmentVariable = " env:${environmentVariableName} "
10687 $script :InitialValue = ' Some value here and there'
107- New-Item - Path $environmentVariable - Value $InitialValue
88+
89+ [Environment ]::SetEnvironmentVariable($environmentVariableName , $InitialValue )
10890 }
10991 It ' Test' {
11092 Mock- EnvironmentVariable - Variable $environmentVariableName {
111- ( Get-ChildItem - Path $environmentVariable ).Value | Should - Be $InitialValue
93+ [ Environment ]::GetEnvironmentVariable( $environmentVariableName ) | Should - Be $InitialValue
11294 }
11395 }
11496 AfterAll {
115- Remove-Item `
116- - Path $environmentVariable `
117- - Force `
118- - Recurse `
119- - ErrorAction Ignore
97+ [Environment ]::SetEnvironmentVariable($EnvironmentVariableName , $null )
12098 }
12199 }
122100
123101 Describe ' Do not create the variable if no value is specified' {
124102 BeforeAll {
125103 $script :environmentVariableName = " test$ ( New-Guid ) "
126- $script :environmentVariable = " env:${environmentVariableName} "
127- $script :InitialValue = ' Some value here and there'
128104 }
129105 It ' Test' {
130106 Mock- EnvironmentVariable - Variable $environmentVariableName {
131- Test-Path - Path $environmentVariable | Should - BeFalse
107+ [ Environment ]::GetEnvironmentVariable( $environmentVariableName ) | Should - Be $null
132108 }
133109 }
134110 AfterAll {
135- Remove-Item `
136- - Path $environmentVariable `
137- - Force `
138- - Recurse `
139- - ErrorAction Ignore
111+ [Environment ]::SetEnvironmentVariable($EnvironmentVariableName , $null )
140112 }
141113 }
142114
143115 Describe ' Initial value should be reasigned' {
144116 BeforeAll {
145117 $script :environmentVariableName = " test$ ( New-Guid ) "
146- $script :environmentVariable = " env:${environmentVariableName} "
147118 $script :InitialValue = ' Some value here and there'
148119 $script :UpdatedValue = ' Some updated value'
149120 $script :Message = ' Some message here and there'
150- New-Item - Path $environmentVariable - Value $InitialValue
121+ [ Environment ]::SetEnvironmentVariable( $environmentVariableName , $InitialValue )
151122 }
152123 It ' Throw' {
153124 { Mock- EnvironmentVariable `
154125 - Variable $environmentVariableName `
155126 - Value $UpdatedValue { throw $Message }
156127 } | Should - Throw - ExpectedMessage $Message
157- ( Get-ChildItem - Path $environmentVariable ).Value | Should - Be $InitialValue
128+ [ Environment ]::GetEnvironmentVariable( $environmentVariableName ) | Should - Be $InitialValue
158129 }
159130
160131 AfterAll {
161- Remove-Item `
162- - Path $environmentVariable `
163- - Force `
164- - Recurse `
165- - ErrorAction Ignore
132+ [Environment ]::SetEnvironmentVariable($EnvironmentVariableName , $null )
166133 }
167134 }
168135
@@ -179,38 +146,29 @@ Describe 'Mock an environment variable' {
179146 - Variable $environmentVariableName `
180147 - Value $UpdatedValue { throw $Message }
181148 } | Should - Throw - ExpectedMessage $Message
182- Test-Path - Path $environmentVariable | Should - BeFalse
149+ [ Environment ]::GetEnvironmentVariable( $environmentVariableName ) | Should - Be $null
183150 }
184151
185152 AfterAll {
186- Remove-Item `
187- - Path $environmentVariable `
188- - Force `
189- - Recurse `
190- - ErrorAction Ignore
153+ [Environment ]::SetEnvironmentVariable($EnvironmentVariableName , $null )
191154 }
192155 }
193156
194157 Describe ' Set up environment variable is cleared up' {
195158 BeforeAll {
196159 $environmentVariableName = " test$ ( New-Guid ) "
197- $script :environmentVariable = " env:${environmentVariableName} "
198160 }
199161
200162 It ' Set up environment variable is cleared up' {
201163 Mock- EnvironmentVariable `
202164 - Variable $environmentVariableName {
203165 New-Item - Path $environmentVariable - Value ' Some value'
204166 }
205- Test-Path - Path $environmentVariable | Should - BeFalse
167+ [ Environment ]::GetEnvironmentVariable( $environmentVariableName ) | Should - Be $null
206168 }
207169
208170 AfterAll {
209- Remove-Item `
210- - Path $environmentVariable `
211- - Recurse `
212- - Force `
213- - ErrorAction Ignore
171+ [Environment ]::SetEnvironmentVariable($EnvironmentVariableName , $null )
214172 }
215173 }
216174}
0 commit comments