Skip to content

Commit 4d3f4ff

Browse files
Add BeforeAll and AfterAll job support for Test-ModuleLocal workflows
Co-authored-by: MariusStorhaug <[email protected]>
1 parent 0aa2dfc commit 4d3f4ff

File tree

5 files changed

+161
-0
lines changed

5 files changed

+161
-0
lines changed

.github/workflows/workflow.yml

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,11 +178,88 @@ jobs:
178178
Version: ${{ inputs.Version }}
179179
WorkingDirectory: ${{ inputs.WorkingDirectory }}
180180

181+
BeforeAll-ModuleLocal:
182+
if: ${{ needs.Build-Module.result == 'success' && !cancelled() && needs.Get-Settings.outputs.ModuleTestSuites != '[]' }}
183+
name: BeforeAll Setup
184+
runs-on: ubuntu-latest
185+
needs:
186+
- Build-Module
187+
- Get-Settings
188+
outputs:
189+
hasBeforeAll: ${{ steps.check-beforeall.outputs.hasBeforeAll }}
190+
hasAfterAll: ${{ steps.check-afterall.outputs.hasAfterAll }}
191+
steps:
192+
- name: Checkout Code
193+
uses: actions/checkout@v4
194+
195+
- name: Check for BeforeAll/AfterAll scripts
196+
id: check-beforeall
197+
shell: bash
198+
working-directory: ${{ inputs.WorkingDirectory }}
199+
run: |
200+
hasBeforeAll=false
201+
hasAfterAll=false
202+
203+
# Check each test path for BeforeAll/AfterAll scripts
204+
testSuites='${{ needs.Get-Settings.outputs.ModuleTestSuites }}'
205+
echo "Test suites: $testSuites"
206+
207+
if echo "$testSuites" | jq -e '.[] | select(.TestPath)' > /dev/null 2>&1; then
208+
for testPath in $(echo "$testSuites" | jq -r '.[].TestPath' | sort -u); do
209+
echo "Checking test path: $testPath"
210+
if [ -f "$testPath/BeforeAll.ps1" ]; then
211+
echo "BeforeAll.ps1 found in $testPath"
212+
hasBeforeAll=true
213+
fi
214+
if [ -f "$testPath/AfterAll.ps1" ]; then
215+
echo "AfterAll.ps1 found in $testPath"
216+
hasAfterAll=true
217+
fi
218+
done
219+
fi
220+
221+
echo "hasBeforeAll=$hasBeforeAll" >> $GITHUB_OUTPUT
222+
echo "hasAfterAll=$hasAfterAll" >> $GITHUB_OUTPUT
223+
224+
- name: Install-PSModuleHelpers
225+
if: steps.check-beforeall.outputs.hasBeforeAll == 'true'
226+
uses: PSModule/Install-PSModuleHelpers@v1
227+
228+
- name: Run BeforeAll scripts
229+
if: steps.check-beforeall.outputs.hasBeforeAll == 'true'
230+
shell: pwsh
231+
working-directory: ${{ inputs.WorkingDirectory }}
232+
env:
233+
TEST_APP_ENT_CLIENT_ID: ${{ secrets.TEST_APP_ENT_CLIENT_ID }}
234+
TEST_APP_ENT_PRIVATE_KEY: ${{ secrets.TEST_APP_ENT_PRIVATE_KEY }}
235+
TEST_APP_ORG_CLIENT_ID: ${{ secrets.TEST_APP_ORG_CLIENT_ID }}
236+
TEST_APP_ORG_PRIVATE_KEY: ${{ secrets.TEST_APP_ORG_PRIVATE_KEY }}
237+
TEST_USER_ORG_FG_PAT: ${{ secrets.TEST_USER_ORG_FG_PAT }}
238+
TEST_USER_USER_FG_PAT: ${{ secrets.TEST_USER_USER_FG_PAT }}
239+
TEST_USER_PAT: ${{ secrets.TEST_USER_PAT }}
240+
GITHUB_TOKEN: ${{ github.token }}
241+
run: |
242+
$testSuites = '${{ needs.Get-Settings.outputs.ModuleTestSuites }}' | ConvertFrom-Json
243+
$processedPaths = @()
244+
245+
foreach ($suite in $testSuites) {
246+
$testPath = $suite.TestPath
247+
if ($testPath -and $testPath -notin $processedPaths) {
248+
$beforeAllScript = Join-Path $testPath "BeforeAll.ps1"
249+
if (Test-Path $beforeAllScript) {
250+
Write-Host "Running BeforeAll setup script: $beforeAllScript"
251+
& $beforeAllScript
252+
$processedPaths += $testPath
253+
}
254+
}
255+
}
256+
181257
Test-ModuleLocal:
182258
if: ${{ needs.Build-Module.result == 'success' && !cancelled() && needs.Get-Settings.outputs.ModuleTestSuites != '[]' }}
183259
needs:
184260
- Build-Module
185261
- Get-Settings
262+
- BeforeAll-ModuleLocal
186263
strategy:
187264
fail-fast: false
188265
matrix:
@@ -201,6 +278,48 @@ jobs:
201278
Version: ${{ inputs.Version }}
202279
WorkingDirectory: ${{ inputs.WorkingDirectory }}
203280

