Skip to content

Fix test failures #28

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 15, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions Tests/Disabled Rules/AvoidOneChar.tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
Import-Module PSScriptAnalyzer
$oneCharMessage = "The cmdlet name O only has one character."
$oneCharName = "PSOneChar"
$directory = Split-Path -Parent $MyInvocation.MyCommand.Path
$invoke = Invoke-ScriptAnalyzer $directory\AvoidUsingReservedCharOneCharNames.ps1 | Where-Object {$_.RuleName -eq $oneCharName}
$noViolations = Invoke-ScriptAnalyzer $directory\GoodCmdlet.ps1 | Where-Object {$_.RuleName -eq $oneCharName}

Describe "Avoid Using One Char" {
Context "When there are violations" {
It "has 1 One Char Violation" {
$oneCharViolations.Count | Should Be 1
}

It "has the correct description message" {
$oneCharViolations[0].Message | Should Match $oneCharMessage
}
}

Context "When there are no violations" {
It "has no violations" {
$noReservedCharViolations.Count | Should Be 0
}
}
}
24 changes: 24 additions & 0 deletions Tests/Disabled Rules/AvoidUnloadableModule.tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
Import-Module PSScriptAnalyzer
$unloadableMessage = [regex]::Escape("Cannot load the module TestBadModule that the file TestBadModule.psd1 is in.")
$unloadableName = "PSAvoidUnloadableModule"
$directory = Split-Path -Parent $MyInvocation.MyCommand.Path
$violations = Invoke-ScriptAnalyzer $directory\TestBadModule\TestBadModule.psd1 | Where-Object {$_.RuleName -eq $unloadableName}
$noViolations = Invoke-ScriptAnalyzer $directory\TestGoodModule\TestGoodModule.psd1 | Where-Object {$_.RuleName -eq $unloadableName}

Describe "AvoidUnloadableModule" {
Context "When there are violations" {
It "has 1 unloadable module violation" {
$violations.Count | Should Be 1
}

It "has the correct description message" {
$violations.Message | Should Match $unloadableMessage
}
}

Context "When there are no violations" {
It "returns no violations" {
$noViolations.Count | Should Be 0
}
}
}
25 changes: 25 additions & 0 deletions Tests/Disabled Rules/AvoidUsingClearHost.tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
Import-Module PSScriptAnalyzer
Set-Alias ctss ConvertTo-SecureString
$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."
$clearHostName = "PSAvoidUsingClearHost"
$directory = Split-Path -Parent $MyInvocation.MyCommand.Path
$violations = Invoke-ScriptAnalyzer $directory\AvoidUsingClearHost.ps1 | Where-Object {$_.RuleName -eq $clearHostName}
$noViolations = Invoke-ScriptAnalyzer $directory\AvoidUsingClearHostNoViolations.ps1 | Where-Object {$_.RuleName -eq $writeHostName}

