Skip to content

Commit 2027a0b

Browse files
Rework BeforeAll/AfterAll to use PowerShell-based steps
Co-authored-by: MariusStorhaug <[email protected]>
1 parent 2a30035 commit 2027a0b

File tree

2 files changed

+45
-121
lines changed

2 files changed

+45
-121
lines changed

.github/workflows/Test-ModuleLocal.yml

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,28 @@ jobs:
104104
"name=$name" >> $env:GITHUB_OUTPUT
105105
"path=$path" >> $env:GITHUB_OUTPUT
106106
107+
- name: Run BeforeAll Setup Script
108+
shell: pwsh
109+
working-directory: ${{ inputs.WorkingDirectory }}
110+
env:
111+
TEST_APP_ENT_CLIENT_ID: ${{ secrets.TEST_APP_ENT_CLIENT_ID }}
112+
TEST_APP_ENT_PRIVATE_KEY: ${{ secrets.TEST_APP_ENT_PRIVATE_KEY }}
113+
TEST_APP_ORG_CLIENT_ID: ${{ secrets.TEST_APP_ORG_CLIENT_ID }}
114+
TEST_APP_ORG_PRIVATE_KEY: ${{ secrets.TEST_APP_ORG_PRIVATE_KEY }}
115+
TEST_USER_ORG_FG_PAT: ${{ secrets.TEST_USER_ORG_FG_PAT }}
116+
TEST_USER_USER_FG_PAT: ${{ secrets.TEST_USER_USER_FG_PAT }}
117+
TEST_USER_PAT: ${{ secrets.TEST_USER_PAT }}
118+
GITHUB_TOKEN: ${{ github.token }}
119+
run: |
120+
$testPath = '${{ inputs.TestPath }}'
121+
$beforeAllScript = Join-Path $testPath "BeforeAll.ps1"
122+
if (Test-Path $beforeAllScript) {
123+
Write-Host "Running BeforeAll setup script: $beforeAllScript"
124+
& $beforeAllScript
125+
} else {
126+
Write-Host "No BeforeAll.ps1 script found in $testPath"
127+
}
128+
107129
- name: Test-ModuleLocal
108130
uses: PSModule/Invoke-Pester@v4
109131
env:
@@ -133,3 +155,26 @@ jobs:
133155
Prescript: | # This is to speed up module loading in Pester.
134156
Install-PSResource -Repository PSGallery -TrustRepository -Name PSCustomObject
135157
Import-Module -Name '${{ steps.import-module.outputs.name }}' -RequiredVersion 999.0.0
158+
159+
- name: Run AfterAll Teardown Script
160+
if: always()
161+
shell: pwsh
162+
working-directory: ${{ inputs.WorkingDirectory }}
163+
env:
164+
TEST_APP_ENT_CLIENT_ID: ${{ secrets.TEST_APP_ENT_CLIENT_ID }}
165+
TEST_APP_ENT_PRIVATE_KEY: ${{ secrets.TEST_APP_ENT_PRIVATE_KEY }}
166+
TEST_APP_ORG_CLIENT_ID: ${{ secrets.TEST_APP_ORG_CLIENT_ID }}
167+
TEST_APP_ORG_PRIVATE_KEY: ${{ secrets.TEST_APP_ORG_PRIVATE_KEY }}
168+
TEST_USER_ORG_FG_PAT: ${{ secrets.TEST_USER_ORG_FG_PAT }}
169+
TEST_USER_USER_FG_PAT: ${{ secrets.TEST_USER_USER_FG_PAT }}
170+
TEST_USER_PAT: ${{ secrets.TEST_USER_PAT }}
171+
GITHUB_TOKEN: ${{ github.token }}
172+
run: |
173+
$testPath = '${{ inputs.TestPath }}'
174+
$afterAllScript = Join-Path $testPath "AfterAll.ps1"
175+
if (Test-Path $afterAllScript) {
176+
Write-Host "Running AfterAll teardown script: $afterAllScript"
177+
& $afterAllScript
178+
} else {
179+
Write-Host "No AfterAll.ps1 script found in $testPath"
180+
}

.github/workflows/workflow.yml

Lines changed: 0 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -178,88 +178,11 @@ 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-
257181
Test-ModuleLocal:
258182
if: ${{ needs.Build-Module.result == 'success' && !cancelled() && needs.Get-Settings.outputs.ModuleTestSuites != '[]' }}
259183
needs:
260184
- Build-Module
261185
- Get-Settings
262-
- BeforeAll-ModuleLocal
263186
strategy:
264187
fail-fast: false
265188
matrix:
@@ -278,48 +201,6 @@ jobs:
278201
Version: ${{ inputs.Version }}
279202
WorkingDirectory: ${{ inputs.WorkingDirectory }}
280203

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-
323204
Get-TestResults:
324205
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())
325206
needs:
@@ -328,7 +209,6 @@ jobs:
328209
- Lint-SourceCode
329210
- Test-Module
330211
- Test-ModuleLocal
331-
- AfterAll-ModuleLocal
332212
uses: ./.github/workflows/Get-TestResults.yml
333213
secrets: inherit
334214
with:
@@ -346,7 +226,6 @@ jobs:
346226
- Get-Settings
347227
- Test-Module
348228
- Test-ModuleLocal
349-
- AfterAll-ModuleLocal
350229
uses: ./.github/workflows/Get-CodeCoverage.yml
351230
secrets: inherit
352231
with:

0 commit comments

Comments
 (0)