Skip to content

Commit c1faae5

Browse files
Add path normalization function for PSModulePath and update file processing
1 parent e2b31bc commit c1faae5

File tree

2 files changed

+46
-3
lines changed

2 files changed

+46
-3
lines changed

scripts/Helpers.psm1

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,41 @@ function Normalize-IndentationExceptFirst {
7777
# Reconstruct the final code: first line + adjusted subsequent lines
7878
return ($firstLine + $newLine + ($subsequentLines -join $newLine))
7979
}
80+
81+
82+
# Improve path normalization by using PSModulePath
83+
function ConvertTo-NormalizedModulePath {
84+
[CmdletBinding()]
85+
param(
86+
[Parameter(Mandatory, ValueFromPipeline)]
87+
[string] $Path
88+
)
89+
90+
process {
91+
# Split PSModulePath into individual paths and normalize them
92+
$modulePaths = $env:PSModulePath -split [IO.Path]::PathSeparator |
93+
Where-Object { -not [string]::IsNullOrWhiteSpace($_) } |
94+
ForEach-Object { $_.TrimEnd('\', '/') }
95+
96+
# Try to match the start of the path with any module path
97+
foreach ($modulePath in $modulePaths) {
98+
if ($Path -match [regex]::Escape($modulePath)) {
99+
$normalizedPath = $Path -replace [regex]::Escape($modulePath), ''
100+
# Remove any leading path separators
101+
$normalizedPath = $normalizedPath.TrimStart('\', '/')
102+
103+
# If path was successfully normalized, return it
104+
if ($normalizedPath -ne $Path) {
105+
return "Modules/$normalizedPath"
106+
}
107+
} elseif ($Path -match '(runner|runneradmin)[/\\].*[/\\]Modules[/\\]') {
108+
# Handle common GitHub runner paths that might not be in PSModulePath
109+
$normalizedPath = $Path -replace '.*?(Modules[/\\])', 'Modules/'
110+
return $normalizedPath
111+
}
112+
}
113+
114+
# If no module path matched, use the existing relative path function
115+
return ConvertTo-RelativePath $Path
116+
}
117+
}

scripts/main.ps1

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ LogGroup 'List files' {
2323
$files.Name | Out-String
2424
}
2525

26+
LogGroup 'Module paths' {
27+
Write-Output "PSModulePath entries:"
28+
$env:PSModulePath -split [IO.Path]::PathSeparator | ForEach-Object { " $_" }
29+
}
30+
2631
# Accumulators for coverage items across all files
2732
$allMissed = @()
2833
$allExecuted = @()
@@ -39,21 +44,21 @@ foreach ($file in $files) {
3944
# 1. Normalize every "File" property in CommandsMissed
4045
foreach ($missed in $jsonContent.CommandsMissed) {
4146
if ($missed.File) {
42-
$missed.File = ConvertTo-RelativePath $missed.File
47+
$missed.File = ConvertTo-NormalizedModulePath $missed.File
4348
}
4449
}
4550

4651
# 2. Normalize every "File" property in CommandsExecuted
4752
foreach ($exec in $jsonContent.CommandsExecuted) {
4853
if ($exec.File) {
49-
$exec.File = ConvertTo-RelativePath $exec.File
54+
$exec.File = ConvertTo-NormalizedModulePath $exec.File
5055
}
5156
}
5257

5358
# 3. Normalize the file paths in FilesAnalyzed
5459
$normalizedFiles = @()
5560
foreach ($fa in $jsonContent.FilesAnalyzed) {
56-
$normalizedFiles += ConvertTo-RelativePath $fa
61+
$normalizedFiles += ConvertTo-NormalizedModulePath $fa
5762
}
5863
$jsonContent.FilesAnalyzed = $normalizedFiles
5964

0 commit comments

Comments
 (0)