Skip to content

Commit 6b5898a

Browse files
🩹 [Patch]: Update path resolution logic to support recursive search (#26)
## Description This pull request includes several changes to improve the configuration handling and directory management in the testing scripts and workflows, including updates to the workflow configuration, enhancements to the script logic for handling paths and directories, and improvements to the configuration export process. ### Script Enhancements * `scripts/Helpers.psm1`: * Added the `-Recurse` flag to `Get-ChildItem` to search for configuration files recursively in directories. * `scripts/init.ps1`: * Improved the logic for handling provided paths, ensuring proper directory and file validation. * Enhanced the handling of boolean input values by explicitly checking and converting string representations of boolean values. * Updated the configuration export process to use a temporary directory for storing configuration and container files, ensuring cleaner and more organized output. * `scripts/main.ps1`: * Modified the script to import the configuration from the temporary directory and handle missing configuration files more gracefully. ### Testing changes * `.github/workflows/Action-Test.yml`: * Replaced `Path` with `WorkingDirectory` in multiple job steps to better specify the working directory for tests. ## Type of change <!-- Use the check-boxes [x] on the options that are relevant. --> - [ ] 📖 [Docs] - [ ] 🪲 [Fix] - [x] 🩹 [Patch] - [ ] ⚠️ [Security fix] - [ ] 🚀 [Feature] - [ ] 🌟 [Breaking change] ## Checklist <!-- Use the check-boxes [x] on the options that are relevant. --> - [x] I have performed a self-review of my own code - [ ] I have commented my code, particularly in hard-to-understand areas
1 parent 3c3418e commit 6b5898a

4 files changed

Lines changed: 68 additions & 39 deletions

File tree

.github/workflows/Action-Test.yml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ jobs:
5656
id: action-test
5757
continue-on-error: true
5858
with:
59-
Path: tests/1-Simple-Failure
59+
WorkingDirectory: tests/1-Simple-Failure
6060

6161
- name: Status
6262
shell: pwsh
@@ -79,7 +79,7 @@ jobs:
7979
uses: ./
8080
id: action-test
8181
with:
82-
Path: tests/2-Standard
82+
WorkingDirectory: tests/2-Standard
8383
Output_CIFormat: GithubActions
8484

8585
- name: Status
@@ -103,7 +103,8 @@ jobs:
103103
uses: ./
104104
id: action-test
105105
with:
106-
Path: tests/3-Advanced/Pester.Configuration.ps1
106+
Path: Pester.Configuration.ps1
107+
WorkingDirectory: tests/3-Advanced
107108

108109
- name: Status
109110
shell: pwsh

scripts/Helpers.psm1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ function Get-PesterConfiguration {
9797

9898
if ($item.PSIsContainer) {
9999
Write-Verbose 'Path is a directory. Searching for configuration files...'
100-
$file = Get-ChildItem -Path $Path -Filter *.Configuration.*
100+
$file = Get-ChildItem -Path $Path -Filter *.Configuration.* -Recurse
101101
Write-Verbose "Found $($file.Count) configuration files."
102102
if ($file.Count -eq 0) {
103103
Write-Verbose "No configuration files found in path: [$Path]"

scripts/init.ps1

Lines changed: 46 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,9 @@ LogGroup 'Init - Get test kit versions' {
2020

2121
LogGroup 'Init - Load inputs' {
2222
$path = [string]::IsNullOrEmpty($env:PSMODULE_INVOKE_PESTER_INPUT_Path) ? '.' : $env:PSMODULE_INVOKE_PESTER_INPUT_Path
23-
$providedItem = Resolve-Path -Path $path | Select-Object -ExpandProperty Path | Get-Item
24-
if ($providedItem -is [System.IO.DirectoryInfo]) {
25-
$providedPath = $providedItem.FullName
26-
} elseif ($providedItem -is [System.IO.FileInfo]) {
27-
$providedPath = $providedItem.Directory.FullName
28-
} else {
29-
Write-GitHubError "❌ Provided path [$providedItem] is not a valid directory or file."
30-
exit 1
31-
}
3223

3324
$inputs = @{
34-
Path = $providedPath
25+
Path = $path
3526

3627
Run_Path = $env:PSMODULE_INVOKE_PESTER_INPUT_Run_Path
3728
Run_ExcludePath = $env:PSMODULE_INVOKE_PESTER_INPUT_Run_ExcludePath
@@ -104,9 +95,9 @@ LogGroup 'Init - Load configuration - Action overrides' {
10495
ScriptBlock = $inputs.Run_ScriptBlock
10596
Container = $inputs.Run_Container
10697
TestExtension = $inputs.Run_TestExtension
107-
Exit = $inputs.Run_Exit
108-
Throw = $inputs.Run_Throw
109-
SkipRun = $inputs.Run_SkipRun
98+
Exit = $null -ne $inputs.Run_Exit ? $inputs.Run_Exit -eq 'true' : $null
99+
Throw = $null -ne $inputs.Run_Throw ? $inputs.Run_Throw -eq 'true' : $null
100+
SkipRun = $null -ne $inputs.Run_SkipRun ? $inputs.Run_SkipRun -eq 'true' : $null
110101
SkipRemainingOnFailure = $inputs.Run_SkipRemainingOnFailure
111102
}
112103
Filter = @{
@@ -117,19 +108,19 @@ LogGroup 'Init - Load configuration - Action overrides' {
117108
FullName = $inputs.Filter_FullName
118109
}
119110
CodeCoverage = @{
120-
Enabled = $inputs.CodeCoverage_Enabled
111+
Enabled = $null -ne $inputs.CodeCoverage_Enabled ? $inputs.CodeCoverage_Enabled -eq 'true' : $null
121112
OutputFormat = $inputs.CodeCoverage_OutputFormat
122113
OutputPath = $inputs.CodeCoverage_OutputPath
123114
OutputEncoding = $inputs.CodeCoverage_OutputEncoding
124115
Path = $inputs.CodeCoverage_Path
125-
ExcludeTests = $inputs.CodeCoverage_ExcludeTests
126-
RecursePaths = $inputs.CodeCoverage_RecursePaths
127-
CoveragePercentTarget = $inputs.CodeCoverage_CoveragePercentTarget
128-
UseBreakpoints = $inputs.CodeCoverage_UseBreakpoints
129-
SingleHitBreakpoints = $inputs.CodeCoverage_SingleHitBreakpoints
116+
ExcludeTests = $null -ne $inputs.CodeCoverage_ExcludeTests ? $inputs.CodeCoverage_ExcludeTests -eq 'true' : $null
117+
RecursePaths = $null -ne $inputs.CodeCoverage_RecursePaths ? $inputs.CodeCoverage_RecursePaths -eq 'true' : $null
118+
CoveragePercentTarget = [decimal]$inputs.CodeCoverage_CoveragePercentTarget
119+
UseBreakpoints = $null -ne $inputs.CodeCoverage_UseBreakpoints ? $inputs.CodeCoverage_UseBreakpoints -eq 'true' : $null
120+
SingleHitBreakpoints = $null -ne $inputs.CodeCoverage_SingleHitBreakpoints ? $inputs.CodeCoverage_SingleHitBreakpoints -eq 'true' : $null
130121
}
131122
TestResult = @{
132-
Enabled = $inputs.TestResult_Enabled
123+
Enabled = $null -ne $inputs.TestResult_Enabled ? $inputs.TestResult_Enabled -eq 'true' : $null
133124
OutputFormat = $inputs.TestResult_OutputFormat
134125
OutputPath = $inputs.TestResult_OutputPath
135126
OutputEncoding = $inputs.TestResult_OutputEncoding
@@ -139,11 +130,11 @@ LogGroup 'Init - Load configuration - Action overrides' {
139130
ErrorAction = $inputs.Should_ErrorAction
140131
}
141132
Debug = @{
142-
ShowFullErrors = $inputs.Debug_ShowFullErrors
143-
WriteDebugMessages = $inputs.Debug_WriteDebugMessages
133+
ShowFullErrors = $null -ne $inputs.Debug_ShowFullErrors ? $inputs.Debug_ShowFullErrors -eq 'true' : $null
134+
WriteDebugMessages = $null -ne $inputs.Debug_WriteDebugMessages ? $inputs.Debug_WriteDebugMessages -eq 'true' : $null
144135
WriteDebugMessagesFrom = $inputs.Debug_WriteDebugMessagesFrom
145-
ShowNavigationMarkers = $inputs.Debug_ShowNavigationMarkers
146-
ReturnRawResultObject = $inputs.Debug_ReturnRawResultObject
136+
ShowNavigationMarkers = $null -ne $inputs.Debug_ShowNavigationMarkers ? $inputs.Debug_ShowNavigationMarkers -eq 'true' : $null
137+
ReturnRawResultObject = $null -ne $inputs.Debug_ReturnRawResultObject ? $inputs.Debug_ReturnRawResultObject -eq 'true' : $null
147138
}
148139
Output = @{
149140
CIFormat = $inputs.Output_CIFormat
@@ -153,10 +144,10 @@ LogGroup 'Init - Load configuration - Action overrides' {
153144
RenderMode = $inputs.Output_RenderMode
154145
}
155146
TestDrive = @{
156-
Enabled = $inputs.TestDrive_Enabled
147+
Enabled = $null -ne $inputs.TestDrive_Enabled ? $inputs.TestDrive_Enabled -eq 'true' : $null
157148
}
158149
TestRegistry = @{
159-
Enabled = $inputs.TestRegistry_Enabled
150+
Enabled = $null -ne $inputs.TestRegistry_Enabled ? $inputs.TestRegistry_Enabled -eq 'true' : $null
160151
}
161152
}
162153

@@ -165,13 +156,13 @@ LogGroup 'Init - Load configuration - Action overrides' {
165156
}
166157

167158
LogGroup 'Init - Load configuration' {
168-
$defaults = New-PesterConfigurationHashtable
159+
$defaults = New-PesterConfigurationHashtable -Default
169160
$configuration = Merge-PesterConfiguration -BaseConfiguration $defaults -AdditionalConfiguration $customConfig, $customInputs
170161

171162
if ([string]::IsNullOrEmpty($configuration.Run.Path)) {
172163
$configuration.Run.Path = $inputs.Path
173164
}
174-
Write-Output ($configuration | Format-Hashtable | Out-String)
165+
$configuration | Format-Hashtable | Out-String
175166
}
176167

177168
LogGroup 'Init - Export containers' {
@@ -187,16 +178,39 @@ LogGroup 'Init - Export containers' {
187178
Write-Output "Containers from configuration: [$($containers.Count)]"
188179
# Search for "*.Container.*" files in each Run.Path directory
189180
Write-Output 'Searching for containers in same location as config.'
181+
$path = New-Item -Path . -ItemType Directory -Name 'temp' -Force
190182
foreach ($testDir in $inputs.Path) {
183+
#If testDir is a file, get the directory
184+
$testItem = Get-Item -Path $testDir
185+
if ($testItem.PSIsContainer -eq $false) {
186+
$testDir = $testItem.DirectoryName
187+
}
188+
191189
$containerFiles = Get-ChildItem -Path $testDir -Filter *.Container.* -Recurse
192190
Write-Output "Containers found in [$testDir]: [$($containerFiles.Count)]"
191+
if ($containerFiles.Count -eq 0) {
192+
# Look for test files and make a container for each test file.
193+
$testFiles = Get-ChildItem -Path $testDir -Filter *.Tests.ps1 -Recurse
194+
Write-Output "Test files found in [$testDir]: [$($testFiles.Count)]"
195+
foreach ($testFile in $testFiles) {
196+
$container = @{
197+
Path = $testFile.FullName
198+
}
199+
LogGroup "Init - Export containers - Generated - $containerFileName" {
200+
$containerFileName = ($testFile | Split-Path -Leaf).Replace('.Tests.ps1', '.Container.ps1')
201+
Write-Output "Exporting container [$path/$containerFileName]"
202+
Export-Hashtable -Hashtable $container -Path "$path/$containerFileName"
203+
}
204+
Write-Output "Containers created from test files: [$($containers.Count)]"
205+
}
206+
}
193207
foreach ($containerFile in $containerFiles) {
194208
$container = Import-Hashtable $containerFile
195209
$containerFileName = $containerFile | Split-Path -Leaf
196210
LogGroup "Init - Export containers - $containerFileName" {
197211
Format-Hashtable -Hashtable $container
198-
Write-Verbose 'Converting hashtable to PesterContainer'
199-
Export-Hashtable -Hashtable $container -Path "$PSScriptRoot/$containerFileName"
212+
Write-Output "Exporting container [$path/$containerFileName]"
213+
Export-Hashtable -Hashtable $container -Path "$path/$containerFileName"
200214
}
201215
}
202216
}
@@ -210,5 +224,6 @@ LogGroup 'Init - Export configuration' {
210224
$configuration.Run.PassThru = $true
211225

212226
Format-Hashtable -Hashtable $configuration
213-
Export-Hashtable -Hashtable $configuration -Path "$PSScriptRoot/Invoke-Pester.Configuration.ps1"
227+
Write-Output "Exporting configuration [$path/Invoke-Pester.Configuration.ps1]"
228+
Export-Hashtable -Hashtable $configuration -Path "$path/Invoke-Pester.Configuration.ps1"
214229
}

scripts/main.ps1

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,31 @@ $pesterModule = Get-PSResource -Name Pester -Verbose:$false | Sort-Object Versio
2020
} | Format-List
2121
'::endgroup::'
2222

23+
'::group::Exec - Info about environment'
24+
$path = Join-Path -Path $pwd.Path -ChildPath 'temp'
25+
Test-Path -Path $path
26+
Get-ChildItem -Path $path -Recurse | Sort-Object FullName | Format-Table -AutoSize | Out-String
27+
2328
'::group::Exec - Import Configuration'
24-
$configuration = & "$PSScriptRoot/Invoke-Pester.Configuration.ps1"
25-
$configuration | Out-String
29+
$configPath = (Join-Path -Path $path -ChildPath 'Invoke-Pester.Configuration.ps1')
30+
Write-Output "Importing configuration from [$configPath]"
31+
if (-not (Test-Path -Path $configPath)) {
32+
Write-Error "Configuration file [$configPath] not found."
33+
exit 1
34+
}
35+
Get-Content -Path $configPath -Raw
36+
$configuration = . $configPath
2637
$configuration.Run.Container = @()
27-
$containerFiles = Get-ChildItem -Path $PSScriptRoot -Filter *.Container.* -Recurse | Sort-Object FullName
38+
$containerFiles = Get-ChildItem -Path $path -Filter *.Container.* -Recurse | Sort-Object FullName
2839
foreach ($containerFile in $containerFiles) {
2940
$container = & $($containerFile.FullName)
3041
Write-Verbose "Processing container [$container]"
3142
Write-Verbose 'Converting hashtable to PesterContainer'
3243
$configuration.Run.Container += New-PesterContainer @container
3344
}
34-
$configuration.Run.Container | ConvertTo-Json
45+
46+
$configuration | ConvertTo-Json
47+
3548
'::endgroup::'
3649

3750
'::group::Exec - Available modules'

0 commit comments

Comments
 (0)