Skip to content

Commit b9ffbd7

Browse files
feat: Add BeforeAll and AfterAll steps for module local testing in CI workflows
1 parent 4ef34b4 commit b9ffbd7

File tree

3 files changed

+232
-108
lines changed

3 files changed

+232
-108
lines changed

.github/workflows/CI.yml

Lines changed: 107 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,22 +176,128 @@ jobs:
176176
Version: ${{ inputs.Version }}
177177
WorkingDirectory: ${{ inputs.WorkingDirectory }}
178178

179+
BeforeAll-ModuleLocal:
180+
if: ${{ needs.Build-Module.result == 'success' && !cancelled() && needs.Get-Settings.outputs.ModuleTestSuites != '[]' }}
181+
name: BeforeAll-ModuleLocal
182+
runs-on: ubuntu-latest
183+
needs:
184+
- Build-Module
185+
- Get-Settings
186+
steps:
187+
- name: Checkout Code
188+
uses: actions/checkout@v5
189+
190+
- name: Install-PSModuleHelpers
191+
uses: PSModule/Install-PSModuleHelpers@v1
192+
193+
- name: Run BeforeAll Setup Scripts
194+
uses: PSModule/GitHub-Script@v1
195+
with:
196+
Name: BeforeAll-ModuleLocal
197+
ShowInfo: false
198+
ShowOutput: true
199+
Debug: ${{ inputs.Debug }}
200+
Prerelease: ${{ inputs.Prerelease }}
201+
Verbose: ${{ inputs.Verbose }}
202+
Version: ${{ inputs.Version }}
203+
WorkingDirectory: ${{ inputs.WorkingDirectory }}
204+
Script: |
205+
LogGroup "Running BeforeAll Setup Scripts" {
206+
$beforeAllScript = 'tests/BeforeAll.ps1'
207+
208+
if (-not (Test-Path $beforeAllScript)) {
209+
Write-Host "No BeforeAll.ps1 script found at [$beforeAllScript] - exiting successfully"
210+
exit 0
211+
}
212+
213+
Write-Host "Running BeforeAll setup script: $beforeAllScript"
214+
try {
215+
Push-Location 'tests'
216+
& $beforeAllScript
217+
Write-Host "BeforeAll script completed successfully: $beforeAllScript"
218+
}
219+
catch {
220+
Write-Error "BeforeAll script failed: $beforeAllScript - $_"
221+
throw
222+
}
223+
finally {
224+
Pop-Location
225+
}
226+
}
227+
179228
Test-ModuleLocal:
180229
if: ${{ needs.Build-Module.result == 'success' && !cancelled() && needs.Get-Settings.outputs.ModuleTestSuites != '[]' }}
181230
needs:
182231
- Build-Module
183232
- Get-Settings
233+
- BeforeAll-ModuleLocal
234+
strategy:
235+
fail-fast: false
236+
matrix:
237+
include: ${{ fromJson(needs.Get-Settings.outputs.ModuleTestSuites) }}
184238
uses: ./.github/workflows/Test-ModuleLocal.yml
185239
secrets: inherit
186240
with:
187-
ModuleTestSuites: ${{ needs.Get-Settings.outputs.ModuleTestSuites }}
241+
RunsOn: ${{ matrix.RunsOn }}
242+
OSName: ${{ matrix.OSName }}
243+
TestName: ${{ matrix.TestName }}
244+
TestPath: ${{ matrix.TestPath }}
188245
Name: ${{ fromJson(needs.Get-Settings.outputs.Settings).Name }}
189246
Debug: ${{ inputs.Debug }}
190247
Prerelease: ${{ inputs.Prerelease }}
191248
Verbose: ${{ inputs.Verbose }}
192249
Version: ${{ inputs.Version }}
193250
WorkingDirectory: ${{ inputs.WorkingDirectory }}
194251

