Skip to content

Commit 18156d9

Browse files
author
quoctruong
committed
Merge pull request #28 from PowerShell/FixTestFailures
Fix test failures
2 parents c530148 + b2cdae3 commit 18156d9

File tree

51 files changed

+205
-210
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+205
-210
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
Import-Module PSScriptAnalyzer
2+
$oneCharMessage = "The cmdlet name O only has one character."
3+
$oneCharName = "PSOneChar"
4+
$directory = Split-Path -Parent $MyInvocation.MyCommand.Path
5+
$invoke = Invoke-ScriptAnalyzer $directory\AvoidUsingReservedCharOneCharNames.ps1 | Where-Object {$_.RuleName -eq $oneCharName}
6+
$noViolations = Invoke-ScriptAnalyzer $directory\GoodCmdlet.ps1 | Where-Object {$_.RuleName -eq $oneCharName}
7+
8+
Describe "Avoid Using One Char" {
9+
Context "When there are violations" {
10+
It "has 1 One Char Violation" {
11+
$oneCharViolations.Count | Should Be 1
12+
}
13+
14+
It "has the correct description message" {
15+
$oneCharViolations[0].Message | Should Match $oneCharMessage
16+
}
17+
}
18+
19+
Context "When there are no violations" {
20+
It "has no violations" {
21+
$noReservedCharViolations.Count | Should Be 0
22+
}
23+
}
24+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
Import-Module PSScriptAnalyzer
2+
$unloadableMessage = [regex]::Escape("Cannot load the module TestBadModule that the file TestBadModule.psd1 is in.")
3+
$unloadableName = "PSAvoidUnloadableModule"
4+
$directory = Split-Path -Parent $MyInvocation.MyCommand.Path
5+
$violations = Invoke-ScriptAnalyzer $directory\TestBadModule\TestBadModule.psd1 | Where-Object {$_.RuleName -eq $unloadableName}
6+
$noViolations = Invoke-ScriptAnalyzer $directory\TestGoodModule\TestGoodModule.psd1 | Where-Object {$_.RuleName -eq $unloadableName}
7+
8+
Describe "AvoidUnloadableModule" {
9+
Context "When there are violations" {
10+
It "has 1 unloadable module violation" {
11+
$violations.Count | Should Be 1
12+
}
13+
14+
It "has the correct description message" {
15+
$violations.Message | Should Match $unloadableMessage
16+
}
17+
}
18+
19+
Context "When there are no violations" {
20+
It "returns no violations" {
21+
$noViolations.Count | Should Be 0
22+
}
23+
}
24+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
Import-Module PSScriptAnalyzer
2+
Set-Alias ctss ConvertTo-SecureString
3+
$clearHostMessage = "File 'AvoidUsingClearHostWriteHost.ps1' uses Clear-Host. This is not recommended because it may not work in some hosts or there may even be no hosts at all."
4+
$clearHostName = "PSAvoidUsingClearHost"
5+
$directory = Split-Path -Parent $MyInvocation.MyCommand.Path
6+
$violations = Invoke-ScriptAnalyzer $directory\AvoidUsingClearHost.ps1 | Where-Object {$_.RuleName -eq $clearHostName}
7+
$noViolations = Invoke-ScriptAnalyzer $directory\AvoidUsingClearHostNoViolations.ps1 | Where-Object {$_.RuleName -eq $writeHostName}
8+
9+
Describe "AvoidUsingClearHost" {
10+
Context "When there are violations" {
11+
It "has 3 Clear-Host violations" {
12+
$violations.Count | Should Be 3
13+
}
14+
15+
It "has the correct description message for Clear-Host" {
16+
$violations[0].Message | Should Match $clearHostMessage
17+
}
18+
}
19+
20+
Context "When there are no violations" {
21+
It "returns no violations" {
22+
$noViolations.Count | Should Be 0
23+
}
24+
}
25+
}

Tests/Rules/AvoidUsingFilePath.tests.ps1 renamed to Tests/Disabled Rules/AvoidUsingFilePath.tests.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Import-Module ScriptAnalyzer
1+
Import-Module PSScriptAnalyzer
22
$violationMessage = @'
33
The file path "D:\\Code" of AvoidUsingFilePath.ps1 is rooted. This should be avoided if AvoidUsingFilePath.ps1 is published online
44
'@
File renamed without changes.

Tests/Rules/TypeNotFound.tests.ps1 renamed to Tests/Disabled Rules/TypeNotFound.tests.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Import-Module -Verbose ScriptAnalyzer
1+
Import-Module -Verbose PSScriptAnalyzer
22
$violationMessage = "Type Stre is not found"
33
$violationName = "PSTypeNotFound"
44
$directory = Split-Path -Parent $MyInvocation.MyCommand.Path

Tests/Rules/UseTypeAtVariableAssignment.tests.ps1 renamed to Tests/Disabled Rules/UseTypeAtVariableAssignment.tests.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Import-Module -Verbose ScriptAnalyzer
1+
Import-Module -Verbose PSScriptAnalyzer
22
$violationMessage = [regex]::Escape('Specify type at the assignment of variable $test')
33
$violationName = "PSUseTypeAtVariableAssignment"
44
$directory = Split-Path -Parent $MyInvocation.MyCommand.Path

Tests/Engine/CommunityAnalyzerRules/CommunityAnalyzerRules.psm1

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ function Measure-RequiresRunAsAdministrator
9595
$result = [Microsoft.Windows.Powershell.ScriptAnalyzer.Generic.DiagnosticRecord]@{"Message" = $Messages.MeasureRequiresRunAsAdministrator;
9696
"Extent" = $assignmentAst.Extent;
9797
"RuleName" = $PSCmdlet.MyInvocation.InvocationName;
98-
"Severity" = "Strict"}
98+
"Severity" = "Information"}
9999
$results += $result
100100
}
101101
}
@@ -106,7 +106,7 @@ function Measure-RequiresRunAsAdministrator
106106
$result = [Microsoft.Windows.Powershell.ScriptAnalyzer.Generic.DiagnosticRecord]@{"Message" = $Messages.MeasureRequiresRunAsAdministrator;
107107
"Extent" = $assignmentAst.Extent;
108108
"RuleName" = $PSCmdlet.MyInvocation.InvocationName;
109-
"Severity" = "Strict"}
109+
"Severity" = "Information"}
110110
$results += $result
111111
}
112112
}
@@ -195,7 +195,7 @@ function Measure-RequiresModules
195195
$result = [Microsoft.Windows.Powershell.ScriptAnalyzer.Generic.DiagnosticRecord]@{"Message" = $Messages.MeasureRequiresModules;
196196
"Extent" = $ast.Extent;
197197
"RuleName" = $PSCmdlet.MyInvocation.InvocationName;
198-
"Severity" = "Strict"}
198+
"Severity" = "Information"}
199199
$results += $result
200200
}
201201
}
@@ -209,7 +209,7 @@ function Measure-RequiresModules
209209
$result = [Microsoft.Windows.Powershell.ScriptAnalyzer.Generic.DiagnosticRecord]@{"Message" = $Messages.MeasureRequiresModules;
210210
"Extent" = $ast.Extent;
211211
"RuleName" = $PSCmdlet.MyInvocation.InvocationName;
212-
"Severity" = "Strict"}
212+
"Severity" = "Information"}
213213
$results += $result
214214
}
215215
}
@@ -288,7 +288,7 @@ function Measure-LongClassName
288288
$result = [Microsoft.Windows.Powershell.ScriptAnalyzer.Generic.DiagnosticRecord]@{"Message" = $Messages.MeasureLongClassName;
289289
"Extent" = $sbResult.BoundParameters["TypeName"].Value.Extent;
290290
"RuleName" = $PSCmdlet.MyInvocation.InvocationName;
291-
"Severity" = "Strict"}
291+
"Severity" = "Information"}
292292
$results += $result
293293
}
294294
}
@@ -346,7 +346,7 @@ function Measure-DeprecatedWMIClass
346346
$result = [Microsoft.Windows.Powershell.ScriptAnalyzer.Generic.DiagnosticRecord]@{"Message" = $Messages.MeasureDeprecatedWMIClass;
347347
"Extent" = $StringConstantExpressionAst.Extent;
348348
"RuleName" = $PSCmdlet.MyInvocation.InvocationName;
349-
"Severity" = "Strict"}
349+
"Severity" = "Information"}
350350
$results += $result
351351
}
352352

