Skip to content

Commit a5a1456

Browse files
🩹 [CI]: Refactor Get-TestSuites workflow to improve test skipping logic and enhance output clarity
1 parent 63cbccd commit a5a1456

File tree

1 file changed

+67
-92
lines changed

1 file changed

+67
-92
lines changed

‎.github/workflows/Get-TestSuites.yml

Lines changed: 67 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -73,15 +73,13 @@ jobs:
7373
Version: ${{ inputs.Version }}
7474
WorkingDirectory: ${{ inputs.WorkingDirectory }}
7575
Script: |
76-
# Define test configurations as an array of hashtables.
77-
$osConfigs = @(
78-
@{ RunsOn = 'ubuntu-latest'; OSName = 'Linux' }
79-
@{ RunsOn = 'macos-latest'; OSName = 'macOS' }
80-
@{ RunsOn = 'windows-latest'; OSName = 'Windows' }
81-
)
82-
76+
# Get-TestSuites
8377
$skipTests = $env:PSMODULE_GET_TESTSUITES_INPUT_SkipTests -Split ",|\s+" | ForEach-Object { $_.Trim() }
8478
79+
LogGroup 'Tests to be skipped:' {
80+
$skipTests | ForEach-Object { " - $_ " }
81+
}
82+
8583
if ($skipTests -contains 'All') {
8684
Write-Host "Skipping all tests as requested."
8785
Set-GitHubOutput -Name SourceCodeTestSuites -Value '[]'
@@ -90,116 +88,93 @@ jobs:
9088
exit 0
9189
}
9290
93-
# Filter out OS-specific tests early
94-
$osConfigs = $osConfigs | Where-Object { $skipTests -notcontains $_.OSName }
91+
# Define test configurations as an array of hashtables.
92+
$osConfigs = @(
93+
@{ RunsOn = 'ubuntu-latest'; OSName = 'Linux' }
94+
@{ RunsOn = 'macos-latest'; OSName = 'macOS' }
95+
@{ RunsOn = 'windows-latest'; OSName = 'Windows' }
96+
) | Where-Object { $skipTests -notcontains $_.OSName }
9597
9698
if (-not $osConfigs) {
97-
Write-Host "No OS configurations left after filtering. Exiting."
99+
Write-Host "Skipping all OS configurations."
98100
Set-GitHubOutput -Name SourceCodeTestSuites -Value '[]'
99101
Set-GitHubOutput -Name ModuleTestSuites -Value '[]'
100102
Set-GitHubOutput -Name ModuleLocalTestSuites -Value '[]'
101103
exit 0
102104
}
103105
104-
# Locate the tests directory.
105-
$testsPath = Resolve-Path 'tests' -ErrorAction SilentlyContinue | Select-Object -ExpandProperty Path
106-
if (-not $testsPath -or -not (Test-Path -Path $testsPath)) {
107-
Write-Warning 'No tests found'
108-
exit 0
106+
LogGroup 'Source Code Test Suites:' {
107+
if ($skipTests -notcontain 'SourceCode') {
108+
$osConfigs | Format-Table -AutoSize | Out-String
109+
Set-GitHubOutput -Name SourceCodeTestSuites -Value $osConfigs
110+
}
109111
}
110-
Write-Host "Tests found at [$testsPath]"
111112
112-
function Get-TestItemsFromFolder {
113-
param ([string]$FolderPath)
114-
115-
$configFiles = Get-ChildItem -Path $FolderPath -File -Filter '*.Configuration.ps1'
116-
if ($configFiles.Count -eq 1) {
117-
return @($configFiles)
118-
} elseif ($configFiles.Count -gt 1) {
119-
throw "Multiple configuration files found in [$FolderPath]. Please separate configurations into different folders."
113+
LogGroup 'Module Test Suites:' {
114+
if ($skipTests -notcontain 'Module') {
115+
$osConfigs | Format-Table -AutoSize | Out-String
116+
Set-GitHubOutput -Name ModuleTestSuites -Value $osConfigs
120117
}
118+
}
121119
122-
$containerFiles = Get-ChildItem -Path $FolderPath -File -Filter '*.Container.ps1'
123-
if ($containerFiles.Count -ge 1) {
124-
return $containerFiles
120+
LogGroup 'Module Local Test Suites:' {
121+
# Locate the tests directory.
122+
$testsPath = Resolve-Path 'tests' -ErrorAction SilentlyContinue | Select-Object -ExpandProperty Path
123+
if (-not $testsPath -or -not (Test-Path -Path $testsPath)) {
124+
Write-Warning 'No tests found'
125+
exit 0
125126
}
127+
Write-Host "Tests found at [$testsPath]"
126128
127-
$testFiles = Get-ChildItem -Path $FolderPath -File -Filter '*.Tests.ps1'
128-
return $testFiles
129-
}
129+
function Get-TestItemsFromFolder {
130+
param ([string]$FolderPath)
130131
131-
function Find-TestDirectories {
132-
param ([string]$Path)
132+
$configFiles = Get-ChildItem -Path $FolderPath -File -Filter '*.Configuration.ps1'
133+
if ($configFiles.Count -eq 1) {
134+
return @($configFiles)
135+
} elseif ($configFiles.Count -gt 1) {
136+
throw "Multiple configuration files found in [$FolderPath]. Please separate configurations into different folders."
137+
}
133138
134-
$directories = @()
135-
$childDirs = Get-ChildItem -Path $Path -Directory
139+
$containerFiles = Get-ChildItem -Path $FolderPath -File -Filter '*.Container.ps1'
140+
if ($containerFiles.Count -ge 1) {
141+
return $containerFiles
142+
}
136143
137-
foreach ($dir in $childDirs) {
138-
$directories += $dir.FullName
139-
$directories += Find-TestDirectories -Path $dir.FullName
144+
$testFiles = Get-ChildItem -Path $FolderPath -File -Filter '*.Tests.ps1'
145+
return $testFiles
140146
}
141147
142-
return $directories
143-
}
144-
145-
$allTestFolders = @($testsPath) + (Find-TestDirectories -Path $testsPath)
148+
function Find-TestDirectories {
149+
param ([string]$Path)
146150
147-
$skipTests = $env:PSMODULE_GET_TESTSUITES_INPUT_SkipTests -Split ",|\s+" | ForEach-Object { $_.Trim() }
151+
$directories = @()
152+
$childDirs = Get-ChildItem -Path $Path -Directory
148153
149-
LogGroup 'Tests to be skipped:' {
150-
$skipTests | ForEach-Object { " - $_ " }
151-
}
154+
foreach ($dir in $childDirs) {
155+
$directories += $dir.FullName
156+
$directories += Find-TestDirectories -Path $dir.FullName
157+
}
152158
153-
$testSuites = foreach ($folder in $allTestFolders) {
154-
$testItems = Get-TestItemsFromFolder -FolderPath $folder
155-
foreach ($item in $testItems) {
156-
foreach ($osConfig in $osConfigs) {
157-
if ($skipTests -contains 'All' -or
158-
$skipTests -contains $osConfig.OSName -or
159-
($skipTests -contains 'Module' -and $item.Name -match '\.Tests\.ps1$') -or
160-
($skipTests -contains 'SourceCode' -and $item.Name -match '\.(Configuration|Container)\.ps1$')) {
161-
continue
162-
}
159+
return $directories
160+
}
163161
164-
[pscustomobject]@{
165-
RunsOn = $osConfig.RunsOn
166-
OSName = $osConfig.OSName
167-
TestPath = Resolve-Path -Path $item.FullName -Relative
168-
TestName = ($item.BaseName -Split '.')[0]
162+
$allTestFolders = @($testsPath) + (Find-TestDirectories -Path $testsPath)
163+
164+
$moduleLocalTestSuites = foreach ($folder in $allTestFolders) {
165+
$testItems = Get-TestItemsFromFolder -FolderPath $folder
166+
foreach ($item in $testItems) {
167+
foreach ($osConfig in $osConfigs) {
168+
[pscustomobject]@{
169+
RunsOn = $osConfig.RunsOn
170+
OSName = $osConfig.OSName
171+
TestPath = Resolve-Path -Path $item.FullName -Relative
172+
TestName = ($item.BaseName -Split '.')[0]
173+
}
169174
}
170175
}
171176
}
172-
}
173177
174-
# Adjust source code test suites based on SkipTests
175-
$sourceCodeTestSuites = $osConfigs | Where-Object {
176-
-not ($skipTests -contains 'All' -or $skipTests -contains $_.OSName -or $skipTests -contains 'SourceCode')
177-
} | Select-Object -Property RunsOn, OSName -Unique
178-
179-
# Generate ModuleTestSuites (static tests)
180-
$moduleTestSuites = $testSuites | Where-Object {
181-
$_.TestName -match '\.(Configuration|Container)\.ps1$' -and
182-
-not ($skipTests -contains 'All' -or $skipTests -contains $_.OSName -or $skipTests -contains 'Module')
183-
}
184-
185-
# Generate ModuleLocalTestSuites (local tests)
186-
$moduleLocalTestSuites = $testSuites | Where-Object {
187-
$_.TestName -match '\.Tests\.ps1$' -and
188-
-not ($skipTests -contains 'All' -or $skipTests -contains $_.OSName -or $skipTests -contains 'Module')
189-
}
190-
191-
# Display the generated matrices for verification.
192-
LogGroup 'Source Code Test Suites:' {
193-
$sourceCodeTestSuites | Format-Table -AutoSize | Out-String
178+
$moduleLocalTestSuites | Format-Table -AutoSize | Out-String
179+
Set-GitHubOutput -Name ModuleLocalTestSuites -Value ($moduleLocalTestSuites ?? '[]')
194180
}
195-
LogGroup 'Module Test Suites:' {
196-
$moduleTestSuites | Format-Table -AutoSize | Out-String
197-
}
198-
LogGroup 'Module Local Test Suites:' {
199-
$moduleLocalTestSuites | Format-Table -AutoSize | Out-String
200-
}
201-
202-
# Pass the final objects to GitHub Actions output as JSON
203-
Set-GitHubOutput -Name SourceCodeTestSuites -Value ($sourceCodeTestSuites ?? '[]')
204-
Set-GitHubOutput -Name ModuleTestSuites -Value ($moduleTestSuites ?? '[]')
205-
Set-GitHubOutput -Name ModuleLocalTestSuites -Value ($moduleLocalTestSuites ?? '[]')

0 commit comments

Comments
 (0)