From d849e11e30988b0f2cacfe2ef6b9c3863efe0c10 Mon Sep 17 00:00:00 2001 From: "Raghu Shantha [MSFT]" Date: Mon, 14 Dec 2015 16:39:37 -0800 Subject: [PATCH 1/3] Add a new switch parameter to run default rules along with custom ones --- Engine/Commands/GetScriptAnalyzerRuleCommand.cs | 2 +- Engine/Commands/InvokeScriptAnalyzerCommand.cs | 13 +++++++++++++ Engine/ScriptAnalyzer.cs | 15 ++++++++++----- 3 files changed, 24 insertions(+), 6 deletions(-) diff --git a/Engine/Commands/GetScriptAnalyzerRuleCommand.cs b/Engine/Commands/GetScriptAnalyzerRuleCommand.cs index cb38d7b55..95ed669cc 100644 --- a/Engine/Commands/GetScriptAnalyzerRuleCommand.cs +++ b/Engine/Commands/GetScriptAnalyzerRuleCommand.cs @@ -91,7 +91,7 @@ protected override void BeginProcessing() { string[] rulePaths = Helper.ProcessCustomRulePaths(customRulePath, this.SessionState, recurseCustomRulePath); - ScriptAnalyzer.Instance.Initialize(this, rulePaths); + ScriptAnalyzer.Instance.Initialize(this, rulePaths, null, null, null, null == rulePaths ? true : false); } /// diff --git a/Engine/Commands/InvokeScriptAnalyzerCommand.cs b/Engine/Commands/InvokeScriptAnalyzerCommand.cs index ada75920b..936d38925 100644 --- a/Engine/Commands/InvokeScriptAnalyzerCommand.cs +++ b/Engine/Commands/InvokeScriptAnalyzerCommand.cs @@ -93,6 +93,18 @@ public SwitchParameter RecurseCustomRulePath } private bool recurseCustomRulePath; + /// + /// IncludeDefaultRules: Invoke default rules along with Custom rules + /// + [Parameter(Mandatory = false)] + [SuppressMessage("Microsoft.Performance", "CA1819:PropertiesShouldNotReturnArrays")] + public SwitchParameter IncludeDefaultRules + { + get { return includeDefaultRules; } + set { includeDefaultRules = value; } + } + private bool includeDefaultRules; + /// /// ExcludeRule: Array of names of rules to be disabled. /// @@ -195,6 +207,7 @@ protected override void BeginProcessing() this.includeRule, this.excludeRule, this.severity, + null == rulePaths ? true : this.includeDefaultRules, this.suppressedOnly, this.configuration); } diff --git a/Engine/ScriptAnalyzer.cs b/Engine/ScriptAnalyzer.cs index c0e685c40..71a00f27c 100644 --- a/Engine/ScriptAnalyzer.cs +++ b/Engine/ScriptAnalyzer.cs @@ -96,10 +96,11 @@ public static ScriptAnalyzer Instance /// internal void Initialize( TCmdlet cmdlet, - string[] customizedRulePath = null, + string[] customizedRulePath = null, string[] includeRuleNames = null, string[] excludeRuleNames = null, string[] severity = null, + bool includeDefaultRules = false, bool suppressedOnly = false, string profile = null) where TCmdlet : PSCmdlet, IOutputWriter @@ -117,6 +118,7 @@ internal void Initialize( includeRuleNames, excludeRuleNames, severity, + includeDefaultRules, suppressedOnly, profile); } @@ -127,10 +129,11 @@ internal void Initialize( public void Initialize( Runspace runspace, IOutputWriter outputWriter, - string[] customizedRulePath = null, + string[] customizedRulePath = null, string[] includeRuleNames = null, string[] excludeRuleNames = null, string[] severity = null, + bool includeDefaultRules = false, bool suppressedOnly = false, string profile = null) { @@ -147,6 +150,7 @@ public void Initialize( includeRuleNames, excludeRuleNames, severity, + includeDefaultRules, suppressedOnly, profile); } @@ -308,10 +312,11 @@ private void Initialize( IOutputWriter outputWriter, PathIntrinsics path, CommandInvocationIntrinsics invokeCommand, - string[] customizedRulePath, - string[] includeRuleNames, + string[] customizedRulePath, + string[] includeRuleNames, string[] excludeRuleNames, string[] severity, + bool includeDefaultRules = false, bool suppressedOnly = false, string profile = null) { @@ -358,7 +363,7 @@ private void Initialize( try { - this.LoadRules(this.validationResults, invokeCommand, null == customizedRulePath ? true : false); + this.LoadRules(this.validationResults, invokeCommand, includeDefaultRules); } catch (Exception ex) { From 23b22e7c381081cc90d0f8d8519ed002254ac7df Mon Sep 17 00:00:00 2001 From: "Raghu Shantha [MSFT]" Date: Tue, 15 Dec 2015 10:08:17 -0800 Subject: [PATCH 2/3] Added Test cases, plugged Test Holes --- Tests/Engine/CustomizedRule.tests.ps1 | 100 ++++++++++--------- Tests/Engine/GetScriptAnalyzerRule.tests.ps1 | 5 + 2 files changed, 60 insertions(+), 45 deletions(-) diff --git a/Tests/Engine/CustomizedRule.tests.ps1 b/Tests/Engine/CustomizedRule.tests.ps1 index 7f7ded15c..6c22076cd 100644 --- a/Tests/Engine/CustomizedRule.tests.ps1 +++ b/Tests/Engine/CustomizedRule.tests.ps1 @@ -86,33 +86,30 @@ Describe "Test importing correct customized rules" { $customizedRulePath.Count | Should Be 1 } - if (!$testingLibraryUsage) - { - It "will show the custom rule when given a rule folder path with trailing backslash" { - $customizedRulePath = Get-ScriptAnalyzerRule -CustomizedRulePath $directory\samplerule\ | Where-Object {$_.RuleName -eq $measure} - $customizedRulePath.Count | Should Be 1 - } + It "will show the custom rule when given a rule folder path with trailing backslash" { + $customizedRulePath = Get-ScriptAnalyzerRule -CustomizedRulePath $directory\samplerule\ | Where-Object {$_.RuleName -eq $measure} + $customizedRulePath.Count | Should Be 1 + } - It "will show the custom rules when given a glob" { - $customizedRulePath = Get-ScriptAnalyzerRule -CustomizedRulePath $directory\samplerule\samplerule* | Where-Object {$_.RuleName -match $measure} - $customizedRulePath.Count | Should be 4 - } + It "will show the custom rules when given a glob" { + $customizedRulePath = Get-ScriptAnalyzerRule -CustomizedRulePath $directory\samplerule\samplerule* | Where-Object {$_.RuleName -match $measure} + $customizedRulePath.Count | Should be 4 + } - It "will show the custom rules when given recurse switch" { - $customizedRulePath = Get-ScriptAnalyzerRule -RecurseCustomRulePath -CustomizedRulePath "$directory\samplerule", "$directory\samplerule\samplerule2" | Where-Object {$_.RuleName -eq $measure} - $customizedRulePath.Count | Should be 5 - } + It "will show the custom rules when given recurse switch" { + $customizedRulePath = Get-ScriptAnalyzerRule -RecurseCustomRulePath -CustomizedRulePath "$directory\samplerule", "$directory\samplerule\samplerule2" | Where-Object {$_.RuleName -eq $measure} + $customizedRulePath.Count | Should be 5 + } - it "will show the custom rules when given glob with recurse switch" { - $customizedRulePath = Get-ScriptAnalyzerRule -RecurseCustomRulePath -CustomizedRulePath $directory\samplerule\samplerule* | Where-Object {$_.RuleName -eq $measure} - $customizedRulePath.Count | Should be 5 - } - - it "will show the custom rules when given glob with recurse switch" { - $customizedRulePath = Get-ScriptAnalyzerRule -RecurseCustomRulePath -CustomizedRulePath $directory\samplerule* | Where-Object {$_.RuleName -eq $measure} - $customizedRulePath.Count | Should be 3 - } + It "will show the custom rules when given glob with recurse switch" { + $customizedRulePath = Get-ScriptAnalyzerRule -RecurseCustomRulePath -CustomizedRulePath $directory\samplerule\samplerule* | Where-Object {$_.RuleName -eq $measure} + $customizedRulePath.Count | Should be 5 } + + It "will show the custom rules when given glob with recurse switch" { + $customizedRulePath = Get-ScriptAnalyzerRule -RecurseCustomRulePath -CustomizedRulePath $directory\samplerule* | Where-Object {$_.RuleName -eq $measure} + $customizedRulePath.Count | Should be 3 + } } Context "Test Invoke-ScriptAnalyzer with customized rules" { @@ -126,33 +123,46 @@ Describe "Test importing correct customized rules" { $customizedRulePath.Count | Should Be 1 } - if (!$testingLibraryUsage) - { - It "will show the custom rule in the results when given a rule folder path with trailing backslash" { - $customizedRulePath = Invoke-ScriptAnalyzer $directory\TestScript.ps1 -CustomizedRulePath $directory\samplerule\ | Where-Object {$_.Message -eq $message} - $customizedRulePath.Count | Should Be 1 - } + It "will show the custom rule in the results when given a rule folder path with trailing backslash" { + $customizedRulePath = Invoke-ScriptAnalyzer $directory\TestScript.ps1 -CustomizedRulePath $directory\samplerule\ | Where-Object {$_.Message -eq $message} + $customizedRulePath.Count | Should Be 1 + } - It "will show the custom rules when given a glob" { - $customizedRulePath = Invoke-ScriptAnalyzer $directory\TestScript.ps1 -CustomizedRulePath $directory\samplerule\samplerule* | Where-Object {$_.Message -eq $message} - $customizedRulePath.Count | Should be 3 - } + It "will show the custom rules when given a glob" { + $customizedRulePath = Invoke-ScriptAnalyzer $directory\TestScript.ps1 -CustomizedRulePath $directory\samplerule\samplerule* | Where-Object {$_.Message -eq $message} + $customizedRulePath.Count | Should be 3 + } - It "will show the custom rules when given recurse switch" { - $customizedRulePath = Invoke-ScriptAnalyzer $directory\TestScript.ps1 -RecurseCustomRulePath -CustomizedRulePath $directory\samplerule | Where-Object {$_.Message -eq $message} - $customizedRulePath.Count | Should be 3 - } + It "will show the custom rules when given recurse switch" { + $customizedRulePath = Invoke-ScriptAnalyzer $directory\TestScript.ps1 -RecurseCustomRulePath -CustomizedRulePath $directory\samplerule | Where-Object {$_.Message -eq $message} + $customizedRulePath.Count | Should be 3 + } - it "will show the custom rules when given glob with recurse switch" { - $customizedRulePath = Invoke-ScriptAnalyzer $directory\TestScript.ps1 -RecurseCustomRulePath -CustomizedRulePath $directory\samplerule\samplerule* | Where-Object {$_.Message -eq $message} - $customizedRulePath.Count | Should be 4 - } + It "will show the custom rules when given glob with recurse switch" { + $customizedRulePath = Invoke-ScriptAnalyzer $directory\TestScript.ps1 -RecurseCustomRulePath -CustomizedRulePath $directory\samplerule\samplerule* | Where-Object {$_.Message -eq $message} + $customizedRulePath.Count | Should be 4 + } - it "will show the custom rules when given glob with recurse switch" { - $customizedRulePath = Invoke-ScriptAnalyzer $directory\TestScript.ps1 -RecurseCustomRulePath -CustomizedRulePath $directory\samplerule* | Where-Object {$_.Message -eq $message} - $customizedRulePath.Count | Should be 3 - } + It "will show the custom rules when given glob with recurse switch" { + $customizedRulePath = Invoke-ScriptAnalyzer $directory\TestScript.ps1 -RecurseCustomRulePath -CustomizedRulePath $directory\samplerule* | Where-Object {$_.Message -eq $message} + $customizedRulePath.Count | Should be 3 } + + It "Using IncludeDefaultRules Switch with CustomRulePath" { + $customizedRulePath = Invoke-ScriptAnalyzer $directory\TestScript.ps1 -CustomRulePath $directory\samplerule\samplerule.psm1 -IncludeDefaultRules + $customizedRulePath.Count | Should Be 2 + } + + It "Using IncludeDefaultRules Switch without CustomRulePath" { + $customizedRulePath = Invoke-ScriptAnalyzer $directory\TestScript.ps1 -IncludeDefaultRules + $customizedRulePath.Count | Should Be 1 + } + + It "Not Using IncludeDefaultRules Switch and without CustomRulePath" { + $customizedRulePath = Invoke-ScriptAnalyzer $directory\TestScript.ps1 + $customizedRulePath.Count | Should Be 1 + } + } } diff --git a/Tests/Engine/GetScriptAnalyzerRule.tests.ps1 b/Tests/Engine/GetScriptAnalyzerRule.tests.ps1 index fdc945f37..d992f5ca4 100644 --- a/Tests/Engine/GetScriptAnalyzerRule.tests.ps1 +++ b/Tests/Engine/GetScriptAnalyzerRule.tests.ps1 @@ -53,6 +53,11 @@ Describe "Test Name parameters" { ($rules | Where-Object {$_.RuleName -eq $singularNouns}).Count | Should Be 1 ($rules | Where-Object {$_.RuleName -eq $approvedVerbs}).Count | Should Be 1 } + + It "Get Rules with no parameters supplied" { + $defaultRules = Get-ScriptAnalyzerRule + $defaultRules.Count | Should be 38 + } } Context "When used incorrectly" { From aa40394257517b1495baa9210ad6b259c806f3ce Mon Sep 17 00:00:00 2001 From: "Raghu Shantha [MSFT]" Date: Tue, 15 Dec 2015 11:36:31 -0800 Subject: [PATCH 3/3] Fix failing tests --- Engine/ScriptAnalyzer.cs | 8 +-- Tests/Engine/CustomizedRule.tests.ps1 | 73 ++++++++++++++------------- Tests/Engine/LibraryUsage.tests.ps1 | 19 ++++--- 3 files changed, 53 insertions(+), 47 deletions(-) diff --git a/Engine/ScriptAnalyzer.cs b/Engine/ScriptAnalyzer.cs index 71a00f27c..e9241770d 100644 --- a/Engine/ScriptAnalyzer.cs +++ b/Engine/ScriptAnalyzer.cs @@ -391,11 +391,11 @@ private void Initialize( { this.outputWriter.ThrowTerminatingError( new ErrorRecord( - new Exception(), + new Exception(), string.Format( - CultureInfo.CurrentCulture, - Strings.RulesNotFound), - ErrorCategory.ResourceExists, + CultureInfo.CurrentCulture, + Strings.RulesNotFound), + ErrorCategory.ResourceExists, this)); } diff --git a/Tests/Engine/CustomizedRule.tests.ps1 b/Tests/Engine/CustomizedRule.tests.ps1 index 6c22076cd..6c3cb10e9 100644 --- a/Tests/Engine/CustomizedRule.tests.ps1 +++ b/Tests/Engine/CustomizedRule.tests.ps1 @@ -123,44 +123,47 @@ Describe "Test importing correct customized rules" { $customizedRulePath.Count | Should Be 1 } - It "will show the custom rule in the results when given a rule folder path with trailing backslash" { - $customizedRulePath = Invoke-ScriptAnalyzer $directory\TestScript.ps1 -CustomizedRulePath $directory\samplerule\ | Where-Object {$_.Message -eq $message} - $customizedRulePath.Count | Should Be 1 - } - - It "will show the custom rules when given a glob" { - $customizedRulePath = Invoke-ScriptAnalyzer $directory\TestScript.ps1 -CustomizedRulePath $directory\samplerule\samplerule* | Where-Object {$_.Message -eq $message} - $customizedRulePath.Count | Should be 3 - } - - It "will show the custom rules when given recurse switch" { - $customizedRulePath = Invoke-ScriptAnalyzer $directory\TestScript.ps1 -RecurseCustomRulePath -CustomizedRulePath $directory\samplerule | Where-Object {$_.Message -eq $message} - $customizedRulePath.Count | Should be 3 - } + if (!$testingLibraryUsage) + { + It "will show the custom rule in the results when given a rule folder path with trailing backslash" { + $customizedRulePath = Invoke-ScriptAnalyzer $directory\TestScript.ps1 -CustomizedRulePath $directory\samplerule\ | Where-Object {$_.Message -eq $message} + $customizedRulePath.Count | Should Be 1 + } + + It "will show the custom rules when given a glob" { + $customizedRulePath = Invoke-ScriptAnalyzer $directory\TestScript.ps1 -CustomizedRulePath $directory\samplerule\samplerule* | Where-Object {$_.Message -eq $message} + $customizedRulePath.Count | Should be 3 + } + + It "will show the custom rules when given recurse switch" { + $customizedRulePath = Invoke-ScriptAnalyzer $directory\TestScript.ps1 -RecurseCustomRulePath -CustomizedRulePath $directory\samplerule | Where-Object {$_.Message -eq $message} + $customizedRulePath.Count | Should be 3 + } - It "will show the custom rules when given glob with recurse switch" { - $customizedRulePath = Invoke-ScriptAnalyzer $directory\TestScript.ps1 -RecurseCustomRulePath -CustomizedRulePath $directory\samplerule\samplerule* | Where-Object {$_.Message -eq $message} - $customizedRulePath.Count | Should be 4 - } - - It "will show the custom rules when given glob with recurse switch" { - $customizedRulePath = Invoke-ScriptAnalyzer $directory\TestScript.ps1 -RecurseCustomRulePath -CustomizedRulePath $directory\samplerule* | Where-Object {$_.Message -eq $message} - $customizedRulePath.Count | Should be 3 - } - - It "Using IncludeDefaultRules Switch with CustomRulePath" { - $customizedRulePath = Invoke-ScriptAnalyzer $directory\TestScript.ps1 -CustomRulePath $directory\samplerule\samplerule.psm1 -IncludeDefaultRules - $customizedRulePath.Count | Should Be 2 - } + It "will show the custom rules when given glob with recurse switch" { + $customizedRulePath = Invoke-ScriptAnalyzer $directory\TestScript.ps1 -RecurseCustomRulePath -CustomizedRulePath $directory\samplerule\samplerule* | Where-Object {$_.Message -eq $message} + $customizedRulePath.Count | Should be 4 + } + + It "will show the custom rules when given glob with recurse switch" { + $customizedRulePath = Invoke-ScriptAnalyzer $directory\TestScript.ps1 -RecurseCustomRulePath -CustomizedRulePath $directory\samplerule* | Where-Object {$_.Message -eq $message} + $customizedRulePath.Count | Should be 3 + } + + It "Using IncludeDefaultRules Switch with CustomRulePath" { + $customizedRulePath = Invoke-ScriptAnalyzer $directory\TestScript.ps1 -CustomRulePath $directory\samplerule\samplerule.psm1 -IncludeDefaultRules + $customizedRulePath.Count | Should Be 2 + } - It "Using IncludeDefaultRules Switch without CustomRulePath" { - $customizedRulePath = Invoke-ScriptAnalyzer $directory\TestScript.ps1 -IncludeDefaultRules - $customizedRulePath.Count | Should Be 1 - } + It "Using IncludeDefaultRules Switch without CustomRulePath" { + $customizedRulePath = Invoke-ScriptAnalyzer $directory\TestScript.ps1 -IncludeDefaultRules + $customizedRulePath.Count | Should Be 1 + } - It "Not Using IncludeDefaultRules Switch and without CustomRulePath" { - $customizedRulePath = Invoke-ScriptAnalyzer $directory\TestScript.ps1 - $customizedRulePath.Count | Should Be 1 + It "Not Using IncludeDefaultRules Switch and without CustomRulePath" { + $customizedRulePath = Invoke-ScriptAnalyzer $directory\TestScript.ps1 + $customizedRulePath.Count | Should Be 1 + } } } diff --git a/Tests/Engine/LibraryUsage.tests.ps1 b/Tests/Engine/LibraryUsage.tests.ps1 index 41d2de3d5..432ac8cee 100644 --- a/Tests/Engine/LibraryUsage.tests.ps1 +++ b/Tests/Engine/LibraryUsage.tests.ps1 @@ -31,22 +31,24 @@ function Invoke-ScriptAnalyzer { [ValidateSet("Warning", "Error", "Information", IgnoreCase = $true)] [Parameter(Mandatory = $false)] [string[]] $Severity = $null, - + [Parameter(Mandatory = $false)] [switch] $Recurse, + [Parameter(Mandatory = $false)] + [switch] $IncludeDefaultRules, + [Parameter(Mandatory = $false)] [switch] $SuppressedOnly, [Parameter(Mandatory = $false)] [string] $Profile = $null - ) - # There is an inconsistency between this implementation and c# implementation of the cmdlet. - # The CustomRulePath parameter here is of "string[]" type whereas in the c# implementation it is of "string" type. - # If we set the CustomRulePath parameter here to "string[]", then the library usage test fails when run as an administrator. - # We want to note that the library usage test doesn't fail when run as a non-admin user. - # The following is the error statement when the test runs as an administrator. - # 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)" + ) + + if ($null -eq $CustomRulePath) + { + $IncludeDefaultRules = $true + } $scriptAnalyzer = New-Object "Microsoft.Windows.PowerShell.ScriptAnalyzer.ScriptAnalyzer"; $scriptAnalyzer.Initialize( @@ -56,6 +58,7 @@ function Invoke-ScriptAnalyzer { $IncludeRule, $ExcludeRule, $Severity, + $IncludeDefaultRules.IsPresent, $SuppressedOnly.IsPresent, $Profile );