Skip to content

Commit 73a1e7b

Browse files
author
Kapil Borle
committed
Update tests for PS v3 and v4 compatibility
1 parent 080e872 commit 73a1e7b

File tree

1 file changed

+75
-78
lines changed

1 file changed

+75
-78
lines changed

Tests/Engine/Settings.tests.ps1

Lines changed: 75 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
if (!(Get-Module PSScriptAnalyzer))
2-
{
3-
Import-Module PSScriptAnalyzer
1+
if (!(Get-Module PSScriptAnalyzer)) {
2+
Import-Module PSScriptAnalyzer
43
}
54

65
$directory = Split-Path $MyInvocation.MyCommand.Path
@@ -11,100 +10,98 @@ $project2Root = [System.IO.Path]::Combine($settingsTestDirectory, "Project2")
1110
Describe "Settings Precedence" {
1211
Context "settings object is explicit" {
1312
It "runs rules from the explicit setting file" {
14-
$settingsFilepath = [System.IO.Path]::Combine($project1Root, "ExplicitSettings.psd1")
15-
$violations = Invoke-ScriptAnalyzer -Path $project1Root -Settings $settingsFilepath -Recurse
16-
$violations.Count | Should Be 2
13+
$settingsFilepath = [System.IO.Path]::Combine($project1Root, "ExplicitSettings.psd1")
14+
$violations = Invoke-ScriptAnalyzer -Path $project1Root -Settings $settingsFilepath -Recurse
15+
$violations.Count | Should Be 2
1716
}
18-
}
17+
}
1918

20-
Context "settings file is implicit" {
19+
Context "settings file is implicit" {
2120
It "runs rules from the implicit setting file" {
22-
$violations = Invoke-ScriptAnalyzer -Path $project1Root -Recurse
23-
$violations.Count | Should Be 1
24-
$violations[0].RuleName | Should Be "PSAvoidUsingCmdletAliases"
21+
$violations = Invoke-ScriptAnalyzer -Path $project1Root -Recurse
22+
$violations.Count | Should Be 1
23+
$violations[0].RuleName | Should Be "PSAvoidUsingCmdletAliases"
2524
}
2625

2726
It "cannot find file if not named PSScriptAnalyzerSettings.psd1" {
28-
$violations = Invoke-ScriptAnalyzer -Path $project2Root -Recurse
29-
$violations.Count | Should Be 2
27+
$violations = Invoke-ScriptAnalyzer -Path $project2Root -Recurse
28+
$violations.Count | Should Be 2
3029
}
31-
}
30+
}
3231
}
3332

3433
Describe "Settings Class" {
35-
Context "When an empty hashtable is provided" {
36-
BeforeAll {
37-
$settings = [Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.Settings]::new(@{})
38-
}
34+
Context "When an empty hashtable is provided" {
35+
BeforeAll {
36+
$settings = New-Object -TypeName 'Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.Settings' `
37+
-ArgumentList @{}
38+
}
3939

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-
)
40+
'IncludeRules', 'ExcludeRules', 'Severity', 'RuleArguments' | ForEach-Object {
41+
It ("Should return empty {0} property" -f $_) {
42+
$settings.${$_}.Count | Should Be 0
5543
}
44+
}
45+
}
5646

57-
It "Should return an IncludeRules array with 1 element" {
58-
$settings.IncludeRules.Count | Should Be 1
59-
}
47+
Context "When a string is provided for IncludeRules in a hashtable" {
48+
BeforeAll {
49+
$ruleName = "PSAvoidCmdletAliases"
50+
$settings = New-Object -TypeName 'Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.Settings' `
51+
-ArgumentList @{ IncludeRules = $ruleName }
52+
}
6053

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-
}
54+
It "Should return an IncludeRules array with 1 element" {
55+
$settings.IncludeRules.Count | Should Be 1
56+
}
7857

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"
58+
It "Should return the rule in the IncludeRules array" {
59+
$settings.IncludeRules[0] | Should Be $ruleName
60+
}
61+
}
62+
63+
Context "When rule arguments are provided in a hashtable" {
64+
BeforeAll {
65+
$settingsHashtable = @{
66+
Rules = @{
67+
PSAvoidUsingCmdletAliases = @{
68+
WhiteList = @("cd", "cp")
69+
}
70+
}
8371
}
72+
$settings = New-Object -TypeName 'Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.Settings' `
73+
-ArgumentList $settingsHashtable
74+
}
8475

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-
}
76+
It "Should return the rule arguments" {
77+
$settings.RuleArguments["PSAvoidUsingCmdletAliases"]["WhiteList"].Count | Should Be 2
78+
$settings.RuleArguments["PSAvoidUsingCmdletAliases"]["WhiteList"][0] | Should Be "cd"
79+
$settings.RuleArguments["PSAvoidUsingCmdletAliases"]["WhiteList"][1] | Should Be "cp"
80+
}
9181

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-
}
82+
It "Should be case insesitive" {
83+
$settings.RuleArguments["psAvoidUsingCmdletAliases"]["whiteList"].Count | Should Be 2
84+
$settings.RuleArguments["psAvoidUsingCmdletAliases"]["whiteList"][0] | Should Be "cd"
85+
$settings.RuleArguments["psAvoidUsingCmdletAliases"]["whiteList"][1] | Should Be "cp"
86+
}
87+
}
9788

98-
It "Should return 2 IncludeRules" {
99-
$settings.IncludeRules.Count | Should Be 2
100-
}
89+
Context "When a settings file path is provided" {
90+
BeforeAll {
91+
$settings = New-Object -TypeName 'Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.Settings' `
92+
-ArgumentList ([System.IO.Path]::Combine($project1Root, "ExplicitSettings.psd1"))
93+
}
10194

102-
It "Should return 2 ExcludeRules" {
103-
$settings.ExcludeRules.Count | Should Be 3
104-
}
95+
It "Should return 2 IncludeRules" {
96+
$settings.IncludeRules.Count | Should Be 2
97+
}
10598

106-
It "Should return 1 rule argument" {
107-
$settings.RuleArguments.Count | Should Be 1
108-
}
109-
}
99+
It "Should return 2 ExcludeRules" {
100+
$settings.ExcludeRules.Count | Should Be 3
101+
}
102+
103+
It "Should return 1 rule argument" {
104+
$settings.RuleArguments.Count | Should Be 1
105+
}
106+
}
110107
}

0 commit comments

Comments
 (0)