1
- # REQUIRES -Modules Utilities , PSScriptAnalyzer , Pester
2
-
3
- function Test-PSModule {
1
+ function Test-PSModule {
4
2
<#
5
3
. SYNOPSIS
6
4
Performs tests on a module.
@@ -27,156 +25,140 @@ function Test-PSModule {
27
25
$testModule = $TestType -eq ' Module'
28
26
$moduleTestsPath = Join-Path - Path $env: GITHUB_WORKSPACE - ChildPath $TestsPath
29
27
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
61
31
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 ) "
70
36
}
71
- Write-Verbose ' ContainerParams:'
72
- Write-Verbose " $ ( $containerParams | ConvertTo-Json ) "
73
- $containers += New-PesterContainer @containerParams
74
- Stop-LogGroup
75
- # endregion
76
37
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
80
43
$containerParams = @ {
81
- Path = Join-Path - Path $PSScriptRoot - ChildPath ' ..\tests\PSModule\Module .Tests.ps1'
44
+ Path = Join-Path $PSSATestsPath ' PSScriptAnalyzer .Tests.ps1'
82
45
Data = @ {
83
- Path = $Path
84
- Verbose = $true
46
+ Path = $Path
47
+ SettingsFilePath = $settingsFilePath
48
+ Verbose = $true
85
49
}
86
50
}
87
51
Write-Verbose ' ContainerParams:'
88
52
Write-Verbose " $ ( $containerParams | ConvertTo-Json ) "
89
53
$containers += New-PesterContainer @containerParams
90
- Stop-LogGroup
91
54
}
92
- # endregion
93
55
94
- # region Add test - SourceCode - PSModule
95
- if ($testSourceCode ) {
96
- Start-LogGroup ' Add test - SourceCode - PSModule'
56
+ LogGroup ' Add test - Common - PSModule' {
97
57
$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'
99
59
Data = @ {
100
- Path = $Path
101
- TestsPath = $moduleTestsPath
102
- Verbose = $true
60
+ Path = $Path
61
+ Verbose = $true
103
62
}
104
63
}
105
64
Write-Verbose ' ContainerParams:'
106
65
Write-Verbose " $ ( $containerParams | ConvertTo-Json ) "
107
66
$containers += New-PesterContainer @containerParams
108
- Stop-LogGroup
109
67
}
110
- # endregion
111
68
112
- # region Add test - Module - $moduleName
113
69
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' {
116
86
$containerParams = @ {
117
- Path = $moduleTestsPath
87
+ Path = Join-Path - Path $PSScriptRoot - ChildPath ' ..\tests\PSModule\SourceCode.Tests.ps1 '
118
88
Data = @ {
119
- Path = $Path
89
+ Path = $Path
90
+ TestsPath = $moduleTestsPath
91
+ Verbose = $true
120
92
}
121
93
}
122
94
Write-Verbose ' ContainerParams:'
123
95
Write-Verbose " $ ( $containerParams | ConvertTo-Json ) "
124
96
$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
+ }
126
113
} else {
127
114
Write-Warning " ⚠️ No tests found - [$moduleTestsPath ]"
128
115
}
129
116
}
130
- # endregion
131
117
132
- # region Import module
133
118
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
+ }
138
123
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
+ }
144
129
}
145
- # endregion
146
130
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
+ }
173
157
}
174
158
}
159
+ Write-Verbose ' PesterParams:'
160
+ Write-Verbose " $ ( $pesterParams | ConvertTo-Json - Depth 4 - WarningAction SilentlyContinue) "
175
161
}
176
- Write-Verbose ' PesterParams:'
177
- Write-Verbose " $ ( $pesterParams | ConvertTo-Json - Depth 4 - WarningAction SilentlyContinue) "
178
- Stop-LogGroup
179
- # endregion
180
162
181
163
# region Run tests
182
164
$verbosepref = $VerbosePreference
0 commit comments