From db4916dba7264fa8a58c7f801c95dc123798d3e1 Mon Sep 17 00:00:00 2001 From: David Nelson Date: Fri, 9 Apr 2021 02:08:49 -0700 Subject: [PATCH] Additional code review changes --- .../DSC_WindowsEventLog.psm1 | 2 +- tests/Unit/DSC_WindowsEventLog.Tests.ps1 | 53 +++++++++++-------- 2 files changed, 32 insertions(+), 23 deletions(-) diff --git a/source/DSCResources/DSC_WindowsEventLog/DSC_WindowsEventLog.psm1 b/source/DSCResources/DSC_WindowsEventLog/DSC_WindowsEventLog.psm1 index 3c0251bc..b61e0f66 100644 --- a/source/DSCResources/DSC_WindowsEventLog/DSC_WindowsEventLog.psm1 +++ b/source/DSCResources/DSC_WindowsEventLog/DSC_WindowsEventLog.psm1 @@ -991,7 +991,7 @@ function Set-WindowsEventLogRetentionDays else { $message = $script:localizedData.SetWindowsEventLogRetentionDaysWrongMode -f $LogName - New-InvalidOperationException -Message $message -ErrorRecord $_ + New-InvalidArgumentException -Message $message -ArgumentName 'LogMode' } } diff --git a/tests/Unit/DSC_WindowsEventLog.Tests.ps1 b/tests/Unit/DSC_WindowsEventLog.Tests.ps1 index 5110932d..d84e7261 100644 --- a/tests/Unit/DSC_WindowsEventLog.Tests.ps1 +++ b/tests/Unit/DSC_WindowsEventLog.Tests.ps1 @@ -181,6 +181,13 @@ try ) Describe "DSC_WindowsEventLog\Get-TargetResource" -Tag 'Get' { + Context 'When getting a request for a non-existent target resource' { + $errorMessage = $script:localizedData.GetWindowsEventLogFailure -f 'UndefinedLog' + It 'Should throw when an event log does not exist' { + { Get-TargetResource -LogName 'UndefinedLog' } | Should -Throw $errorMessage + } + } + Context 'When getting the default target resource' { Mock -CommandName Get-EventLog -MockWith { return $mocks.GetEventLogAppLogDefaults @@ -526,15 +533,23 @@ try } It 'Should throw when the New-EventLog cmdlet encounters an error' { - Mock -CommandName New-EventLog -MockWith { throw } + Mock -CommandName New-EventLog -MockWith { throw 'New-EventLog Error' } - { Register-WindowsEventLogSource -LogName 'Application' -SourceName 'PesterTest' } | Should -Throw + { Register-WindowsEventLogSource ` + -LogName 'Application' ` + -SourceName 'PesterTest' + } | Should -Throw 'New-EventLog Error' } It 'Should throw when the Remove-EventLog cmdlet encounters an error' { - Mock -CommandName Remove-EventLog -MockWith { throw } + Mock -CommandName Remove-EventLog -MockWith { throw } + $errorRecord = Get-InvalidOperationRecord ` + -Message ($script:localizedData.RegisterWindowsEventLogSourceFailure -f 'Application', '') - { Register-WindowsEventLogSource -LogName 'Application' -SourceName 'PesterTest' } | Should -Throw + { Register-WindowsEventLogSource ` + -LogName 'Application' ` + -SourceName 'PesterTest' + } | Should -Throw $errorRecord } } } @@ -543,13 +558,13 @@ try Context 'When testing a request with values that are out of bounds or invalid' { It 'Should throw when the Set-ItemProperty cmdlet encounters an error' { - Mock -CommandName Set-ItemProperty -MockWith { throw } + Mock -CommandName Set-ItemProperty -MockWith { throw 'Set-ItemProperty Error' } { Set-WindowsEventLogRestrictGuestAccess ` -LogName 'Application' ` -RestrictGuestAccess $true ` -Sddl 'O:BAG:SYD:(A;;0x2;;;S-1-15-2-1)' - } | Should -Throw + } | Should -Throw 'Set-ItemProperty Error' } } } @@ -569,43 +584,37 @@ try Context 'When testing a request with values that are out of bounds or invalid' { It 'Should throw when setting retention days in the wrong log mode' { - Mock -CommandName Get-EventLog -MockWith { throw } + $errorRecord = Get-InvalidArgumentRecord ` + -Message ($script:localizedData.SetWindowsEventLogRetentionDaysWrongMode -f 'Application') ` + -ArgumentName 'LogMode' { Set-WindowsEventLogRetentionDays ` -LogName 'Application' ` -LogRetentionDays 30 ` -LogMode Circular - } | Should -Throw - } - - It 'Should throw when the Get-EventLog cmdlet encounters an error' { - Mock -CommandName Get-EventLog -MockWith { throw } - - { Set-WindowsEventLogRetentionDays ` - -LogName 'Application' ` - -LogRetentionDays 30 ` - -LogMode AutoBackup - } | Should -Throw + } | Should -Throw $errorRecord } It 'Should throw when the Get-EventLog cmdlet encounters an error' { - Mock -CommandName Get-EventLog -MockWith { throw } + Mock -CommandName Get-EventLog -MockWith { throw 'Get-EventLog Error' } { Set-WindowsEventLogRetentionDays ` -LogName 'Application' ` -LogRetentionDays 30 ` -LogMode AutoBackup - } | Should -Throw + } | Should -Throw 'Get-EventLog Error' } It 'Should throw when the Limit-EventLog cmdlet encounters an error' { - Mock -CommandName Limit-EventLog -MockWith { throw } + Mock -CommandName Limit-EventLog -MockWith { throw } + $errorRecord = Get-InvalidOperationRecord ` + -Message ($script:localizedData.GetWindowsEventLogRetentionDaysFailure -f 'Application') { Set-WindowsEventLogRetentionDays ` -LogName 'Application' ` -LogRetentionDays 30 ` -LogMode AutoBackup - } | Should -Throw + } | Should -Throw $errorRecord } } }