Skip to content

Commit a748d20

Browse files
committed
Merge pull request #402 from PowerShell/CustomRulePathFixBranch
Add ability to supply collection of custom rule paths
2 parents e284305 + 3ba1596 commit a748d20

File tree

6 files changed

+43
-37
lines changed

6 files changed

+43
-37
lines changed

Engine/Commands/GetScriptAnalyzerRuleCommand.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,12 @@ public class GetScriptAnalyzerRuleCommand : PSCmdlet, IOutputWriter
3434
[ValidateNotNullOrEmpty]
3535
[SuppressMessage("Microsoft.Performance", "CA1819:PropertiesShouldNotReturnArrays")]
3636
[Alias("CustomizedRulePath")]
37-
public string CustomRulePath
37+
public string[] CustomRulePath
3838
{
3939
get { return customRulePath; }
4040
set { customRulePath = value; }
4141
}
42-
private string customRulePath;
42+
private string[] customRulePath;
4343

4444
/// <summary>
4545
/// RecurseCustomRulePath: Find rules within subfolders under the path

Engine/Commands/InvokeScriptAnalyzerCommand.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,12 @@ public string ScriptDefinition
7474
[ValidateNotNull]
7575
[SuppressMessage("Microsoft.Performance", "CA1819:PropertiesShouldNotReturnArrays")]
7676
[Alias("CustomizedRulePath")]
77-
public string CustomRulePath
77+
public string[] CustomRulePath
7878
{
7979
get { return customRulePath; }
8080
set { customRulePath = value; }
8181
}
82-
private string customRulePath;
82+
private string[] customRulePath;
8383

8484
/// <summary>
8585
/// RecurseCustomRulePath: Find rules within subfolders under the path

Engine/Helper.cs

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -984,38 +984,44 @@ public Tuple<List<SuppressedRecord>, List<DiagnosticRecord>> SuppressRule(string
984984
return result;
985985
}
986986

987-
public static string[] ProcessCustomRulePaths(string rulePath, SessionState sessionState, bool recurse = false)
987+
public static string[] ProcessCustomRulePaths(string[] rulePaths, SessionState sessionState, bool recurse = false)
988988
{
989989
//if directory is given, list all the psd1 files
990990
List<string> outPaths = new List<string>();
991-
if (rulePath == null)
991+
if (rulePaths == null)
992992
{
993993
return null;
994994
}
995-
try
995+
996+
Collection<PathInfo> pathInfo = new Collection<PathInfo>();
997+
foreach (string rulePath in rulePaths)
996998
{
997-
Collection<PathInfo> pathInfo = sessionState.Path.GetResolvedPSPathFromPSPath(rulePath);
998-
foreach (PathInfo pinfo in pathInfo)
999+
Collection<PathInfo> pathInfosForRulePath = sessionState.Path.GetResolvedPSPathFromPSPath(rulePath);
1000+
if (null != pathInfosForRulePath)
9991001
{
1000-
string path = pinfo.Path;
1001-
if (Directory.Exists(path))
1002+
foreach (PathInfo pathInfoForRulePath in pathInfosForRulePath)
10021003
{
1003-
path = path.TrimEnd('\\');
1004-
if (recurse)
1005-
{
1006-
outPaths.AddRange(Directory.GetDirectories(pinfo.Path, "*", SearchOption.AllDirectories));
1007-
}
1004+
pathInfo.Add(pathInfoForRulePath);
10081005
}
1009-
outPaths.Add(path);
10101006
}
1011-
return outPaths.ToArray();
10121007
}
1013-
catch
1008+
1009+
foreach (PathInfo pinfo in pathInfo)
10141010
{
1015-
// need to do this as the path validation takes place later in the hierarchy.
1016-
outPaths.Add(rulePath);
1017-
return outPaths.ToArray();
1011+
string path = pinfo.Path;
1012+
if (Directory.Exists(path))
1013+
{
1014+
path = path.TrimEnd('\\');
1015+
if (recurse)
1016+
{
1017+
outPaths.AddRange(Directory.GetDirectories(pinfo.Path, "*", SearchOption.AllDirectories));
1018+
}
1019+
}
1020+
outPaths.Add(path);
10181021
}
1022+
1023+
return outPaths.ToArray();
1024+
10191025
}
10201026