Describe "AvoidUsingClearHost" {
Context "When there are violations" {
It "has 3 Clear-Host violations" {
$violations.Count | Should Be 3
}

It "has the correct description message for Clear-Host" {
$violations[0].Message | Should Match $clearHostMessage
}
}

Context "When there are no violations" {
It "returns no violations" {
$noViolations.Count | Should Be 0
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Import-Module ScriptAnalyzer
Import-Module PSScriptAnalyzer
$violationMessage = @'
The file path "D:\\Code" of AvoidUsingFilePath.ps1 is rooted. This should be avoided if AvoidUsingFilePath.ps1 is published online
'@
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Import-Module -Verbose ScriptAnalyzer
Import-Module -Verbose PSScriptAnalyzer
$violationMessage = "Type Stre is not found"
$violationName = "PSTypeNotFound"
$directory = Split-Path -Parent $MyInvocation.MyCommand.Path
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Import-Module -Verbose ScriptAnalyzer
Import-Module -Verbose PSScriptAnalyzer
$violationMessage = [regex]::Escape('Specify type at the assignment of variable $test')
$violationName = "PSUseTypeAtVariableAssignment"
$directory = Split-Path -Parent $MyInvocation.MyCommand.Path
Expand Down
14 changes: 7 additions & 7 deletions Tests/Engine/CommunityAnalyzerRules/CommunityAnalyzerRules.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ function Measure-RequiresRunAsAdministrator
$result = [Microsoft.Windows.Powershell.ScriptAnalyzer.Generic.DiagnosticRecord]@{"Message" = $Messages.MeasureRequiresRunAsAdministrator;
"Extent" = $assignmentAst.Extent;
"RuleName" = $PSCmdlet.MyInvocation.InvocationName;
"Severity" = "Strict"}
"Severity" = "Information"}
$results += $result
}
}
Expand All @@ -106,7 +106,7 @@ function Measure-RequiresRunAsAdministrator
$result = [Microsoft.Windows.Powershell.ScriptAnalyzer.Generic.DiagnosticRecord]@{"Message" = $Messages.MeasureRequiresRunAsAdministrator;
"Extent" = $assignmentAst.Extent;
"RuleName" = $PSCmdlet.MyInvocation.InvocationName;
"Severity" = "Strict"}
"Severity" = "Information"}
$results += $result
}
}
Expand Down Expand Up @@ -195,7 +195,7 @@ function Measure-RequiresModules
$result = [Microsoft.Windows.Powershell.ScriptAnalyzer.Generic.DiagnosticRecord]@{"Message" = $Messages.MeasureRequiresModules;
"Extent" = $ast.Extent;
"RuleName" = $PSCmdlet.MyInvocation.InvocationName;
"Severity" = "Strict"}
"Severity" = "Information"}
$results += $result
}
}
Expand All @@ -209,7 +209,7 @@ function Measure-RequiresModules
$result = [Microsoft.Windows.Powershell.ScriptAnalyzer.Generic.DiagnosticRecord]@{"Message" = $Messages.MeasureRequiresModules;
"Extent" = $ast.Extent;
"RuleName" = $PSCmdlet.MyInvocation.InvocationName;
"Severity" = "Strict"}
"Severity" = "Information"}
$results += $result
}
}
Expand Down Expand Up @@ -288,7 +288,7 @@ function Measure-LongClassName
$result = [Microsoft.Windows.Powershell.ScriptAnalyzer.Generic.DiagnosticRecord]@{"Message" = $Messages.MeasureLongClassName;
"Extent" = $sbResult.BoundParameters["TypeName"].Value.Extent;
"RuleName" = $PSCmdlet.MyInvocation.InvocationName;
"Severity" = "Strict"}
"Severity" = "Information"}
$results += $result
}
}
Expand Down Expand Up @@ -346,7 +346,7 @@ function Measure-DeprecatedWMIClass
$result = [Microsoft.Windows.Powershell.ScriptAnalyzer.Generic.DiagnosticRecord]@{"Message" = $Messages.MeasureDeprecatedWMIClass;
"Extent" = $StringConstantExpressionAst.Extent;
"RuleName" = $PSCmdlet.MyInvocation.InvocationName;
"Severity" = "Strict"}
"Severity" = "Information"}
$results += $result
}

Expand Down Expand Up @@ -511,7 +511,7 @@ function Measure-CurlyBracket
$result = [Microsoft.Windows.Powershell.ScriptAnalyzer.Generic.DiagnosticRecord]@{"Message" = $Messages.MeasureCurlyBracket;
"Extent" = $ast.Extent;
"RuleName" = $PSCmdlet.MyInvocation.InvocationName;
"Severity" = "Strict"}
"Severity" = "Information"}
$results += $result
}
}
Expand Down
8 changes: 3 additions & 5 deletions Tests/Engine/GetScriptAnalyzerRule.tests.ps1
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
Import-Module -Verbose ScriptAnalyzer
Import-Module -Verbose PSScriptAnalyzer
$sa = Get-Command Get-ScriptAnalyzerRule
$directory = Split-Path -Parent $MyInvocation.MyCommand.Path
$singularNouns = "PSUseSingularNouns"
$approvedVerbs = "PSUseApprovedVerbs"
$unloadableModule = "PSAvoidUnloadableModule"
$dscIdentical = "PSDSCUseIdenticalParametersForDSC"

Describe "Test available parameters" {
Expand Down Expand Up @@ -45,9 +44,8 @@ Describe "Test Name parameters" {
}

It "works with 3 names" {
$rules = Get-ScriptAnalyzerRule -Name $unloadableModule, $approvedVerbs, $singularNouns
$rules.Count | Should Be 3
($rules | Where-Object {$_.Name -eq $unloadableModule}).Count | Should Be 1
$rules = Get-ScriptAnalyzerRule -Name $approvedVerbs, $singularNouns
$rules.Count | Should Be 2
($rules | Where-Object {$_.Name -eq $singularNouns}).Count | Should Be 1
($rules | Where-Object {$_.Name -eq $approvedVerbs}).Count | Should Be 1
}
Expand Down
24 changes: 7 additions & 17 deletions Tests/Engine/InvokeScriptAnalyzer.tests.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Import-Module ScriptAnalyzer
Import-Module PSScriptAnalyzer
$sa = Get-Command Invoke-ScriptAnalyzer
$directory = Split-Path -Parent $MyInvocation.MyCommand.Path
$singularNouns = "PSUseSingularNouns"
Expand Down Expand Up @@ -26,16 +26,6 @@ Describe "Test available parameters" {
}
}

