Skip to content

Commit 5de5d8e

Browse files
author
Kapil Borle
committed
Add tests for settings class
1 parent 1469738 commit 5de5d8e

File tree

2 files changed

+91
-6
lines changed

2 files changed

+91
-6
lines changed

Tests/Engine/Settings.tests.ps1

Lines changed: 84 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,19 @@ if (!(Get-Module PSScriptAnalyzer))
44
}
55

66
$directory = Split-Path $MyInvocation.MyCommand.Path
7+
$settingsTestDirectory = [System.IO.Path]::Combine($directory, "SettingsTest")
8+
$project1Root = [System.IO.Path]::Combine($settingsTestDirectory, "Project1")
9+
$project2Root = [System.IO.Path]::Combine($settingsTestDirectory, "Project2")
10+
711
Describe "Settings Precedence" {
8-
$settingsTestDirectory = [System.IO.Path]::Combine($directory, "SettingsTest")
9-
$project1Root = [System.IO.Path]::Combine($settingsTestDirectory, "Project1")
10-
$project2Root = [System.IO.Path]::Combine($settingsTestDirectory, "Project2")
1112
Context "settings object is explicit" {
1213
It "runs rules from the explicit setting file" {
1314
$settingsFilepath = [System.IO.Path]::Combine($project1Root, "ExplicitSettings.psd1")
1415
$violations = Invoke-ScriptAnalyzer -Path $project1Root -Settings $settingsFilepath -Recurse
15-
$violations.Count | Should Be 1
16-
$violations[0].RuleName | Should Be "PSAvoidUsingWriteHost"
16+
$violations.Count | Should Be 2
1717
}
1818
}
19+
1920
Context "settings file is implicit" {
2021
It "runs rules from the implicit setting file" {
2122
$violations = Invoke-ScriptAnalyzer -Path $project1Root -Recurse
@@ -28,4 +29,82 @@ Describe "Settings Precedence" {
2829
$violations.Count | Should Be 2
2930
}
3031
}
32+
}
33+
34+
Describe "Settings Class" {
35+
Context "When an empty hashtable is provided" {
36+
BeforeAll {
37+
$settings = [Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.Settings]::new(@{})
38+
}
39+
40+
'IncludeRules', 'ExcludeRules', 'Severity', 'RuleArguments' | ForEach-Object {
41+
It ("Should return empty {0} property" -f $_) {
42+
$settings.${$_}.Count | Should Be 0
43+
}
44+
}
45+
}
46+
47+
Context "When a string is provided for IncludeRules in a hashtable" {
48+
BeforeAll {
49+
$ruleName = "PSAvoidCmdletAliases"
50+
$settings = [Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.Settings]::new(
51+
@{
52+
IncludeRules = $ruleName
53+
}
54+
)
55+
}
56+
57+
It "Should return an IncludeRules array with 1 element" {
58+
$settings.IncludeRules.Count | Should Be 1
59+
}
60+
61+
It "Should return the rule in the IncludeRules array" {
62+
$settings.IncludeRules[0] | Should Be $ruleName
63+
}
64+
}
65+
66+
Context "When rule arguments are provided in a hashtable" {
67+
BeforeAll {
68+
$settings = [Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.Settings]::new(
69+
@{
70+
Rules = @{
71+
PSAvoidUsingCmdletAliases = @{
72+
WhiteList = @("cd", "cp")
73+
}
74+
}
75+
}
76+
)
77+
}
78+
79+
It "Should return the rule arguments" {
80+
$settings.RuleArguments["PSAvoidUsingCmdletAliases"]["WhiteList"].Count | Should Be 2
81+
$settings.RuleArguments["PSAvoidUsingCmdletAliases"]["WhiteList"][0] | Should Be "cd"
82+
$settings.RuleArguments["PSAvoidUsingCmdletAliases"]["WhiteList"][1] | Should Be "cp"
83+
}
84+
85+
It "Should be case insesitive" {
86+
$settings.RuleArguments["psAvoidUsingCmdletAliases"]["whiteList"].Count | Should Be 2
87+
$settings.RuleArguments["psAvoidUsingCmdletAliases"]["whiteList"][0] | Should Be "cd"
88+
$settings.RuleArguments["psAvoidUsingCmdletAliases"]["whiteList"][1] | Should Be "cp"
89+
}
90+
}
91+
92+
Context "When a settings file path is provided" {
93+
BeforeAll {
94+
$type = [Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.Settings]
95+
$settings = $type::new([System.IO.Path]::Combine($project1Root, "ExplicitSettings.psd1"))
96+
}
97+
98+
It "Should return 2 IncludeRules" {
99+
$settings.IncludeRules.Count | Should Be 2
100+
}
101+
102+
It "Should return 2 ExcludeRules" {
103+
$settings.ExcludeRules.Count | Should Be 3
104+
}
105+
106+
It "Should return 1 rule argument" {
107+
$settings.RuleArguments.Count | Should Be 1
108+
}
109+
}
31110
}
Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
11
@{
2-
"IncludeRules" = @("PSAvoidUsingWriteHost")
2+
"IncludeRules" = @("PSAvoidUsingCmdletAliases", "PSAvoidUsingWriteHost")
3+
"ExcludeRules" = @("PSShouldProcess", "PSAvoidUsingWMICmdlet", "PSUseCmdletCorrectly")
4+
"rules" = @{
5+
PSAvoidUsingCmdletAliases = @{
6+
WhiteList = @("cd", "cp")
7+
}
8+
}
39
}

0 commit comments

Comments
 (0)