Skip to content

Commit 26bbb14

Browse files
committed
Make the tests not depend on the source
1 parent 744bccd commit 26bbb14

File tree

4 files changed

+64
-54
lines changed

4 files changed

+64
-54
lines changed

Source/Public/ConvertFrom-SourceLineNumber.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
function ConvertFrom-SourceLineNumber {
22
<#
33
.SYNOPSIS
4-
Convert the line number in a built module to a file and line number in source
4+
Convert a source file path and line number to the line number in the built output
55
.EXAMPLE
66
ConvertFrom-SourceLineNumber -Module ~\2.0.0\ModuleBuilder.psm1 -SourceFile ~\Source\Public\Build-Module.ps1 -Line 27
77
#>
Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
#requires -Module ModuleBuilder
22
Describe "ResolveBuildManifest" {
3-
4-
[string]${Global:Test Root Path} = Resolve-Path $PSScriptRoot\..\..\Source
3+
# use the integration test code
4+
BeforeAll {
5+
[string]${Global:Test Root Path} = Convert-Path "$PSScriptRoot/../Integration/Source1"
6+
}
57

68
It "Should return the build.psd1 path when passed the build.psd1 path" {
79
$Expected = InModuleScope ModuleBuilder { ResolveBuildManifest (Join-Path ${Global:Test Root Path} "build.psd1") }
810
$Expected | Should -Be (Join-Path ${Global:Test Root Path} "build.psd1")
911
}
1012

1113
It "Should return the build.psd1 path when passed the module manifest path" {
12-
$Expected = InModuleScope ModuleBuilder { ResolveBuildManifest (Join-Path ${Global:Test Root Path} 'ModuleBuilder.psd1') }
14+
$Expected = InModuleScope ModuleBuilder { ResolveBuildManifest (Join-Path ${Global:Test Root Path} 'Source1.psd1') }
1315
$Expected | Should -Be (Join-Path ${Global:Test Root Path} "build.psd1")
1416
}
1517

@@ -19,10 +21,10 @@ Describe "ResolveBuildManifest" {
1921
}
2022

2123
It "Should return the build.psd1 path when passed the relative path of the source folder" {
22-
Push-Location $PSScriptRoot -StackName TestRelativePath
23-
$Expected = InModuleScope ModuleBuilder { ResolveBuildManifest "..\..\Source" }
24-
Pop-Location -StackName TestRelativePath
24+
Push-Location $PSScriptRoot
25+
$Expected = InModuleScope ModuleBuilder { ResolveBuildManifest "../Integration/Source1" }
2526
$Expected | Should -Be (Join-Path ${Global:Test Root Path} "build.psd1")
27+
Pop-Location
2628
}
2729

2830
It "Returns nothing when passed a wrong absolute module manifest" {
@@ -33,7 +35,7 @@ Describe "ResolveBuildManifest" {
3335

3436
It "Returns nothing when passed the wrong folder path" {
3537
InModuleScope ModuleBuilder {
36-
ResolveBuildManifest (Join-Path ${Global:Test Root Path} "..") | Should -BeNullOrEmpty
38+
ResolveBuildManifest (Join-Path ${Global:Test Root Path} "../..") | Should -BeNullOrEmpty
3739
}
3840
}
3941

@@ -42,5 +44,4 @@ Describe "ResolveBuildManifest" {
4244
ResolveBuildManifest (Join-Path . ..) | Should -BeNullOrEmpty
4345
}
4446
}
45-
4647
}
Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,32 @@
11
Describe "Convert-CodeCoverage" {
2-
3-
$ModulePath = Join-Path (Get-Module ModuleBuilder).ModuleBase ModuleBuilder.psm1
4-
$ModuleContent = Get-Content $ModulePath
5-
6-
$ModuleSource = Resolve-Path "$PSScriptRoot\..\..\Source"
7-
8-
$lineNumber = Get-Random -min 3 -max $ModuleContent.Count
9-
while($ModuleContent[$lineNumber] -match "^#(END)?REGION") {
10-
$lineNumber += 5
2+
# use the integration test code
3+
BeforeAll {
4+
Build-Module $PSScriptRoot\..\Integration\Source1\build.psd1 -Passthru
5+
Push-Location $PSScriptRoot -StackName Convert-CodeCoverage
6+
}
7+
AfterAll {
8+
Pop-Location -StackName Convert-CodeCoverage
119
}
1210

1311
It 'Should extract code coverage from Pester objects and add Source conversions' {
12+
$ModulePath = Convert-Path ".\..\Integration\Result1\Source1\1.0.0\Source1.psm1"
13+
$ModuleSource = Convert-Path ".\..\Integration\Source1"
1414

1515
# Note: Pester does not currently apply custom types...
1616
$PesterResults = [PSCustomObject]@{
1717
CodeCoverage = [PSCustomObject]@{
1818
MissedCommands = [PSCustomObject]@{
1919
# these are pipeline bound
2020
File = $ModulePath
21-
Line = 26 # 1 offset with the Using Statement introduced in MoveUsingStatements
21+
Line = 25 # [Alias("gs","gsou")]
2222
}
2323
}
2424
}
2525

2626
$SourceLocation = $PesterResults | Convert-CodeCoverage -SourceRoot $ModuleSource
2727

2828
# Needs to match the actual module source (on line 25)
29-
$SourceLocation.SourceFile | Should -Be ".\Private\ConvertToAst.ps1"
30-
$SourceLocation.Line | Should -Be 25
29+
$SourceLocation.SourceFile | Should -Be ".\Public\Get-Source.ps1"
30+
$SourceLocation.Line | Should -Be 5
3131
}
3232
}
Lines changed: 42 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,80 @@
11
#requires -Module ModuleBuilder
22
Describe "Convert-LineNumber" {
3+
# use the integration test code
4+
BeforeAll {
5+
Build-Module $PSScriptRoot\..\Integration\Source1\build.psd1 -Passthru
6+
Push-Location $PSScriptRoot -StackName Convert-CodeCoverage
37

4-
$ModulePath = Join-Path (Get-Module ModuleBuilder).ModuleBase ModuleBuilder.psm1
5-
$ModuleContent = Get-Content $ModulePath
6-
$ModuleSource = Resolve-Path (Join-Path $PSScriptRoot "\..\..\Source")
8+
$global:Convert_LineNumber_ModulePath = Convert-Path ".\..\Integration\Result1\Source1\1.0.0\Source1.psm1"
9+
$global:Convert_LineNumber_ModuleSource = Convert-Path ".\..\Integration\Source1"
10+
$global:Convert_LineNumber_ModuleContent = Get-Content $global:Convert_LineNumber_ModulePath
11+
}
12+
AfterAll {
13+
Pop-Location -StackName Convert-CodeCoverage
14+
}
715

8-
for ($i=0; $i -lt 5; $i++) {
16+
$TestCases = @(
17+
@{ outputLine = 6; sourceFile = ".\Private\GetFinale.ps1"; sourceLine = 4 }
18+
@{ outputLine = 18; sourceFile = ".\Private\GetPreview.ps1"; sourceLine = 7 }
19+
@{ outputLine = 25; sourceFile = ".\Public\Get-Source.ps1"; sourceLine = 5 }
20+
)
921

10-
# I don't know why I keep trying to do this using random numbers
11-
$lineNumber = Get-Random -min 3 -max $ModuleContent.Count
12-
# but I have to keep avoiding the lines that don't make sense
13-
while($ModuleContent[$lineNumber] -match "^\s*$|^#(END)?REGION|^\s*function\s") {
14-
$lineNumber += 5
15-
}
22+
It "Should map line <outputLine> in the Module to line <sourceLine> in the source of <sourceFile>" -TestCases $TestCases {
23+
param($outputLine, $sourceFile, $sourceLine)
1624

17-
It "Should map line number $lineNumber in the Module to the matching line the Source" {
18-
$SourceLocation = Convert-LineNumber $ModulePath $lineNumber
25+
$SourceLocation = Convert-LineNumber $Convert_LineNumber_ModulePath $outputLine
26+
$SourceLocation.SourceFile | Should -Be $SourceFile
27+
$SourceLocation.SourceLineNumber | Should -Be $SourceLine
1928

20-
$line = (Get-Content (Join-Path $ModuleSource $SourceLocation.SourceFile))[$SourceLocation.SourceLineNumber]
21-
try {
22-
$ModuleContent[$lineNumber] | Should -Be $line
23-
} catch {
24-
throw "Failed to match module line $lineNumber to $($SourceLocation.SourceFile) line $($SourceLocation.SourceLineNumber).`nExpected $Line`nBut got $($ModuleContent[$lineNumber])"
25-
}
29+
$line = (Get-Content (Join-Path $Convert_LineNumber_ModuleSource $SourceLocation.SourceFile))[$SourceLocation.SourceLineNumber - 1]
30+
try {
31+
$Convert_LineNumber_ModuleContent[$outputLine -1] | Should -Be $line
32+
} catch {
33+
throw "Failed to match module line $outputLine to $($SourceLocation.SourceFile) line $($SourceLocation.SourceLineNumber).`nExpected $Line`nBut got $($Convert_LineNumber_ModuleContent[$outputLine -1])"
2634
}
2735
}
2836

2937
It "Should throw if the SourceFile doesn't exist" {
30-
{ Convert-LineNumber -SourceFile TestDrive:\NoSuchFile -SourceLineNumber 10 } | Should Throw "'TestDrive:\NoSuchFile' does not exist"
38+
{ Convert-LineNumber -SourceFile TestDrive:\NoSuchFile -SourceLineNumber 10 } |
39+
Should -Throw "'TestDrive:\NoSuchFile' does not exist"
3140
}
3241

3342
It 'Should work with an error PositionMessage' {
34-
$line = Select-String -Path $ModulePath 'function ParseLineNumber {' | % LineNumber
43+
$line = Select-String -Path $Convert_LineNumber_ModulePath 'function Set-Source {' | ForEach-Object LineNumber
3544

36-
$SourceLocation = "At ${ModulePath}:$line char:17" | Convert-LineNumber
45+
$SourceLocation = "At ${Convert_LineNumber_ModulePath}:$line char:17" | Convert-LineNumber
3746
# This test is assuming you built the code on Windows. Should Convert-LineNumber convert the path?
38-
$SourceLocation.SourceFile | Should -Be ".\Private\ParseLineNumber.ps1"
47+
$SourceLocation.SourceFile | Should -Be ".\Public\Set-Source.ps1"
3948
$SourceLocation.SourceLineNumber | Should -Be 1
4049
}
4150

4251
It 'Should work with ScriptStackTrace messages' {
4352

44-
$SourceFile = Join-Path $ModuleSource Private\CopyReadMe.ps1 | Convert-Path
53+
$SourceFile = Join-Path $Convert_LineNumber_ModuleSource Public\Set-Source.ps1 | Convert-Path
4554

46-
$outputLine = Select-String -Path $ModulePath 'Write-Verbose "Copy ReadMe to: \$LanguagePath"' | % LineNumber
47-
$sourceLine = Select-String -Path $SourceFile 'Write-Verbose "Copy ReadMe to: \$LanguagePath"' | % LineNumber
55+
$outputLine = Select-String -Path $Convert_LineNumber_ModulePath "sto͞o′pĭd" | % LineNumber
56+
$sourceLine = Select-String -Path $SourceFile "sto͞o′pĭd" | % LineNumber
4857

49-
$SourceLocation = "At CopyReadMe, ${ModulePath}: line $outputLine" | Convert-LineNumber
58+
$SourceLocation = "At Set-Source, ${Convert_LineNumber_ModulePath}: line $outputLine" | Convert-LineNumber
5059

5160
# This test is assuming you built the code on Windows. Should Convert-LineNumber convert the path?
52-
$SourceLocation.SourceFile | Should -Be ".\Private\CopyReadMe.ps1"
61+
$SourceLocation.SourceFile | Should -Be ".\Public\Set-Source.ps1"
5362
$SourceLocation.SourceLineNumber | Should -Be $sourceLine
5463
}
5564

5665
It 'Should pass through InputObject for updating objects like CodeCoverage or ErrorRecord' {
5766
$PesterMiss = [PSCustomObject]@{
5867
# Note these don't really matter, but they're passed through
59-
Function = 'TotalNonsense'
68+
Function = 'Get-Source'
6069
# these are pipeline bound
61-
File = $ModulePath
62-
Line = 26 # 1 offset with the Using Statement introduced in MoveUsingStatements
70+
File = $Convert_LineNumber_ModulePath
71+
Line = 25 # 1 offset with the Using Statement introduced in MoveUsingStatements
6372
}
6473

6574
$SourceLocation = $PesterMiss | Convert-LineNumber -Passthru
6675
# This test is assuming you built the code on Windows. Should Convert-LineNumber convert the path?
67-
$SourceLocation.SourceFile | Should -Be ".\Private\ConvertToAst.ps1"
68-
$SourceLocation.SourceLineNumber | Should -Be 25
69-
$SourceLocation.Function | Should -Be 'TotalNonsense'
76+
$SourceLocation.SourceFile | Should -Be ".\Public\Get-Source.ps1"
77+
$SourceLocation.SourceLineNumber | Should -Be 5
78+
$SourceLocation.Function | Should -Be 'Get-Source'
7079
}
7180
}

0 commit comments

Comments
 (0)