forked from PowerShell/PSScriptAnalyzer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GetScriptAnalyzerRule.tests.ps1
187 lines (157 loc) · 6.82 KB
/
GetScriptAnalyzerRule.tests.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
$directory = Split-Path -Parent $MyInvocation.MyCommand.Path
$testRootDirectory = Split-Path -Parent $directory
Import-Module (Join-Path $testRootDirectory 'PSScriptAnalyzerTestHelper.psm1')
$sa = Get-Command Get-ScriptAnalyzerRule
$singularNouns = "PSUseSingularNouns" # this rule does not exist for coreclr version
$approvedVerbs = "PSUseApprovedVerbs"
$cmdletAliases = "PSAvoidUsingCmdletAliases"
$dscIdentical = "PSDSCUseIdenticalParametersForDSC"
Describe "Test available parameters" {
$params = $sa.Parameters
Context "Name parameter" {
It "has a RuleName parameter" {
$params.ContainsKey("Name") | Should -BeTrue
}
It "accepts string" {
$params["Name"].ParameterType.FullName | Should -Be "System.String[]"
}
}
Context "RuleExtension parameters" {
It "has a RuleExtension parameter" {
$params.ContainsKey("CustomRulePath") | Should -BeTrue
}
It "accepts string array" {
$params["CustomRulePath"].ParameterType.FullName | Should -Be "System.String[]"
}
It "takes CustomizedRulePath parameter as an alias of CustomRulePath parameter" {
$params.CustomRulePath.Aliases.Contains("CustomizedRulePath") | Should -BeTrue
}
}
}
Describe "Test Name parameters" {
Context "When used correctly" {
It "works with 1 name" {
$rule = Get-ScriptAnalyzerRule -Name $cmdletAliases
$rule.Count | Should -Be 1
$rule[0].RuleName | Should -Be $cmdletAliases
}
It "works for DSC Rule" {
$rule = Get-ScriptAnalyzerRule -Name $dscIdentical
$rule.Count | Should -Be 1
$rule[0].RuleName | Should -Be $dscIdentical
}
It "works with 2 names" {
$rules = Get-ScriptAnalyzerRule -Name $approvedVerbs, $cmdletAliases
$rules.Count | Should -Be 2
($rules | Where-Object {$_.RuleName -eq $cmdletAliases}).Count | Should -Be 1
($rules | Where-Object {$_.RuleName -eq $approvedVerbs}).Count | Should -Be 1
}
It "get Rules with no parameters supplied" {
$defaultRules = Get-ScriptAnalyzerRule
$expectedNumRules = 60
if ((Test-PSEditionCoreClr) -or (Test-PSVersionV3) -or (Test-PSVersionV4))
{
# for PSv3 PSAvoidGlobalAliases is not shipped because
# it uses StaticParameterBinder.BindCommand which is
# available only on PSv4 and above
# for PowerShell Core, PSUseSingularNouns is not
# shipped because it uses APIs that are not present
# in dotnet core.
$expectedNumRules--
}
$defaultRules.Count | Should -Be $expectedNumRules
}
It "is a positional parameter" {
$rules = Get-ScriptAnalyzerRule "PSAvoidUsingCmdletAliases"
$rules.Count | Should -Be 1
}
}
Context "When used incorrectly" {
It "1 incorrect name" {
$rule = Get-ScriptAnalyzerRule -Name "This is a wrong name"
$rule.Count | Should -Be 0
}
It "1 incorrect and 1 correct" {
$rule = Get-ScriptAnalyzerRule -Name $cmdletAliases, "This is a wrong name"
$rule.Count | Should -Be 1
$rule[0].RuleName | Should -Be $cmdletAliases
}
}
}
Describe "Test RuleExtension" {
$community = "CommunityAnalyzerRules"
$measureRequired = "Measure-RequiresModules"
Context "When used correctly" {
$expectedNumCommunityRules = 10
if ($PSVersionTable.PSVersion -ge [Version]'4.0.0')
{
$expectedNumCommunityRules = 12
}
It "with the module folder path" {
$ruleExtension = Get-ScriptAnalyzerRule -CustomizedRulePath $directory\CommunityAnalyzerRules | Where-Object {$_.SourceName -eq $community}
$ruleExtension.Count | Should -Be $expectedNumCommunityRules
}
It "with the psd1 path" {
$ruleExtension = Get-ScriptAnalyzerRule -CustomizedRulePath $directory\CommunityAnalyzerRules\CommunityAnalyzerRules.psd1 | Where-Object {$_.SourceName -eq $community}
$ruleExtension.Count | Should -Be $expectedNumCommunityRules
}
It "with the psm1 path" {
$ruleExtension = Get-ScriptAnalyzerRule -CustomizedRulePath $directory\CommunityAnalyzerRules\CommunityAnalyzerRules.psm1 | Where-Object {$_.SourceName -eq $community}
$ruleExtension.Count | Should -Be $expectedNumCommunityRules
}
It "with Name of a built-in rules" {
$ruleExtension = Get-ScriptAnalyzerRule -CustomizedRulePath $directory\CommunityAnalyzerRules\CommunityAnalyzerRules.psm1 -Name $singularNouns
$ruleExtension.Count | Should -Be 0
}
It "with Names of built-in, DSC and non-built-in rules" {
$ruleExtension = Get-ScriptAnalyzerRule -CustomizedRulePath $directory\CommunityAnalyzerRules\CommunityAnalyzerRules.psm1 -Name $singularNouns, $measureRequired, $dscIdentical
$ruleExtension.Count | Should -Be 1
($ruleExtension | Where-Object {$_.RuleName -eq $measureRequired}).Count | Should -Be 1
($ruleExtension | Where-Object {$_.RuleName -eq $singularNouns}).Count | Should -Be 0
($ruleExtension | Where-Object {$_.RuleName -eq $dscIdentical}).Count | Should -Be 0
}
}
Context "When used incorrectly" {
It "file cannot be found" {
try
{
Get-ScriptAnalyzerRule -CustomRulePath "Invalid CustomRulePath"
}
catch
{
$_.FullyQualifiedErrorId | Should -Match "PathNotFound,Microsoft.Windows.PowerShell.ScriptAnalyzer.Commands.GetScriptAnalyzerRuleCommand"
}
}
}
}
Describe "TestSeverity" {
It "filters rules based on the specified rule severity" {
$rules = Get-ScriptAnalyzerRule -Severity Error
$rules.Count | Should -Be 6
}
It "filters rules based on multiple severity inputs"{
$rules = Get-ScriptAnalyzerRule -Severity Error,Information
$rules.Count | Should -Be 16
}
It "takes lower case inputs" {
$rules = Get-ScriptAnalyzerRule -Severity error
$rules.Count | Should -Be 6
}
}
Describe "TestWildCard" {
It "filters rules based on the -Name wild card input" {
$rules = Get-ScriptAnalyzerRule -Name PSDSC*
$rules.Count | Should -Be 7
}
It "filters rules based on wild card input and severity"{
$rules = Get-ScriptAnalyzerRule -Name PSDSC* -Severity Information
$rules.Count | Should -Be 4
}
}
Describe "TestImplementingType" {
It "retrieves rule which have an implementing type" {
$rule = Get-ScriptAnalyzerRule PSPlaceCloseBrace
$type = $rule.ImplementingType
$type.BaseType.Name | Should -Be "ConfigurableRule"
}
}