-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rules.psm1
187 lines (153 loc) · 8.64 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
179
180
181
182
183
184
185
186
187
using namespace System.Management.Automation.Language;
Set-StrictMode -version 1.0
<#
.DESCRIPTION
Custom rule text when you call Invoke-Something.
#>
function Write-BeforeAll {
param ($Statements, $Start, $End, $Message)
Write-DebugDiag -message1 "Write-BeforeAll -start" -Message2 "$($Statements.Count) $Start $End $Message" -Extent $statement[0].Extent -Level 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 = "sdfsd"#$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 -Message1 "Write-BeforeAll -end" -Messge2 "Respone = $($Response.Message)" -Extent $statement[0].Extent -Level 0
}
catch {
Write-DebugDiag -Message1 "Write-BeforeAll -error" -Extent $_ $statement[0].Extent -Level 0
}
}
function Write-DebugDiag {
param ($message1, $message2, $Extent, $level)
$indent = (" " * 2 * $level)
try {
if ($null -ne $Env:DEBUGSCRIPTANAL) {
Write-output ([Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.DiagnosticRecord]::new($indent + $message1, $Extent, $message2, "Information", $null, $null, $null))
}
}
Catch {
Write-Host $_
}
}
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
$bad = ""
foreach ($statement in $o.statements) {
$BadStatement = $false
Write-DebugDiag -message1 "Processing " -Message2 "$StatementIndex/$($o.Statements.Count) $FirstBadStatementInBlock $LastBadStatementInBlock " -Extent $statement.Extent -Level $level
if ($statement -isNot [PipelineAst] ) {
#All Pester statements are Pipelines
$bad = "Code found outside of Pester Block Not PipelineAST $statement"
$BadStatement=$true
}
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 Not CommandAst $statement"
$BadStatement=$true
}
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"
$BadStatement=$true
}
}
#Check if statement is continuous
if ($badstatement) {
Write-DebugDiag -message1 "Found bad block" -Message2 $bad -Extent $statement.Extent -Level $level
if (-not $badBlock) {
$FirstBadStatementInBlock = $StatementIndex
$badBlock = $true
}
$LastBadStatementInBlock = $StatementIndex
}
else {
if ($badBlock) {
Write-DebugDiag -message1 "Save Bad Block on Good Statement" -Message2 "$StatementIndex $FirstBadStatementInBlock $LastBadStatementInBlock" -Extent $statement.Extent -Level $level
Write-BeforeAll -statements $o.statements -Start $FirstBadStatementInBlock -end $lastBadStatementInBlock -message $bad#"--"
$badBlock = $false
$bad = ""
}
}
if ( $statementIndex -eq ($o.statements.Count - 1) -and $badBlock) {
Write-BeforeAll -statements $o.statements -Start $FirstBadStatementInBlock -end $lastBadStatementInBlock -message $bad
$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 -Script $ScriptBlockAst -File "" -level 2
}
}
}
catch {
$PSCmdlet.ThrowTerminatingError( $_ )
}
}
}
Export-ModuleMember -Function PesterScriptBlocksForV5Compat