|
1 | 1 | #requires -Module ModuleBuilder |
2 | 2 | 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 |
3 | 7 |
|
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 | + } |
7 | 15 |
|
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 | + ) |
9 | 21 |
|
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) |
16 | 24 |
|
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 |
19 | 28 |
|
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])" |
26 | 34 | } |
27 | 35 | } |
28 | 36 |
|
29 | 37 | 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" |
31 | 40 | } |
32 | 41 |
|
33 | 42 | 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 |
35 | 44 |
|
36 | | - $SourceLocation = "At ${ModulePath}:$line char:17" | Convert-LineNumber |
| 45 | + $SourceLocation = "At ${Convert_LineNumber_ModulePath}:$line char:17" | Convert-LineNumber |
37 | 46 | # 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" |
39 | 48 | $SourceLocation.SourceLineNumber | Should -Be 1 |
40 | 49 | } |
41 | 50 |
|
42 | 51 | It 'Should work with ScriptStackTrace messages' { |
43 | 52 |
|
44 | | - $SourceFile = Join-Path $ModuleSource Private\CopyReadMe.ps1 | Convert-Path |
| 53 | + $SourceFile = Join-Path $Convert_LineNumber_ModuleSource Public\Set-Source.ps1 | Convert-Path |
45 | 54 |
|
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 |
48 | 57 |
|
49 | | - $SourceLocation = "At CopyReadMe, ${ModulePath}: line $outputLine" | Convert-LineNumber |
| 58 | + $SourceLocation = "At Set-Source, ${Convert_LineNumber_ModulePath}: line $outputLine" | Convert-LineNumber |
50 | 59 |
|
51 | 60 | # 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" |
53 | 62 | $SourceLocation.SourceLineNumber | Should -Be $sourceLine |
54 | 63 | } |
55 | 64 |
|
56 | 65 | It 'Should pass through InputObject for updating objects like CodeCoverage or ErrorRecord' { |
57 | 66 | $PesterMiss = [PSCustomObject]@{ |
58 | 67 | # Note these don't really matter, but they're passed through |
59 | | - Function = 'TotalNonsense' |
| 68 | + Function = 'Get-Source' |
60 | 69 | # 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 |
63 | 72 | } |
64 | 73 |
|
65 | 74 | $SourceLocation = $PesterMiss | Convert-LineNumber -Passthru |
66 | 75 | # 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' |
70 | 79 | } |
71 | 80 | } |
0 commit comments