-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rules.psm1
178 lines (142 loc) · 8.24 KB
/
Rules.psm1
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
using namespace System.Management.Automation.Language;
Set-StrictMode -version 1.0
<#
.DESCRIPTION
Custom rule text when you call Invoke-Something.
#>
function Generate-BeforeAll {
param ($Statements, $Start, $End, $Message)
Write-DebugDiag "Generate-BeforeAll -start" "$($Statements.Count) $Start $End $Message" $statement[0].Extent 0
try {
$StartingExtent = $statements[$Start].Extent
$EndingExtent = $statements[$End].Extent
$extent = [ScriptExtent]::new([ScriptPosition]::new("", $StartingExtent.StartScriptPosition.LineNumber, $StartingExtent.StartScriptPosition.ColumnNumber, "")
, [ScriptPosition]::new("", $EndingExtent.EndScriptPosition.LineNumber, $EndingExtent.EndScriptPosition.ColumnNumber, ""))
$Response = [Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.DiagnosticRecord]@{
Message = $Message
; Extent = $Extent
;
; RuleName = $PSCmdlet.MyInvocation.InvocationName
; Severity = "Warning"
}
$startLineNumber = $Response.Extent.StartLineNumber
[int]$endLineNumber = $Response.Extent.EndLineNumber
[int]$startColumnNumber = $Response.Extent.StartColumnNumber
[int]$endColumnNumber = $Response.Extent.EndColumnNumber
[string]$correction = "BeforeAll {`n " + ($statements[$start..$end].extent.text -join "`n ") + "`n}"
[string]$optionalDescription = 'Wrap statements in BeforeAll'
$correctionExtent = New-Object Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.CorrectionExtent $startLineNumber, $endLineNumber, $startColumnNumber, $endColumnNumber, $correction, "",$optionalDescription
$suggestedCorrections = New-Object System.Collections.ObjectModel.Collection['Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.CorrectionExtent']
$suggestedCorrections.add($correctionExtent) | out-null
$Response.SuggestedCorrections = $suggestedCorrections
Write-Output $Response
Write-DebugDiag "Generate-BeforeAll -end" "Respone = $($Response.Message)" $statement[0].Extent 0
}
catch {
Write-DebugDiag "Generate-BeforeAll -error" $_ $statement[0].Extent 0
}
}
function Write-DebugDiag {
param ($message1, $message2, $Extent, $level)
$indent = (" " * 2 * $level)
if ($null -ne $Env:DEBUGSCRIPTANAL) {
Write-output ([Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.DiagnosticRecord]::new($indent + $message1, $Extent, $message2, "Information", $null, $null, $null))
}
}
function PesterScriptBlocksForV5Compat {
[CmdletBinding()]
[OutputType([Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.DiagnosticRecord[]])]
param (
[Parameter(Mandatory)] [ValidateNotNullOrEmpty()] [System.Management.Automation.Language.ScriptBlockAst]$ScriptBlockAst,
[string] $ScriptFilePath
)
Begin {
$PesterBlockCommands = "Describe", "Context", "InPesterModuleScope"
$PesterRunCommands = "BeforeAll", "BeforeEach", "BeforeDiscovery", "AfterAll", "AfterEach", "InModuleScope", "It"
function get-FileLink([StatementAst]$Statement, $file) {
return "$($Statement.Extent.File)`:$($Statement.Extent.StartLineNumber)`:$($Statement.Extent.StartColumnNumber)"
}
function Find-BadBlocks {
param($script, $file, $level = 1)
$blocks = $script.FindAll( { param ($i) return ($i -is [NamedBlockAst]) }, $false)
foreach ( $o in $blocks ) {
##find all statements in a block, each should be a command Ast with command of a pester command
#We navigate the statements as using Find results in finding items within command calls
[int]$FirstBadStatementInBlock = -2
[int]$lastBadStatementInBlock = -2
$StatementIndex = 0
$badBlock = $false
foreach ($statement in $o.statements) {
Write-DebugDiag "Processing " "$StatementIndex/$($o.Statements.Count) $FirstBadStatementInBlock $LastBadStatementInBlock " $statement.Extent $level
$bad = ""
if ($statement -isNot [PipelineAst] ) {
#All Pester statements are Pipelines
$bad = "Code found outside of Pester Block $statement"
}
elseif ( $statement.PipelineElements[0] -isnot [CommandAst]) {
#The item in the pipeline needs to be a Command, expressions and other things aren't valid pester
$bad = "Code found outside of Pester Block $statement"
}
else {
$command = $statement.PipelineElements[0].CommandElements
if ($null -eq $Command) {
Write-Verbose "Should definitely not get here"
}
if ($PesterBlockCommands -contains $Command[0].Value ) {
Write-Verbose "Found Command $($Command[0])"
#We are using this rather than find otherwise scripts for ForEach or other parameter values are incorrectly identified
$scripts = $command | Where-Object { $_ -is [ScriptBlockExpressionAst] }
if ($scripts.Length -gt 1 ) {
Throw "should only have 1 script in a describe block"
}
else {
#Check child blocks
Find-BadBlocks $scripts[0].ScriptBlock -file $File -level ($level + 1)
}
}
elseif ($PesterRunCommands -notContains $Command[0].Value ) {
$bad = "Code found outside of Pester Block $statement"
}
}
#Check if statement is continuous
if ($bad -ne "") {
Write-DebugDiag "Found bad block" "" $statement.Extent $level
if (-not $badBlock){
$FirstBadStatementInBlock = $StatementIndex
$badBlock = $true
}
$LastBadStatementInBlock = $StatementIndex
}
else {
if ($badBlock) {
Write-DebugDiag "Save Bad Block on Good Statement" "$StatementIndex $FirstBadStatementInBlock $LastBadStatementInBlock" $statement.Extent $level
Generate-BeforeAll -statements $o.statements -Start $FirstBadStatementInBlock -end $lastBadStatementInBlock -message "--"
$badBlock = $false
}
}
if ( $statementIndex -eq ($o.statements.Count - 1) -and $badBlock) {
Generate-BeforeAll -statements $o.statements -Start $FirstBadStatementInBlock -end $lastBadStatementInBlock -message $bad
}
$StatementIndex ++
}
}
}
}
Process {
Try {
#it seems VSCode runs this on fragments, if we dont do this it slows stuff down and we only need to check the global content
#We possibly could work at a fragment level to see if its valid
if ($null -eq $ScriptBlockAst.Parent.Parent) {
#Check for Pester
$Pester = $ScriptBlockAst.Find( { param ($command) $command -is [CommandAst] -and ([commandAst]$command).CommandElements[0].Value -eq "Describe" }, $true)
if ($null -ne $Pester) {
Find-BadBlocks $ScriptBlockAst "" 2
}
}
}
catch {
$PSCmdlet.ThrowTerminatingError( $_ )
}
}
}
Export-ModuleMember -Function PesterScriptBlocksForV5Compat