Skip to content

Commit 00e3be3

Browse files
committed
Merge pull request #403 from PowerShell/IncludeDefaultRulesBranch
Add Ability to run Default Rules along with Custom in the same invocation
2 parents a748d20 + aa40394 commit 00e3be3

File tree

6 files changed

+103
-64
lines changed

6 files changed

+103
-64
lines changed

Engine/Commands/GetScriptAnalyzerRuleCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ protected override void BeginProcessing()
9191
{
9292
string[] rulePaths = Helper.ProcessCustomRulePaths(customRulePath,
9393
this.SessionState, recurseCustomRulePath);
94-
ScriptAnalyzer.Instance.Initialize(this, rulePaths);
94+
ScriptAnalyzer.Instance.Initialize(this, rulePaths, null, null, null, null == rulePaths ? true : false);
9595
}
9696

9797
/// <summary>

Engine/Commands/InvokeScriptAnalyzerCommand.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,18 @@ public SwitchParameter RecurseCustomRulePath
9393
}
9494
private bool recurseCustomRulePath;
9595

96+
/// <summary>
97+
/// IncludeDefaultRules: Invoke default rules along with Custom rules
98+
/// </summary>
99+
[Parameter(Mandatory = false)]
100+
[SuppressMessage("Microsoft.Performance", "CA1819:PropertiesShouldNotReturnArrays")]
101+
public SwitchParameter IncludeDefaultRules
102+
{
103+
get { return includeDefaultRules; }
104+
set { includeDefaultRules = value; }
105+
}
106+
private bool includeDefaultRules;
107+
96108
/// <summary>
97109
/// ExcludeRule: Array of names of rules to be disabled.
98110
/// </summary>
@@ -195,6 +207,7 @@ protected override void BeginProcessing()
195207
this.includeRule,
196208
this.excludeRule,
197209
this.severity,
210+
null == rulePaths ? true : this.includeDefaultRules,
198211
this.suppressedOnly,
199212
this.configuration);
200213
}

