From 26a8fc1909aaa1d46435d3eb3585c94c9d09b675 Mon Sep 17 00:00:00 2001 From: Malini Valliath Date: Tue, 14 Aug 2018 16:37:39 -0400 Subject: [PATCH] Remove registry item works if item already removed Signed-off-by: Simon Jones --- .../modules/BOSH.Utils/BOSH.Utils.Tests.ps1 | 35 +++++++++++- .../BOSH.WindowsUpdates.Tests.ps1 | 53 ------------------- 2 files changed, 34 insertions(+), 54 deletions(-) diff --git a/bosh-psmodules/modules/BOSH.Utils/BOSH.Utils.Tests.ps1 b/bosh-psmodules/modules/BOSH.Utils/BOSH.Utils.Tests.ps1 index b774c607..64a320fe 100644 --- a/bosh-psmodules/modules/BOSH.Utils/BOSH.Utils.Tests.ps1 +++ b/bosh-psmodules/modules/BOSH.Utils/BOSH.Utils.Tests.ps1 @@ -16,7 +16,40 @@ function Restore-RegistryState { Set-ItemProperty -path $KeyPath -Name $ValueName -Value $ValueData } } else { - Remove-Item -Path $KeyPath + Remove-Item -Path $KeyPath -ErrorAction SilentlyContinue + } +} + +Describe "Restore-RegistryState" { + BeforeEach { + Mock Remove-ItemProperty {} + Mock Set-ItemProperty {} + Mock Remove-Item {} + } + It "restores the registry by deleting a registry key created by the test" { + Restore-RegistryState -KeyExists $false -KeyPath "HKLM:\Some registry key" + + Assert-MockCalled Remove-Item -Times 1 -Scope It -ParameterFilter { $Path -eq "HKLM:\Some registry key" } + Assert-MockCalled Remove-ItemProperty -Times 0 -Scope It + Assert-MockCalled Set-ItemProperty -Times 0 -Scope It + } + + It "restores the registry by deleting a registry value created by the test" { + Restore-RegistryState -KeyExist $true -KeyPath "HKLM:\Some registry key" -ValueName "SomeValue" + + Assert-MockCalled Remove-Item -Times 0 -Scope It + Assert-MockCalled Remove-ItemProperty -Times 1 -Scope It -ParameterFilter { $Path -eq "HKLM:\Some registry key" -and $Name -eq "SomeValue"} + Assert-MockCalled Set-ItemProperty -Times 0 -Scope It + } + + It "restores the registry by restoring a registry data modified by the test" { + Restore-RegistryState -KeyExist $true -KeyPath "HKLM:\Some registry key" -ValueName "SomeValue" -ValueData "Some Data" + Restore-RegistryState -KeyExist $true -KeyPath "HKLM:\Some dword reg key" -ValueName "SomeDwordValye" -ValueData 85432 + + Assert-MockCalled Remove-Item -Times 0 -Scope It + Assert-MockCalled Remove-ItemProperty -Times 0 -Scope It + Assert-MockCalled Set-ItemProperty -Times 1 -Scope It -ParameterFilter { $Path -eq "HKLM:\Some registry key" -and $Name -eq "SomeValue" -and $Value -eq "Some Data" } + Assert-MockCalled Set-ItemProperty -Times 1 -Scope It -ParameterFilter { $Path -eq "HKLM:\Some dword reg key" -and $Name -eq "SomeDwordValye" -and $Value -eq 85432 } } } diff --git a/bosh-psmodules/modules/BOSH.WindowsUpdates/BOSH.WindowsUpdates.Tests.ps1 b/bosh-psmodules/modules/BOSH.WindowsUpdates/BOSH.WindowsUpdates.Tests.ps1 index 93b43673..88ff14f4 100644 --- a/bosh-psmodules/modules/BOSH.WindowsUpdates/BOSH.WindowsUpdates.Tests.ps1 +++ b/bosh-psmodules/modules/BOSH.WindowsUpdates/BOSH.WindowsUpdates.Tests.ps1 @@ -4,59 +4,6 @@ Import-Module ./BOSH.WindowsUpdates.psm1 Remove-Module -Name BOSH.Utils -ErrorAction Ignore Import-Module ../BOSH.Utils/BOSH.Utils.psm1 -#As of now, this function only supports DWords and Strings. -function Restore-RegistryState { - param( - [bool]$KeyExists, - [String]$KeyPath, - [String]$ValueName, - [PSObject]$ValueData - ) - if ($KeyExists) { - if ($ValueData -eq $null) { - Remove-ItemProperty -path $KeyPath -Name $ValueName - } else { - Set-ItemProperty -path $KeyPath -Name $ValueName -Value $ValueData - } - } else { - Remove-Item -Path $KeyPath - } -} - -Describe "Restore-RegistryState" { - BeforeEach { - Mock Remove-ItemProperty {} - Mock Set-ItemProperty {} - Mock Remove-Item {} - } - It "restores the registry by deleting a registry key created by the test" { - Restore-RegistryState -KeyExists $false -KeyPath "HKLM:\Some registry key" - - Assert-MockCalled Remove-Item -Times 1 -Scope It -ParameterFilter { $Path -eq "HKLM:\Some registry key" } - Assert-MockCalled Remove-ItemProperty -Times 0 -Scope It - Assert-MockCalled Set-ItemProperty -Times 0 -Scope It - } - - It "restores the registry by deleting a registry value created by the test" { - Restore-RegistryState -KeyExist $true -KeyPath "HKLM:\Some registry key" -ValueName "SomeValue" - - Assert-MockCalled Remove-Item -Times 0 -Scope It - Assert-MockCalled Remove-ItemProperty -Times 1 -Scope It -ParameterFilter { $Path -eq "HKLM:\Some registry key" -and $Name -eq "SomeValue"} - Assert-MockCalled Set-ItemProperty -Times 0 -Scope It - } - - It "restores the registry by restoring a registry data modified by the test" { - Restore-RegistryState -KeyExist $true -KeyPath "HKLM:\Some registry key" -ValueName "SomeValue" -ValueData "Some Data" - Restore-RegistryState -KeyExist $true -KeyPath "HKLM:\Some dword reg key" -ValueName "SomeDwordValye" -ValueData 85432 - - Assert-MockCalled Remove-Item -Times 0 -Scope It - Assert-MockCalled Remove-ItemProperty -Times 0 -Scope It - Assert-MockCalled Set-ItemProperty -Times 1 -Scope It -ParameterFilter { $Path -eq "HKLM:\Some registry key" -and $Name -eq "SomeValue" -and $Value -eq "Some Data" } - Assert-MockCalled Set-ItemProperty -Times 1 -Scope It -ParameterFilter { $Path -eq "HKLM:\Some dword reg key" -and $Name -eq "SomeDwordValye" -and $Value -eq 85432 } - } -} - - Describe "Disable-AutomaticUpdates" { BeforeEach {