@@ -511,7 +511,7 @@ function Measure-CurlyBracket
511511
$result = [Microsoft.Windows.Powershell.ScriptAnalyzer.Generic.DiagnosticRecord]@{"Message" = $Messages.MeasureCurlyBracket;
512512
"Extent" = $ast.Extent;
513513
"RuleName" = $PSCmdlet.MyInvocation.InvocationName;
514-
"Severity" = "Strict"}
514+
"Severity" = "Information"}
515515
$results += $result
516516
}
517517
}

Tests/Engine/GetScriptAnalyzerRule.tests.ps1

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
Import-Module -Verbose ScriptAnalyzer
1+
Import-Module -Verbose PSScriptAnalyzer
22
$sa = Get-Command Get-ScriptAnalyzerRule
33
$directory = Split-Path -Parent $MyInvocation.MyCommand.Path
44
$singularNouns = "PSUseSingularNouns"
55
$approvedVerbs = "PSUseApprovedVerbs"
6-
$unloadableModule = "PSAvoidUnloadableModule"
76
$dscIdentical = "PSDSCUseIdenticalParametersForDSC"
87

98
Describe "Test available parameters" {
@@ -45,9 +44,8 @@ Describe "Test Name parameters" {
4544
}
4645

4746
It "works with 3 names" {
48-
$rules = Get-ScriptAnalyzerRule -Name $unloadableModule, $approvedVerbs, $singularNouns
49-
$rules.Count | Should Be 3
50-
($rules | Where-Object {$_.Name -eq $unloadableModule}).Count | Should Be 1
47+
$rules = Get-ScriptAnalyzerRule -Name $approvedVerbs, $singularNouns
48+
$rules.Count | Should Be 2
5149
($rules | Where-Object {$_.Name -eq $singularNouns}).Count | Should Be 1
5250
($rules | Where-Object {$_.Name -eq $approvedVerbs}).Count | Should Be 1
5351
}

Tests/Engine/InvokeScriptAnalyzer.tests.ps1

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Import-Module ScriptAnalyzer
1+
Import-Module PSScriptAnalyzer
22
$sa = Get-Command Invoke-ScriptAnalyzer
33
$directory = Split-Path -Parent $MyInvocation.MyCommand.Path
44
$singularNouns = "PSUseSingularNouns"
@@ -26,16 +26,6 @@ Describe "Test available parameters" {
2626
}
2727
}
2828

