forked from PowerShell/PSScriptAnalyzer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPSScriptAnalyzerTestHelper.psm1
More file actions
83 lines (73 loc) · 2.52 KB
/
Copy pathPSScriptAnalyzerTestHelper.psm1
File metadata and controls
83 lines (73 loc) · 2.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
Function Get-ExtentTextFromContent
{
Param(
[Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.CorrectionExtent] $violation,
[string] $rawContent
)
$scriptContent = New-Object -TypeName 'System.Collections.ArrayList'
$stringReader = New-Object -TypeName 'System.IO.StringReader' -ArgumentList @($rawContent)
while ($stringReader.Peek() -ne -1)
{
$scriptContent.Add($stringReader.ReadLine()) | Out-Null
}
$typeScriptPos = 'System.Management.Automation.Language.ScriptPosition'
$start = New-Object -TypeName $typeScriptPos -ArgumentList @($scriptPath, $violation.StartLineNumber, $violation.StartColumnNumber, $scriptContent[$violation.StartLineNumber - 1])
$end = New-Object -TypeName $typeScriptPos -ArgumentList @($scriptPath, $violation.EndLineNumber, $violation.EndColumnNumber, $scriptContent[$violation.EndLineNumber - 1])
$extent = New-Object -TypeName 'System.Management.Automation.Language.ScriptExtent' -ArgumentList @($start, $end)
$extent.Text
}
Function Test-CorrectionExtent
{
Param(
[string] $violationFilepath,
[Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.DiagnosticRecord] $diagnosticRecord,
[int] $correctionsCount,
[string] $violationText,
[string] $correctionText
)
Test-CorrectionExtentFromContent (Get-Content $violationFilepath -Raw) `
$diagnosticRecord `
$correctionsCount `
$violationText `
$correctionText
}
Function Test-CorrectionExtentFromContent {
param(
[string] $rawContent,
[Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.DiagnosticRecord] $diagnosticRecord,
[ValidateRange(0, 1)]
[int] $correctionsCount,
[string] $violationText,
[string] $correctionText
)
$corrections = $diagnosticRecord.SuggestedCorrections
$corrections.Count | Should -Be $correctionsCount
$corrections[0].Text | Should -Be $correctionText
Get-ExtentTextFromContent $corrections[0] $rawContent | `
Should -Be $violationText
}
Function Test-PSEditionCoreCLR
{
[bool]$IsCoreCLR
}
Function Test-PSVersionV3
{
$PSVersionTable.PSVersion.Major -eq 3
}
Function Test-PSVersionV4
{
$PSVersionTable.PSVersion.Major -eq 4
}
Function Get-Count
{
Begin {$count = 0}
Process {$count++}
End {$count}
}
Export-ModuleMember -Function Get-ExtentText
Export-ModuleMember -Function Test-CorrectionExtent
Export-ModuleMember -Function Test-CorrectionExtentFromContent
Export-ModuleMember -Function Test-PSEditionCoreCLR
Export-ModuleMember -Function Test-PSVersionV3
Export-ModuleMember -Function Test-PSVersionV4
Export-ModuleMember -Function Get-Count