diff --git a/tests/QA/module.tests.ps1 b/tests/QA/module.tests.ps1 index dc7c7b8f..2e872e4f 100644 --- a/tests/QA/module.tests.ps1 +++ b/tests/QA/module.tests.ps1 @@ -11,15 +11,26 @@ param ( Write-Verbose ("repoRootPath: $repoRootPath") -Verbose Write-Verbose ("modules: $($modules.Count)") -Verbose -Describe 'Module tests' -Tags 'FunctionalQuality' { - Context 'General resource folder test' { - $moduleResources = [System.Collections.ArrayList]@() - - foreach ($module in $modules) { - $moduleResources += @{ - moduleName = $module.BaseName - modulePath = $module.FullName - } +BeforeDiscovery { + $moduleResources = [System.Collections.ArrayList]@() + + foreach ($module in $modules) { + $moduleResources += @{ + moduleName = $module.BaseName + modulePath = $module.FullName + } + } +} + +Describe 'Module tests' { + Context 'General resource folder test' -Tags 'FunctionalQuality' { + It '[]' -TestCases $testCases -Skip:(-not $scriptAnalyzerRules) { + $functionFile = Get-ChildItem -Path $sourcePath -Recurse -Include "$Name.ps1" + + $pssaResult = (Invoke-ScriptAnalyzer -Path $functionFile.FullName) + $report = $pssaResult | Format-Table -AutoSize | Out-String -Width 110 + $pssaResult | Should -BeNullOrEmpty -Because ` + "some rule triggered.`r`n`r`n $report" } It '[] Should import without error' -TestCases $moduleResources { @@ -39,5 +50,72 @@ Describe 'Module tests' -Tags 'FunctionalQuality' { Get-Module $moduleName | Should -BeNullOrEmpty } + + It '[] Should have unit test' -TestCases $moduleResources { + Get-ChildItem -Path 'tests\' -Recurse -Include "$ModuleName.Tests.ps1" | Should -Not -BeNullOrEmpty + } + } + + Context 'Quality checks' -Tags 'TestQuality' { + BeforeDiscovery { + if (Get-Command -Name Invoke-ScriptAnalyzer -ErrorAction SilentlyContinue) { + $scriptAnalyzerRules = Get-ScriptAnalyzerRule + } else { + if ($ErrorActionPreference -ne 'Stop') { + Write-Warning -Message 'ScriptAnalyzer not found!' + } else { + throw 'ScriptAnalyzer not found!' + } + } + } + + It '[] Should pass PSScriptAnalyzer' -TestCases $moduleResources -Skip:(-not $scriptAnalyzerRules) { + param ( + [string] $modulePath, + [string] $moduleName + ) + + $pssaResult = Invoke-ScriptAnalyzer -Path $modulePath + $report = $pssaResult | Format-Table -AutoSize | Out-String -Width 110 + $pssaResult | Should -BeNullOrEmpty -Because ` + "some rule triggered.`r`n`r`n $report" + } + } + + Context 'Documentation checks' -Tags 'DocQuality' -ForEach $moduleResources { + $moduleResource = $_ + $moduleImport = Import-PowerShellDataFile -Path $moduleResource.ModulePath.Replace('.psm1', '.psd1') + + $resources = [System.Collections.ArrayList]@() + + foreach ($resource in $moduleImport.DscResourcesToExport) { + $resources += @{ + moduleName = $moduleResource.ModuleName + resource = $resource + HelpFile = Join-Path $repoRootPath 'resources' 'Help' $moduleResource.ModuleName "$resource.md" + } + } + + It '[] Should have a help file for [] resource' -TestCases $resources { + param ( + [string] $moduleName, + [string] $resource, + [string] $helpFile + ) + + $expectedFile = Test-Path $helpFile -ErrorAction SilentlyContinue + $expectedFile | Should -Be $true + } + + It '[] Should have a help file for [] resource that is not empty' -TestCases $resources { + param ( + [string] $moduleName, + [string] $resource, + [string] $helpFile + ) + + $file = Get-Item -Path $helpFile -ErrorAction SilentlyContinue + $file.Length | Should -BeGreaterThan 0 + } } }