diff --git a/src/PesterExtensions/Public/Mock-EnvironmentVariable.ps1 b/src/PesterExtensions/Public/Mock-EnvironmentVariable.ps1 index bc7e36e..5bfb149 100644 --- a/src/PesterExtensions/Public/Mock-EnvironmentVariable.ps1 +++ b/src/PesterExtensions/Public/Mock-EnvironmentVariable.ps1 @@ -1,23 +1,19 @@ function Restore { param ( - [string] - $Variable, - - [EnvironmentVariableTarget[]] - $Targets, - [hashtable] $Backup ) - foreach ($target in $targets) { - [Environment]::SetEnvironmentVariable($Variable, $Backup[$target], $target) + foreach ($variable in $Backup.Keys) { + foreach ($target in $Backup.$variable.Keys) { + [Environment]::SetEnvironmentVariable($variable, $Backup.$variable.$target, $target) + } } } -function Set-Values { +function Backup { param ( - [string] - $Variable, + [string[]] + $Variables, [EnvironmentVariableTarget[]] $Targets, @@ -26,13 +22,17 @@ function Set-Values { $value ) $Backup = @{} - - foreach ($target in $Targets) { - $OriginalValue = [Environment]::GetEnvironmentVariable($Variable, $target) - $Backup.Add($target, $OriginalValue) - if ($Value) { - [Environment]::SetEnvironmentVariable($Variable, $Value, $target) + + foreach ($variable in $Variables) { + $VariableBackup = @{} + foreach ($target in $Targets) { + $OriginalValue = [Environment]::GetEnvironmentVariable($variable, $target) + $VariableBackup.Add($target, $OriginalValue) + if ($Value) { + [Environment]::SetEnvironmentVariable($variable, $Value, $target) + } } + $Backup.Add($variable, $VariableBackup) } return $Backup @@ -42,7 +42,7 @@ function Set-Values { function Mock-EnvironmentVariable { param ( [parameter(Mandatory = $true)] - [string] + [string[]] $Variable, [Parameter(Mandatory = $true)] @@ -58,11 +58,10 @@ function Mock-EnvironmentVariable { $Targets = @([EnvironmentVariableTarget]::Process) ) - $Backup = Set-Values -Variable $Variable -Targets $Targets -value $Value + $Backup = Backup -Variables $Variable -Targets $Targets -value $Value try { Invoke-Command -ScriptBlock $Fixture } - catch { throw $_ } - finally { Restore -Variable $Variable -Targets $Targets -Backup $Backup } + finally { Restore -Backup $Backup } <# .PARAMETER Variable diff --git a/tests/PesterExtensions/Public/Mock-EnvironmentVariable.Tests.ps1 b/tests/PesterExtensions/Public/Mock-EnvironmentVariable.Tests.ps1 index 8b1b289..85385d9 100644 --- a/tests/PesterExtensions/Public/Mock-EnvironmentVariable.Tests.ps1 +++ b/tests/PesterExtensions/Public/Mock-EnvironmentVariable.Tests.ps1 @@ -288,6 +288,305 @@ Describe 'Check env mocking' -ForEach @( } } +Describe 'Check env mocking' -ForEach @( + @{ + Arguments = @{ + Value = 'Mocked Value'; + } + MockedTargets = @([EnvironmentVariableTarget]::Process) + InitialValue = 'Initial Value'; + ValueInsideTheScript = 'Mocked Value'; + Script = { } + } + @{ + Arguments = @{ + Value = 'Mocked Value' + } + MockedTargets = @([EnvironmentVariableTarget]::Process) + InitialValue = $null; + ValueInsideTheScript = 'Mocked Value'; + Script = { } + } + @{ + Arguments = @{ } + MockedTargets = @([EnvironmentVariableTarget]::Process) + InitialValue = 'Initial Value'; + ValueInsideTheScript = 'Initial Value'; + Script = { } + } + @{ + MockedTargets = @([EnvironmentVariableTarget]::Process) + Script = { } + } + @{ + Arguments = @{ + Value = 'Mocked Value' + } + MockedTargets = @([EnvironmentVariableTarget]::Process) + InitialValue = 'Initial Value'; + ValueInsideTheScript = 'Mocked Value'; + Script = { + foreach ($variable in $args[0]) { + [Environment]::SetEnvironmentVariable($variable, 'Some updated value') + } + } + } + @{ + Arguments = @{ } + MockedTargets = @([EnvironmentVariableTarget]::Process) + InitialValue = 'Initial Value'; + ValueInsideTheScript = 'Initial Value'; + Script = { + foreach ($variable in $args[0]) { + [Environment]::SetEnvironmentVariable($variable, 'Some updated value') + } + } + } + @{ + Arguments = @{ + Value = 'Mocked Value' + } + MockedTargets = @([EnvironmentVariableTarget]::Process) + InitialValue = $null; + ValueInsideTheScript = 'Mocked Value'; + Script = { + foreach ($variable in $args[0]) { + [Environment]::SetEnvironmentVariable($variable, 'Some updated value') + } + } + } + @{ + Arguments = @{ } + MockedTargets = @([EnvironmentVariableTarget]::Process) + InitialValue = $null; + ValueInsideTheScript = $null; + Script = { + foreach ($variable in $args[0]) { + [Environment]::SetEnvironmentVariable($variable, 'Some updated value') + } + } + } + @{ + Arguments = @{ + Value = 'Mocked Value'; + Targets = @([EnvironmentVariableTarget]::User) + } + MockedTargets = @([EnvironmentVariableTarget]::User) + InitialValue = 'Initial Value'; + ValueInsideTheScript = 'Mocked Value'; + Script = { } + } + @{ + Arguments = @{ + Value = 'Mocked Value'; + Targets = @([EnvironmentVariableTarget]::User) + } + MockedTargets = @([EnvironmentVariableTarget]::User) + InitialValue = $null; + ValueInsideTheScript = 'Mocked Value'; + Script = { } + } + @{ + Arguments = @{ + Targets = @([EnvironmentVariableTarget]::User) + } + MockedTargets = @([EnvironmentVariableTarget]::User) + InitialValue = 'Initial Value'; + ValueInsideTheScript = 'Initial Value'; + Script = { } + } + @{ + Arguments = @{ + Targets = @([EnvironmentVariableTarget]::User) + } + MockedTargets = @([EnvironmentVariableTarget]::User) + Script = { } + } + @{ + Arguments = @{ + Targets = @([EnvironmentVariableTarget]::User) + Value = 'Mocked Value' + } + MockedTargets = @([EnvironmentVariableTarget]::User) + InitialValue = 'Initial Value'; + ValueInsideTheScript = 'Mocked Value'; + Script = { + foreach ($variable in $args[0]) { + [Environment]::SetEnvironmentVariable($variable, 'Some updated value', 'User') + } + } + } + @{ + Arguments = @{ + Targets = @([EnvironmentVariableTarget]::User) + } + MockedTargets = @([EnvironmentVariableTarget]::User) + InitialValue = 'Initial Value'; + ValueInsideTheScript = 'Initial Value'; + Script = { + foreach ($variable in $args[0]) { + [Environment]::SetEnvironmentVariable($variable, 'Some updated value', 'User') + } + } + } + @{ + Arguments = @{ + Value = 'Mocked Value'; + Targets = @([EnvironmentVariableTarget]::User) + } + MockedTargets = @([EnvironmentVariableTarget]::User) + InitialValue = $null; + ValueInsideTheScript = 'Mocked Value'; + Script = { + foreach ($variable in $args[0]) { + [Environment]::SetEnvironmentVariable($variable, 'Some updated value', 'User') + } + } + } + @{ + Arguments = @{ + Targets = @([EnvironmentVariableTarget]::User) + } + MockedTargets = @([EnvironmentVariableTarget]::User) + InitialValue = $null; + ValueInsideTheScript = $null; + Script = { + foreach ($variable in $args[0]) { + [Environment]::SetEnvironmentVariable($variable, 'Some updated value', 'User') + } + } + } + + @{ + Arguments = @{ + Value = 'Mocked Value'; + Targets = @([EnvironmentVariableTarget]::User, [EnvironmentVariableTarget]::Process) + } + MockedTargets = @([EnvironmentVariableTarget]::User, [EnvironmentVariableTarget]::Process) + InitialValue = 'Initial Value'; + ValueInsideTheScript = 'Mocked Value'; + Script = { } + } + @{ + Arguments = @{ + Value = 'Mocked Value'; + Targets = @([EnvironmentVariableTarget]::User, [EnvironmentVariableTarget]::Process) + } + MockedTargets = @([EnvironmentVariableTarget]::User, [EnvironmentVariableTarget]::Process) + InitialValue = $null; + ValueInsideTheScript = 'Mocked Value'; + Script = { } + } + @{ + Arguments = @{ + Targets = @([EnvironmentVariableTarget]::User, [EnvironmentVariableTarget]::Process) + } + MockedTargets = @([EnvironmentVariableTarget]::User, [EnvironmentVariableTarget]::Process) + InitialValue = 'Initial Value'; + ValueInsideTheScript = 'Initial Value'; + Script = { } + } + @{ + Arguments = @{ + Targets = @([EnvironmentVariableTarget]::User, [EnvironmentVariableTarget]::Process) + } + MockedTargets = @([EnvironmentVariableTarget]::User, [EnvironmentVariableTarget]::Process) + Script = { } + } + @{ + Arguments = @{ + Targets = @([EnvironmentVariableTarget]::User, [EnvironmentVariableTarget]::Process) + Value = 'Mocked Value' + } + MockedTargets = @([EnvironmentVariableTarget]::User, [EnvironmentVariableTarget]::Process) + InitialValue = 'Initial Value'; + ValueInsideTheScript = 'Mocked Value'; + Script = { + foreach ($variable in $args[0]) { + [Environment]::SetEnvironmentVariable($variable, 'Some updated value', 'User') + [Environment]::SetEnvironmentVariable($variable, 'Some updated value', 'Process') + } + } + } + @{ + Arguments = @{ + Targets = @([EnvironmentVariableTarget]::User, [EnvironmentVariableTarget]::Process) + } + MockedTargets = @([EnvironmentVariableTarget]::User, [EnvironmentVariableTarget]::Process) + InitialValue = 'Initial Value'; + ValueInsideTheScript = 'Initial Value'; + Script = { + foreach ($variable in $args[0]) { + [Environment]::SetEnvironmentVariable($variable, 'Some updated value', 'User') + [Environment]::SetEnvironmentVariable($variable, 'Some updated value', 'Process') + } + } + } + @{ + Arguments = @{ + Value = 'Mocked Value'; + Targets = @([EnvironmentVariableTarget]::User, [EnvironmentVariableTarget]::Process) + } + MockedTargets = @([EnvironmentVariableTarget]::User, [EnvironmentVariableTarget]::Process) + InitialValue = $null; + ValueInsideTheScript = 'Mocked Value'; + Script = { + foreach ($variable in $args[0]) { + [Environment]::SetEnvironmentVariable($variable, 'Some updated value', 'User') + [Environment]::SetEnvironmentVariable($variable, 'Some updated value', 'Process') + } + } + } + @{ + Arguments = @{ + Targets = @([EnvironmentVariableTarget]::User, [EnvironmentVariableTarget]::Process) + } + MockedTargets = @([EnvironmentVariableTarget]::User, [EnvironmentVariableTarget]::Process) + InitialValue = $null; + ValueInsideTheScript = $null; + Script = { + [Environment]::SetEnvironmentVariable($args[0], 'Some updated value', 'User') + [Environment]::SetEnvironmentVariable($args[0], 'Some updated value', 'Process') + } + } +) { + BeforeAll { + $variables = @( + "test_$(New-Guid)" + "test_$(New-Guid)" + "test_$(New-Guid)" + ) + foreach ($target in $MockedTargets) { + foreach ($environmentVariableName in $variables) { + [Environment]::SetEnvironmentVariable($environmentVariableName, $InitialValue, $target) + } + } + } + It 'Test ' { + Mock-EnvironmentVariable -Variable $variables @arguments -Fixture { + foreach ($target in $MockedTargets) { + foreach ($environmentVariableName in $variables) { + [Environment]::GetEnvironmentVariable($environmentVariableName, $target) | Should -Be $ValueInsideTheScript + } + } + Invoke-Command -ScriptBlock $script -ArgumentList $variables + } + foreach ($target in $MockedTargets) { + foreach ($environmentVariableName in $variables) { + [Environment]::GetEnvironmentVariable($environmentVariableName, $target) | Should -Be $InitialValue + } + } + } + AfterAll { + foreach ($target in $MockedTargets) { + foreach ($environmentVariableName in $variables) { + [Environment]::SetEnvironmentVariable($environmentVariableName, $null, $target) + } + } + } +} + + Describe 'Should throw' -ForEach @( @{ InitialValue = 'Initial Value';