252+
AfterAll-ModuleLocal:
253+
if: ${{ needs.Test-ModuleLocal.result != 'skipped' && always() }}
254+
name: AfterAll-ModuleLocal
255+
runs-on: ubuntu-latest
256+
needs:
257+
- Test-ModuleLocal
258+
steps:
259+
- name: Checkout Code
260+
uses: actions/checkout@v5
261+
262+
- name: Install-PSModuleHelpers
263+
uses: PSModule/Install-PSModuleHelpers@v1
264+
265+
- name: Run AfterAll Teardown Scripts
266+
if: always()
267+
uses: PSModule/GitHub-Script@v1
268+
with:
269+
Name: AfterAll-ModuleLocal
270+
ShowInfo: false
271+
ShowOutput: true
272+
Debug: ${{ inputs.Debug }}
273+
Prerelease: ${{ inputs.Prerelease }}
274+
Verbose: ${{ inputs.Verbose }}
275+
Version: ${{ inputs.Version }}
276+
WorkingDirectory: ${{ inputs.WorkingDirectory }}
277+
Script: |
278+
LogGroup "Running AfterAll Teardown Scripts" {
279+
$afterAllScript = 'tests/AfterAll.ps1'
280+
281+
if (-not (Test-Path $afterAllScript)) {
282+
Write-Host "No AfterAll.ps1 script found at [$afterAllScript] - exiting successfully"
283+
exit 0
284+
}
285+
286+
Write-Host "Running AfterAll teardown script: $afterAllScript"
287+
try {
288+
Push-Location 'tests'
289+
& $afterAllScript
290+
Write-Host "AfterAll script completed successfully: $afterAllScript"
291+
}
292+
catch {
293+
Write-Warning "AfterAll script failed: $afterAllScript - $_"
294+
# Don't throw for teardown scripts to ensure other cleanup scripts can run
295+
}
296+
finally {
297+
Pop-Location
298+
}
299+
}
300+
195301
Get-TestResults:
196302
if: needs.Get-Settings.result == 'success' && !fromJson(needs.Get-Settings.outputs.Settings).Test.TestResults.Skip && (needs.Get-Settings.outputs.SourceCodeTestSuites != '[]' || needs.Get-Settings.outputs.PSModuleTestSuites != '[]' || needs.Get-Settings.outputs.ModuleTestSuites != '[]') && (always() && !cancelled())
197303
needs:

.github/workflows/Test-ModuleLocal.yml

Lines changed: 18 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,21 @@ on:
2525
description: The classic personal access token for running tests.
2626
required: false
2727
inputs:
28-
ModuleTestSuites:
28+
RunsOn:
2929
type: string
30-
description: JSON array of module test suites to run (from Get-Settings output).
30+
description: The type of runner to use for the job.
31+
required: true
32+
OSName:
33+
type: string
34+
description: The operating system name.
35+
required: true
36+
TestName:
37+
type: string
38+
description: The name of the test suite.
39+
required: true
40+
TestPath:
41+
type: string
42+
description: The path to the test files.
3143
required: true
3244
Name:
3345
type: string
@@ -73,60 +85,9 @@ env:
7385
GITHUB_TOKEN: ${{ github.token }}
7486

