forked from PowerShell/PSScriptAnalyzer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CustomizedRule.tests.ps1
268 lines (226 loc) · 13.3 KB
/
CustomizedRule.tests.ps1
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
BeforeAll {
if (-not $IsCoreCLR)
{
# Force Get-Help not to prompt for interactive input to download help using Update-Help
# By adding this registry key we turn off Get-Help interactivity logic during ScriptRule parsing
$null,"Wow6432Node" | ForEach-Object {
try
{
Set-ItemProperty -Name "DisablePromptToUpdateHelp" -Path "HKLM:\SOFTWARE\$($_)\Microsoft\PowerShell" -Value 1 -Force -ErrorAction SilentlyContinue
}
catch
{
# Ignore for cases when tests are running in non-elevated more or registry key does not exist or not accessible
}
}
}
$message = "this is help"
$measure = "Measure-RequiresRunAsAdministrator"
}
Describe "Test importing customized rules with null return results" {
Context "Test Get-ScriptAnalyzer with customized rules" {
It "will not terminate the engine" {
$customizedRulePath = Get-ScriptAnalyzerRule -CustomizedRulePath $PSScriptRoot\samplerule\SampleRulesWithErrors.psm1 | Where-Object {$_.RuleName -eq $measure}
$customizedRulePath.Count | Should -Be 1
}
}
Context "Test Invoke-ScriptAnalyzer with customized rules" {
It "will not terminate the engine" {
$customizedRulePath = Invoke-ScriptAnalyzer $PSScriptRoot\TestScript.ps1 -CustomizedRulePath $PSScriptRoot\samplerule\SampleRulesWithErrors.psm1 | Where-Object {$_.RuleName -eq $measure}
$customizedRulePath.Count | Should -Be 0
}
}
}
Describe "Test importing correct customized rules" {
# if(-not ([bool] $IsCoreCLR))
# {
Context "Test Get-Help functionality in ScriptRule parsing logic" -Skip:$IsCoreCLR {
It "ScriptRule help section must be correctly processed when Get-Help is called for the first time" {
# Force Get-Help to prompt for interactive input to download help using Update-Help
# By removing this registry key we force to turn on Get-Help interactivity logic during ScriptRule parsing
$null,"Wow6432Node" | ForEach-Object {
try
{
Remove-ItemProperty -Name "DisablePromptToUpdateHelp" -Path "HKLM:\SOFTWARE\$($_)\Microsoft\PowerShell" -ErrorAction Stop
} catch {
#Ignore for cases when tests are running in non-elevated more or registry key does not exist or not accessible
}
}
$customizedRulePath = Invoke-ScriptAnalyzer $PSScriptRoot\TestScript.ps1 -CustomizedRulePath $PSScriptRoot\samplerule\samplerule.psm1 | Where-Object {$_.Message -eq $message}
$customizedRulePath.Count | Should -Be 1
# Force Get-Help not to prompt for interactive input to download help using Update-Help
# By adding this registry key we turn off Get-Help interactivity logic during ScriptRule parsing
$null,"Wow6432Node" | ForEach-Object {
try
{
Set-ItemProperty -Name "DisablePromptToUpdateHelp" -Path "HKLM:\SOFTWARE\$($_)\Microsoft\PowerShell" -Value 1 -Force -EA SilentlyContinue
}
catch
{
# Ignore for cases when tests are running in non-elevated more or registry key does not exist or not accessible
}
}
}
}
# }
Context "Test Get-ScriptAnalyzer with customized rules" {
It "will show the custom rule" {
$customizedRulePath = Get-ScriptAnalyzerRule -CustomizedRulePath $PSScriptRoot\samplerule\samplerule.psm1 | Where-Object {$_.RuleName -eq $measure}
$customizedRulePath.Count | Should -Be 1
}
It "will show the custom rule when given a rule folder path" {
$customizedRulePath = Get-ScriptAnalyzerRule -CustomizedRulePath $PSScriptRoot\samplerule | Where-Object {$_.RuleName -eq $measure}
$customizedRulePath.Count | Should -Be 1
}
It "will show the custom rule when given a rule folder path with trailing backslash" -skip:$($IsLinux -or $IsMacOS) {
$customizedRulePath = Get-ScriptAnalyzerRule -CustomizedRulePath $PSScriptRoot/samplerule/ | Where-Object {$_.RuleName -eq $measure}
$customizedRulePath.Count | Should -Be 1
}
It "will show the custom rules when given a glob" {
$customizedRulePath = Get-ScriptAnalyzerRule -CustomizedRulePath $PSScriptRoot\samplerule\samplerule* | Where-Object {$_.RuleName -match $measure}
$customizedRulePath.Count | Should -Be 4
}
It "will show the custom rules when given recurse switch" {
$customizedRulePath = Get-ScriptAnalyzerRule -RecurseCustomRulePath -CustomizedRulePath "$PSScriptRoot\samplerule", "$PSScriptRoot\samplerule\samplerule2" | Where-Object {$_.RuleName -eq $measure}
$customizedRulePath.Count | Should -Be 5
}
It "will show the custom rules when given glob with recurse switch" {
$customizedRulePath = Get-ScriptAnalyzerRule -RecurseCustomRulePath -CustomizedRulePath $PSScriptRoot\samplerule\samplerule* | Where-Object {$_.RuleName -eq $measure}
$customizedRulePath.Count | Should -Be 5
}
It "will show the custom rules when given glob with recurse switch" {
$customizedRulePath = Get-ScriptAnalyzerRule -RecurseCustomRulePath -CustomizedRulePath $PSScriptRoot\samplerule* | Where-Object {$_.RuleName -eq $measure}
$customizedRulePath.Count | Should -Be 3
}
}
Context "Test Invoke-ScriptAnalyzer with customized rules" {
It "will show the custom rule in the results" {
$customizedRulePath = Invoke-ScriptAnalyzer $PSScriptRoot\TestScript.ps1 -CustomizedRulePath $PSScriptRoot\samplerule\samplerule.psm1 | Where-Object {$_.Message -eq $message}
$customizedRulePath.Count | Should -Be 1
}
It "will show the custom rule in the results when given a rule folder path" {
$customizedRulePath = Invoke-ScriptAnalyzer $PSScriptRoot\TestScript.ps1 -CustomizedRulePath $PSScriptRoot\samplerule | Where-Object {$_.Message -eq $message}
$customizedRulePath.Count | Should -Be 1
}
It "will set ScriptName property to the target file name" {
$violations = Invoke-ScriptAnalyzer $PSScriptRoot\TestScript.ps1 -CustomizedRulePath $PSScriptRoot\samplerule
$violations[0].ScriptName | Should -Be 'TestScript.ps1'
}
It "will set ScriptPath property to the target file path" {
$violations = Invoke-ScriptAnalyzer $PSScriptRoot\TestScript.ps1 -CustomizedRulePath $PSScriptRoot\samplerule
$expectedScriptPath = Join-Path $PSScriptRoot 'TestScript.ps1'
$violations[0].ScriptPath | Should -Be $expectedScriptPath
}
It "will set SuggestedCorrections" {
$violations = Invoke-ScriptAnalyzer $PSScriptRoot\TestScript.ps1 -CustomizedRulePath $PSScriptRoot\samplerule
$violations[0].SuggestedCorrections | Should -Not -BeNullOrEmpty
$violations[0].SuggestedCorrections.StartLineNumber | Should -Be 1
$violations[0].SuggestedCorrections.EndLineNumber | Should -Be 2
$violations[0].SuggestedCorrections.StartColumnNumber | Should -Be 3
$violations[0].SuggestedCorrections.EndColumnNumber | Should -Be 4
$violations[0].SuggestedCorrections.Text | Should -Be 'text'
$violations[0].SuggestedCorrections.File | Should -Be 'filePath'
$violations[0].SuggestedCorrections.Description | Should -Be 'description'
}
It "can suppress custom rule" {
$script = "[Diagnostics.CodeAnalysis.SuppressMessageAttribute('samplerule\$measure','')]Param()"
$testScriptPath = "TestDrive:\SuppressedCustomRule.ps1"
Set-Content -Path $testScriptPath -Value $script
$customizedRulePath = Invoke-ScriptAnalyzer -Path $testScriptPath -CustomizedRulePath $PSScriptRoot\samplerule\samplerule.psm1 |
Where-Object { $_.Message -eq $message }
$customizedRulePath.Count | Should -Be 0
}
It "can suppress custom rule with rule name expression '<RuleNameExpression>'" -TestCases @(
@{RuleNameExpression = '$MyInvocation.MyCommand.Name'; RuleName = 'WarningAboutDoSomething' }
@{RuleNameExpression = '$MyInvocation.InvocationName'; RuleName = 'MyCustomRule\WarningAboutDoSomething' }
@{RuleNameExpression = "'MyRuleName'"; RuleName = 'MyRuleName' }
) {
Param($RuleNameExpression, $RuleName)
$script = @"
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('$RuleName', '')]
Param()
Invoke-Something
"@
$customRuleContent = @'
function WarningAboutDoSomething {
param (
[System.Management.Automation.Language.CommandAst]$ast
)
if ($ast.GetCommandName() -eq 'Invoke-Something') {
New-Object -Typename 'Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.DiagnosticRecord' `
-ArgumentList 'This is help',$ast.Extent,REPLACE_WITH_RULE_NAME_EXPRESSION,Warning,$ast.Extent.File,$null,$null
}
}
'@
$customRuleContent = $customRuleContent.Replace('REPLACE_WITH_RULE_NAME_EXPRESSION', $RuleNameExpression)
$testScriptPath = "TestDrive:\SuppressedCustomRule.ps1"
Set-Content -Path $testScriptPath -Value $script
$customRuleScriptPath = Join-Path $TestDrive 'MyCustomRule.psm1'
Set-Content -Path $customRuleScriptPath -Value $customRuleContent
$violationsWithoutSuppresion = Invoke-ScriptAnalyzer -ScriptDefinition 'Invoke-Something' -CustomRulePath $customRuleScriptPath
$violationsWithoutSuppresion.Count | Should -Be 1
$violations = Invoke-ScriptAnalyzer -Path $testScriptPath -CustomRulePath $customRuleScriptPath
$violations.Count | Should -Be 0
$violationsWithIncludeDefaultRules = Invoke-ScriptAnalyzer -Path $testScriptPath -CustomRulePath $customRuleScriptPath -IncludeDefaultRules
$violationsWithIncludeDefaultRules.Count | Should -Be 0
}
It "will set RuleSuppressionID" {
$violations = Invoke-ScriptAnalyzer $PSScriptRoot\TestScript.ps1 -CustomizedRulePath $PSScriptRoot\samplerule
$violations[0].RuleSuppressionID | Should -Be "MyRuleSuppressionID"
}
# if (!$testingLibraryUsage)
# {
Context "Test Invoke-ScriptAnalyzer with customized rules - Advanced test cases" -Skip:$testingLibraryUsage {
It "will show the custom rule in the results when given a rule folder path with trailing backslash" {
# needs fixing for Linux
if (!$IsLinux -and !$IsMacOS)
{
$customizedRulePath = Invoke-ScriptAnalyzer $PSScriptRoot\TestScript.ps1 -CustomizedRulePath $PSScriptRoot\samplerule\ | Where-Object {$_.Message -eq $message}
$customizedRulePath.Count | Should -Be 1
}
}
It "will show the custom rules when given a glob" {
$customizedRulePath = Invoke-ScriptAnalyzer $PSScriptRoot\TestScript.ps1 -CustomizedRulePath $PSScriptRoot\samplerule\samplerule* | Where-Object {$_.Message -eq $message}
$customizedRulePath.Count | Should -Be 3
}
It "will show the custom rules when given recurse switch" {
$customizedRulePath = Invoke-ScriptAnalyzer $PSScriptRoot\TestScript.ps1 -RecurseCustomRulePath -CustomizedRulePath $PSScriptRoot\samplerule | Where-Object {$_.Message -eq $message}
$customizedRulePath.Count | Should -Be 3
}
It "will show the custom rules when given glob with recurse switch" {
$customizedRulePath = Invoke-ScriptAnalyzer $PSScriptRoot\TestScript.ps1 -RecurseCustomRulePath -CustomizedRulePath $PSScriptRoot\samplerule\samplerule* | Where-Object {$_.Message -eq $message}
$customizedRulePath.Count | Should -Be 4
}
It "will show the custom rules when given glob with recurse switch" {
$customizedRulePath = Invoke-ScriptAnalyzer $PSScriptRoot\TestScript.ps1 -RecurseCustomRulePath -CustomizedRulePath $PSScriptRoot\samplerule* | Where-Object {$_.Message -eq $message}
$customizedRulePath.Count | Should -Be 3
}
It "Using IncludeDefaultRules Switch with CustomRulePath" {
$customizedRulePath = Invoke-ScriptAnalyzer $PSScriptRoot\TestScript.ps1 -CustomRulePath $PSScriptRoot\samplerule\samplerule.psm1 -IncludeDefaultRules
$customizedRulePath.Count | Should -Be 2
}
It "Using IncludeDefaultRules Switch without CustomRulePath" {
$customizedRulePath = Invoke-ScriptAnalyzer $PSScriptRoot\TestScript.ps1 -IncludeDefaultRules
$customizedRulePath.Count | Should -Be 1
}
It "Not Using IncludeDefaultRules Switch and without CustomRulePath" {
$customizedRulePath = Invoke-ScriptAnalyzer $PSScriptRoot\TestScript.ps1
$customizedRulePath.Count | Should -Be 1
}
It "loads custom rules that contain version in their path" -Skip:($PSVersionTable.PSVersion -lt [Version]'5.0.0') {
$customizedRulePath = Invoke-ScriptAnalyzer $PSScriptRoot\TestScript.ps1 -CustomRulePath $PSScriptRoot\VersionedSampleRule\SampleRuleWithVersion
$customizedRulePath.Count | Should -Be 1
$customizedRulePath = Get-ScriptAnalyzerRule -CustomRulePath $PSScriptRoot\VersionedSampleRule\SampleRuleWithVersion
$customizedRulePath.Count | Should -Be 1
}
It "loads custom rules that contain version in their path with the RecurseCustomRule switch" -Skip:($PSVersionTable.PSVersion -lt [Version]'5.0.0') {
$customizedRulePath = Invoke-ScriptAnalyzer $PSScriptRoot\TestScript.ps1 -CustomRulePath $PSScriptRoot\VersionedSampleRule -RecurseCustomRulePath
$customizedRulePath.Count | Should -Be 1
$customizedRulePath = Get-ScriptAnalyzerRule -CustomRulePath $PSScriptRoot\VersionedSampleRule -RecurseCustomRulePath
$customizedRulePath.Count | Should -Be 1
}
}
}
}