Skip to content

Commit 3c7c19d

Browse files
author
Kapil Borle
committed
Add tests for UseDeclaredVarsMoreThanAssignments rule
1 parent 5b602fa commit 3c7c19d

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

Tests/Rules/UseDeclaredVarsMoreThanAssignments.tests.ps1

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1-
Import-Module PSScriptAnalyzer
1+
$directory = Split-Path -Parent $MyInvocation.MyCommand.Path
2+
$testRootDirectory = Split-Path -Parent $directory
3+
4+
Import-Module PSScriptAnalyzer
5+
Import-Module (Join-Path $testRootDirectory 'PSScriptAnalyzerTestHelper.psm1')
6+
27
$violationMessage = "The variable 'declaredVar2' is assigned but never used."
38
$violationName = "PSUseDeclaredVarsMoreThanAssignments"
4-
$directory = Split-Path -Parent $MyInvocation.MyCommand.Path
59
$violations = Invoke-ScriptAnalyzer $directory\UseDeclaredVarsMoreThanAssignments.ps1 | Where-Object {$_.RuleName -eq $violationName}
610
$noViolations = Invoke-ScriptAnalyzer $directory\UseDeclaredVarsMoreThanAssignmentsNoViolations.ps1 | Where-Object {$_.RuleName -eq $violationName}
711

@@ -29,8 +33,21 @@ function MyFunc2() {
2933
$a + $a
3034
}
3135
'@
32-
$local:violations = Invoke-ScriptAnalyzer -ScriptDefinition $target -IncludeRule $violationName
33-
$local:violations.Count | Should Be 1
36+
Invoke-ScriptAnalyzer -ScriptDefinition $target -IncludeRule $violationName | `
37+
Get-Count | `
38+
Should Be 1
39+
}
40+
41+
It "does not flag `$InformationPreference variable" {
42+
Invoke-ScriptAnalyzer -ScriptDefinition '$InformationPreference=Stop' -IncludeRule $violationName | `
43+
Get-Count | `
44+
Should Be 0
45+
}
46+
47+
It "does not flag `$PSModuleAutoLoadingPreference variable" {
48+
Invoke-ScriptAnalyzer -ScriptDefinition '$PSModuleAutoLoadingPreference=None' -IncludeRule $violationName | `
49+
Get-Count | `
50+
Should Be 0
3451
}
3552
}
3653

0 commit comments

Comments
 (0)