10211027

Tests/Engine/CustomizedRule.tests.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,8 @@ Describe "Test importing correct customized rules" {
9999
}
100100

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

106106
it "will show the custom rules when given glob with recurse switch" {

Tests/Engine/GetScriptAnalyzerRule.tests.ps1

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ Describe "Test available parameters" {
2323
}
2424

2525
It "accepts string array" {
26-
$params["CustomRulePath"].ParameterType.FullName | Should Be "System.String"
26+
$params["CustomRulePath"].ParameterType.FullName | Should Be "System.String[]"
2727
}
2828

29-
It "takes CustomizedRulePath parameter as an alias of CustomRulePath paramter" {
29+
It "takes CustomizedRulePath parameter as an alias of CustomRulePath parameter" {
3030
$params.CustomRulePath.Aliases.Contains("CustomizedRulePath") | Should be $true
3131
}
3232
}
@@ -107,11 +107,11 @@ Describe "Test RuleExtension" {
107107
It "file cannot be found" {
108108
try
109109
{
110-
Get-ScriptAnalyzerRule -CustomizedRulePath "Invalid CustomRulePath"
110+
Get-ScriptAnalyzerRule -CustomRulePath "Invalid CustomRulePath"
111111
}
112112
catch
113113
{
114-
$Error[0].FullyQualifiedErrorId | should match "Cannot find ScriptAnalyzer rules in the specified path,Microsoft.Windows.PowerShell.ScriptAnalyzer.Commands.GetScriptAnalyzerRuleCommand"
114+
$Error[0].FullyQualifiedErrorId | should match "PathNotFound,Microsoft.Windows.PowerShell.ScriptAnalyzer.Commands.GetScriptAnalyzerRuleCommand"
115115
}
116116
}
117117

Tests/Engine/InvokeScriptAnalyzer.tests.ps1

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,8 @@ Describe "Test available parameters" {
4040
$params.ContainsKey("CustomRulePath") | Should Be $true
4141
}
4242

43-
It "accepts a string" {
44-
if ($testingLibraryUsage)
45-
{
43+
It "accepts a string array" {
4644
$params["CustomRulePath"].ParameterType.FullName | Should Be "System.String[]"
47-
}
48-
else
49-
{
50-
$params["CustomRulePath"].ParameterType.FullName | Should Be "System.String"
51-
}
5245
}
5346

5447
It "has a CustomizedRulePath alias"{
@@ -315,8 +308,15 @@ Describe "Test CustomizedRulePath" {
315308
$customizedRulePathExclude = Invoke-ScriptAnalyzer $directory\TestScript.ps1 -CustomizedRulePath $directory\CommunityAnalyzerRules\CommunityAnalyzerRules.psm1 -ExcludeRule "Measure-RequiresModules" | Where-Object {$_.RuleName -eq $measureRequired}
316309
$customizedRulePathExclude.Count | Should be 0
317310
}
311+
312+
It "When supplied with a collection of paths" {
313+
$customizedRulePath = Invoke-ScriptAnalyzer $directory\TestScript.ps1 -CustomRulePath ("$directory\CommunityAnalyzerRules", "$directory\SampleRule", "$directory\SampleRule\SampleRule2")
314+
$customizedRulePath.Count | Should Be 3
315+
}
316+
318317
}
319318

319+
320320
Context "When used incorrectly" {
321321
It "file cannot be found" {
322322
try
@@ -327,7 +327,7 @@ Describe "Test CustomizedRulePath" {
327327
{
328328
if (-not $testingLibraryUsage)
329329
{
330-
$Error[0].FullyQualifiedErrorId | should match "Cannot find ScriptAnalyzer rules in the specified path,Microsoft.Windows.PowerShell.ScriptAnalyzer.Commands.InvokeScriptAnalyzerCommand"
330+
$Error[0].FullyQualifiedErrorId | should match "PathNotFound,Microsoft.Windows.PowerShell.ScriptAnalyzer.Commands.InvokeScriptAnalyzerCommand"
331331
}
332332
}
333333
}

0 commit comments

Comments
 (0)