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 )
62{
73 # Force Get-Help not to prompt for interactive input to download help using Update-Help
84 # By adding this registry key we turn off Get-Help interactivity logic during ScriptRule parsing
@@ -25,15 +21,15 @@ $measure = "Measure-RequiresRunAsAdministrator"
2521Describe " Test importing customized rules with null return results" {
2622 Context " Test Get-ScriptAnalyzer with customized rules" {
2723 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 }
2925 $customizedRulePath.Count | Should - Be 1
3026 }
3127
3228 }
3329
3430 Context " Test Invoke-ScriptAnalyzer with customized rules" {
3531 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 }
3733 $customizedRulePath.Count | Should - Be 0
3834 }
3935 }
@@ -42,7 +38,7 @@ Describe "Test importing customized rules with null return results" {
4238
4339Describe " Test importing correct customized rules" {
4440
45- if (-not ( Test-PSEditionCoreCLR ) )
41+ if (-not $IsCoreCLR )
4642 {
4743 Context " Test Get-Help functionality in ScriptRule parsing logic" {
4844 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" {
5854 }
5955 }
6056
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 }
6258 $customizedRulePath.Count | Should - Be 1
6359
6460 # 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" {
7975
8076 Context " Test Get-ScriptAnalyzer with customized rules" {
8177 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 }
8379 $customizedRulePath.Count | Should - Be 1
8480 }
8581
8682 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 }
8884 $customizedRulePath.Count | Should - Be 1
8985 }
9086
9187 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 }
9389 $customizedRulePath.Count | Should - Be 1
9490 }
9591
9692 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 }
10094 $customizedRulePath.Count | Should - Be 4
10195 }
10296
10397 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 }
10599 $customizedRulePath.Count | Should - Be 5
106100 }
107101
108102 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 }
112104 $customizedRulePath.Count | Should - Be 5
113105 }
114106
115107 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 }
117109 $customizedRulePath.Count | Should - Be 3
118110 }
119111 }
120112
121113 Context " Test Invoke-ScriptAnalyzer with customized rules" {
122114 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 }
124116 $customizedRulePath.Count | Should - Be 1
125117 }
126118
127119 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 }
129121 $customizedRulePath.Count | Should - Be 1
130122 }
131123
132124 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
134126 $violations [0 ].ScriptName | Should - Be ' TestScript.ps1'
135127 }
136128
137129 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'
140132 $violations [0 ].ScriptPath | Should - Be $expectedScriptPath
141133 }
142134
143135 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
146137 $violations [0 ].SuggestedCorrections | Should -Not - BeNullOrEmpty
147138 $violations [0 ].SuggestedCorrections.StartLineNumber | Should - Be 1
148139 $violations [0 ].SuggestedCorrections.EndLineNumber | Should - Be 2
@@ -158,7 +149,7 @@ Describe "Test importing correct customized rules" {
158149 $testScriptPath = " TestDrive:\SuppressedCustomRule.ps1"
159150 Set-Content - Path $testScriptPath - Value $script
160151
161- $customizedRulePath = Invoke-ScriptAnalyzer - Path $testScriptPath - CustomizedRulePath $directory \samplerule\samplerule.psm1 |
152+ $customizedRulePath = Invoke-ScriptAnalyzer - Path $testScriptPath - CustomizedRulePath $PSScriptRoot \samplerule\samplerule.psm1 |
162153 Where-Object { $_.Message -eq $message }
163154
164155 $customizedRulePath.Count | Should - Be 0
@@ -202,7 +193,7 @@ Describe "Test importing correct customized rules" {
202193 }
203194
204195 It " will set RuleSuppressionID" {
205- $violations = Invoke-ScriptAnalyzer $directory \TestScript.ps1 - CustomizedRulePath $directory \samplerule
196+ $violations = Invoke-ScriptAnalyzer $PSScriptRoot \TestScript.ps1 - CustomizedRulePath $PSScriptRoot \samplerule
206197 $violations [0 ].RuleSuppressionID | Should - Be " MyRuleSuppressionID"
207198 }
208199
@@ -212,63 +203,61 @@ Describe "Test importing correct customized rules" {
212203 # needs fixing for Linux
213204 if (! $IsLinux -and ! $IsMacOS )
214205 {
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 }
216207 $customizedRulePath.Count | Should - Be 1
217208 }
218209 }
219210
220211 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 }
222213 $customizedRulePath.Count | Should - Be 3
223214 }
224215
225216 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 }
227218 $customizedRulePath.Count | Should - Be 3
228219 }
229220
230221 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 }
232223 $customizedRulePath.Count | Should - Be 4
233224 }
234225
235226 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 }
237228 $customizedRulePath.Count | Should - Be 3
238229 }
239230
240231 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
242233 $customizedRulePath.Count | Should - Be 2
243234 }
244235
245236 It " Using IncludeDefaultRules Switch without CustomRulePath" {
246- $customizedRulePath = Invoke-ScriptAnalyzer $directory \TestScript.ps1 - IncludeDefaultRules
237+ $customizedRulePath = Invoke-ScriptAnalyzer $PSScriptRoot \TestScript.ps1 - IncludeDefaultRules
247238 $customizedRulePath.Count | Should - Be 1
248239 }
249240
250241 It " Not Using IncludeDefaultRules Switch and without CustomRulePath" {
251- $customizedRulePath = Invoke-ScriptAnalyzer $directory \TestScript.ps1
242+ $customizedRulePath = Invoke-ScriptAnalyzer $PSScriptRoot \TestScript.ps1
252243 $customizedRulePath.Count | Should - Be 1
253244 }
254245
255246 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
258249
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
261252 }
262253
263254 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
266257
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+ }
270261 }
271-
272262 }
273263}
274-
0 commit comments