Skip to content

Commit 42031f2

Browse files
author
Kapil Borle
committed
Add method to extract extent from string
1 parent 38e2c69 commit 42031f2

File tree

1 file changed

+30
-6
lines changed

1 file changed

+30
-6
lines changed

Tests/PSScriptAnalyzerTestHelper.psm1

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
1-
Function Get-ExtentText
1+
Function Get-ExtentTextFromContent
22
{
3-
Param(
3+
Param(
44
[Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.CorrectionExtent] $violation,
5-
[string] $scriptPath
5+
[string] $rawContent
66
)
7-
$scriptContent = Get-Content -Path $scriptPath
7+
$scriptContent = New-Object -TypeName 'System.Collections.ArrayList'
8+
$stringReader = New-Object -TypeName 'System.IO.StringReader' -ArgumentList @($rawContent)
9+
while ($stringReader.Peek() -ne -1)
10+
{
11+
$scriptContent.Add($stringReader.ReadLine()) | Out-Null
12+
}
13+
814
$typeScriptPos = 'System.Management.Automation.Language.ScriptPosition'
915
$start = New-Object -TypeName $typeScriptPos -ArgumentList @($scriptPath, $violation.StartLineNumber, $violation.StartColumnNumber, $scriptContent[$violation.StartLineNumber - 1])
1016
$end = New-Object -TypeName $typeScriptPos -ArgumentList @($scriptPath, $violation.EndLineNumber, $violation.EndColumnNumber, $scriptContent[$violation.EndLineNumber - 1])
1117
$extent = New-Object -TypeName 'System.Management.Automation.Language.ScriptExtent' -ArgumentList @($start, $end)
12-
return($extent.Text)
18+
$extent.Text
1319
}
1420

1521
Function Test-CorrectionExtent
@@ -21,10 +27,27 @@ Function Test-CorrectionExtent
2127
[string] $violationText,
2228
[string] $correctionText
2329
)
30+
31+
Test-CorrectionExtentFromContent (Get-Content $violationFilepath -Raw) `
32+
$diagnosticRecord `
33+
$correctionsCount `
34+
$violationText `
35+
$correctionText
36+
}
37+
38+
Function Test-CorrectionExtentFromContent {
39+
param(
40+
[string] $rawContent,
41+
[Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.DiagnosticRecord] $diagnosticRecord,
42+
[int] $correctionsCount,
43+
[string] $violationText,
44+
[string] $correctionText
45+
)
46+
2447
$corrections = $diagnosticRecord.SuggestedCorrections
2548
$corrections.Count | Should Be $correctionsCount
2649
$corrections[0].Text | Should Be $correctionText
27-
Get-ExtentText $corrections[0] $violationFilepath | `
50+
Get-ExtentTextFromContent $corrections[0] $rawContent | `
2851
Should Be $violationText
2952
}
3053

@@ -57,6 +80,7 @@ Function Get-Count
5780

5881
Export-ModuleMember -Function Get-ExtentText
5982
Export-ModuleMember -Function Test-CorrectionExtent
83+
Export-ModuleMember -Function Test-CorrectionExtentFromContent
6084
Export-ModuleMember -Function Test-PSEditionCoreCLR
6185
Export-ModuleMember -Function Test-PSEditionCoreCLRLinux
6286
Export-ModuleMember -Function Test-PSVersionV3

0 commit comments

Comments
 (0)