Skip to content

Commit

Permalink
Replace usage of $MyInvocation.MyCommand.Path with $PSScriptRoot and …
Browse files Browse the repository at this point in the history
…cleanup tests as a preparation for Pester v5 (#1438)

* Replace usage of $MyInvocation.MyCommand.Pat with $PSScriptRoot and cleanup tests

* Fix 2 small mistakes, test should now be green again

* remove redundant [bool] before $IsCoreCLR and make casing consistent

Co-authored-by: Christoph Bergmeister <[email protected]>
  • Loading branch information
bergmeister and Christoph Bergmeister authored Apr 16, 2020
1 parent 03aa085 commit b1c40cd
Show file tree
Hide file tree
Showing 61 changed files with 275 additions and 365 deletions.
8 changes: 2 additions & 6 deletions Tests/Documentation/RuleDocumentation.tests.ps1
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
$directory = Split-Path -Parent $MyInvocation.MyCommand.Path
$testRootDirectory = Split-Path -Parent $directory
$repoRootDirectory = Split-Path -Parent $testRootDirectory
$ruleDocDirectory = Join-Path $repoRootDirectory RuleDocumentation

Describe "Validate rule documentation files" {
BeforeAll {
$ruleDocDirectory = Join-Path $PSScriptRoot '../../RuleDocumentation'
$docs = Get-ChildItem $ruleDocDirectory/*.md -Exclude README.md |
ForEach-Object { "PS" + $_.BaseName} | Sort-Object

Expand Down Expand Up @@ -56,7 +52,7 @@ Describe "Validate rule documentation files" {
$filePath | Should -Exist
}
}

It "Every rule name in the rule documentation README.md file must match the documentation file's basename" {
foreach ($key in $readmeLinks.Keys) {
$link = $readmeLinks[$key]
Expand Down
85 changes: 37 additions & 48 deletions Tests/Engine/CustomizedRule.tests.ps1
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
$directory = Split-Path -Parent $MyInvocation.MyCommand.Path
$testRootDirectory = Split-Path -Parent $directory
Import-Module (Join-Path $testRootDirectory 'PSScriptAnalyzerTestHelper.psm1')

if (-not (Test-PSEditionCoreCLR))
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
Expand All @@ -25,15 +21,15 @@ $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 $directory\samplerule\SampleRulesWithErrors.psm1 | Where-Object {$_.RuleName -eq $measure}
$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 $directory\TestScript.ps1 -CustomizedRulePath $directory\samplerule\SampleRulesWithErrors.psm1 | Where-Object {$_.RuleName -eq $measure}
$customizedRulePath = Invoke-ScriptAnalyzer $PSScriptRoot\TestScript.ps1 -CustomizedRulePath $PSScriptRoot\samplerule\SampleRulesWithErrors.psm1 | Where-Object {$_.RuleName -eq $measure}
$customizedRulePath.Count | Should -Be 0
}
}
Expand All @@ -42,7 +38,7 @@ Describe "Test importing customized rules with null return results" {

Describe "Test importing correct customized rules" {

if(-not (Test-PSEditionCoreCLR))
if(-not $IsCoreCLR)
{
Context "Test Get-Help functionality in ScriptRule parsing logic" {
It "ScriptRule help section must be correctly processed when Get-Help is called for the first time" {
Expand All @@ -58,7 +54,7 @@ Describe "Test importing correct customized rules" {
}
}

$customizedRulePath = Invoke-ScriptAnalyzer $directory\TestScript.ps1 -CustomizedRulePath $directory\samplerule\samplerule.psm1 | Where-Object {$_.Message -eq $message}
$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
Expand All @@ -79,70 +75,65 @@ Describe "Test importing correct customized rules" {

Context "Test Get-ScriptAnalyzer with customized rules" {
It "will show the custom rule" {
$customizedRulePath = Get-ScriptAnalyzerRule -CustomizedRulePath $directory\samplerule\samplerule.psm1 | Where-Object {$_.RuleName -eq $measure}
$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 $directory\samplerule | Where-Object {$_.RuleName -eq $measure}
$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 $directory/samplerule/ | Where-Object {$_.RuleName -eq $measure}
$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" {
# needs fixing for Linux
$expectedNumRules = 4
$customizedRulePath = Get-ScriptAnalyzerRule -CustomizedRulePath $directory\samplerule\samplerule* | Where-Object {$_.RuleName -match $measure}
$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 "$directory\samplerule", "$directory\samplerule\samplerule2" | Where-Object {$_.RuleName -eq $measure}
$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" {
# needs fixing for Linux
$expectedNumRules = 5
$customizedRulePath = Get-ScriptAnalyzerRule -RecurseCustomRulePath -CustomizedRulePath $directory\samplerule\samplerule* | Where-Object {$_.RuleName -eq $measure}
$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 $directory\samplerule* | Where-Object {$_.RuleName -eq $measure}
$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 $directory\TestScript.ps1 -CustomizedRulePath $directory\samplerule\samplerule.psm1 | Where-Object {$_.Message -eq $message}
$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 $directory\TestScript.ps1 -CustomizedRulePath $directory\samplerule | Where-Object {$_.Message -eq $message}
$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 $directory\TestScript.ps1 -CustomizedRulePath $directory\samplerule
$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 $directory\TestScript.ps1 -CustomizedRulePath $directory\samplerule
$expectedScriptPath = Join-Path $directory 'TestScript.ps1'
$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 $directory\TestScript.ps1 -CustomizedRulePath $directory\samplerule
$expectedScriptPath = Join-Path $directory 'TestScript.ps1'
$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
Expand All @@ -158,7 +149,7 @@ Describe "Test importing correct customized rules" {
$testScriptPath = "TestDrive:\SuppressedCustomRule.ps1"
Set-Content -Path $testScriptPath -Value $script

$customizedRulePath = Invoke-ScriptAnalyzer -Path $testScriptPath -CustomizedRulePath $directory\samplerule\samplerule.psm1 |
$customizedRulePath = Invoke-ScriptAnalyzer -Path $testScriptPath -CustomizedRulePath $PSScriptRoot\samplerule\samplerule.psm1 |
Where-Object { $_.Message -eq $message }

$customizedRulePath.Count | Should -Be 0
Expand Down Expand Up @@ -202,7 +193,7 @@ Describe "Test importing correct customized rules" {
}

It "will set RuleSuppressionID" {
$violations = Invoke-ScriptAnalyzer $directory\TestScript.ps1 -CustomizedRulePath $directory\samplerule
$violations = Invoke-ScriptAnalyzer $PSScriptRoot\TestScript.ps1 -CustomizedRulePath $PSScriptRoot\samplerule
$violations[0].RuleSuppressionID | Should -Be "MyRuleSuppressionID"
}

Expand All @@ -212,63 +203,61 @@ Describe "Test importing correct customized rules" {
# needs fixing for Linux
if (!$IsLinux -and !$IsMacOS)
{
$customizedRulePath = Invoke-ScriptAnalyzer $directory\TestScript.ps1 -CustomizedRulePath $directory\samplerule\ | Where-Object {$_.Message -eq $message}
$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 $directory\TestScript.ps1 -CustomizedRulePath $directory\samplerule\samplerule* | Where-Object {$_.Message -eq $message}
$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 $directory\TestScript.ps1 -RecurseCustomRulePath -CustomizedRulePath $directory\samplerule | Where-Object {$_.Message -eq $message}
$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 $directory\TestScript.ps1 -RecurseCustomRulePath -CustomizedRulePath $directory\samplerule\samplerule* | Where-Object {$_.Message -eq $message}
$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 $directory\TestScript.ps1 -RecurseCustomRulePath -CustomizedRulePath $directory\samplerule* | Where-Object {$_.Message -eq $message}
$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 $directory\TestScript.ps1 -CustomRulePath $directory\samplerule\samplerule.psm1 -IncludeDefaultRules
$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 $directory\TestScript.ps1 -IncludeDefaultRules
$customizedRulePath = Invoke-ScriptAnalyzer $PSScriptRoot\TestScript.ps1 -IncludeDefaultRules
$customizedRulePath.Count | Should -Be 1
}

It "Not Using IncludeDefaultRules Switch and without CustomRulePath" {
$customizedRulePath = Invoke-ScriptAnalyzer $directory\TestScript.ps1
$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 $directory\TestScript.ps1 -CustomRulePath $directory\VersionedSampleRule\SampleRuleWithVersion
$customizedRulePath.Count | Should -Be 1
$customizedRulePath = Invoke-ScriptAnalyzer $PSScriptRoot\TestScript.ps1 -CustomRulePath $PSScriptRoot\VersionedSampleRule\SampleRuleWithVersion
$customizedRulePath.Count | Should -Be 1

$customizedRulePath = Get-ScriptAnalyzerRule -CustomRulePath $directory\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 $directory\TestScript.ps1 -CustomRulePath $directory\VersionedSampleRule -RecurseCustomRulePath
$customizedRulePath.Count | Should -Be 1
$customizedRulePath = Invoke-ScriptAnalyzer $PSScriptRoot\TestScript.ps1 -CustomRulePath $PSScriptRoot\VersionedSampleRule -RecurseCustomRulePath
$customizedRulePath.Count | Should -Be 1

$customizedRulePath = Get-ScriptAnalyzerRule -CustomRulePath $directory\VersionedSampleRule -RecurseCustomRulePath
$customizedRulePath.Count | Should -Be 1
}
$customizedRulePath = Get-ScriptAnalyzerRule -CustomRulePath $PSScriptRoot\VersionedSampleRule -RecurseCustomRulePath
$customizedRulePath.Count | Should -Be 1
}
}

}
}

8 changes: 2 additions & 6 deletions Tests/Engine/EditableText.tests.ps1
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
$directory = Split-Path -Parent $MyInvocation.MyCommand.Path
$testRootDirectory = Split-Path -Parent $directory

$testRootDirectory = Split-Path -Parent $PSScriptRoot
Import-Module (Join-Path $testRootDirectory "PSScriptAnalyzerTestHelper.psm1")

$editableTextType = "Microsoft.Windows.PowerShell.ScriptAnalyzer.EditableText"
$textEditType = "Microsoft.Windows.PowerShell.ScriptAnalyzer.TextEdit"

Describe "EditableText class" {
Context "When a sigle edit is given for application" {
Context "When a single edit is given for application" {
It "Should replace in a single line string in the middle" {
$def = 'This is just a single line.'
$edit = New-Object -TypeName $textEditType -ArgumentList 1,14,1,22,"one"
Expand Down Expand Up @@ -93,7 +90,6 @@ function foo {
}
'@
# Editor does not allow trailing white-spaces, hence this weird construct.
$s = ' '
$newText = @"
[CmdletBinding()]
Expand Down
3 changes: 1 addition & 2 deletions Tests/Engine/Extensions.tests.ps1
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
$directory = Split-Path -Parent $MyInvocation.MyCommand.Path
$testRootDirectory = Split-Path -Parent $directory
$testRootDirectory = Split-Path -Parent $PSScriptRoot
Import-Module (Join-Path $testRootDirectory "PSScriptAnalyzerTestHelper.psm1")

function Get-Extent {
Expand Down
13 changes: 6 additions & 7 deletions Tests/Engine/GetScriptAnalyzerRule.tests.ps1
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
$directory = Split-Path -Parent $MyInvocation.MyCommand.Path
$testRootDirectory = Split-Path -Parent $directory
$testRootDirectory = Split-Path -Parent $PSScriptRoot
Import-Module (Join-Path $testRootDirectory 'PSScriptAnalyzerTestHelper.psm1')
$sa = Get-Command Get-ScriptAnalyzerRule

Expand Down Expand Up @@ -105,28 +104,28 @@ Describe "Test RuleExtension" {
$expectedNumCommunityRules = 12
}
It "with the module folder path" {
$ruleExtension = Get-ScriptAnalyzerRule -CustomizedRulePath $directory\CommunityAnalyzerRules | Where-Object {$_.SourceName -eq $community}
$ruleExtension = Get-ScriptAnalyzerRule -CustomizedRulePath $PSScriptRoot\CommunityAnalyzerRules | Where-Object {$_.SourceName -eq $community}
$ruleExtension.Count | Should -Be $expectedNumCommunityRules
}

It "with the psd1 path" {
$ruleExtension = Get-ScriptAnalyzerRule -CustomizedRulePath $directory\CommunityAnalyzerRules\CommunityAnalyzerRules.psd1 | Where-Object {$_.SourceName -eq $community}
$ruleExtension = Get-ScriptAnalyzerRule -CustomizedRulePath $PSScriptRoot\CommunityAnalyzerRules\CommunityAnalyzerRules.psd1 | Where-Object {$_.SourceName -eq $community}
$ruleExtension.Count | Should -Be $expectedNumCommunityRules

}

It "with the psm1 path" {
$ruleExtension = Get-ScriptAnalyzerRule -CustomizedRulePath $directory\CommunityAnalyzerRules\CommunityAnalyzerRules.psm1 | Where-Object {$_.SourceName -eq $community}
$ruleExtension = Get-ScriptAnalyzerRule -CustomizedRulePath $PSScriptRoot\CommunityAnalyzerRules\CommunityAnalyzerRules.psm1 | Where-Object {$_.SourceName -eq $community}
$ruleExtension.Count | Should -Be $expectedNumCommunityRules
}

It "with Name of a built-in rules" {
$ruleExtension = Get-ScriptAnalyzerRule -CustomizedRulePath $directory\CommunityAnalyzerRules\CommunityAnalyzerRules.psm1 -Name $singularNouns
$ruleExtension = Get-ScriptAnalyzerRule -CustomizedRulePath $PSScriptRoot\CommunityAnalyzerRules\CommunityAnalyzerRules.psm1 -Name $singularNouns
$ruleExtension.Count | Should -Be 0
}

It "with Names of built-in, DSC and non-built-in rules" {
$ruleExtension = Get-ScriptAnalyzerRule -CustomizedRulePath $directory\CommunityAnalyzerRules\CommunityAnalyzerRules.psm1 -Name $singularNouns, $measureRequired, $dscIdentical
$ruleExtension = Get-ScriptAnalyzerRule -CustomizedRulePath $PSScriptRoot\CommunityAnalyzerRules\CommunityAnalyzerRules.psm1 -Name $singularNouns, $measureRequired, $dscIdentical
$ruleExtension.Count | Should -Be 1
($ruleExtension | Where-Object {$_.RuleName -eq $measureRequired}).Count | Should -Be 1
($ruleExtension | Where-Object {$_.RuleName -eq $singularNouns}).Count | Should -Be 0
Expand Down
Loading

0 comments on commit b1c40cd

Please sign in to comment.