Skip to content

Commit 6908310

Browse files
Refactor path normalization logic and remove unused ConvertTo-RelativePath function
1 parent 20bbc3e commit 6908310

File tree

2 files changed

+4
-68
lines changed

2 files changed

+4
-68
lines changed

scripts/Helpers.psm1

Lines changed: 1 addition & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,4 @@
1-
function ConvertTo-RelativePath {
2-
param(
3-
[Parameter(Mandatory)]
4-
[string]$Path
5-
)
6-
7-
# 1) Remove everything up to and including "outputs/module/" (case-insensitive),
8-
# allowing either slash or backslash.
9-
$relative = $Path -replace '(?i)^.*outputs[\\/]+module[\\/]+', ''
10-
11-
# 2) Convert all backslashes to forward slashes for consistency
12-
$relative = $relative -replace '\\', '/'
13-
14-
# 3) Remove the *next* folder (the module name) in the path.
15-
# For example, "PSModuleTest/scripts/loader.ps1" => "scripts/loader.ps1"
16-
$segments = $relative -split '/'
17-
if ($segments.Count -gt 1) {
18-
# Skip the first segment (the module name) and rejoin the rest
19-
$relative = ($segments[1..($segments.Count - 1)]) -join '/'
20-
} else {
21-
# If there was only one segment, just keep it (file in the root)
22-
$relative = $segments[0]
23-
}
24-
25-
return $relative
26-
}
27-
28-
function Normalize-IndentationExceptFirst {
1+
function Normalize-IndentationExceptFirst {
292
[CmdletBinding()]
303
param(
314
[Parameter(Mandatory)]
@@ -77,35 +50,3 @@ function Normalize-IndentationExceptFirst {
7750
# Reconstruct the final code: first line + adjusted subsequent lines
7851
return ($firstLine + $newLine + ($subsequentLines -join $newLine))
7952
}
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-
# Normalize backslashes to forward slashes for consistency
92-
$Path = $Path.Replace('\', '/')
93-
94-
# Get only the first module path and normalize it
95-
$modulePath = ($env:PSModulePath -split [IO.Path]::PathSeparator | Where-Object { -not [string]::IsNullOrWhiteSpace($_) } |
96-
Select-Object -First 1).TrimEnd('\', '/').Replace('\', '/')
97-
98-
if ($modulePath -and $Path -match [regex]::Escape($modulePath)) {
99-
# Remove the module path prefix
100-
$normalizedPath = $Path -replace [regex]::Escape($modulePath), ''
101-
# Remove any leading path separators
102-
$normalizedPath = $normalizedPath.TrimStart('/')
103-
104-
# Return with the standard "Modules/" prefix
105-
return "Modules/$normalizedPath"
106-
}
107-
108-
# If no match found, just normalize slashes and return the original path
109-
return $Path
110-
}
111-
}

scripts/main.ps1

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,6 @@ 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-
3126
# Accumulators for coverage items across all files
3227
$allMissed = @()
3328
$allExecuted = @()
@@ -44,21 +39,21 @@ foreach ($file in $files) {
4439
# 1. Normalize every "File" property in CommandsMissed
4540
foreach ($missed in $jsonContent.CommandsMissed) {
4641
if ($missed.File) {
47-
$missed.File = ConvertTo-NormalizedModulePath $missed.File
42+
$missed.File = ($missed.File -Split '999.0.0')[-1].Replace('\', '/').TrimStart('/').TrimEnd('/')
4843
}
4944
}
5045

5146
# 2. Normalize every "File" property in CommandsExecuted
5247
foreach ($exec in $jsonContent.CommandsExecuted) {
5348
if ($exec.File) {
54-
$exec.File = ConvertTo-NormalizedModulePath $exec.File
49+
$exec.File = ($exec.File -Split '999.0.0')[-1].Replace('\', '/').TrimStart('/').TrimEnd('/')
5550
}
5651
}
5752

5853
# 3. Normalize the file paths in FilesAnalyzed
5954
$normalizedFiles = @()
6055
foreach ($fa in $jsonContent.FilesAnalyzed) {
61-
$normalizedFiles += ConvertTo-NormalizedModulePath $fa
56+
$normalizedFiles += ($fa.File -Split '999.0.0')[-1].Replace('\', '/').TrimStart('/').TrimEnd('/')
6257
}
6358
$jsonContent.FilesAnalyzed = $normalizedFiles
6459

0 commit comments

Comments
 (0)