29-
Context "LoggerPath parameters" {
30-
It "has a LoggerPath parameter" {
31-
$params.ContainsKey("LoggerPath") | Should Be $true
32-
}
33-
34-
It "accepts string array" {
35-
$params["LoggerPath"].ParameterType.FullName | Should Be "System.String[]"
36-
}
37-
}
38-
3929
Context "IncludeRule parameters" {
4030
It "has an IncludeRule parameter" {
4131
$params.ContainsKey("IncludeRule") | Should Be $true
@@ -72,21 +62,21 @@ Describe "Test Path" {
7262
}
7363

7464
Context "When given a directory" {
75-
$withoutPathWithDirectory = Invoke-ScriptAnalyzer $directory\RecursionDirectoryTest
76-
$withPathWithDirectory = Invoke-ScriptAnalyzer -Path $directory\RecursionDirectoryTest
65+
$withoutPathWithDirectory = Invoke-ScriptAnalyzer -Recurse $directory\RecursionDirectoryTest
66+
$withPathWithDirectory = Invoke-ScriptAnalyzer -Recurse -Path $directory\RecursionDirectoryTest
7767

7868
It "Has the same count as without Path parameter"{
7969
$withoutPathWithDirectory.Count -eq $withPathWithDirectory.Count | Should Be $true
8070
}
8171

82-
It "Analyzes all the file" {
72+
It "Analyzes all the files" {
8373
$globalVarsViolation = $withPathWithDirectory | Where-Object {$_.RuleName -eq "PSAvoidGlobalVars"}
8474
$clearHostViolation = $withPathWithDirectory | Where-Object {$_.RuleName -eq "PSAvoidUsingClearHost"}
8575
$writeHostViolation = $withPathWithDirectory | Where-Object {$_.RuleName -eq "PSAvoidUsingWriteHost"}
8676
Write-Output $globalVarsViolation.Count
8777
Write-Output $clearHostViolation.Count
8878
Write-Output $writeHostViolation.Count
89-
$globalVarsViolation.Count -eq 1 -and $clearHostViolation.Count -eq 1 -and $writeHostViolation.Count -eq 1 | Should Be $true
79+
$globalVarsViolation.Count -eq 1 -and $writeHostViolation.Count -eq 1 | Should Be $true
9080
}
9181

9282
}
@@ -151,12 +141,12 @@ Describe "Test Exclude And Include" {
151141
Describe "Test Severity" {
152142
Context "When used correctly" {
153143
It "works with one argument" {
154-
$errors = Invoke-ScriptAnalyzer $directory\TestScript.ps1 -Severity Strict
144+
$errors = Invoke-ScriptAnalyzer $directory\TestScript.ps1 -Severity Information
155145
$errors.Count | Should Be 0
156146
}
157147

158148
It "works with 2 arguments" {
159-
$errors = Invoke-ScriptAnalyzer $directory\TestScript.ps1 -Severity Strict, Warning
149+
$errors = Invoke-ScriptAnalyzer $directory\TestScript.ps1 -Severity Information, Warning
160150
$errors.Count | Should Be 2
161151
}
162152
}

Tests/Rules/AvoidConvertToSecureStringWithPlainText.tests.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
Import-Module ScriptAnalyzer
1+
Import-Module PSScriptAnalyzer
22
Set-Alias ctss ConvertTo-SecureString
3-
$violationMessage = "File AvoidConvertToSecureStringWithPlainText.ps1 uses ConvertTo-SecureString with plaintext. This will expose secure information. Encrypted standard strings should be used instead."
3+
$violationMessage = "File 'AvoidConvertToSecureStringWithPlainText.ps1' uses ConvertTo-SecureString with plaintext. This will expose secure information. Encrypted standard strings should be used instead."
44
$violationName = "PSAvoidUsingConvertToSecureStringWithPlainText"
55
$directory = Split-Path -Parent $MyInvocation.MyCommand.Path
66
$violations = Invoke-ScriptAnalyzer $directory\AvoidConvertToSecureStringWithPlainText.ps1 | Where-Object {$_.RuleName -eq $violationName}

Tests/Rules/AvoidDefaultTrueValueSwitchParameter.tests.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
Import-Module ScriptAnalyzer
2-
$violationMessage = "File AvoidDefaultTrueValueSwitchParameter.ps1 has a switch parameter default to true."
1+
Import-Module PSScriptAnalyzer
2+
$violationMessage = "File 'AvoidDefaultTrueValueSwitchParameter.ps1' has a switch parameter default to true."
33
$violationName = "PSAvoidDefaultValueSwitchParameter"
44
$directory = Split-Path -Parent $MyInvocation.MyCommand.Path
55
$violations = Invoke-ScriptAnalyzer $directory\AvoidDefaultTrueValueSwitchParameter.ps1 | Where-Object {$_.RuleName -eq $violationName}

Tests/Rules/AvoidEmptyCatchBlock.tests.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Import-Module ScriptAnalyzer
1+
Import-Module PSScriptAnalyzer
22
$violationMessage = "Empty catch block is used. Please use Write-Error or throw statements in catch blocks."
33
$violationName = "PSAvoidUsingEmptyCatchBlock"
44
$directory = Split-Path -Parent $MyInvocation.MyCommand.Path

Tests/Rules/AvoidGlobalOrUnitializedVars.tests.ps1

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
Import-Module ScriptAnalyzer
1+
Import-Module PSScriptAnalyzer
22
$globalMessage = "Found global variable 'Global:1'."
33
$globalName = "PSAvoidGlobalVars"
44
$nonInitializedName = "PSAvoidUninitializedVariable"
5-
$nonInitializedMessage = "Variable a is not initialized. Non-global variables must be initialized. To fix a violation of this rule, please initialize non-global variables."
5+
$nonInitializedMessage = "Variable 'a' is not initialized. Non-global variables must be initialized. To fix a violation of this rule, please initialize non-global variables."
66
$directory = Split-Path -Parent $MyInvocation.MyCommand.Path
77
$violations = Invoke-ScriptAnalyzer $directory\AvoidGlobalOrUnitializedVars.ps1
88
$globalViolations = $violations | Where-Object {$_.RuleName -eq $globalName}
@@ -18,7 +18,7 @@ Describe "AvoidGlobalVars" {
1818
}
1919

2020
It "has the correct description message" {
21-
$violations[0].Message | Should Match $violationMessage
21+
$violations[0].Message | Should Match $globalMessage
2222
}
2323
}
2424

Tests/Rules/AvoidInvokingEmptyMembers.tests.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Import-Module ScriptAnalyzer
1+
Import-Module PSScriptAnalyzer
22

33
$violationMessage = "() has non-constant members. Invoking empty members may cause bugs in the script."
44
$violationName = "PSAvoidInvokingEmptyMembers"

Tests/Rules/AvoidPositionalParameters.tests.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
Import-Module ScriptAnalyzer
2-
$violationMessage = "Cmdlet Get-Content has positional parameter. Please use named parameters instead of positional parameters when calling a command."
1+
Import-Module PSScriptAnalyzer
2+
$violationMessage = "Cmdlet 'Get-Content' has positional parameter. Please use named parameters instead of positional parameters when calling a command."
33
$violationName = "PSAvoidUsingPositionalParameters"
44
$directory = Split-Path -Parent $MyInvocation.MyCommand.Path
55
$violations = Invoke-ScriptAnalyzer $directory\AvoidPositionalParameters.ps1 | Where-Object {$_.RuleName -eq $violationName}

Tests/Rules/AvoidReservedParams.tests.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Import-Module ScriptAnalyzer
1+
Import-Module PSScriptAnalyzer
22
$violationMessage = [regex]::Escape("Verb-Files' defines the reserved common parameter 'Verbose'.")
33
$violationName = "PSReservedParams"
44
$directory = Split-Path -Parent $MyInvocation.MyCommand.Path

Tests/Rules/AvoidShouldContinueWithoutForce.tests.ps1

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
Import-Module ScriptAnalyzer
2-
$violationMessage = "Function Verb-Noun in file AvoidShouldContinueShouldProcessWithoutForce.ps1 uses ShouldContinue or ShouldProcess but does not have a boolean force parameter. The force parameter will allow users of the script to bypass ShouldContinue prompt"
3-
$violationName = "PSAvoidShouldContinueShouldProcessWithoutForce"
1+
Import-Module PSScriptAnalyzer
2+
$violationMessage = "Function 'Verb-Noun' in file 'AvoidShouldContinueWithoutForce.ps1' uses ShouldContinue but does not have a boolean force parameter. The force parameter will allow users of the script to bypass ShouldContinue prompt"
3+
$violationName = "PSAvoidShouldContinueWithoutForce"
44
$directory = Split-Path -Parent $MyInvocation.MyCommand.Path
5-
$violations = Invoke-ScriptAnalyzer $directory\AvoidShouldContinueShouldProcessWithoutForce.ps1 | Where-Object {$_.RuleName -eq $violationName}
5+
$violations = Invoke-ScriptAnalyzer $directory\AvoidShouldContinueWithoutForce.ps1 | Where-Object {$_.RuleName -eq $violationName}
66
$noViolations = Invoke-ScriptAnalyzer $directory\GoodCmdlet.ps1 | Where-Object {$_.RuleName -eq $violationName}
77

8-
Describe "AvoidShouldContinueShouldProcessWithoutForce" {
8+
Describe "AvoidShouldContinueWithoutForce" {
99
Context "When there are violations" {
10-
It "has 4 avoid ShouldContinue or ShouldProcess without boolean Force parameter violations" {
11-
$violations.Count | Should Be 4
10+
It "has 2 avoid ShouldContinue without boolean Force parameter violations" {
11+
$violations.Count | Should Be 2
1212
}
1313

1414
It "has the correct description message" {

Tests/Rules/AvoidTrapStatements.tests.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Import-Module ScriptAnalyzer
1+
Import-Module PSScriptAnalyzer
22
$violationMessage = "Trap found."
33
$violationName = "PSAvoidTrapStatement"
44
$directory = Split-Path -Parent $MyInvocation.MyCommand.Path

0 commit comments

Comments
 (0)