Skip to content

Commit 4904751

Browse files
authored
Update tests for ResourceBase 2.0 (#2386)
1 parent 7bd3c67 commit 4904751

15 files changed

+2842
-2360
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
167167
- `SqlProtocol`
168168
- Refactored to use the public command `Get-SqlDscServerProtocolName` instead
169169
of the deprecated private function `Get-ProtocolNameProperties`
170+
- `Class-Based Dsc Resource Tests`
171+
- Updated tests for ResourceBase 2.0.
170172

171173
### Fixed
172174

source/Classes/020.SqlAgentAlert.ps1

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -149,17 +149,13 @@ class SqlAgentAlert : SqlResourceBase
149149
[SqlAgentAlert] Get()
150150
{
151151
# Call base implementation to get current state
152-
$currentState = ([ResourceBase] $this).Get()
153-
154-
return $currentState
152+
return ([ResourceBase] $this).Get()
155153
}
156154

157155
[System.Boolean] Test()
158156
{
159157
# Call base implementation to test current state
160-
$inDesiredState = ([ResourceBase] $this).Test()
161-
162-
return $inDesiredState
158+
return ([ResourceBase] $this).Test()
163159
}
164160

165161
[void] Set()

tests/QA/ScriptAnalyzer.Tests.ps1

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,7 @@ BeforeDiscovery {
6666

6767
$moduleFiles = Get-ChildItem -Path $sourcePath -Recurse -Include @('*.psm1', '*.ps1')
6868

69-
$testCases = @()
70-
71-
foreach ($moduleFile in $moduleFiles)
69+
$testCases = foreach ($moduleFile in $moduleFiles)
7270
{
7371
# Skipping Examples on Linux and macOS as they cannot be parsed.
7472
if (($IsLinux -or $IsMacOs) -and $moduleFile.FullName -match 'Examples')
@@ -90,8 +88,8 @@ BeforeDiscovery {
9088
$escapedRepositoryPath = [System.Text.RegularExpressions.RegEx]::Escape($repositoryPathNormalized)
9189
$relativePath = $moduleFilePathNormalized -replace ($escapedRepositoryPath + '/')
9290

93-
$testCases += @{
94-
ScriptPath = $moduleFile.FullName
91+
@{
92+
ScriptPath = $moduleFile.FullName
9593
RelativePath = $relativePath
9694
}
9795
}
@@ -102,24 +100,21 @@ Describe 'Script Analyzer Rules' {
102100
BeforeAll {
103101
$repositoryPath = Resolve-Path -Path (Join-Path -Path $PSScriptRoot -ChildPath '../..')
104102
$scriptAnalyzerSettingsPath = Join-Path -Path $repositoryPath -ChildPath '.vscode/analyzersettings.psd1'
105-
}
106-
107-
It 'Should pass all PS Script Analyzer rules for file ''<RelativePath>''' -ForEach $testCases {
108-
$pssaError = Invoke-ScriptAnalyzer -Path $ScriptPath -Settings $scriptAnalyzerSettingsPath
109103

110104
$parseErrorTypes = @(
111105
'TypeNotFound'
112106
'RequiresModuleInvalid'
113107
)
108+
}
114109

110+
It 'Should pass all PS Script Analyzer rules for file ''<RelativePath>''' -ForEach $testCases {
115111
# Filter out reported parse errors that is unable to be resolved in source files
116-
$pssaError = $pssaError |
112+
$pssaError = Invoke-ScriptAnalyzer -Path $ScriptPath -Settings $scriptAnalyzerSettingsPath |
117113
Where-Object -FilterScript {
118114
$_.RuleName -notin $parseErrorTypes
119115
}
120116

121-
$report = $pssaError |
122-
Format-Table -AutoSize | Out-String -Width 200
117+
$report = $pssaError | Format-Table -AutoSize | Out-String -Width 200
123118

124119
$pssaError | Should -HaveCount 0 -Because "all script analyzer rules should pass.`r`n`r`n $report`r`n"
125120
}

tests/QA/module.tests.ps1

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -127,22 +127,18 @@ BeforeDiscovery {
127127
$allModuleFunctions = & $mut { Get-Command -Module $args[0] -CommandType Function } $script:moduleName
128128

129129
# Build test cases.
130-
$testCasesAllModuleFunction = @()
131-
132-
foreach ($function in $allModuleFunctions)
130+
$testCasesAllModuleFunction = foreach ($function in $allModuleFunctions)
133131
{
134-
$testCasesAllModuleFunction += @{
132+
@{
135133
Name = $function.Name
136134
}
137135
}
138136

139137
$allPublicCommand = (Get-Command -Module $script:moduleName).Name
140138

141-
$testCasesPublicCommand = @()
142-
143-
foreach ($command in $allPublicCommand)
139+
$testCasesPublicCommand = foreach ($command in $allPublicCommand)
144140
{
145-
$testCasesPublicCommand += @{
141+
@{
146142
Name = $command
147143
}
148144
}
@@ -218,9 +214,7 @@ Describe 'Comment-based help structure' -Tags 'helpQuality' {
218214
# Split into lines to check each one
219215
$helpLines = $helpBlock -split "`n"
220216

221-
$invalidDirectives = @()
222-
223-
foreach ($line in $helpLines)
217+
$invalidDirectives = foreach ($line in $helpLines)
224218
{
225219
# Check if line starts with whitespace followed by a period and text
226220
if ($line -match '^\s+\.([a-zA-Z]+)')
@@ -230,7 +224,7 @@ Describe 'Comment-based help structure' -Tags 'helpQuality' {
230224
# Check if it's a valid directive
231225
if ($directive -notin $validDirectives)
232226
{
233-
$invalidDirectives += $directive
227+
$directive
234228
}
235229
}
236230
}

tests/TestHelpers/CommonTestHelper.psm1

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -252,11 +252,12 @@ function Get-NetIPAddressNetwork
252252

253253
$networkAddress = ([IPAddress]($IPAddress.Address -band $subnetMask.Address)).IPAddressToString
254254

255-
$networkObject = New-Object -TypeName PSCustomObject
256-
Add-Member -InputObject $networkObject -MemberType NoteProperty -Name 'IPAddress' -Value $IPAddress
257-
Add-Member -InputObject $networkObject -MemberType NoteProperty -Name 'PrefixLength' -Value $PrefixLength
258-
Add-Member -InputObject $networkObject -MemberType NoteProperty -Name 'SubnetMask' -Value $subnetMask
259-
Add-Member -InputObject $networkObject -MemberType NoteProperty -Name 'NetworkAddress' -Value $networkAddress
255+
$networkObject = [PSCustomObject] @{
256+
IPAddress = $IPAddress
257+
PrefixLength = $PrefixLength
258+
SubnetMask = $subnetMask
259+
NetworkAddress = $networkAddress
260+
}
260261

261262
return $networkObject
262263
}
@@ -423,7 +424,7 @@ function Test-SetupArgument
423424
$actualValues.Count | Should -Be $ExpectedArgument.Count `
424425
-Because ('the expected arguments was: {0}' -f ($ExpectedArgument.Keys -join ','))
425426

426-
Write-Verbose -Message 'Verified actual setup argument values against expected setup argument values' -Verbose
427+
Write-Verbose -Message 'Verified actual setup argument values against expected setup argument values'
427428

428429
foreach ($argumentKey in $ExpectedArgument.Keys)
429430
{

0 commit comments

Comments
 (0)