Engine/ScriptAnalyzer.cs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,11 @@ public static ScriptAnalyzer Instance
9696
/// </summary>
9797
internal void Initialize<TCmdlet>(
9898
TCmdlet cmdlet,
99-
string[] customizedRulePath = null,
99+
string[] customizedRulePath = null,
100100
string[] includeRuleNames = null,
101101
string[] excludeRuleNames = null,
102102
string[] severity = null,
103+
bool includeDefaultRules = false,
103104
bool suppressedOnly = false,
104105
string profile = null)
105106
where TCmdlet : PSCmdlet, IOutputWriter
@@ -117,6 +118,7 @@ internal void Initialize<TCmdlet>(
117118
includeRuleNames,
118119
excludeRuleNames,
119120
severity,
121+
includeDefaultRules,
120122
suppressedOnly,
121123
profile);
122124
}
@@ -127,10 +129,11 @@ internal void Initialize<TCmdlet>(
127129
public void Initialize(
128130
Runspace runspace,
129131
IOutputWriter outputWriter,
130-
string[] customizedRulePath = null,
132+
string[] customizedRulePath = null,
131133
string[] includeRuleNames = null,
132134
string[] excludeRuleNames = null,
133135
string[] severity = null,
136+
bool includeDefaultRules = false,
134137
bool suppressedOnly = false,
135138
string profile = null)
136139
{
@@ -147,6 +150,7 @@ public void Initialize(
147150
includeRuleNames,
148151
excludeRuleNames,
149152
severity,
153+
includeDefaultRules,
150154
suppressedOnly,
151155
profile);
152156
}
@@ -308,10 +312,11 @@ private void Initialize(
308312
IOutputWriter outputWriter,
309313
PathIntrinsics path,
310314
CommandInvocationIntrinsics invokeCommand,
311-
string[] customizedRulePath,
312-
string[] includeRuleNames,
315+
string[] customizedRulePath,
316+
string[] includeRuleNames,
313317
string[] excludeRuleNames,
314318
string[] severity,
319+
bool includeDefaultRules = false,
315320
bool suppressedOnly = false,
316321
string profile = null)
317322
{
@@ -358,7 +363,7 @@ private void Initialize(
358363

359364
try
360365
{
361-
this.LoadRules(this.validationResults, invokeCommand, null == customizedRulePath ? true : false);
366+
this.LoadRules(this.validationResults, invokeCommand, includeDefaultRules);
362367
}
363368
catch (Exception ex)
364369
{
@@ -386,11 +391,11 @@ private void Initialize(
386391
{
387392
this.outputWriter.ThrowTerminatingError(
388393
new ErrorRecord(
389-
new Exception(),
394+
new Exception(),
390395
string.Format(
391-
CultureInfo.CurrentCulture,
392-
Strings.RulesNotFound),
393-
ErrorCategory.ResourceExists,
396+
CultureInfo.CurrentCulture,
397+
Strings.RulesNotFound),
398+
ErrorCategory.ResourceExists,
394399
this));
395400
}
396401

Tests/Engine/CustomizedRule.tests.ps1

Lines changed: 59 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -86,33 +86,30 @@ Describe "Test importing correct customized rules" {
8686
$customizedRulePath.Count | Should Be 1
8787
}
8888

89-
if (!$testingLibraryUsage)
90-
{
91-
It "will show the custom rule when given a rule folder path with trailing backslash" {
92-
$customizedRulePath = Get-ScriptAnalyzerRule -CustomizedRulePath $directory\samplerule\ | Where-Object {$_.RuleName -eq $measure}
93-
$customizedRulePath.Count | Should Be 1
94-
}
89+
It "will show the custom rule when given a rule folder path with trailing backslash" {
90+
$customizedRulePath = Get-ScriptAnalyzerRule -CustomizedRulePath $directory\samplerule\ | Where-Object {$_.RuleName -eq $measure}
91+
$customizedRulePath.Count | Should Be 1
92+
}
9593

96-
It "will show the custom rules when given a glob" {
97-
$customizedRulePath = Get-ScriptAnalyzerRule -CustomizedRulePath $directory\samplerule\samplerule* | Where-Object {$_.RuleName -match $measure}
98-
$customizedRulePath.Count | Should be 4
99-
}
94+
It "will show the custom rules when given a glob" {
95+
$customizedRulePath = Get-ScriptAnalyzerRule -CustomizedRulePath $directory\samplerule\samplerule* | Where-Object {$_.RuleName -match $measure}
96+
$customizedRulePath.Count | Should be 4
97+
}
10098

101-
It "will show the custom rules when given recurse switch" {
102-
$customizedRulePath = Get-ScriptAnalyzerRule -RecurseCustomRulePath -CustomizedRulePath "$directory\samplerule", "$directory\samplerule\samplerule2" | Where-Object {$_.RuleName -eq $measure}
103-
$customizedRulePath.Count | Should be 5
104-
}
99+
It "will show the custom rules when given recurse switch" {
100+
$customizedRulePath = Get-ScriptAnalyzerRule -RecurseCustomRulePath -CustomizedRulePath "$directory\samplerule", "$directory\samplerule\samplerule2" | Where-Object {$_.RuleName -eq $measure}
101+
$customizedRulePath.Count | Should be 5
102+
}
105103

106-
it "will show the custom rules when given glob with recurse switch" {
107-
$customizedRulePath = Get-ScriptAnalyzerRule -RecurseCustomRulePath -CustomizedRulePath $directory\samplerule\samplerule* | Where-Object {$_.RuleName -eq $measure}
108-
$customizedRulePath.Count | Should be 5
109-
}
110-
111-
it "will show the custom rules when given glob with recurse switch" {
112-
$customizedRulePath = Get-ScriptAnalyzerRule -RecurseCustomRulePath -CustomizedRulePath $directory\samplerule* | Where-Object {$_.RuleName -eq $measure}
113-
$customizedRulePath.Count | Should be 3
114-
}
104+
It "will show the custom rules when given glob with recurse switch" {
105+
$customizedRulePath = Get-ScriptAnalyzerRule -RecurseCustomRulePath -CustomizedRulePath $directory\samplerule\samplerule* | Where-Object {$_.RuleName -eq $measure}
106+
$customizedRulePath.Count | Should be 5
115107
}
108+
109+
It "will show the custom rules when given glob with recurse switch" {
110+
$customizedRulePath = Get-ScriptAnalyzerRule -RecurseCustomRulePath -CustomizedRulePath $directory\samplerule* | Where-Object {$_.RuleName -eq $measure}
111+
$customizedRulePath.Count | Should be 3
112+
}
116113
}
117114

118115
Context "Test Invoke-ScriptAnalyzer with customized rules" {
@@ -126,33 +123,49 @@ Describe "Test importing correct customized rules" {
126123
$customizedRulePath.Count | Should Be 1
127124
}
128125

129-
if (!$testingLibraryUsage)
126+
if (!$testingLibraryUsage)
130127
{
131-
It "will show the custom rule in the results when given a rule folder path with trailing backslash" {
132-
$customizedRulePath = Invoke-ScriptAnalyzer $directory\TestScript.ps1 -CustomizedRulePath $directory\samplerule\ | Where-Object {$_.Message -eq $message}
133-
$customizedRulePath.Count | Should Be 1
134-
}
128+
It "will show the custom rule in the results when given a rule folder path with trailing backslash" {
129+
$customizedRulePath = Invoke-ScriptAnalyzer $directory\TestScript.ps1 -CustomizedRulePath $directory\samplerule\ | Where-Object {$_.Message -eq $message}
130+
$customizedRulePath.Count | Should Be 1
131+
}
132+
133+
It "will show the custom rules when given a glob" {
134+
$customizedRulePath = Invoke-ScriptAnalyzer $directory\TestScript.ps1 -CustomizedRulePath $directory\samplerule\samplerule* | Where-Object {$_.Message -eq $message}
135+
$customizedRulePath.Count | Should be 3
136+
}
137+
138+
It "will show the custom rules when given recurse switch" {
139+
$customizedRulePath = Invoke-ScriptAnalyzer $directory\TestScript.ps1 -RecurseCustomRulePath -CustomizedRulePath $directory\samplerule | Where-Object {$_.Message -eq $message}
140+
$customizedRulePath.Count | Should be 3
141+
}
142+
143+
It "will show the custom rules when given glob with recurse switch" {
144+
$customizedRulePath = Invoke-ScriptAnalyzer $directory\TestScript.ps1 -RecurseCustomRulePath -CustomizedRulePath $directory\samplerule\samplerule* | Where-Object {$_.Message -eq $message}
145+
$customizedRulePath.Count | Should be 4
146+
}
147+
148+
It "will show the custom rules when given glob with recurse switch" {
149+
$customizedRulePath = Invoke-ScriptAnalyzer $directory\TestScript.ps1 -RecurseCustomRulePath -CustomizedRulePath $directory\samplerule* | Where-Object {$_.Message -eq $message}
150+
$customizedRulePath.Count | Should be 3
151+
}
152+
153+
It "Using IncludeDefaultRules Switch with CustomRulePath" {
154+
$customizedRulePath = Invoke-ScriptAnalyzer $directory\TestScript.ps1 -CustomRulePath $directory\samplerule\samplerule.psm1 -IncludeDefaultRules
155+
$customizedRulePath.Count | Should Be 2
156+
}
135157

136-
It "will show the custom rules when given a glob" {
137-
$customizedRulePath = Invoke-ScriptAnalyzer $directory\TestScript.ps1 -CustomizedRulePath $directory\samplerule\samplerule* | Where-Object {$_.Message -eq $message}
138-
$customizedRulePath.Count | Should be 3
139-
}
158+
It "Using IncludeDefaultRules Switch without CustomRulePath" {
159+
$customizedRulePath = Invoke-ScriptAnalyzer $directory\TestScript.ps1 -IncludeDefaultRules
160+
$customizedRulePath.Count | Should Be 1
161+
}
140162

141-
It "will show the custom rules when given recurse switch" {
142-
$customizedRulePath = Invoke-ScriptAnalyzer $directory\TestScript.ps1 -RecurseCustomRulePath -CustomizedRulePath $directory\samplerule | Where-Object {$_.Message -eq $message}
143-
$customizedRulePath.Count | Should be 3
144-
}
163+
It "Not Using IncludeDefaultRules Switch and without CustomRulePath" {
164+
$customizedRulePath = Invoke-ScriptAnalyzer $directory\TestScript.ps1
165+
$customizedRulePath.Count | Should Be 1
166+
}
167+
}
145168

146-
it "will show the custom rules when given glob with recurse switch" {
147-
$customizedRulePath = Invoke-ScriptAnalyzer $directory\TestScript.ps1 -RecurseCustomRulePath -CustomizedRulePath $directory\samplerule\samplerule* | Where-Object {$_.Message -eq $message}
148-
$customizedRulePath.Count | Should be 4
149-
}
150-
151-
it "will show the custom rules when given glob with recurse switch" {
152-
$customizedRulePath = Invoke-ScriptAnalyzer $directory\TestScript.ps1 -RecurseCustomRulePath -CustomizedRulePath $directory\samplerule* | Where-Object {$_.Message -eq $message}
153-
$customizedRulePath.Count | Should be 3
154-
}
155-
}
156169
}
157170
}
158171

Tests/Engine/GetScriptAnalyzerRule.tests.ps1

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@ Describe "Test Name parameters" {
5353
($rules | Where-Object {$_.RuleName -eq $singularNouns}).Count | Should Be 1
5454
($rules | Where-Object {$_.RuleName -eq $approvedVerbs}).Count | Should Be 1
5555
}
56+
57+
It "Get Rules with no parameters supplied" {
58+
$defaultRules = Get-ScriptAnalyzerRule
59+
$defaultRules.Count | Should be 38
60+
}
5661
}
5762

5863
Context "When used incorrectly" {

Tests/Engine/LibraryUsage.tests.ps1

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,22 +31,24 @@ function Invoke-ScriptAnalyzer {
3131
[ValidateSet("Warning", "Error", "Information", IgnoreCase = $true)]
3232
[Parameter(Mandatory = $false)]
3333
[string[]] $Severity = $null,
34-
34+
3535
[Parameter(Mandatory = $false)]
3636
[switch] $Recurse,
3737

38+
[Parameter(Mandatory = $false)]
39+
[switch] $IncludeDefaultRules,
40+
3841
[Parameter(Mandatory = $false)]
3942
[switch] $SuppressedOnly,
4043

4144
[Parameter(Mandatory = $false)]
4245
[string] $Profile = $null
43-
)
44-
# There is an inconsistency between this implementation and c# implementation of the cmdlet.
45-
# The CustomRulePath parameter here is of "string[]" type whereas in the c# implementation it is of "string" type.
46-
# If we set the CustomRulePath parameter here to "string[]", then the library usage test fails when run as an administrator.
47-
# We want to note that the library usage test doesn't fail when run as a non-admin user.
48-
# The following is the error statement when the test runs as an administrator.
49-
# Assert failed on "Initialize" with "7" argument(s): "Test failed due to terminating error: The module was expected to contain an assembly manifest. (Exception from HRESULT: 0x80131018)"
46+
)
47+
48+
if ($null -eq $CustomRulePath)
49+
{
50+
$IncludeDefaultRules = $true
51+
}
5052

5153
$scriptAnalyzer = New-Object "Microsoft.Windows.PowerShell.ScriptAnalyzer.ScriptAnalyzer";
5254
$scriptAnalyzer.Initialize(
@@ -56,6 +58,7 @@ function Invoke-ScriptAnalyzer {
5658
$IncludeRule,
5759
$ExcludeRule,
5860
$Severity,
61+
$IncludeDefaultRules.IsPresent,
5962
$SuppressedOnly.IsPresent,
6063
$Profile
6164
);

0 commit comments

Comments
 (0)