Skip to content

Commit 7b69fe7

Browse files
Implement separate BeforeAll/AfterAll jobs with conditional execution
Co-authored-by: MariusStorhaug <[email protected]>
1 parent fdfea9e commit 7b69fe7

File tree

6 files changed

+414
-45
lines changed

6 files changed

+414
-45
lines changed
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
name: AfterAll-ModuleLocal
2+
3+
on:
4+
workflow_call:
5+
secrets:
6+
TEST_APP_ENT_CLIENT_ID:
7+
description: The client ID of an Enterprise GitHub App for running tests.
8+
required: false
9+
TEST_APP_ENT_PRIVATE_KEY:
10+
description: The private key of an Enterprise GitHub App for running tests.
11+
required: false
12+
TEST_APP_ORG_CLIENT_ID:
13+
description: The client ID of an Organization GitHub App for running tests.
14+
required: false
15+
TEST_APP_ORG_PRIVATE_KEY:
16+
description: The private key of an Organization GitHub App for running tests.
17+
required: false
18+
TEST_USER_ORG_FG_PAT:
19+
description: The fine-grained personal access token with org access for running tests.
20+
required: false
21+
TEST_USER_USER_FG_PAT:
22+
description: The fine-grained personal access token with user account access for running tests.
23+
required: false
24+
TEST_USER_PAT:
25+
description: The classic personal access token for running tests.
26+
required: false
27+
inputs:
28+
Name:
29+
type: string
30+
description: The name of the module to process. Scripts default to the repository name if nothing is specified.
31+
required: false
32+
Debug:
33+
type: boolean
34+
description: Enable debug output.
35+
required: false
36+
default: false
37+
Verbose:
38+
type: boolean
39+
description: Enable verbose output.
40+
required: false
41+
default: false
42+
Version:
43+
type: string
44+
description: Specifies the version of the GitHub module to be installed. The value must be an exact version.
45+
required: false
46+
default: ''
47+
Prerelease:
48+
type: boolean
49+
description: Whether to use a prerelease version of the 'GitHub' module.
50+
required: false
51+
default: false
52+
WorkingDirectory:
53+
type: string
54+
description: The working directory where the script will run from.
55+
required: false
56+
default: '.'
57+
58+
permissions:
59+
contents: read # to checkout the repo
60+
61+
jobs:
62+
AfterAll-ModuleLocal:
63+
name: AfterAll-ModuleLocal
64+
runs-on: ubuntu-latest
65+
steps:
66+
- name: Checkout Code
67+
uses: actions/checkout@v5
68+
69+
- name: Install-PSModuleHelpers
70+
uses: PSModule/Install-PSModuleHelpers@v1
71+
72+
- name: Run AfterAll Teardown Scripts
73+
if: always()
74+
uses: PSModule/GitHub-Script@v1
75+
env:
76+
TEST_APP_ENT_CLIENT_ID: ${{ secrets.TEST_APP_ENT_CLIENT_ID }}
77+
TEST_APP_ENT_PRIVATE_KEY: ${{ secrets.TEST_APP_ENT_PRIVATE_KEY }}
78+
TEST_APP_ORG_CLIENT_ID: ${{ secrets.TEST_APP_ORG_CLIENT_ID }}
79+
TEST_APP_ORG_PRIVATE_KEY: ${{ secrets.TEST_APP_ORG_PRIVATE_KEY }}
80+
TEST_USER_ORG_FG_PAT: ${{ secrets.TEST_USER_ORG_FG_PAT }}
81+
TEST_USER_USER_FG_PAT: ${{ secrets.TEST_USER_USER_FG_PAT }}
82+
TEST_USER_PAT: ${{ secrets.TEST_USER_PAT }}
83+
GITHUB_TOKEN: ${{ github.token }}
84+
with:
85+
Name: AfterAll-ModuleLocal
86+
ShowInfo: false
87+
ShowOutput: true
88+
Debug: ${{ inputs.Debug }}
89+
Prerelease: ${{ inputs.Prerelease }}
90+
Verbose: ${{ inputs.Verbose }}
91+
Version: ${{ inputs.Version }}
92+
WorkingDirectory: ${{ inputs.WorkingDirectory }}
93+
Script: |
94+
LogGroup "Running AfterAll Teardown Scripts" {
95+
function Find-TestDirectories {
96+
param ([string]$Path)
97+
98+
$directories = @()
99+
$childDirs = Get-ChildItem -Path $Path -Directory
100+
101+
foreach ($dir in $childDirs) {
102+
$directories += $dir.FullName
103+
$directories += Find-TestDirectories -Path $dir.FullName
104+
}
105+
106+
return $directories
107+
}
108+
109+
# Locate the tests directory.
110+
$testsPath = Resolve-Path 'tests' -ErrorAction SilentlyContinue | Select-Object -ExpandProperty Path
111+
if (-not $testsPath) {
112+
Write-Warning 'No tests directory found'
113+
exit 0
114+
}
115+
Write-Host "Tests found at [$testsPath]"
116+
117+
$allTestFolders = @($testsPath) + (Find-TestDirectories -Path $testsPath)
118+
$processedDirectories = @()
119+
120+
foreach ($folder in $allTestFolders) {
121+
$afterAllScript = Join-Path $folder "AfterAll.ps1"
122+
123+
if (Test-Path $afterAllScript -PathType Leaf) {
124+
# Get unique directory path to avoid duplicate execution
125+
$uniqueDirectory = Resolve-Path $folder -Relative
126+
if ($processedDirectories -notcontains $uniqueDirectory) {
127+
$processedDirectories += $uniqueDirectory
128+
129+
Write-Host "Running AfterAll teardown script: $afterAllScript"
130+
try {
131+
Push-Location $folder
132+
& $afterAllScript
133+
Write-Host "AfterAll script completed successfully: $afterAllScript"
134+
}
135+
catch {
136+
Write-Warning "AfterAll script failed: $afterAllScript - $_"
137+
# Don't throw for teardown scripts to ensure other cleanup scripts can run
138+
}
139+
finally {
140+
Pop-Location
141+
}
142+
}
143+
}
144+
}
145+
146+
if ($processedDirectories.Count -eq 0) {
147+
Write-Host "No AfterAll.ps1 scripts found in test directories"
148+
} else {
149+
Write-Host "Processed AfterAll scripts in $($processedDirectories.Count) directories:"
150+
$processedDirectories | ForEach-Object { Write-Host " - $_" }
151+
}
152+
}
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
name: BeforeAll-ModuleLocal
2+
3+
on:
4+
workflow_call:
5+
secrets:
6+
TEST_APP_ENT_CLIENT_ID:
7+
description: The client ID of an Enterprise GitHub App for running tests.
8+
required: false
9+
TEST_APP_ENT_PRIVATE_KEY:
10+
description: The private key of an Enterprise GitHub App for running tests.
11+
required: false
12+
TEST_APP_ORG_CLIENT_ID:
13+
description: The client ID of an Organization GitHub App for running tests.
14+
required: false
15+
TEST_APP_ORG_PRIVATE_KEY:
16+
description: The private key of an Organization GitHub App for running tests.
17+
required: false
18+
TEST_USER_ORG_FG_PAT:
19+
description: The fine-grained personal access token with org access for running tests.
20+
required: false
21+
TEST_USER_USER_FG_PAT:
22+
description: The fine-grained personal access token with user account access for running tests.
23+
required: false
24+
TEST_USER_PAT:
25+
description: The classic personal access token for running tests.
26+
required: false
27+
inputs:
28+
Name:
29+
type: string
30+
description: The name of the module to process. Scripts default to the repository name if nothing is specified.
31+
required: false
32+
Debug:
33+
type: boolean
34+
description: Enable debug output.
35+
required: false
36+
default: false
37+
Verbose:
38+
type: boolean
39+
description: Enable verbose output.
40+
required: false
41+
default: false
42+
Version:
43+
type: string
44+
description: Specifies the version of the GitHub module to be installed. The value must be an exact version.
45+
required: false
46+
default: ''
47+
Prerelease:
48+
type: boolean
49+
description: Whether to use a prerelease version of the 'GitHub' module.
50+
required: false
51+
default: false
52+
WorkingDirectory:
53+
type: string
54+
description: The working directory where the script will run from.
55+
required: false
56+
default: '.'
57+
58+
permissions:
59+
contents: read # to checkout the repo
60+
61+
jobs:
62+
BeforeAll-ModuleLocal:
63+
name: BeforeAll-ModuleLocal
64+
runs-on: ubuntu-latest
65+
steps:
66+
- name: Checkout Code
67+
uses: actions/checkout@v5
68+
69+
- name: Install-PSModuleHelpers
70+
uses: PSModule/Install-PSModuleHelpers@v1
71+
72+
- name: Run BeforeAll Setup Scripts
73+
uses: PSModule/GitHub-Script@v1
74+
env:
75+
TEST_APP_ENT_CLIENT_ID: ${{ secrets.TEST_APP_ENT_CLIENT_ID }}
76+
TEST_APP_ENT_PRIVATE_KEY: ${{ secrets.TEST_APP_ENT_PRIVATE_KEY }}
77+
TEST_APP_ORG_CLIENT_ID: ${{ secrets.TEST_APP_ORG_CLIENT_ID }}
78+
TEST_APP_ORG_PRIVATE_KEY: ${{ secrets.TEST_APP_ORG_PRIVATE_KEY }}
79+
TEST_USER_ORG_FG_PAT: ${{ secrets.TEST_USER_ORG_FG_PAT }}
80+
TEST_USER_USER_FG_PAT: ${{ secrets.TEST_USER_USER_FG_PAT }}
81+
TEST_USER_PAT: ${{ secrets.TEST_USER_PAT }}
82+
GITHUB_TOKEN: ${{ github.token }}
83+
with:
84+
Name: BeforeAll-ModuleLocal
85+
ShowInfo: false
86+
ShowOutput: true
87+
Debug: ${{ inputs.Debug }}
88+
Prerelease: ${{ inputs.Prerelease }}
89+
Verbose: ${{ inputs.Verbose }}
90+
Version: ${{ inputs.Version }}
91+
WorkingDirectory: ${{ inputs.WorkingDirectory }}
92+
Script: |
93+
LogGroup "Running BeforeAll Setup Scripts" {
94+
function Find-TestDirectories {
95+
param ([string]$Path)
96+
97+
$directories = @()
98+
$childDirs = Get-ChildItem -Path $Path -Directory
99+
100+
foreach ($dir in $childDirs) {
101+
$directories += $dir.FullName
102+
$directories += Find-TestDirectories -Path $dir.FullName
103+
}
104+
105+
return $directories
106+
}
107+
108+
# Locate the tests directory.
109+
$testsPath = Resolve-Path 'tests' -ErrorAction SilentlyContinue | Select-Object -ExpandProperty Path
110+
if (-not $testsPath) {
111+
Write-Warning 'No tests directory found'
112+
exit 0
113+
}
114+
Write-Host "Tests found at [$testsPath]"
115+
116+
$allTestFolders = @($testsPath) + (Find-TestDirectories -Path $testsPath)
117+
$processedDirectories = @()
118+
119+
foreach ($folder in $allTestFolders) {
120+
$beforeAllScript = Join-Path $folder "BeforeAll.ps1"
121+
122+
if (Test-Path $beforeAllScript -PathType Leaf) {
123+
# Get unique directory path to avoid duplicate execution
124+
$uniqueDirectory = Resolve-Path $folder -Relative
125+
if ($processedDirectories -notcontains $uniqueDirectory) {
126+
$processedDirectories += $uniqueDirectory
127+
128+
Write-Host "Running BeforeAll setup script: $beforeAllScript"
129+
try {
130+
Push-Location $folder
131+
& $beforeAllScript
132+
Write-Host "BeforeAll script completed successfully: $beforeAllScript"
133+
}
134+
catch {
135+
Write-Error "BeforeAll script failed: $beforeAllScript - $_"
136+
throw
137+
}
138+
finally {
139+
Pop-Location
140+
}
141+
}
142+
}
143+
}
144+
145+
if ($processedDirectories.Count -eq 0) {
146+
Write-Host "No BeforeAll.ps1 scripts found in test directories"
147+
} else {
148+
Write-Host "Processed BeforeAll scripts in $($processedDirectories.Count) directories:"
149+
$processedDirectories | ForEach-Object { Write-Host " - $_" }
150+
}
151+
}

