Skip to content

Commit b2cdae3

Browse files
author
quoctruong
committed
Put test cases for disabled rule in a separate folder
1 parent cb5680a commit b2cdae3

22 files changed

+134
-127
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+
}
File renamed without changes.

Tests/Rules/AvoidUnloadableModuleOrMissingRequiredFieldInManifest.tests.ps1

Lines changed: 5 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,24 @@
11
Import-Module PSScriptAnalyzer
22
$missingMessage = "The member 'ModuleVersion' is not present in the module manifest."
33
$missingName = "PSMissingModuleManifestField"
4-
$unloadableMessage = [regex]::Escape("Cannot load the module TestBadModule that the file TestBadModule.psd1 is in.")
5-
$unloadableName = "PSAvoidUnloadableModule"
64
$directory = Split-Path -Parent $MyInvocation.MyCommand.Path
7-
$violations = Invoke-ScriptAnalyzer $directory\TestBadModule\TestBadModule.psd1
8-
$missingViolations = $violations | Where-Object {$_.RuleName -eq $missingName}
9-
$unloadableViolations = $violations | Where-Object {$_.RuleName -eq $unloadableName}
10-
$noViolations = Invoke-ScriptAnalyzer $directory\TestGoodModule\TestGoodModule.psd1
11-
$noMissingViolations = $noViolations | Where-Object {$_.RuleName -eq $missingName}
12-
$noUnloadableViolations = $noViolations | Where-Object {$_.RuleName -eq $unloadableName}
13-
14-
Describe "AvoidUnloadableModule" {
15-
Context "When there are violations" {
16-
It "has 1 unloadable module violation" {
17-
$unloadableViolations.Count | Should Be 1
18-
}
19-
20-
It "has the correct description message" {
21-
$unloadableViolations.Message | Should Match $unloadableMessage
22-
}
23-
}
24-
25-
Context "When there are no violations" {
26-
It "returns no violations" {
27-
$noUnloadableViolations.Count | Should Be 0
28-
}
29-
}
30-
}
5+
$violations = Invoke-ScriptAnalyzer $directory\TestBadModule\TestBadModule.psd1 | Where-Object {$_.RuleName -eq $missingName}
6+
$noViolations = Invoke-ScriptAnalyzer $directory\TestGoodModule\TestGoodModule.psd1 | Where-Object {$_.RuleName -eq $missingName}
317

328
Describe "MissingRequiredFieldModuleManifest" {
339
Context "When there are violations" {
3410
It "has 1 missing required field module manifest violation" {
35-
$missingViolations.Count | Should Be 1
11+
$violations.Count | Should Be 1
3612
}
3713

3814
It "has the correct description message" {
39-
$missingViolations.Message | Should Match $missingMessage
15+
$violations.Message | Should Match $missingMessage
4016
}
4117
}
4218

4319
Context "When there are no violations" {
4420
It "returns no violations" {
45-
$noMissingViolations.Count | Should Be 0
21+
$noViolations.Count | Should Be 0
4622
}
4723
}
4824
}

Tests/Rules/AvoidUsingClearHostWriteHost.tests.ps1

Lines changed: 0 additions & 50 deletions
This file was deleted.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
Import-Module PSScriptAnalyzer
2+
$reservedCharMessage = "The cmdlet 'Use-#Reserved' uses a reserved char in its name."
3+
$reservedCharName = "PSReservedCmdletChar"
4+
$directory = Split-Path -Parent $MyInvocation.MyCommand.Path
5+
$violations = Invoke-ScriptAnalyzer $directory\AvoidUsingReservedCharNames.ps1 | Where-Object {$_.RuleName -eq $reservedCharName}
6+
$noViolations = Invoke-ScriptAnalyzer $directory\GoodCmdlet.ps1 | Where-Object {$_.RuleName -eq $reservedCharName}
7+
8+
Describe "Avoid Using Reserved Char" {
9+
Context "When there are violations" {
10+
It "has 1 Reserved Char Violation" {
11+
$violations.Count | Should Be 1
12+
}
13+
14+
It "has the correct description message" {
15+
$violations[0].Message | Should Match $reservedCharMessage
16+
}
17+
}
18+
19+
Context "When there are no violations" {
20+
It "has no violations" {
21+
$noViolations.Count | Should Be 0
22+
}
23+
}
24+
}

Tests/Rules/AvoidUsingReservedCharOneCharNames.tests.ps1

Lines changed: 0 additions & 48 deletions
This file was deleted.

Tests/Rules/AvoidUsingWriteHost.ps1

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Clear-Host
2+
cls
3+
Write-Host "aaa"
4+
clear
5+
[System.Console]::Write("abcdefg");
6+
[System.Console]::WriteLine("No console.writeline plz!");
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+
$writeHostMessage = [Regex]::Escape("File 'AvoidUsingWriteHost.ps1' uses Write-Host. This is not recommended because it may not work in some hosts or there may even be no hosts at all. Use Write-Output instead.")
4+
$writeHostName = "PSAvoidUsingWriteHost"
5+
$directory = Split-Path -Parent $MyInvocation.MyCommand.Path
6+
$violations = Invoke-ScriptAnalyzer $directory\AvoidUsingWriteHost.ps1 | Where-Object {$_.RuleName -eq $writeHostName}
7+
$noViolations = Invoke-ScriptAnalyzer $directory\AvoidUsingWriteHostNoViolations.ps1 | Where-Object {$_.RuleName -eq $clearHostName}
8+
9+
Describe "AvoidUsingWriteHost" {
10+
Context "When there are violations" {
11+
It "has 3 Write-Host violations" {
12+
$violations.Count | Should Be 3
13+
}
14+
15+
It "has the correct description message for Write-Host" {
16+
$violations[0].Message | Should Match $writeHostMessage
17+
}
18+
}
19+
20+
Context "When there are no violations" {
21+
It "returns no violations" {
22+
$noViolations.Count | Should Be 0
23+
}
24+
}
25+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Write-Output "This is the correct way to write output"

0 commit comments

Comments
 (0)