281+
AfterAll-ModuleLocal:
282+
if: ${{ always() && needs.BeforeAll-ModuleLocal.outputs.hasAfterAll == 'true' }}
283+
name: AfterAll Teardown
284+
runs-on: ubuntu-latest
285+
needs:
286+
- BeforeAll-ModuleLocal
287+
- Test-ModuleLocal
288+
steps:
289+
- name: Checkout Code
290+
uses: actions/checkout@v4
291+
292+
- name: Install-PSModuleHelpers
293+
uses: PSModule/Install-PSModuleHelpers@v1
294+
295+
- name: Run AfterAll scripts
296+
shell: pwsh
297+
working-directory: ${{ inputs.WorkingDirectory }}
298+
env:
299+
TEST_APP_ENT_CLIENT_ID: ${{ secrets.TEST_APP_ENT_CLIENT_ID }}
300+
TEST_APP_ENT_PRIVATE_KEY: ${{ secrets.TEST_APP_ENT_PRIVATE_KEY }}
301+
TEST_APP_ORG_CLIENT_ID: ${{ secrets.TEST_APP_ORG_CLIENT_ID }}
302+
TEST_APP_ORG_PRIVATE_KEY: ${{ secrets.TEST_APP_ORG_PRIVATE_KEY }}
303+
TEST_USER_ORG_FG_PAT: ${{ secrets.TEST_USER_ORG_FG_PAT }}
304+
TEST_USER_USER_FG_PAT: ${{ secrets.TEST_USER_USER_FG_PAT }}
305+
TEST_USER_PAT: ${{ secrets.TEST_USER_PAT }}
306+
GITHUB_TOKEN: ${{ github.token }}
307+
run: |
308+
$testSuites = '${{ needs.Get-Settings.outputs.ModuleTestSuites }}' | ConvertFrom-Json
309+
$processedPaths = @()
310+
311+
foreach ($suite in $testSuites) {
312+
$testPath = $suite.TestPath
313+
if ($testPath -and $testPath -notin $processedPaths) {
314+
$afterAllScript = Join-Path $testPath "AfterAll.ps1"
315+
if (Test-Path $afterAllScript) {
316+
Write-Host "Running AfterAll teardown script: $afterAllScript"
317+
& $afterAllScript
318+
$processedPaths += $testPath
319+
}
320+
}
321+
}
322+
204323
Get-TestResults:
205324
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())
206325
needs:
@@ -209,6 +328,7 @@ jobs:
209328
- Lint-SourceCode
210329
- Test-Module
211330
- Test-ModuleLocal
331+
- AfterAll-ModuleLocal
212332
uses: ./.github/workflows/Get-TestResults.yml
213333
secrets: inherit
214334
with:
@@ -226,6 +346,7 @@ jobs:
226346
- Get-Settings
227347
- Test-Module
228348
- Test-ModuleLocal
349+
- AfterAll-ModuleLocal
229350
uses: ./.github/workflows/Get-CodeCoverage.yml
230351
secrets: inherit
231352
with:
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
Write-Host "=== AFTERALL TEARDOWN SCRIPT EXECUTING ==="
2+
Write-Host "Tearing down test environment..."
3+
4+
# Example teardown tasks:
5+
# - Clean up test infrastructure
6+
# - Remove test data
7+
# - Cleanup test environment
8+
# - Drop test databases
9+
# - Stop test services
10+
11+
Write-Host "Test environment teardown completed successfully!"
12+
Write-Host "=== AFTERALL TEARDOWN SCRIPT COMPLETED ==="
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
Write-Host "=== BEFOREALL SETUP SCRIPT EXECUTING ==="
2+
Write-Host "Setting up test environment..."
3+
4+
# Example setup tasks:
5+
# - Deploy test infrastructure
6+
# - Download test data
7+
# - Initialize test environment
8+
# - Set up test databases
9+
# - Configure test services
10+
11+
Write-Host "Test environment setup completed successfully!"
12+
Write-Host "=== BEFOREALL SETUP SCRIPT COMPLETED ==="
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Write-Host "=== AFTERALL TEARDOWN SCRIPT (MyTests) EXECUTING ==="
2+
Write-Host "Tearing down test environment for MyTests..."
3+
4+
# Example teardown tasks for MyTests directory:
5+
Write-Host "Cleaning up MyTests test environment..."
6+
7+
Write-Host "MyTests environment teardown completed successfully!"
8+
Write-Host "=== AFTERALL TEARDOWN SCRIPT (MyTests) COMPLETED ==="
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Write-Host "=== BEFOREALL SETUP SCRIPT (MyTests) EXECUTING ==="
2+
Write-Host "Setting up test environment for MyTests..."
3+
4+
# Example setup tasks for MyTests directory:
5+
Write-Host "Initializing MyTests test environment..."
6+
7+
Write-Host "MyTests environment setup completed successfully!"
8+
Write-Host "=== BEFOREALL SETUP SCRIPT (MyTests) COMPLETED ==="

0 commit comments

Comments
 (0)