.github/workflows/CI.yml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,11 +176,27 @@ 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+
needs:
182+
- Build-Module
183+
- Get-Settings
184+
uses: ./.github/workflows/BeforeAll-ModuleLocal.yml
185+
secrets: inherit
186+
with:
187+
Name: ${{ fromJson(needs.Get-Settings.outputs.Settings).Name }}
188+
Debug: ${{ inputs.Debug }}
189+
Prerelease: ${{ inputs.Prerelease }}
190+
Verbose: ${{ inputs.Verbose }}
191+
Version: ${{ inputs.Version }}
192+
WorkingDirectory: ${{ inputs.WorkingDirectory }}
193+
179194
Test-ModuleLocal:
180195
if: ${{ needs.Build-Module.result == 'success' && !cancelled() && needs.Get-Settings.outputs.ModuleTestSuites != '[]' }}
181196
needs:
182197
- Build-Module
183198
- Get-Settings
199+
- BeforeAll-ModuleLocal
184200
strategy:
185201
fail-fast: false
186202
matrix:
@@ -199,6 +215,21 @@ jobs:
199215
Version: ${{ inputs.Version }}
200216
WorkingDirectory: ${{ inputs.WorkingDirectory }}
201217

218+
AfterAll-ModuleLocal:
219+
if: ${{ always() && !cancelled() && needs.Get-Settings.outputs.ModuleTestSuites != '[]' }}
220+
needs:
221+
- Get-Settings
222+
- Test-ModuleLocal
223+
uses: ./.github/workflows/AfterAll-ModuleLocal.yml
224+
secrets: inherit
225+
with:
226+
Name: ${{ fromJson(needs.Get-Settings.outputs.Settings).Name }}
227+
Debug: ${{ inputs.Debug }}
228+
Prerelease: ${{ inputs.Prerelease }}
229+
Verbose: ${{ inputs.Verbose }}
230+
Version: ${{ inputs.Version }}
231+
WorkingDirectory: ${{ inputs.WorkingDirectory }}
232+
202233
Get-TestResults:
203234
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())
204235
needs:
@@ -207,6 +238,7 @@ jobs:
207238
- Lint-SourceCode
208239
- Test-Module
209240
- Test-ModuleLocal
241+
- AfterAll-ModuleLocal
210242
uses: ./.github/workflows/Get-TestResults.yml
211243
secrets: inherit
212244
with:
@@ -224,6 +256,7 @@ jobs:
224256
- Get-Settings
225257
- Test-Module
226258
- Test-ModuleLocal
259+
- AfterAll-ModuleLocal
227260
uses: ./.github/workflows/Get-CodeCoverage.yml
228261
secrets: inherit
229262
with:

0 commit comments

Comments
 (0)