-
-
Notifications
You must be signed in to change notification settings - Fork 0
197 lines (175 loc) · 7 KB
/
test-powershell-module.yml
File metadata and controls
197 lines (175 loc) · 7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
name: Test PowerShell Module
permissions:
contents: read
on:
workflow_call:
inputs:
module-name:
description: 'Name of the PowerShell module to test'
required: true
type: string
module-path:
description: 'Path to the module directory'
required: true
type: string
test-path:
description: 'Path to the test directory (relative to module-path)'
required: false
type: string
default: 'Tests'
exclude-tags:
description: 'Comma-separated list of Pester tags to exclude'
required: false
type: string
default: ''
exclude-path:
description: 'Comma-separated list of test file paths to exclude (relative to test-path)'
required: false
type: string
default: ''
settings-path:
description: 'Path to PSScriptAnalyzer settings file (relative to repo root)'
required: false
type: string
default: 'PSScriptAnalyzerSettings.psd1'
jobs:
lint:
runs-on: ubuntu-latest
if: false # Flaky with: The term 'Get-Command' is not recognized as a name of a cmdlet,
defaults:
run:
working-directory: ${{ inputs.module-path }}
shell: pwsh
steps:
- name: Checkout repository
uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4
- run: $PSVersionTable
- name: Show PSScriptAnalyzer versions
run: |
Get-Module PSScriptAnalyzer | Format-Table -AutoSize
Get-Command Invoke-ScriptAnalyzer | Format-Table -AutoSize
- name: Run PowerShell Script Analyzer
run: |
Write-Host "Running PSScriptAnalyzer on ${{ inputs.module-name }} module..."
$settingsPath = Join-Path ".." "${{ inputs.settings-path }}"
$analysisResults = Invoke-ScriptAnalyzer -Path . -Recurse -ReportSummary -Settings $settingsPath
if ($analysisResults) {
Write-Host "PSScriptAnalyzer found issues:" -ForegroundColor Yellow
$analysisResults | Format-Table -AutoSize
if ($analysisResults | Where-Object { $_.Severity -eq 'Error' }) {
Write-Error "PSScriptAnalyzer found errors. Please fix them before merging."
exit 1
}
} else {
Write-Host "PSScriptAnalyzer passed with no issues" -ForegroundColor Green
}
unit-tests:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [windows-latest, macos-latest, ubuntu-latest]
defaults:
run:
working-directory: ${{ inputs.module-path }}
shell: pwsh
env:
SAUCE_USERNAME: ${{ secrets.SAUCE_USERNAME }}
SAUCE_ACCESS_KEY: ${{ secrets.SAUCE_ACCESS_KEY }}
SAUCE_REGION: us-west-1
steps:
- name: Checkout repository
uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4
- name: Run Pester Tests
run: |
Write-Host "Running Pester tests for ${{ inputs.module-name }} module..."
$config = New-PesterConfiguration
$config.Run.Path = "${{ inputs.test-path }}"
$config.TestResult.Enabled = $true
$config.TestResult.OutputFormat = "NUnitXml"
$config.TestResult.OutputPath = "TestResults.xml"
$config.Output.Verbosity = "Detailed"
$excludeTags = "${{ inputs.exclude-tags }}"
if ($excludeTags) {
$config.Filter.ExcludeTag = $excludeTags.Split(',').Trim()
}
$excludePath = "${{ inputs.exclude-path }}"
if ($excludePath) {
$testPath = "${{ inputs.test-path }}"
$config.Run.ExcludePath = $excludePath.Split(',').Trim() | ForEach-Object {
$relativePath = Join-Path $testPath $_
# ExcludePath requires absolute paths
Join-Path (Get-Location) $relativePath
}
}
$testResults = Invoke-Pester -Configuration $config
Write-Host "Test Summary:" -ForegroundColor Cyan
Write-Host "Total Tests: $($testResults.TotalCount)" -ForegroundColor White
Write-Host "Passed: $($testResults.PassedCount)" -ForegroundColor Green
Write-Host "Failed: $($testResults.FailedCount)" -ForegroundColor Red
Write-Host "Skipped: $($testResults.SkippedCount)" -ForegroundColor Yellow
if ($testResults.FailedCount -gt 0) {
Write-Host "Failed Tests:" -ForegroundColor Red
$testResults.Tests | Where-Object { $_.Result -eq 'Failed' } | ForEach-Object {
Write-Host " - $($_.Name): $($_.ErrorRecord.Exception.Message)" -ForegroundColor Red
}
exit 1
}
Write-Host "All tests passed successfully!" -ForegroundColor Green
- name: Upload test results
uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4
if: always()
with:
name: ${{ inputs.module-name }}-test-results-${{ matrix.os }}
path: ${{ inputs.module-path }}/TestResults.xml
validate-manifest:
runs-on: ubuntu-latest
defaults:
run:
working-directory: ${{ inputs.module-path }}
shell: pwsh
steps:
- name: Checkout repository
uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4
- name: Validate module manifest
run: |
Write-Host "Validating PowerShell module manifest..."
try {
$manifest = Test-ModuleManifest -Path "${{ inputs.module-name }}.psd1"
Write-Host "Module manifest validation passed" -ForegroundColor Green
Write-Host "Module: $($manifest.Name) v$($manifest.Version)" -ForegroundColor White
Write-Host "Author: $($manifest.Author)" -ForegroundColor White
Write-Host "Description: $($manifest.Description)" -ForegroundColor White
} catch {
Write-Error "Module manifest validation failed: $($_.Exception.Message)"
exit 1
}
test-import:
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os: [windows-latest, macos-latest, ubuntu-latest]
defaults:
run:
working-directory: ${{ inputs.module-path }}
shell: pwsh
steps:
- name: Checkout repository
uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4
- name: Test module import
run: |
Write-Host "Testing module import..."
try {
Import-Module "./${{ inputs.module-name }}.psd1" -Force
$importedModule = Get-Module ${{ inputs.module-name }}
if ($importedModule) {
Write-Host "Module imported successfully" -ForegroundColor Green
Write-Host "Exported Functions: $($importedModule.ExportedFunctions.Keys -join ', ')" -ForegroundColor White
} else {
Write-Error "Module import failed"
exit 1
}
} catch {
Write-Error "Module import failed: $($_.Exception.Message)"
exit 1
}