1- # REQUIRES -Modules Utilities , PSScriptAnalyzer , Pester
2-
3- function Test-PSModule {
1+ function Test-PSModule {
42 <#
53 . SYNOPSIS
64 Performs tests on a module.
@@ -27,156 +25,140 @@ function Test-PSModule {
2725 $testModule = $TestType -eq ' Module'
2826 $moduleTestsPath = Join-Path - Path $env: GITHUB_WORKSPACE - ChildPath $TestsPath
2927
30- # region Get test kit versions
31- Start-LogGroup ' Get test kit versions'
32- $PSSAModule = Get-PSResource - Name PSScriptAnalyzer - Verbose:$false | Sort-Object Version - Descending | Select-Object - First 1
33- $pesterModule = Get-PSResource - Name Pester - Verbose:$false | Sort-Object Version - Descending | Select-Object - First 1
34-
35- Write-Verbose ' Testing with:'
36- Write-Verbose " PowerShell $ ( $PSVersionTable.PSVersion.ToString ()) "
37- Write-Verbose " Pester $ ( $pesterModule.version ) "
38- Write-Verbose " PSScriptAnalyzer $ ( $PSSAModule.version ) "
39- Stop-LogGroup
40- # endregion
41-
42- # region Add test - Common - PSScriptAnalyzer
43- Start-LogGroup ' Add test - Common - PSScriptAnalyzer'
44- $containers = @ ()
45- $PSSATestsPath = Join-Path - Path $PSScriptRoot - ChildPath ' ..\tests\PSScriptAnalyzer'
46- $settingsFileName = if ($testModule ) { ' Settings.Module.psd1' } else { ' Settings.SourceCode.psd1' }
47- $settingsFilePath = Join-Path - Path $PSSATestsPath - ChildPath $settingsFileName
48- $containerParams = @ {
49- Path = Join-Path $PSSATestsPath ' PSScriptAnalyzer.Tests.ps1'
50- Data = @ {
51- Path = $Path
52- SettingsFilePath = $settingsFilePath
53- Verbose = $true
54- }
55- }
56- Write-Verbose ' ContainerParams:'
57- Write-Verbose " $ ( $containerParams | ConvertTo-Json ) "
58- $containers += New-PesterContainer @containerParams
59- Stop-LogGroup
60- # endregion
28+ LogGroup ' Get test kit versions' {
29+ $PSSAModule = Get-PSResource - Name PSScriptAnalyzer - Verbose:$false | Sort-Object Version - Descending | Select-Object - First 1
30+ $pesterModule = Get-PSResource - Name Pester - Verbose:$false | Sort-Object Version - Descending | Select-Object - First 1
6131
62- # region Add test - Common - PSModule
63- Start-LogGroup ' Add test - Common - PSModule'
64- $containerParams = @ {
65- Path = Join-Path - Path $PSScriptRoot - ChildPath ' ..\tests\PSModule\Common.Tests.ps1'
66- Data = @ {
67- Path = $Path
68- Verbose = $true
69- }
32+ Write-Verbose ' Testing with:'
33+ Write-Verbose " PowerShell $ ( $PSVersionTable.PSVersion.ToString ()) "
34+ Write-Verbose " Pester $ ( $pesterModule.version ) "
35+ Write-Verbose " PSScriptAnalyzer $ ( $PSSAModule.version ) "
7036 }
71- Write-Verbose ' ContainerParams:'
72- Write-Verbose " $ ( $containerParams | ConvertTo-Json ) "
73- $containers += New-PesterContainer @containerParams
74- Stop-LogGroup
75- # endregion
7637
77- # region Add test - Module - PSModule
78- if ($testModule ) {
79- Start-LogGroup ' Add test - Module - PSModule'
38+ LogGroup ' Add test - Common - PSScriptAnalyzer' {
39+ $containers = @ ()
40+ $PSSATestsPath = Join-Path - Path $PSScriptRoot - ChildPath ' ..\tests\PSScriptAnalyzer'
41+ $settingsFileName = if ($testModule ) { ' Settings.Module.psd1' } else { ' Settings.SourceCode.psd1' }
42+ $settingsFilePath = Join-Path - Path $PSSATestsPath - ChildPath $settingsFileName
8043 $containerParams = @ {
81- Path = Join-Path - Path $PSScriptRoot - ChildPath ' ..\tests\PSModule\Module .Tests.ps1'
44+ Path = Join-Path $PSSATestsPath ' PSScriptAnalyzer .Tests.ps1'
8245 Data = @ {
83- Path = $Path
84- Verbose = $true
46+ Path = $Path
47+ SettingsFilePath = $settingsFilePath
48+ Verbose = $true
8549 }
8650 }
8751 Write-Verbose ' ContainerParams:'
8852 Write-Verbose " $ ( $containerParams | ConvertTo-Json ) "
8953 $containers += New-PesterContainer @containerParams
90- Stop-LogGroup
9154 }
92- # endregion
9355
94- # region Add test - SourceCode - PSModule
95- if ($testSourceCode ) {
96- Start-LogGroup ' Add test - SourceCode - PSModule'
56+ LogGroup ' Add test - Common - PSModule' {
9757 $containerParams = @ {
98- Path = Join-Path - Path $PSScriptRoot - ChildPath ' ..\tests\PSModule\SourceCode .Tests.ps1'
58+ Path = Join-Path - Path $PSScriptRoot - ChildPath ' ..\tests\PSModule\Common .Tests.ps1'
9959 Data = @ {
100- Path = $Path
101- TestsPath = $moduleTestsPath
102- Verbose = $true
60+ Path = $Path
61+ Verbose = $true
10362 }
10463 }
10564 Write-Verbose ' ContainerParams:'
10665 Write-Verbose " $ ( $containerParams | ConvertTo-Json ) "
10766 $containers += New-PesterContainer @containerParams
108- Stop-LogGroup
10967 }
110- # endregion
11168
112- # region Add test - Module - $moduleName
11369 if ($testModule ) {
114- if (Test-Path - Path $moduleTestsPath ) {
115- Start-LogGroup " Add test - Module - $moduleName "
70+ LogGroup ' Add test - Module - PSModule' {
71+ $containerParams = @ {
72+ Path = Join-Path - Path $PSScriptRoot - ChildPath ' ..\tests\PSModule\Module.Tests.ps1'
73+ Data = @ {
74+ Path = $Path
75+ Verbose = $true
76+ }
77+ }
78+ Write-Verbose ' ContainerParams:'
79+ Write-Verbose " $ ( $containerParams | ConvertTo-Json ) "
80+ $containers += New-PesterContainer @containerParams
81+ }
82+ }
83+
84+ if ($testSourceCode ) {
85+ LogGroup ' Add test - SourceCode - PSModule' {
11686 $containerParams = @ {
117- Path = $moduleTestsPath
87+ Path = Join-Path - Path $PSScriptRoot - ChildPath ' ..\tests\PSModule\SourceCode.Tests.ps1 '
11888 Data = @ {
119- Path = $Path
89+ Path = $Path
90+ TestsPath = $moduleTestsPath
91+ Verbose = $true
12092 }
12193 }
12294 Write-Verbose ' ContainerParams:'
12395 Write-Verbose " $ ( $containerParams | ConvertTo-Json ) "
12496 $containers += New-PesterContainer @containerParams
125- Stop-LogGroup
97+ }
98+ }
99+
100+ if ($testModule ) {
101+ if (Test-Path - Path $moduleTestsPath ) {
102+ LogGroup " Add test - Module - $moduleName " {
103+ $containerParams = @ {
104+ Path = $moduleTestsPath
105+ Data = @ {
106+ Path = $Path
107+ }
108+ }
109+ Write-Verbose ' ContainerParams:'
110+ Write-Verbose " $ ( $containerParams | ConvertTo-Json ) "
111+ $containers += New-PesterContainer @containerParams
112+ }
126113 } else {
127114 Write-Warning " ⚠️ No tests found - [$moduleTestsPath ]"
128115 }
129116 }
130- # endregion
131117
132- # region Import module
133118 if ((Test-Path - Path $moduleTestsPath ) -and $testModule ) {
134- Start- LogGroup ' Install module dependencies'
135- $moduleManifestPath = Join-Path - Path $Path - ChildPath " $moduleName .psd1"
136- Resolve-PSModuleDependency - ManifestFilePath $moduleManifestPath
137- Stop-LogGroup
119+ LogGroup ' Install module dependencies' {
120+ $moduleManifestPath = Join-Path - Path $Path - ChildPath " $moduleName .psd1"
121+ Resolve-PSModuleDependency - ManifestFilePath $moduleManifestPath
122+ }
138123
139- Start- LogGroup " Importing module: $moduleName "
140- Add-PSModulePath - Path (Split-Path $Path - Parent)
141- Get-Module - Name $moduleName - ListAvailable | Remove-Module - Force
142- Import-Module - Name $moduleName - Force - RequiredVersion ' 999.0.0' - Global
143- Stop-LogGroup
124+ LogGroup " Importing module: $moduleName " {
125+ Add-PSModulePath - Path (Split-Path $Path - Parent)
126+ Get-Module - Name $moduleName - ListAvailable | Remove-Module - Force
127+ Import-Module - Name $moduleName - Force - RequiredVersion ' 999.0.0' - Global
128+ }
144129 }
145- # endregion
146130
147- # region Pester config
148- Start-LogGroup ' Pester config '
149- $pesterParams = @ {
150- Configuration = @ {
151- Run = @ {
152- Path = $Path
153- Container = $containers
154- PassThru = $true
155- }
156- TestResult = @ {
157- Enabled = $testModule
158- OutputFormat = ' NUnitXml '
159- OutputPath = Join-Path - Path $ env: GITHUB_WORKSPACE - ChildPath ' outputs\Test-Report.xml '
160- TestSuiteName = ' Unit tests '
161- }
162- CodeCoverage = @ {
163- Enabled = $testModule
164- OutputPath = Join-Path - Path $ env: GITHUB_WORKSPACE - ChildPath ' outputs\CodeCoverage-Report.xml '
165- OutputFormat = ' JaCoCo '
166- OutputEncoding = ' UTF8 '
167- CoveragePercentTarget = 75
168- }
169- Output = @ {
170- CIFormat = ' Auto '
171- StackTraceVerbosity = ' None '
172- Verbosity = ' Detailed '
131+ LogGroup ' Pester config' {
132+ $pesterParams = @ {
133+ Configuration = @ {
134+ Run = @ {
135+ Path = $Path
136+ Container = $containers
137+ PassThru = $true
138+ }
139+ TestResult = @ {
140+ Enabled = $testModule
141+ OutputFormat = ' NUnitXml '
142+ OutputPath = Join-Path - Path $ env: GITHUB_WORKSPACE - ChildPath ' outputs\Test-Report.xml '
143+ TestSuiteName = ' Unit tests '
144+ }
145+ CodeCoverage = @ {
146+ Enabled = $testModule
147+ OutputPath = Join-Path - Path $ env: GITHUB_WORKSPACE - ChildPath ' outputs\CodeCoverage-Report.xml '
148+ OutputFormat = ' JaCoCo '
149+ OutputEncoding = ' UTF8 '
150+ CoveragePercentTarget = 75
151+ }
152+ Output = @ {
153+ CIFormat = ' Auto '
154+ StackTraceVerbosity = ' None '
155+ Verbosity = ' Detailed '
156+ }
173157 }
174158 }
159+ Write-Verbose ' PesterParams:'
160+ Write-Verbose " $ ( $pesterParams | ConvertTo-Json - Depth 4 - WarningAction SilentlyContinue) "
175161 }
176- Write-Verbose ' PesterParams:'
177- Write-Verbose " $ ( $pesterParams | ConvertTo-Json - Depth 4 - WarningAction SilentlyContinue) "
178- Stop-LogGroup
179- # endregion
180162
181163 # region Run tests
182164 $verbosepref = $VerbosePreference
0 commit comments