Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Engine/Commands/GetScriptAnalyzerRuleCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/// <summary>
Expand Down
13 changes: 13 additions & 0 deletions Engine/Commands/InvokeScriptAnalyzerCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,18 @@ public SwitchParameter RecurseCustomRulePath
}
private bool recurseCustomRulePath;

/// <summary>
/// IncludeDefaultRules: Invoke default rules along with Custom rules
/// </summary>
[Parameter(Mandatory = false)]
[SuppressMessage("Microsoft.Performance", "CA1819:PropertiesShouldNotReturnArrays")]
public SwitchParameter IncludeDefaultRules
{
get { return includeDefaultRules; }
set { includeDefaultRules = value; }
}
private bool includeDefaultRules;

/// <summary>
/// ExcludeRule: Array of names of rules to be disabled.
/// </summary>
Expand Down Expand Up @@ -195,6 +207,7 @@ protected override void BeginProcessing()
this.includeRule,
this.excludeRule,
this.severity,
null == rulePaths ? true : this.includeDefaultRules,
this.suppressedOnly,
this.configuration);
}
Expand Down
23 changes: 14 additions & 9 deletions Engine/ScriptAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,11 @@ public static ScriptAnalyzer Instance
/// </summary>
internal void Initialize<TCmdlet>(
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
Expand All @@ -117,6 +118,7 @@ internal void Initialize<TCmdlet>(
includeRuleNames,
excludeRuleNames,
severity,
includeDefaultRules,
suppressedOnly,
profile);
}
Expand All @@ -127,10 +129,11 @@ internal void Initialize<TCmdlet>(
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)
{
Expand All @@ -147,6 +150,7 @@ public void Initialize(
includeRuleNames,
excludeRuleNames,
severity,
includeDefaultRules,
suppressedOnly,
profile);
}
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -386,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));
}

Expand Down
105 changes: 59 additions & 46 deletions Tests/Engine/CustomizedRule.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand All @@ -126,33 +123,49 @@ Describe "Test importing correct customized rules" {
$customizedRulePath.Count | Should Be 1
}

if (!$testingLibraryUsage)
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 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 a glob" {
$customizedRulePath = Invoke-ScriptAnalyzer $directory\TestScript.ps1 -CustomizedRulePath $directory\samplerule\samplerule* | Where-Object {$_.Message -eq $message}
$customizedRulePath.Count | Should be 3
}
It "Using IncludeDefaultRules Switch without CustomRulePath" {
$customizedRulePath = Invoke-ScriptAnalyzer $directory\TestScript.ps1 -IncludeDefaultRules
$customizedRulePath.Count | Should Be 1
}

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 "Not Using IncludeDefaultRules Switch and without CustomRulePath" {
$customizedRulePath = Invoke-ScriptAnalyzer $directory\TestScript.ps1
$customizedRulePath.Count | Should Be 1
}
}

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
}
}
}
}

5 changes: 5 additions & 0 deletions Tests/Engine/GetScriptAnalyzerRule.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand Down
19 changes: 11 additions & 8 deletions Tests/Engine/LibraryUsage.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -56,6 +58,7 @@ function Invoke-ScriptAnalyzer {
$IncludeRule,
$ExcludeRule,
$Severity,
$IncludeDefaultRules.IsPresent,
$SuppressedOnly.IsPresent,
$Profile
);
Expand Down