7587
jobs:
76-
BeforeAll-ModuleLocal:
77-
name: BeforeAll-ModuleLocal
78-
runs-on: ubuntu-latest
79-
steps:
80-
- name: Checkout Code
81-
uses: actions/checkout@v5
82-
83-
- name: Install-PSModuleHelpers
84-
uses: PSModule/Install-PSModuleHelpers@v1
85-
86-
- name: Run BeforeAll Setup Scripts
87-
uses: PSModule/GitHub-Script@v1
88-
with:
89-
Name: BeforeAll-ModuleLocal
90-
ShowInfo: false
91-
ShowOutput: true
92-
Debug: ${{ inputs.Debug }}
93-
Prerelease: ${{ inputs.Prerelease }}
94-
Verbose: ${{ inputs.Verbose }}
95-
Version: ${{ inputs.Version }}
96-
WorkingDirectory: ${{ inputs.WorkingDirectory }}
97-
Script: |
98-
LogGroup "Running BeforeAll Setup Scripts" {
99-
$beforeAllScript = 'tests/BeforeAll.ps1'
100-
101-
if (-not (Test-Path $beforeAllScript)) {
102-
Write-Host "No BeforeAll.ps1 script found at [$beforeAllScript] - exiting successfully"
103-
exit 0
104-
}
105-
106-
Write-Host "Running BeforeAll setup script: $beforeAllScript"
107-
try {
108-
Push-Location 'tests'
109-
& $beforeAllScript
110-
Write-Host "BeforeAll script completed successfully: $beforeAllScript"
111-
}
112-
catch {
113-
Write-Error "BeforeAll script failed: $beforeAllScript - $_"
114-
throw
115-
}
116-
finally {
117-
Pop-Location
118-
}
119-
}
120-
12188
Test-ModuleLocal:
122-
name: Test-${{ matrix.TestName }} (${{ matrix.OSName }})
123-
runs-on: ${{ matrix.RunsOn }}
124-
needs:
125-
- BeforeAll-ModuleLocal
126-
strategy:
127-
fail-fast: false
128-
matrix:
129-
include: ${{ fromJson(inputs.ModuleTestSuites) }}
89+
name: Test-${{ inputs.TestName }} (${{ inputs.OSName }})
90+
runs-on: ${{ inputs.RunsOn }}
13091
steps:
13192
- name: Checkout Code
13293
uses: actions/checkout@v5
@@ -158,65 +119,16 @@ jobs:
158119
Prerelease: ${{ inputs.Prerelease }}
159120
Verbose: ${{ inputs.Verbose }}
160121
Version: ${{ inputs.Version }}
161-
TestResult_TestSuiteName: ${{ matrix.TestName }}-${{ matrix.OSName }}
122+
TestResult_TestSuiteName: ${{ inputs.TestName }}-${{ inputs.OSName }}
162123
TestResult_Enabled: true
163124
CodeCoverage_Enabled: true
164125
Output_Verbosity: Detailed
165126
CodeCoverage_OutputFormat: JaCoCo
166127
CodeCoverage_CoveragePercentTarget: 0
167128
Filter_ExcludeTag: Flaky
168-
Path: ${{ matrix.TestPath }}
129+
Path: ${{ inputs.TestPath }}
169130
Run_Path: ${{ steps.import-module.outputs.path }}
170131
WorkingDirectory: ${{ inputs.WorkingDirectory }}
171132
Prescript: | # This is to speed up module loading in Pester.
172133
Install-PSResource -Repository PSGallery -TrustRepository -Name PSCustomObject
173134
Import-Module -Name '${{ steps.import-module.outputs.name }}' -RequiredVersion 999.0.0
174-
175-
AfterAll-ModuleLocal:
176-
name: AfterAll-ModuleLocal
177-
runs-on: ubuntu-latest
178-
needs:
179-
- Test-ModuleLocal
180-
if: always()
181-
steps:
182-
- name: Checkout Code
183-
uses: actions/checkout@v5
184-
185-
- name: Install-PSModuleHelpers
186-
uses: PSModule/Install-PSModuleHelpers@v1
187-
188-
- name: Run AfterAll Teardown Scripts
189-
if: always()
190-
uses: PSModule/GitHub-Script@v1
191-
with:
192-
Name: AfterAll-ModuleLocal
193-
ShowInfo: false
194-
ShowOutput: true
195-
Debug: ${{ inputs.Debug }}
196-
Prerelease: ${{ inputs.Prerelease }}
197-
Verbose: ${{ inputs.Verbose }}
198-
Version: ${{ inputs.Version }}
199-
WorkingDirectory: ${{ inputs.WorkingDirectory }}
200-
Script: |
201-
LogGroup "Running AfterAll Teardown Scripts" {
202-
$afterAllScript = 'tests/AfterAll.ps1'
203-
204-
if (-not (Test-Path $afterAllScript)) {
205-
Write-Host "No AfterAll.ps1 script found at [$afterAllScript] - exiting successfully"
206-
exit 0
207-
}
208-
209-
Write-Host "Running AfterAll teardown script: $afterAllScript"
210-
try {
211-
Push-Location 'tests'
212-
& $afterAllScript
213-
Write-Host "AfterAll script completed successfully: $afterAllScript"
214-
}
215-
catch {
216-
Write-Warning "AfterAll script failed: $afterAllScript - $_"
217-
# Don't throw for teardown scripts to ensure other cleanup scripts can run
218-
}
219-
finally {
220-
Pop-Location
221-
}
222-
}

0 commit comments

Comments
 (0)