Skip to content

Commit

Permalink
feat: Update Mock-EnvironmentVariable
Browse files Browse the repository at this point in the history
Add the posibility to mock multiple environment variables at once.
  • Loading branch information
petru committed Jul 28, 2022
1 parent 441802b commit 8456537
Show file tree
Hide file tree
Showing 2 changed files with 319 additions and 21 deletions.
41 changes: 20 additions & 21 deletions src/PesterExtensions/Public/Mock-EnvironmentVariable.ps1
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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
Expand All @@ -42,7 +42,7 @@ function Set-Values {
function Mock-EnvironmentVariable {
param (
[parameter(Mandatory = $true)]
[string]
[string[]]
$Variable,

[Parameter(Mandatory = $true)]
Expand All @@ -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
Expand Down
299 changes: 299 additions & 0 deletions tests/PesterExtensions/Public/Mock-EnvironmentVariable.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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 <MockedTargets>' {
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';
Expand Down

0 comments on commit 8456537

Please sign in to comment.