Skip to content

Commit 6354195

Browse files
committed
Add PSScriptAnalyzer and doc checker
1 parent 2e4cbe5 commit 6354195

File tree

1 file changed

+87
-9
lines changed

1 file changed

+87
-9
lines changed

tests/QA/module.tests.ps1

Lines changed: 87 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,26 @@ param (
1111
Write-Verbose ("repoRootPath: $repoRootPath") -Verbose
1212
Write-Verbose ("modules: $($modules.Count)") -Verbose
1313

14-
Describe 'Module tests' -Tags 'FunctionalQuality' {
15-
Context 'General resource folder test' {
16-
$moduleResources = [System.Collections.ArrayList]@()
17-
18-
foreach ($module in $modules) {
19-
$moduleResources += @{
20-
moduleName = $module.BaseName
21-
modulePath = $module.FullName
22-
}
14+
BeforeDiscovery {
15+
$moduleResources = [System.Collections.ArrayList]@()
16+
17+
foreach ($module in $modules) {
18+
$moduleResources += @{
19+
moduleName = $module.BaseName
20+
modulePath = $module.FullName
21+
}
22+
}
23+
}
24+
25+
Describe 'Module tests' {
26+
Context 'General resource folder test' -Tags 'FunctionalQuality' {
27+
It '[<moduleName>]' -TestCases $testCases -Skip:(-not $scriptAnalyzerRules) {
28+
$functionFile = Get-ChildItem -Path $sourcePath -Recurse -Include "$Name.ps1"
29+
30+
$pssaResult = (Invoke-ScriptAnalyzer -Path $functionFile.FullName)
31+
$report = $pssaResult | Format-Table -AutoSize | Out-String -Width 110
32+
$pssaResult | Should -BeNullOrEmpty -Because `
33+
"some rule triggered.`r`n`r`n $report"
2334
}
2435

2536
It '[<moduleName>] Should import without error' -TestCases $moduleResources {
@@ -39,5 +50,72 @@ Describe 'Module tests' -Tags 'FunctionalQuality' {
3950

4051
Get-Module $moduleName | Should -BeNullOrEmpty
4152
}
53+
54+
It '[<moduleName>] Should have unit test' -TestCases $moduleResources {
55+
Get-ChildItem -Path 'tests\' -Recurse -Include "$ModuleName.Tests.ps1" | Should -Not -BeNullOrEmpty
56+
}
57+
}
58+
59+
Context 'Quality checks' -Tags 'TestQuality' {
60+
BeforeDiscovery {
61+
if (Get-Command -Name Invoke-ScriptAnalyzer -ErrorAction SilentlyContinue) {
62+
$scriptAnalyzerRules = Get-ScriptAnalyzerRule
63+
} else {
64+
if ($ErrorActionPreference -ne 'Stop') {
65+
Write-Warning -Message 'ScriptAnalyzer not found!'
66+
} else {
67+
throw 'ScriptAnalyzer not found!'
68+
}
69+
}
70+
}
71+
72+
It '[<moduleName>] Should pass PSScriptAnalyzer' -TestCases $moduleResources -Skip:(-not $scriptAnalyzerRules) {
73+
param (
74+
[string] $modulePath,
75+
[string] $moduleName
76+
)
77+
78+
$pssaResult = Invoke-ScriptAnalyzer -Path $modulePath
79+
$report = $pssaResult | Format-Table -AutoSize | Out-String -Width 110
80+
$pssaResult | Should -BeNullOrEmpty -Because `
81+
"some rule triggered.`r`n`r`n $report"
82+
}
83+
}
84+
85+
Context 'Documentation checks' -Tags 'DocQuality' -ForEach $moduleResources {
86+
$moduleResource = $_
87+
$moduleImport = Import-PowerShellDataFile -Path $moduleResource.ModulePath.Replace('.psm1', '.psd1')
88+
89+
$resources = [System.Collections.ArrayList]@()
90+
91+
foreach ($resource in $moduleImport.DscResourcesToExport) {
92+
$resources += @{
93+
moduleName = $moduleResource.ModuleName
94+
resource = $resource
95+
HelpFile = Join-Path $repoRootPath 'resources' 'Help' $moduleResource.ModuleName "$resource.md"
96+
}
97+
}
98+
99+
It '[<moduleName>] Should have a help file for [<resource>] resource' -TestCases $resources {
100+
param (
101+
[string] $moduleName,
102+
[string] $resource,
103+
[string] $helpFile
104+
)
105+
106+
$expectedFile = Test-Path $helpFile -ErrorAction SilentlyContinue
107+
$expectedFile | Should -Be $true
108+
}
109+
110+
It '[<moduleName>] Should have a help file for [<resource>] resource that is not empty' -TestCases $resources {
111+
param (
112+
[string] $moduleName,
113+
[string] $resource,
114+
[string] $helpFile
115+
)
116+
117+
$file = Get-Item -Path $helpFile -ErrorAction SilentlyContinue
118+
$file.Length | Should -BeGreaterThan 0
119+
}
42120
}
43121
}

0 commit comments

Comments
 (0)