Skip to content

Commit bd0742b

Browse files
committed
Tests for Verbose DSC Rule
1 parent 12e6a6a commit bd0742b

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

Tests/Rules/DSCResources/MSFT_WaitForAny/MSFT_WaitForAny.psm1

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ function Get-TargetResource
2929
[Uint32] $ThrottleLimit = 32 #Powershell New-CimSession default throttle value
3030
)
3131

32+
Write-Verbose "In Get-TargetResource"
33+
3234
Import-Module $PSScriptRoot\..\..\PSDSCxMachine.psm1
3335

3436
$b = @{"hash" = "table"}
@@ -75,10 +77,14 @@ function Set-TargetResource
7577
[Uint32] $ThrottleLimit = 32 #Powershell New-CimSession default throttle value
7678
)
7779

80+
Write-Verbose "In Set-TargetResource"
81+
7882
Import-Module $PSScriptRoot\..\..\PSDSCxMachine.psm1
7983

8084
if ($PSBoundParameters["Verbose"])
8185
{
86+
Write-Verbose "Calling xMachine with Verbose parameter"
87+
8288
PSDSCxMachine\Set-_InternalPSDscXMachineTR `
8389
-RemoteResourceId $ResourceName `
8490
-RemoteMachine $NodeName `
@@ -130,6 +136,8 @@ function Test-TargetResource
130136
[Uint32] $ThrottleLimit = 32 #Powershell New-CimSession default throttle value
131137
)
132138

139+
Write-Verbose "In Test-TargetResource"
140+
133141
Import-Module $PSScriptRoot\..\..\PSDSCxMachine.psm1
134142

135143
$a = $true
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
Import-Module PSScriptAnalyzer
2+
3+
$violationMessage = "There is no call to Write-Verbose in DSC function ‘Set-TargetResource’. If you are using Write-Verbose in a helper function, suppress this rule application."
4+
$violationName = "PSDSCUseVerboseMessageInDSCResource"
5+
$directory = Split-Path -Parent $MyInvocation.MyCommand.Path
6+
$violations = Invoke-ScriptAnalyzer $directory\DSCResources\MSFT_WaitForAll\MSFT_WaitForAll.psm1 | Where-Object {$_.RuleName -eq $violationName}
7+
$noViolations = Invoke-ScriptAnalyzer $directory\DSCResources\MSFT_WaitForAny\MSFT_WaitForAny.psm1 | Where-Object {$_.RuleName -eq $violationName}
8+
$noClassViolations = Invoke-ScriptAnalyzer -ErrorAction SilentlyContinue $directory\DSCResources\MyDscResource\MyDscResource.psm1 | Where-Object {$_.RuleName -eq $violationName}
9+
10+
Describe "UseVerboseMessageInDSCResource" {
11+
Context "When there are violations" {
12+
It "has 2 Verbose Message violations" {
13+
$violations.Count | Should Be 2
14+
}
15+
16+
It "has the correct description message" {
17+
$violations[0].Message | Should Match $violationMessage
18+
}
19+
}
20+
21+
Context "When there are no violations" {
22+
It "returns no violations" {
23+
$noViolations.Count | Should Be 0
24+
}
25+
}
26+
}

0 commit comments

Comments
 (0)