Skip to content

Commit 9ab158f

Browse files
committed
Simplify failing tests by inlining it and don't check the message as all we should really check is the rule violation
1 parent 0f562df commit 9ab158f

4 files changed

+18
-33
lines changed

Tests/Rules/AvoidPositionalParameters.ps1

-2
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,18 @@
11
Import-Module PSScriptAnalyzer
2-
$violationMessage = "Cmdlet 'Get-Command' has positional parameter. Please use named parameters instead of positional parameters when calling a command."
3-
$violationName = "PSAvoidUsingPositionalParameters"
4-
$directory = Split-Path -Parent $MyInvocation.MyCommand.Path
5-
$violations = Invoke-ScriptAnalyzer $directory\AvoidPositionalParameters.ps1 | Where-Object {$_.RuleName -eq $violationName}
6-
$noViolations = Invoke-ScriptAnalyzer $directory\AvoidPositionalParametersNoViolations.ps1 | Where-Object {$_.RuleName -eq $violationName}
7-
$noViolationsDSC = Invoke-ScriptAnalyzer -ErrorAction SilentlyContinue $directory\serviceconfigdisabled.ps1 | Where-Object {$_.RuleName -eq $violationName}
82

93
Describe "AvoidPositionalParameters" {
4+
BeforeAll {
5+
$directory = $PSScriptRoot
6+
$violationName = "PSAvoidUsingPositionalParameters"
7+
$violation = Invoke-ScriptAnalyzer -ScriptDefinition 'Get-Command "abc" 4 4.3'
8+
$noViolations = Invoke-ScriptAnalyzer $directory\AvoidPositionalParametersNoViolations.ps1 | Where-Object {$_.RuleName -eq $violationName}
9+
$noViolationsDSC = Invoke-ScriptAnalyzer -ErrorAction SilentlyContinue $directory\serviceconfigdisabled.ps1 | Where-Object {$_.RuleName -eq $violationName}
10+
}
1011
Context "When there are violations" {
1112
It "has 1 avoid positional parameters violation" {
12-
$violations.Count | Should -Be 1
13-
}
14-
15-
It "has the correct description message" {
16-
$violations[0].Message | Should -Match $violationMessage
13+
@($violation).Count | Should -Be 1
14+
$violation.RuleName | Should -Be $violationName
1715
}
18-
1916
}
2017

2118
Context "When there are no violations" {
@@ -27,4 +24,4 @@ Describe "AvoidPositionalParameters" {
2724
$noViolationsDSC.Count | Should -Be 0
2825
}
2926
}
30-
}
27+
}

Tests/Rules/UseCmdletCorrectly.ps1

-5
This file was deleted.
+8-13
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,19 @@
1-
Import-Module -Verbose PSScriptAnalyzer
2-
$violationMessage = "Cmdlet 'Write-Warning' may be used incorrectly. Please check that all mandatory parameters are supplied."
3-
$violationName = "PSUseCmdletCorrectly"
4-
$directory = Split-Path -Parent $MyInvocation.MyCommand.Path
5-
$violations = Invoke-ScriptAnalyzer $directory\UseCmdletCorrectly.ps1 | Where-Object {$_.RuleName -eq $violationName}
6-
$noViolations = Invoke-ScriptAnalyzer $directory\GoodCmdlet.ps1 | Where-Object {$_.RuleName -eq $violationName}
7-
1+
82
Describe "UseCmdletCorrectly" {
93
Context "When there are violations" {
104
It "has 1 Use Cmdlet Correctly violation" {
11-
$violations.Count | Should -Be 1
12-
}
13-
14-
It "has the correct description message" {
15-
$violations[0].Message | Should -Match $violationMessage
5+
$violationName = "PSUseCmdletCorrectly"
6+
$violation = Invoke-ScriptAnalyzer -ScriptDefinition 'Write-Warning;Write-Warning -Message "a warning"'
7+
$violation.Count | Should -Be 1
8+
$violation.RuleName | Should -Be $violationName
169
}
1710
}
1811

1912
Context "When there are no violations" {
2013
It "returns no violations" {
14+
$directory = $PSScriptRoot
15+
$noViolations = Invoke-ScriptAnalyzer $directory\GoodCmdlet.ps1
2116
$noViolations.Count | Should -Be 0
2217
}
2318
}
24-
}
19+
}

0 commit comments

Comments
 (0)