Skip to content

Commit 93b3429

Browse files
committed
Add new line before the content of each script file (fixes #126)
1 parent d183d88 commit 93b3429

File tree

6 files changed

+79
-15
lines changed

6 files changed

+79
-15
lines changed

Source/Private/SetModuleContent.ps1

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,9 @@ function SetModuleContent {
4646
foreach($file in $SourceFile) {
4747
if($SourceName = Resolve-Path $file -Relative -ErrorAction SilentlyContinue) {
4848
Write-Verbose "Adding $SourceName"
49-
$SetContent.Process("#Region '$SourceName' 0")
49+
# Setting offset to -1 because of the new line we're adding
50+
# because this is needed for the code coverage.
51+
$SetContent.Process("#Region '$SourceName' -1`n")
5052
Get-Content $SourceName -OutVariable source | ForEach-Object { $SetContent.Process($_) }
5153
$SetContent.Process("#EndRegion '$SourceName' $($Source.Count+1)")
5254
} else {

Source/Public/ConvertFrom-SourceLineNumber.ps1

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,16 @@ function ConvertFrom-SourceLineNumber {
4040
try {
4141
if (!$filemap.ContainsKey($Module)) {
4242
# Note: the new pattern is #Region but the old one was # BEGIN
43-
$regions = Select-String '^(?:#Region|# BEGIN) (?<SourceFile>.*) (?<LineNumber>\d+)?$' -Path $Module
43+
$regions = Select-String '^(?:#Region|# BEGIN) (?<SourceFile>.*) (?<LineNumber>-?\d+)?$' -Path $Module
4444
$filemap[$Module] = @($regions.ForEach{
4545
[PSCustomObject]@{
4646
PSTypeName = "BuildSourceMapping"
4747
SourceFile = $_.Matches[0].Groups["SourceFile"].Value.Trim("'")
4848
StartLineNumber = $_.LineNumber
49+
# This offset is subtracted when calculating the line number
50+
# because of the new line we're adding prior to each script file
51+
# in the built module.
52+
Offset = $_.Matches[0].Groups["LineNumber"].Value
4953
}
5054
})
5155
}
@@ -56,13 +60,13 @@ function ConvertFrom-SourceLineNumber {
5660
[PSCustomObject]@{
5761
PSTypeName = "OutputLocation"
5862
Script = $Module
59-
Line = $Source.StartLineNumber + $SourceLineNumber
63+
Line = $Source.StartLineNumber + $SourceLineNumber - $Source.Offset
6064
}
6165
} elseif($Source -eq $Module) {
6266
[PSCustomObject]@{
6367
PSTypeName = "OutputLocation"
6468
Script = $Module
65-
Line = $SourceLineNumber
69+
Line = $SourceLineNumber - $Source.Offset
6670
}
6771
} else {
6872
Write-Warning "'$SourceFile' not found in $Module"

Source/Public/ConvertTo-SourceLineNumber.ps1

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ function ConvertTo-SourceLineNumber {
4949
try {
5050
if (!$filemap.ContainsKey($SourceFile)) {
5151
# Note: the new pattern is #Region but the old one was # BEGIN
52-
$regions = Select-String '^(?:#Region|# BEGIN) (?<SourceFile>.*) (?<LineNumber>\d+)?$' -Path $SourceFile
52+
$regions = Select-String '^(?:#Region|# BEGIN) (?<SourceFile>.*) (?<LineNumber>-?\d+)?$' -Path $SourceFile
5353
if ($regions.Count -eq 0) {
5454
Write-Warning "No SourceMap for $SourceFile"
5555
return
@@ -59,6 +59,10 @@ function ConvertTo-SourceLineNumber {
5959
PSTypeName = "BuildSourceMapping"
6060
SourceFile = $_.Matches[0].Groups["SourceFile"].Value.Trim("'")
6161
StartLineNumber = $_.LineNumber
62+
# This offset is added when calculating the line number
63+
# because of the new line we're adding prior to each script file
64+
# in the built module.
65+
Offset = $_.Matches[0].Groups["LineNumber"].Value
6266
}
6367
})
6468
}
@@ -74,12 +78,12 @@ function ConvertTo-SourceLineNumber {
7478
if($Passthru) {
7579
$InputObject |
7680
Add-Member -MemberType NoteProperty -Name SourceFile -Value $Source.SourceFile -PassThru -Force |
77-
Add-Member -MemberType NoteProperty -Name SourceLineNumber -Value ($SourceLineNumber - $Source.StartLineNumber) -PassThru -Force
81+
Add-Member -MemberType NoteProperty -Name SourceLineNumber -Value ($SourceLineNumber - $Source.StartLineNumber + $Source.Offset) -PassThru -Force
7882
} else {
7983
[PSCustomObject]@{
8084
PSTypeName = "SourceLocation"
8185
SourceFile = $Source.SourceFile
82-
SourceLineNumber = $SourceLineNumber - $Source.StartLineNumber
86+
SourceLineNumber = $SourceLineNumber - $Source.StartLineNumber + $Source.Offset
8387
}
8488
}
8589
} finally {

Tests/Private/SetModuleContent.Tests.ps1

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,4 +134,58 @@ Describe "SetModuleContent" {
134134
}
135135
}
136136

137-
}
137+
Context "Adds a newline before the content of each script file" {
138+
${global:mock get content index} = 1
139+
140+
Mock Get-Content -ModuleName ModuleBuilder {
141+
"Script Content"
142+
"File $((${global:mock get content index}++))"
143+
"From $Path"
144+
}
145+
146+
Mock Resolve-Path -ModuleName ModuleBuilder {
147+
if ($path -match "TestDrive:") {
148+
$path -replace "TestDrive:\\", ".\"
149+
} else {
150+
write-error "$path not found"
151+
}
152+
} -ParameterFilter { $Relative }
153+
154+
155+
InModuleScope ModuleBuilder {
156+
$Files = "using module Configuration",
157+
"TestDrive:\Private\First.ps1",
158+
"TestDrive:\Private\Second.ps1",
159+
"TestDrive:\Public\Third.ps1",
160+
"Export-ModuleMember Stuff"
161+
SetModuleContent -Source $Files -Output TestDrive:\Output.psm1
162+
}
163+
164+
It "Calls get-content on every source file" {
165+
Assert-MockCalled Get-Content -ModuleName ModuleBuilder -ParameterFilter { $Path -eq ".\Private\First.ps1" }
166+
Assert-MockCalled Get-Content -ModuleName ModuleBuilder -ParameterFilter { $Path -eq ".\Private\Second.ps1" }
167+
Assert-MockCalled Get-Content -ModuleName ModuleBuilder -ParameterFilter { $Path -eq ".\Public\Third.ps1" }
168+
}
169+
170+
It "Copies all three files into the Output" {
171+
$Content = Get-Content TestDrive:\Output.psm1 -Raw
172+
$Content | Should -Match "File 1"
173+
$Content | Should -Match "First.ps1"
174+
175+
$Content | Should -Match "File 2"
176+
$Content | Should -Match "Second.ps1"
177+
178+
$Content | Should -Match "File 3"
179+
$Content | Should -Match "Third.ps1"
180+
}
181+
182+
It "Include a new line before the content of each script file" {
183+
# Replacing CRLF to LF to support cross-platform testing.
184+
$Content = (Get-Content TestDrive:\Output.psm1 -Raw) -replace '\r?\n', "`n"
185+
186+
$Content | Should -Match "\#Region\ '\.\\Private\\First\.ps1'\ -1\n{1,}"
187+
$Content | Should -Match "\#Region\ '\.\\Private\\Second\.ps1'\ -1\n{1,}"
188+
$Content | Should -Match "\#Region\ '\.\\Public\\Third\.ps1'\ -1\n{1,}"
189+
}
190+
}
191+
}

Tests/Public/ConvertFrom-SourceLineNumber.Tests.ps1

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ Describe "ConvertFrom-SourceLineNumber" {
1111
${global:\} = [io.path]::DirectorySeparatorChar
1212

1313
$global:TestCases = @(
14-
@{ outputLine = 36; sourceFile = ".${\}Private${\}TestUnExportedAliases.ps1"; sourceLine = 13; Module = $Convert_LineNumber_ModulePath }
15-
@{ outputLine = 43; sourceFile = ".${\}public${\}Get-Source.ps1"; sourceLine = 5; Module = $Convert_LineNumber_ModulePath }
16-
@{ outputLine = 50; sourceFile = ".${\}public${\}Set-Source.ps1"; sourceLine = 3; Module = $Convert_LineNumber_ModulePath }
14+
@{ outputLine = 40; sourceFile = ".${\}Private${\}TestUnExportedAliases.ps1"; sourceLine = 13; Module = $Convert_LineNumber_ModulePath }
15+
@{ outputLine = 48; sourceFile = ".${\}Public${\}Get-Source.ps1"; sourceLine = 5; Module = $Convert_LineNumber_ModulePath }
16+
@{ outputLine = 56; sourceFile = ".${\}Public${\}Set-Source.ps1"; sourceLine = 3; Module = $Convert_LineNumber_ModulePath }
1717
)
1818
}
1919
AfterAll {

Tests/Public/ConvertTo-SourceLineNumber.Tests.ps1

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ Describe "ConvertTo-SourceLineNumber" {
1111
${global:\} = [io.path]::DirectorySeparatorChar
1212

1313
$global:TestCases = @(
14-
@{ outputLine = 36; sourceFile = ".${\}Private${\}TestUnExportedAliases.ps1"; sourceLine = 13 }
15-
@{ outputLine = 43; sourceFile = ".${\}Public${\}Get-Source.ps1"; sourceLine = 5 }
16-
@{ outputLine = 50; sourceFile = ".${\}Public${\}Set-Source.ps1"; sourceLine = 3 }
14+
@{ outputLine = 40; sourceFile = ".${\}Private${\}TestUnExportedAliases.ps1"; sourceLine = 13 }
15+
@{ outputLine = 48; sourceFile = ".${\}Public${\}Get-Source.ps1"; sourceLine = 5 }
16+
@{ outputLine = 56; sourceFile = ".${\}Public${\}Set-Source.ps1"; sourceLine = 3 }
1717
)
1818
}
1919
AfterAll {
@@ -70,7 +70,7 @@ Describe "ConvertTo-SourceLineNumber" {
7070
Function = 'Get-Source'
7171
# these are pipeline bound
7272
File = $Convert_LineNumber_ModulePath
73-
Line = 43 # 1 offset with the Using Statement introduced in MoveUsingStatements
73+
Line = 48 # 1 offset with the Using Statement introduced in MoveUsingStatements
7474
}
7575

7676
$SourceLocation = $PesterMiss | Convert-LineNumber -Passthru

0 commit comments

Comments
 (0)