Skip to content

Commit 4ef34b4

Browse files
fix: Simplify BeforeAll and AfterAll script execution in workflows by removing redundant directory search logic
1 parent 879e315 commit 4ef34b4

File tree

2 files changed

+29
-103
lines changed

2 files changed

+29
-103
lines changed

.github/workflows/Test-ModuleLocal.yml

Lines changed: 28 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -96,62 +96,25 @@ jobs:
9696
WorkingDirectory: ${{ inputs.WorkingDirectory }}
9797
Script: |
9898
LogGroup "Running BeforeAll Setup Scripts" {
99-
function Find-TestDirectories {
100-
param ([string]$Path)
99+
$beforeAllScript = 'tests/BeforeAll.ps1'
101100
102-
$directories = @()
103-
$childDirs = Get-ChildItem -Path $Path -Directory
104-
105-
foreach ($dir in $childDirs) {
106-
$directories += $dir.FullName
107-
$directories += Find-TestDirectories -Path $dir.FullName
108-
}
109-
110-
return $directories
111-
}
112-
113-
# Locate the tests directory.
114-
$testsPath = Resolve-Path 'tests' -ErrorAction SilentlyContinue | Select-Object -ExpandProperty Path
115-
if (-not $testsPath) {
116-
Write-Host 'No tests directory found - exiting successfully'
101+
if (-not (Test-Path $beforeAllScript)) {
102+
Write-Host "No BeforeAll.ps1 script found at [$beforeAllScript] - exiting successfully"
117103
exit 0
118104
}
119-
Write-Host "Tests found at [$testsPath]"
120-
121-
$allTestFolders = @($testsPath) + (Find-TestDirectories -Path $testsPath)
122-
$processedDirectories = @()
123105
124-
foreach ($folder in $allTestFolders) {
125-
$beforeAllScript = Join-Path $folder "BeforeAll.ps1"
126-
127-
if (Test-Path $beforeAllScript -PathType Leaf) {
128-
# Get unique directory path to avoid duplicate execution
129-
$uniqueDirectory = Resolve-Path $folder -Relative
130-
if ($processedDirectories -notcontains $uniqueDirectory) {
131-
$processedDirectories += $uniqueDirectory
132-
133-
Write-Host "Running BeforeAll setup script: $beforeAllScript"
134-
try {
135-
Push-Location $folder
136-
& $beforeAllScript
137-
Write-Host "BeforeAll script completed successfully: $beforeAllScript"
138-
}
139-
catch {
140-
Write-Error "BeforeAll script failed: $beforeAllScript - $_"
141-
throw
142-
}
143-
finally {
144-
Pop-Location
145-
}
146-
}
147-
}
106+
Write-Host "Running BeforeAll setup script: $beforeAllScript"
107+
try {
108+
Push-Location 'tests'
109+
& $beforeAllScript
110+
Write-Host "BeforeAll script completed successfully: $beforeAllScript"
148111
}
149-
150-
if ($processedDirectories.Count -eq 0) {
151-
Write-Host "No BeforeAll.ps1 scripts found in test directories - exiting successfully"
152-
} else {
153-
Write-Host "Processed BeforeAll scripts in $($processedDirectories.Count) directories:"
154-
$processedDirectories | ForEach-Object { Write-Host " - $_" }
112+
catch {
113+
Write-Error "BeforeAll script failed: $beforeAllScript - $_"
114+
throw
115+
}
116+
finally {
117+
Pop-Location
155118
}
156119
}
157120
@@ -236,61 +199,24 @@ jobs:
236199
WorkingDirectory: ${{ inputs.WorkingDirectory }}
237200
Script: |
238201
LogGroup "Running AfterAll Teardown Scripts" {
239-
function Find-TestDirectories {
240-
param ([string]$Path)
202+
$afterAllScript = 'tests/AfterAll.ps1'
241203
242-
$directories = @()
243-
$childDirs = Get-ChildItem -Path $Path -Directory
244-
245-
foreach ($dir in $childDirs) {
246-
$directories += $dir.FullName
247-
$directories += Find-TestDirectories -Path $dir.FullName
248-
}
249-
250-
return $directories
251-
}
252-
253-
# Locate the tests directory.
254-
$testsPath = Resolve-Path 'tests' -ErrorAction SilentlyContinue | Select-Object -ExpandProperty Path
255-
if (-not $testsPath) {
256-
Write-Host 'No tests directory found - exiting successfully'
204+
if (-not (Test-Path $afterAllScript)) {
205+
Write-Host "No AfterAll.ps1 script found at [$afterAllScript] - exiting successfully"
257206
exit 0
258207
}
259-
Write-Host "Tests found at [$testsPath]"
260-
261-
$allTestFolders = @($testsPath) + (Find-TestDirectories -Path $testsPath)
262-
$processedDirectories = @()
263208
264-
foreach ($folder in $allTestFolders) {
265-
$afterAllScript = Join-Path $folder "AfterAll.ps1"
266-
267-
if (Test-Path $afterAllScript -PathType Leaf) {
268-
# Get unique directory path to avoid duplicate execution
269-
$uniqueDirectory = Resolve-Path $folder -Relative
270-
if ($processedDirectories -notcontains $uniqueDirectory) {
271-
$processedDirectories += $uniqueDirectory
272-
273-
Write-Host "Running AfterAll teardown script: $afterAllScript"
274-
try {
275-
Push-Location $folder
276-
& $afterAllScript
277-
Write-Host "AfterAll script completed successfully: $afterAllScript"
278-
}
279-
catch {
280-
Write-Warning "AfterAll script failed: $afterAllScript - $_"
281-
# Don't throw for teardown scripts to ensure other cleanup scripts can run
282-
}
283-
finally {
284-
Pop-Location
285-
}
286-
}
287-
}
209+
Write-Host "Running AfterAll teardown script: $afterAllScript"
210+
try {
211+
Push-Location 'tests'
212+
& $afterAllScript
213+
Write-Host "AfterAll script completed successfully: $afterAllScript"
288214
}
289-
290-
if ($processedDirectories.Count -eq 0) {
291-
Write-Host "No AfterAll.ps1 scripts found in test directories - exiting successfully"
292-
} else {
293-
Write-Host "Processed AfterAll scripts in $($processedDirectories.Count) directories:"
294-
$processedDirectories | ForEach-Object { Write-Host " - $_" }
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
295221
}
296222
}

specs/001-building-on-this/tasks.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@
163163
- Include DO examples: Azure/AWS deployment, external database initialization, SaaS test data creation
164164
- Include DON'T examples: OS-specific dependencies, platform-specific files, test-specific resources
165165
- Provide practical examples with Azure CLI and REST API calls
166-
- Document when to use BeforeAll/AfterAll vs. in-test setup
166+
- Document when to use BeforeAll/AfterAll vs. in-test setup:
167167
- [ ] T027 [P] Update Process-PSModule and Template-PSModule documentation
168168
- Update Process-PSModule README with composite action documentation
169169
- Update Template-PSModule with example BeforeAll/AfterAll scripts showing external resource management

0 commit comments

Comments
 (0)