Context "LoggerPath parameters" {
It "has a LoggerPath parameter" {
$params.ContainsKey("LoggerPath") | Should Be $true
}

It "accepts string array" {
$params["LoggerPath"].ParameterType.FullName | Should Be "System.String[]"
}
}

Context "IncludeRule parameters" {
It "has an IncludeRule parameter" {
$params.ContainsKey("IncludeRule") | Should Be $true
Expand Down Expand Up @@ -72,21 +62,21 @@ Describe "Test Path" {
}

Context "When given a directory" {
$withoutPathWithDirectory = Invoke-ScriptAnalyzer $directory\RecursionDirectoryTest
$withPathWithDirectory = Invoke-ScriptAnalyzer -Path $directory\RecursionDirectoryTest
$withoutPathWithDirectory = Invoke-ScriptAnalyzer -Recurse $directory\RecursionDirectoryTest
$withPathWithDirectory = Invoke-ScriptAnalyzer -Recurse -Path $directory\RecursionDirectoryTest

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

It "Analyzes all the file" {
It "Analyzes all the files" {
$globalVarsViolation = $withPathWithDirectory | Where-Object {$_.RuleName -eq "PSAvoidGlobalVars"}
$clearHostViolation = $withPathWithDirectory | Where-Object {$_.RuleName -eq "PSAvoidUsingClearHost"}
$writeHostViolation = $withPathWithDirectory | Where-Object {$_.RuleName -eq "PSAvoidUsingWriteHost"}
Write-Output $globalVarsViolation.Count
Write-Output $clearHostViolation.Count
Write-Output $writeHostViolation.Count
$globalVarsViolation.Count -eq 1 -and $clearHostViolation.Count -eq 1 -and $writeHostViolation.Count -eq 1 | Should Be $true
$globalVarsViolation.Count -eq 1 -and $writeHostViolation.Count -eq 1 | Should Be $true
}

}
Expand Down Expand Up @@ -151,12 +141,12 @@ Describe "Test Exclude And Include" {
Describe "Test Severity" {
Context "When used correctly" {
It "works with one argument" {
$errors = Invoke-ScriptAnalyzer $directory\TestScript.ps1 -Severity Strict
$errors = Invoke-ScriptAnalyzer $directory\TestScript.ps1 -Severity Information
$errors.Count | Should Be 0
}

It "works with 2 arguments" {
$errors = Invoke-ScriptAnalyzer $directory\TestScript.ps1 -Severity Strict, Warning
$errors = Invoke-ScriptAnalyzer $directory\TestScript.ps1 -Severity Information, Warning
$errors.Count | Should Be 2
}
}
Expand Down
4 changes: 2 additions & 2 deletions Tests/Rules/AvoidConvertToSecureStringWithPlainText.tests.ps1
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Import-Module ScriptAnalyzer
Import-Module PSScriptAnalyzer
Set-Alias ctss ConvertTo-SecureString
$violationMessage = "File AvoidConvertToSecureStringWithPlainText.ps1 uses ConvertTo-SecureString with plaintext. This will expose secure information. Encrypted standard strings should be used instead."
$violationMessage = "File 'AvoidConvertToSecureStringWithPlainText.ps1' uses ConvertTo-SecureString with plaintext. This will expose secure information. Encrypted standard strings should be used instead."
$violationName = "PSAvoidUsingConvertToSecureStringWithPlainText"
$directory = Split-Path -Parent $MyInvocation.MyCommand.Path
$violations = Invoke-ScriptAnalyzer $directory\AvoidConvertToSecureStringWithPlainText.ps1 | Where-Object {$_.RuleName -eq $violationName}
Expand Down
4 changes: 2 additions & 2 deletions Tests/Rules/AvoidDefaultTrueValueSwitchParameter.tests.ps1
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Import-Module ScriptAnalyzer
$violationMessage = "File AvoidDefaultTrueValueSwitchParameter.ps1 has a switch parameter default to true."
Import-Module PSScriptAnalyzer
$violationMessage = "File 'AvoidDefaultTrueValueSwitchParameter.ps1' has a switch parameter default to true."
$violationName = "PSAvoidDefaultValueSwitchParameter"
$directory = Split-Path -Parent $MyInvocation.MyCommand.Path
$violations = Invoke-ScriptAnalyzer $directory\AvoidDefaultTrueValueSwitchParameter.ps1 | Where-Object {$_.RuleName -eq $violationName}
Expand Down
2 changes: 1 addition & 1 deletion Tests/Rules/AvoidEmptyCatchBlock.tests.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Import-Module ScriptAnalyzer
Import-Module PSScriptAnalyzer
$violationMessage = "Empty catch block is used. Please use Write-Error or throw statements in catch blocks."
$violationName = "PSAvoidUsingEmptyCatchBlock"
$directory = Split-Path -Parent $MyInvocation.MyCommand.Path
Expand Down
6 changes: 3 additions & 3 deletions Tests/Rules/AvoidGlobalOrUnitializedVars.tests.ps1
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Import-Module ScriptAnalyzer
Import-Module PSScriptAnalyzer
$globalMessage = "Found global variable 'Global:1'."
$globalName = "PSAvoidGlobalVars"
$nonInitializedName = "PSAvoidUninitializedVariable"
$nonInitializedMessage = "Variable a is not initialized. Non-global variables must be initialized. To fix a violation of this rule, please initialize non-global variables."
$nonInitializedMessage = "Variable 'a' is not initialized. Non-global variables must be initialized. To fix a violation of this rule, please initialize non-global variables."
$directory = Split-Path -Parent $MyInvocation.MyCommand.Path
$violations = Invoke-ScriptAnalyzer $directory\AvoidGlobalOrUnitializedVars.ps1
$globalViolations = $violations | Where-Object {$_.RuleName -eq $globalName}
Expand All @@ -18,7 +18,7 @@ Describe "AvoidGlobalVars" {
}

It "has the correct description message" {
$violations[0].Message | Should Match $violationMessage
$violations[0].Message | Should Match $globalMessage
}
}

Expand Down
2 changes: 1 addition & 1 deletion Tests/Rules/AvoidInvokingEmptyMembers.tests.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Import-Module ScriptAnalyzer
Import-Module PSScriptAnalyzer

$violationMessage = "() has non-constant members. Invoking empty members may cause bugs in the script."
$violationName = "PSAvoidInvokingEmptyMembers"
Expand Down
4 changes: 2 additions & 2 deletions Tests/Rules/AvoidPositionalParameters.tests.ps1
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Import-Module ScriptAnalyzer
$violationMessage = "Cmdlet Get-Content has positional parameter. Please use named parameters instead of positional parameters when calling a command."
Import-Module PSScriptAnalyzer
$violationMessage = "Cmdlet 'Get-Content' has positional parameter. Please use named parameters instead of positional parameters when calling a command."
$violationName = "PSAvoidUsingPositionalParameters"
$directory = Split-Path -Parent $MyInvocation.MyCommand.Path
$violations = Invoke-ScriptAnalyzer $directory\AvoidPositionalParameters.ps1 | Where-Object {$_.RuleName -eq $violationName}
Expand Down
2 changes: 1 addition & 1 deletion Tests/Rules/AvoidReservedParams.tests.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Import-Module ScriptAnalyzer
Import-Module PSScriptAnalyzer
$violationMessage = [regex]::Escape("Verb-Files' defines the reserved common parameter 'Verbose'.")
$violationName = "PSReservedParams"
$directory = Split-Path -Parent $MyInvocation.MyCommand.Path
Expand Down
14 changes: 7 additions & 7 deletions Tests/Rules/AvoidShouldContinueWithoutForce.tests.ps1
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
Import-Module ScriptAnalyzer
$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"
$violationName = "PSAvoidShouldContinueShouldProcessWithoutForce"
Import-Module PSScriptAnalyzer
$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"
$violationName = "PSAvoidShouldContinueWithoutForce"
$directory = Split-Path -Parent $MyInvocation.MyCommand.Path
$violations = Invoke-ScriptAnalyzer $directory\AvoidShouldContinueShouldProcessWithoutForce.ps1 | Where-Object {$_.RuleName -eq $violationName}
$violations = Invoke-ScriptAnalyzer $directory\AvoidShouldContinueWithoutForce.ps1 | Where-Object {$_.RuleName -eq $violationName}
$noViolations = Invoke-ScriptAnalyzer $directory\GoodCmdlet.ps1 | Where-Object {$_.RuleName -eq $violationName}

Describe "AvoidShouldContinueShouldProcessWithoutForce" {
Describe "AvoidShouldContinueWithoutForce" {
Context "When there are violations" {
It "has 4 avoid ShouldContinue or ShouldProcess without boolean Force parameter violations" {
$violations.Count | Should Be 4
It "has 2 avoid ShouldContinue without boolean Force parameter violations" {
$violations.Count | Should Be 2
}

It "has the correct description message" {
Expand Down
2 changes: 1 addition & 1 deletion Tests/Rules/AvoidTrapStatements.tests.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Import-Module ScriptAnalyzer
Import-Module PSScriptAnalyzer
$violationMessage = "Trap found."
$violationName = "PSAvoidTrapStatement"
$directory = Split-Path -Parent $MyInvocation.MyCommand.Path
Expand Down
Loading