1
- $directory = Split-Path - Parent $MyInvocation.MyCommand.Path
2
- $testRootDirectory = Split-Path - Parent $directory
3
- Import-Module (Join-Path $testRootDirectory ' PSScriptAnalyzerTestHelper.psm1' )
4
-
5
- if (-not (Test-PSEditionCoreCLR ))
1
+ if (-not $IsCoreCLR )
6
2
{
7
3
# Force Get-Help not to prompt for interactive input to download help using Update-Help
8
4
# By adding this registry key we turn off Get-Help interactivity logic during ScriptRule parsing
@@ -25,15 +21,15 @@ $measure = "Measure-RequiresRunAsAdministrator"
25
21
Describe " Test importing customized rules with null return results" {
26
22
Context " Test Get-ScriptAnalyzer with customized rules" {
27
23
It " will not terminate the engine" {
28
- $customizedRulePath = Get-ScriptAnalyzerRule - CustomizedRulePath $directory \samplerule\SampleRulesWithErrors.psm1 | Where-Object {$_.RuleName -eq $measure }
24
+ $customizedRulePath = Get-ScriptAnalyzerRule - CustomizedRulePath $PSScriptRoot \samplerule\SampleRulesWithErrors.psm1 | Where-Object {$_.RuleName -eq $measure }
29
25
$customizedRulePath.Count | Should - Be 1
30
26
}
31
27
32
28
}
33
29
34
30
Context " Test Invoke-ScriptAnalyzer with customized rules" {
35
31
It " will not terminate the engine" {
36
- $customizedRulePath = Invoke-ScriptAnalyzer $directory \TestScript.ps1 - CustomizedRulePath $directory \samplerule\SampleRulesWithErrors.psm1 | Where-Object {$_.RuleName -eq $measure }
32
+ $customizedRulePath = Invoke-ScriptAnalyzer $PSScriptRoot \TestScript.ps1 - CustomizedRulePath $PSScriptRoot \samplerule\SampleRulesWithErrors.psm1 | Where-Object {$_.RuleName -eq $measure }
37
33
$customizedRulePath.Count | Should - Be 0
38
34
}
39
35
}
@@ -42,7 +38,7 @@ Describe "Test importing customized rules with null return results" {
42
38
43
39
Describe " Test importing correct customized rules" {
44
40
45
- if (-not ( Test-PSEditionCoreCLR ) )
41
+ if (-not $IsCoreCLR )
46
42
{
47
43
Context " Test Get-Help functionality in ScriptRule parsing logic" {
48
44
It " ScriptRule help section must be correctly processed when Get-Help is called for the first time" {
@@ -58,7 +54,7 @@ Describe "Test importing correct customized rules" {
58
54
}
59
55
}
60
56
61
- $customizedRulePath = Invoke-ScriptAnalyzer $directory \TestScript.ps1 - CustomizedRulePath $directory \samplerule\samplerule.psm1 | Where-Object {$_.Message -eq $message }
57
+ $customizedRulePath = Invoke-ScriptAnalyzer $PSScriptRoot \TestScript.ps1 - CustomizedRulePath $PSScriptRoot \samplerule\samplerule.psm1 | Where-Object {$_.Message -eq $message }
62
58
$customizedRulePath.Count | Should - Be 1
63
59
64
60
# Force Get-Help not to prompt for interactive input to download help using Update-Help
@@ -79,70 +75,65 @@ Describe "Test importing correct customized rules" {
79
75
80
76
Context " Test Get-ScriptAnalyzer with customized rules" {
81
77
It " will show the custom rule" {
82
- $customizedRulePath = Get-ScriptAnalyzerRule - CustomizedRulePath $directory \samplerule\samplerule.psm1 | Where-Object {$_.RuleName -eq $measure }
78
+ $customizedRulePath = Get-ScriptAnalyzerRule - CustomizedRulePath $PSScriptRoot \samplerule\samplerule.psm1 | Where-Object {$_.RuleName -eq $measure }
83
79
$customizedRulePath.Count | Should - Be 1
84
80
}
85
81
86
82
It " will show the custom rule when given a rule folder path" {
87
- $customizedRulePath = Get-ScriptAnalyzerRule - CustomizedRulePath $directory \samplerule | Where-Object {$_.RuleName -eq $measure }
83
+ $customizedRulePath = Get-ScriptAnalyzerRule - CustomizedRulePath $PSScriptRoot \samplerule | Where-Object {$_.RuleName -eq $measure }
88
84
$customizedRulePath.Count | Should - Be 1
89
85
}
90
86
91
87
It " will show the custom rule when given a rule folder path with trailing backslash" - skip:$ ($IsLinux -or $IsMacOS ) {
92
- $customizedRulePath = Get-ScriptAnalyzerRule - CustomizedRulePath $directory / samplerule/ | Where-Object {$_.RuleName -eq $measure }
88
+ $customizedRulePath = Get-ScriptAnalyzerRule - CustomizedRulePath $PSScriptRoot / samplerule/ | Where-Object {$_.RuleName -eq $measure }
93
89
$customizedRulePath.Count | Should - Be 1
94
90
}
95
91
96
92
It " will show the custom rules when given a glob" {
97
- # needs fixing for Linux
98
- $expectedNumRules = 4
99
- $customizedRulePath = Get-ScriptAnalyzerRule - CustomizedRulePath $directory \samplerule\samplerule* | Where-Object {$_.RuleName -match $measure }
93
+ $customizedRulePath = Get-ScriptAnalyzerRule - CustomizedRulePath $PSScriptRoot \samplerule\samplerule* | Where-Object {$_.RuleName -match $measure }
100
94
$customizedRulePath.Count | Should - Be 4
101
95
}
102
96
103
97
It " will show the custom rules when given recurse switch" {
104
- $customizedRulePath = Get-ScriptAnalyzerRule - RecurseCustomRulePath - CustomizedRulePath " $directory \samplerule" , " $directory \samplerule\samplerule2" | Where-Object {$_.RuleName -eq $measure }
98
+ $customizedRulePath = Get-ScriptAnalyzerRule - RecurseCustomRulePath - CustomizedRulePath " $PSScriptRoot \samplerule" , " $PSScriptRoot \samplerule\samplerule2" | Where-Object {$_.RuleName -eq $measure }
105
99
$customizedRulePath.Count | Should - Be 5
106
100
}
107
101
108
102
It " will show the custom rules when given glob with recurse switch" {
109
- # needs fixing for Linux
110
- $expectedNumRules = 5
111
- $customizedRulePath = Get-ScriptAnalyzerRule - RecurseCustomRulePath - CustomizedRulePath $directory \samplerule\samplerule* | Where-Object {$_.RuleName -eq $measure }
103
+ $customizedRulePath = Get-ScriptAnalyzerRule - RecurseCustomRulePath - CustomizedRulePath $PSScriptRoot \samplerule\samplerule* | Where-Object {$_.RuleName -eq $measure }
112
104
$customizedRulePath.Count | Should - Be 5
113
105
}
114
106
115
107
It " will show the custom rules when given glob with recurse switch" {
116
- $customizedRulePath = Get-ScriptAnalyzerRule - RecurseCustomRulePath - CustomizedRulePath $directory \samplerule* | Where-Object {$_.RuleName -eq $measure }
108
+ $customizedRulePath = Get-ScriptAnalyzerRule - RecurseCustomRulePath - CustomizedRulePath $PSScriptRoot \samplerule* | Where-Object {$_.RuleName -eq $measure }
117
109
$customizedRulePath.Count | Should - Be 3
118
110
}
119
111
}
120
112
121
113
Context " Test Invoke-ScriptAnalyzer with customized rules" {
122
114
It " will show the custom rule in the results" {
123
- $customizedRulePath = Invoke-ScriptAnalyzer $directory \TestScript.ps1 - CustomizedRulePath $directory \samplerule\samplerule.psm1 | Where-Object {$_.Message -eq $message }
115
+ $customizedRulePath = Invoke-ScriptAnalyzer $PSScriptRoot \TestScript.ps1 - CustomizedRulePath $PSScriptRoot \samplerule\samplerule.psm1 | Where-Object {$_.Message -eq $message }
124
116
$customizedRulePath.Count | Should - Be 1
125
117
}
126
118
127
119
It " will show the custom rule in the results when given a rule folder path" {
128
- $customizedRulePath = Invoke-ScriptAnalyzer $directory \TestScript.ps1 - CustomizedRulePath $directory \samplerule | Where-Object {$_.Message -eq $message }
120
+ $customizedRulePath = Invoke-ScriptAnalyzer $PSScriptRoot \TestScript.ps1 - CustomizedRulePath $PSScriptRoot \samplerule | Where-Object {$_.Message -eq $message }
129
121
$customizedRulePath.Count | Should - Be 1
130
122
}
131
123
132
124
It " will set ScriptName property to the target file name" {
133
- $violations = Invoke-ScriptAnalyzer $directory \TestScript.ps1 - CustomizedRulePath $directory \samplerule
125
+ $violations = Invoke-ScriptAnalyzer $PSScriptRoot \TestScript.ps1 - CustomizedRulePath $PSScriptRoot \samplerule
134
126
$violations [0 ].ScriptName | Should - Be ' TestScript.ps1'
135
127
}
136
128
137
129
It " will set ScriptPath property to the target file path" {
138
- $violations = Invoke-ScriptAnalyzer $directory \TestScript.ps1 - CustomizedRulePath $directory \samplerule
139
- $expectedScriptPath = Join-Path $directory ' TestScript.ps1'
130
+ $violations = Invoke-ScriptAnalyzer $PSScriptRoot \TestScript.ps1 - CustomizedRulePath $PSScriptRoot \samplerule
131
+ $expectedScriptPath = Join-Path $PSScriptRoot ' TestScript.ps1'
140
132
$violations [0 ].ScriptPath | Should - Be $expectedScriptPath
141
133
}
142
134
143
135
It " will set SuggestedCorrections" {
144
- $violations = Invoke-ScriptAnalyzer $directory \TestScript.ps1 - CustomizedRulePath $directory \samplerule
145
- $expectedScriptPath = Join-Path $directory ' TestScript.ps1'
136
+ $violations = Invoke-ScriptAnalyzer $PSScriptRoot \TestScript.ps1 - CustomizedRulePath $PSScriptRoot \samplerule
146
137
$violations [0 ].SuggestedCorrections | Should -Not - BeNullOrEmpty
147
138
$violations [0 ].SuggestedCorrections.StartLineNumber | Should - Be 1
148
139
$violations [0 ].SuggestedCorrections.EndLineNumber | Should - Be 2
@@ -158,7 +149,7 @@ Describe "Test importing correct customized rules" {
158
149
$testScriptPath = " TestDrive:\SuppressedCustomRule.ps1"
159
150
Set-Content - Path $testScriptPath - Value $script
160
151
161
- $customizedRulePath = Invoke-ScriptAnalyzer - Path $testScriptPath - CustomizedRulePath $directory \samplerule\samplerule.psm1 |
152
+ $customizedRulePath = Invoke-ScriptAnalyzer - Path $testScriptPath - CustomizedRulePath $PSScriptRoot \samplerule\samplerule.psm1 |
162
153
Where-Object { $_.Message -eq $message }
163
154
164
155
$customizedRulePath.Count | Should - Be 0
@@ -202,7 +193,7 @@ Describe "Test importing correct customized rules" {
202
193
}
203
194
204
195
It " will set RuleSuppressionID" {
205
- $violations = Invoke-ScriptAnalyzer $directory \TestScript.ps1 - CustomizedRulePath $directory \samplerule
196
+ $violations = Invoke-ScriptAnalyzer $PSScriptRoot \TestScript.ps1 - CustomizedRulePath $PSScriptRoot \samplerule
206
197
$violations [0 ].RuleSuppressionID | Should - Be " MyRuleSuppressionID"
207
198
}
208
199
@@ -212,63 +203,61 @@ Describe "Test importing correct customized rules" {
212
203
# needs fixing for Linux
213
204
if (! $IsLinux -and ! $IsMacOS )
214
205
{
215
- $customizedRulePath = Invoke-ScriptAnalyzer $directory \TestScript.ps1 - CustomizedRulePath $directory \samplerule\ | Where-Object {$_.Message -eq $message }
206
+ $customizedRulePath = Invoke-ScriptAnalyzer $PSScriptRoot \TestScript.ps1 - CustomizedRulePath $PSScriptRoot \samplerule\ | Where-Object {$_.Message -eq $message }
216
207
$customizedRulePath.Count | Should - Be 1
217
208
}
218
209
}
219
210
220
211
It " will show the custom rules when given a glob" {
221
- $customizedRulePath = Invoke-ScriptAnalyzer $directory \TestScript.ps1 - CustomizedRulePath $directory \samplerule\samplerule* | Where-Object {$_.Message -eq $message }
212
+ $customizedRulePath = Invoke-ScriptAnalyzer $PSScriptRoot \TestScript.ps1 - CustomizedRulePath $PSScriptRoot \samplerule\samplerule* | Where-Object {$_.Message -eq $message }
222
213
$customizedRulePath.Count | Should - Be 3
223
214
}
224
215
225
216
It " will show the custom rules when given recurse switch" {
226
- $customizedRulePath = Invoke-ScriptAnalyzer $directory \TestScript.ps1 - RecurseCustomRulePath - CustomizedRulePath $directory \samplerule | Where-Object {$_.Message -eq $message }
217
+ $customizedRulePath = Invoke-ScriptAnalyzer $PSScriptRoot \TestScript.ps1 - RecurseCustomRulePath - CustomizedRulePath $PSScriptRoot \samplerule | Where-Object {$_.Message -eq $message }
227
218
$customizedRulePath.Count | Should - Be 3
228
219
}
229
220
230
221
It " will show the custom rules when given glob with recurse switch" {
231
- $customizedRulePath = Invoke-ScriptAnalyzer $directory \TestScript.ps1 - RecurseCustomRulePath - CustomizedRulePath $directory \samplerule\samplerule* | Where-Object {$_.Message -eq $message }
222
+ $customizedRulePath = Invoke-ScriptAnalyzer $PSScriptRoot \TestScript.ps1 - RecurseCustomRulePath - CustomizedRulePath $PSScriptRoot \samplerule\samplerule* | Where-Object {$_.Message -eq $message }
232
223
$customizedRulePath.Count | Should - Be 4
233
224
}
234
225
235
226
It " will show the custom rules when given glob with recurse switch" {
236
- $customizedRulePath = Invoke-ScriptAnalyzer $directory \TestScript.ps1 - RecurseCustomRulePath - CustomizedRulePath $directory \samplerule* | Where-Object {$_.Message -eq $message }
227
+ $customizedRulePath = Invoke-ScriptAnalyzer $PSScriptRoot \TestScript.ps1 - RecurseCustomRulePath - CustomizedRulePath $PSScriptRoot \samplerule* | Where-Object {$_.Message -eq $message }
237
228
$customizedRulePath.Count | Should - Be 3
238
229
}
239
230
240
231
It " Using IncludeDefaultRules Switch with CustomRulePath" {
241
- $customizedRulePath = Invoke-ScriptAnalyzer $directory \TestScript.ps1 - CustomRulePath $directory \samplerule\samplerule.psm1 - IncludeDefaultRules
232
+ $customizedRulePath = Invoke-ScriptAnalyzer $PSScriptRoot \TestScript.ps1 - CustomRulePath $PSScriptRoot \samplerule\samplerule.psm1 - IncludeDefaultRules
242
233
$customizedRulePath.Count | Should - Be 2
243
234
}
244
235
245
236
It " Using IncludeDefaultRules Switch without CustomRulePath" {
246
- $customizedRulePath = Invoke-ScriptAnalyzer $directory \TestScript.ps1 - IncludeDefaultRules
237
+ $customizedRulePath = Invoke-ScriptAnalyzer $PSScriptRoot \TestScript.ps1 - IncludeDefaultRules
247
238
$customizedRulePath.Count | Should - Be 1
248
239
}
249
240
250
241
It " Not Using IncludeDefaultRules Switch and without CustomRulePath" {
251
- $customizedRulePath = Invoke-ScriptAnalyzer $directory \TestScript.ps1
242
+ $customizedRulePath = Invoke-ScriptAnalyzer $PSScriptRoot \TestScript.ps1
252
243
$customizedRulePath.Count | Should - Be 1
253
244
}
254
245
255
246
It " loads custom rules that contain version in their path" - Skip:($PSVersionTable.PSVersion -lt [Version ]' 5.0.0' ) {
256
- $customizedRulePath = Invoke-ScriptAnalyzer $directory \TestScript.ps1 - CustomRulePath $directory \VersionedSampleRule\SampleRuleWithVersion
257
- $customizedRulePath.Count | Should - Be 1
247
+ $customizedRulePath = Invoke-ScriptAnalyzer $PSScriptRoot \TestScript.ps1 - CustomRulePath $PSScriptRoot \VersionedSampleRule\SampleRuleWithVersion
248
+ $customizedRulePath.Count | Should - Be 1
258
249
259
- $customizedRulePath = Get-ScriptAnalyzerRule - CustomRulePath $directory \VersionedSampleRule\SampleRuleWithVersion
260
- $customizedRulePath.Count | Should - Be 1
250
+ $customizedRulePath = Get-ScriptAnalyzerRule - CustomRulePath $PSScriptRoot \VersionedSampleRule\SampleRuleWithVersion
251
+ $customizedRulePath.Count | Should - Be 1
261
252
}
262
253
263
254
It " loads custom rules that contain version in their path with the RecurseCustomRule switch" - Skip:($PSVersionTable.PSVersion -lt [Version ]' 5.0.0' ) {
264
- $customizedRulePath = Invoke-ScriptAnalyzer $directory \TestScript.ps1 - CustomRulePath $directory \VersionedSampleRule - RecurseCustomRulePath
265
- $customizedRulePath.Count | Should - Be 1
255
+ $customizedRulePath = Invoke-ScriptAnalyzer $PSScriptRoot \TestScript.ps1 - CustomRulePath $PSScriptRoot \VersionedSampleRule - RecurseCustomRulePath
256
+ $customizedRulePath.Count | Should - Be 1
266
257
267
- $customizedRulePath = Get-ScriptAnalyzerRule - CustomRulePath $directory \VersionedSampleRule - RecurseCustomRulePath
268
- $customizedRulePath.Count | Should - Be 1
269
- }
258
+ $customizedRulePath = Get-ScriptAnalyzerRule - CustomRulePath $PSScriptRoot \VersionedSampleRule - RecurseCustomRulePath
259
+ $customizedRulePath.Count | Should - Be 1
260
+ }
270
261
}
271
-
272
262
}
273
263
}
274
-
0 commit comments