Skip to content

Commit dfb70f6

Browse files
committed
Exclude Default RuleSet when using with CustomRule feature
1 parent 710b43b commit dfb70f6

File tree

5 files changed

+33
-25
lines changed

5 files changed

+33
-25
lines changed

Engine/ScriptAnalyzer.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ private void Initialize(
350350

351351
try
352352
{
353-
this.LoadRules(this.validationResults, invokeCommand);
353+
this.LoadRules(this.validationResults, invokeCommand, null == customizedRulePath ? true : false);
354354
}
355355
catch (Exception ex)
356356
{
@@ -418,7 +418,7 @@ private List<string> GetValidCustomRulePaths(string[] customizedRulePath, PathIn
418418
return paths;
419419
}
420420

421-
private void LoadRules(Dictionary<string, List<string>> result, CommandInvocationIntrinsics invokeCommand)
421+
private void LoadRules(Dictionary<string, List<string>> result, CommandInvocationIntrinsics invokeCommand, bool loadBuiltInRules)
422422
{
423423
List<string> paths = new List<string>();
424424

@@ -472,6 +472,11 @@ private void LoadRules(Dictionary<string, List<string>> result, CommandInvocatio
472472
}
473473
}
474474

475+
if (!loadBuiltInRules)
476+
{
477+
this.ScriptRules = null;
478+
}
479+
475480
// Gets external rules.
476481
if (result.ContainsKey("ValidModPaths") && result["ValidModPaths"].Count > 0)
477482
{

Engine/Strings.Designer.cs

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Engine/Strings.resx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@
160160
<value>RULE_ERROR</value>
161161
</data>
162162
<data name="RulesNotFound" xml:space="preserve">
163-
<value>Cannot find analyzer rules.</value>
163+
<value>Cannot find ScriptAnalyzer rules in the specified path</value>
164164
</data>
165165
<data name="RuleSuppressionErrorFormat" xml:space="preserve">
166166
<value>Suppression Message Attribute error at line {0} in {1} : {2}</value>

Tests/Engine/GetScriptAnalyzerRule.tests.ps1

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -91,24 +91,28 @@ Describe "Test RuleExtension" {
9191

9292
It "with Name of a built-in rules" {
9393
$ruleExtension = Get-ScriptAnalyzerRule -CustomizedRulePath $directory\CommunityAnalyzerRules\CommunityAnalyzerRules.psm1 -Name $singularNouns
94-
$ruleExtension.Count | Should Be 1
95-
$ruleExtension[0].RuleName | Should Be $singularNouns
94+
$ruleExtension.Count | Should Be 0
9695
}
9796

9897
It "with Names of built-in, DSC and non-built-in rules" {
9998
$ruleExtension = Get-ScriptAnalyzerRule -CustomizedRulePath $directory\CommunityAnalyzerRules\CommunityAnalyzerRules.psm1 -Name $singularNouns, $measureRequired, $dscIdentical
100-
$ruleExtension.Count | Should be 3
99+
$ruleExtension.Count | Should be 1
101100
($ruleExtension | Where-Object {$_.RuleName -eq $measureRequired}).Count | Should Be 1
102-
($ruleExtension | Where-Object {$_.RuleName -eq $singularNouns}).Count | Should Be 1
103-
($ruleExtension | Where-Object {$_.RuleName -eq $dscIdentical}).Count | Should Be 1
101+
($ruleExtension | Where-Object {$_.RuleName -eq $singularNouns}).Count | Should Be 0
102+
($ruleExtension | Where-Object {$_.RuleName -eq $dscIdentical}).Count | Should Be 0
104103
}
105104
}
106105

107106
Context "When used incorrectly" {
108107
It "file cannot be found" {
109-
$wrongFile = Get-ScriptAnalyzerRule -CustomizedRulePath "This is a wrong rule" 3>&1
110-
($wrongFile | Select-Object -First 1) | Should Match "Cannot find rule extension 'This is a wrong rule'."
111-
($wrongFile | Where-Object {$_.RuleName -eq $singularNouns}).Count | Should Be 1
108+
try
109+
{
110+
Get-ScriptAnalyzerRule -CustomizedRulePath "Invalid CustomRulePath"
111+
}
112+
catch
113+
{
114+
$Error[0].FullyQualifiedErrorId | should match "Cannot find ScriptAnalyzer rules in the specified path,Microsoft.Windows.PowerShell.ScriptAnalyzer.Commands.GetScriptAnalyzerRuleCommand"
115+
}
112116
}
113117

114118
}

Tests/Engine/InvokeScriptAnalyzer.tests.ps1

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -318,19 +318,18 @@ Describe "Test CustomizedRulePath" {
318318
}
319319

320320
Context "When used incorrectly" {
321-
It "file cannot be found" {
322-
$wrongRule = Invoke-ScriptAnalyzer $directory\TestScript.ps1 -CustomizedRulePath "This is a wrong rule" 3>&1 | Select-Object -First 1
323-
324-
if ($testingLibraryUsage)
325-
{
326-
# Special case for library usage testing: warning output written
327-
# with PSHost.UI.WriteWarningLine does not get redirected correctly
328-
# so we can't use this approach for checking the warning message.
329-
# Instead, reach into the test IOutputWriter implementation to find it.
330-
$wrongRule = $testOutputWriter.MostRecentWarningMessage
331-
}
332-
333-
$wrongRule | Should Match "Cannot find rule extension 'This is a wrong rule'."
321+
It "file cannot be found" {
322+
try
323+
{
324+
Invoke-ScriptAnalyzer $directory\TestScript.ps1 -CustomRulePath "Invalid CustomRulePath"
325+
}
326+
catch
327+
{
328+
if (-not $testingLibraryUsage)
329+
{
330+
$Error[0].FullyQualifiedErrorId | should match "Cannot find ScriptAnalyzer rules in the specified path,Microsoft.Windows.PowerShell.ScriptAnalyzer.Commands.InvokeScriptAnalyzerCommand"
331+
}
332+
}
334333
}
335334
}
336335
}

0 commit comments

Comments
 (0)