Skip to content

Commit

Permalink
Add PSScriptAnalyzer and doc checker
Browse files Browse the repository at this point in the history
  • Loading branch information
Gijsreyn committed Nov 14, 2024
1 parent 5cc272f commit f792228
Showing 1 changed file with 87 additions and 9 deletions.
96 changes: 87 additions & 9 deletions tests/QA/module.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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 '[<moduleName>]' -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 '[<moduleName>] Should import without error' -TestCases $moduleResources {
Expand All @@ -39,5 +50,72 @@ Describe 'Module tests' -Tags 'FunctionalQuality' {

Get-Module $moduleName | Should -BeNullOrEmpty
}

It '[<moduleName>] 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 '[<moduleName>] 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 '[<moduleName>] Should have a help file for [<resource>] resource' -TestCases $resources {
param (
[string] $moduleName,
[string] $resource,
[string] $helpFile
)

$expectedFile = Test-Path $helpFile -ErrorAction SilentlyContinue
$expectedFile | Should -Be $true
}

It '[<moduleName>] Should have a help file for [<resource>] 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
}
}
}

0 comments on commit f792228

Please sign in to comment.