Skip to content

Commit d21cf9f

Browse files
author
Kapil Borle
committed
Update UsePSCredentialType tests
1 parent 6277a8a commit d21cf9f

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

Tests/PSScriptAnalyzerTestHelper.psm1

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ Function Test-PSVersionV3
4343
$PSVersionTable.PSVersion.Major -eq 3
4444
}
4545

46+
Function Test-PSVersionV4
47+
{
48+
$PSVersionTable.PSVersion.Major -eq 4
49+
}
50+
4651
Function Get-Count
4752
{
4853
Begin {$count = 0}
@@ -55,4 +60,5 @@ Export-ModuleMember -Function Test-CorrectionExtent
5560
Export-ModuleMember -Function Test-PSEditionCoreCLR
5661
Export-ModuleMember -Function Test-PSEditionCoreCLRLinux
5762
Export-ModuleMember -Function Test-PSVersionV3
63+
Export-ModuleMember -Function Test-PSVersionV4
5864
Export-ModuleMember -Function Get-Count

Tests/Rules/PSCredentialType.tests.ps1

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
1-
Import-Module PSScriptAnalyzer
1+
$directory = Split-Path -Parent $MyInvocation.MyCommand.Path
2+
$testRootDirectory = Split-Path -Parent $directory
3+
Import-Module (Join-Path $testRootDirectory 'PSScriptAnalyzerTestHelper.psm1')
4+
Import-Module PSScriptAnalyzer
5+
26
$violationMessage = "The Credential parameter in 'Credential' must be of type PSCredential. For PowerShell 4.0 and earlier, please define a credential transformation attribute, e.g. [System.Management.Automation.Credential()], after the PSCredential type attribute."
37
$violationName = "PSUsePSCredentialType"
4-
$directory = Split-Path -Parent $MyInvocation.MyCommand.Path
58
$violations = Invoke-ScriptAnalyzer $directory\PSCredentialType.ps1 | Where-Object {$_.RuleName -eq $violationName}
69
$noViolations = Invoke-ScriptAnalyzer $directory\PSCredentialTypeNoViolations.ps1 | Where-Object {$_.RuleName -eq $violationName}
710

811
Describe "PSCredentialType" {
912
Context "When there are violations" {
10-
It "has 2 PSCredential type violation" {
11-
$violations.Count | Should Be 2
13+
$expectedViolations = 1
14+
if (Test-PSVersionV3 -or Test-PSVersionV4) {
15+
$expectedViolations = 2
16+
}
17+
It ("has {0} PSCredential type violation" -f $expectedViolations) {
18+
$violations.Count | Should Be $expectedViolations
1219
}
1320

1421
It "has the correct description message" {
@@ -31,9 +38,9 @@ function Get-Credential
3138

3239
}
3340

34-
Context ("When there are {0} violations" -f $expectedViolationCount) {
35-
It ("returns {0} violations" -f $expectedViolationCount) {
36-
$noViolations.Count | Should Be $expectedViolationCount
41+
Context ("When there are no violations") {
42+
It "returns no violations" {
43+
$noViolations.Count | Should Be 0
3744
}
3845
}
3946
}

0 commit comments

Comments
 (0)