Skip to content

Commit b73aa4d

Browse files
committed
Added unit tests
Also added parameterType to external rule to separate parameter type and parameter name
1 parent be781aa commit b73aa4d

File tree

4 files changed

+90
-8
lines changed

4 files changed

+90
-8
lines changed

Engine/ScriptAnalyzer.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ public List<ExternalRule> GetExternalRule(string[] moduleNames)
251251
string desc =posh.AddScript(script).Invoke()[0].ImmediateBaseObject.ToString()
252252
.Replace("\r\n", " ").Trim();
253253

254-
rules.Add(new ExternalRule(funcInfo.Name, funcInfo.Name, desc, param.ParameterType.Name,
254+
rules.Add(new ExternalRule(funcInfo.Name, funcInfo.Name, desc, param.Name,param.ParameterType.FullName,
255255
funcInfo.ModuleName, funcInfo.Module.Path));
256256
}
257257
}
@@ -290,12 +290,12 @@ public IEnumerable<DiagnosticRecord> GetExternalRecord(Ast ast, Token[] token, E
290290
// Groups rules by AstType and Tokens.
291291
Dictionary<string, List<ExternalRule>> astRuleGroups = rules
292292
.Where<ExternalRule>(item => item.GetParameter().EndsWith("ast", StringComparison.OrdinalIgnoreCase))
293-
.GroupBy<ExternalRule, string>(item => item.GetParameter())
293+
.GroupBy<ExternalRule, string>(item => item.GetParameterType())
294294
.ToDictionary(item => item.Key, item => item.ToList());
295295

296296
Dictionary<string, List<ExternalRule>> tokenRuleGroups = rules
297297
.Where<ExternalRule>(item => item.GetParameter().EndsWith("token", StringComparison.OrdinalIgnoreCase))
298-
.GroupBy<ExternalRule, string>(item => item.GetParameter())
298+
.GroupBy<ExternalRule, string>(item => item.GetParameterType())
299299
.ToDictionary(item => item.Key, item => item.ToList());
300300

301301
using (rsp)
@@ -337,7 +337,7 @@ public IEnumerable<DiagnosticRecord> GetExternalRecord(Ast ast, Token[] token, E
337337
{
338338
// Find all AstTypes that appeared in rule groups.
339339
IEnumerable<Ast> childAsts = ast.FindAll(new Func<Ast, bool>((testAst) =>
340-
(String.Equals(testAst.GetType().Name, astRuleGroup.Key, StringComparison.OrdinalIgnoreCase))), false);
340+
(astRuleGroup.Key.IndexOf(testAst.GetType().FullName,StringComparison.OrdinalIgnoreCase) != -1)), false);
341341

342342
foreach (Ast childAst in childAsts)
343343
{

Tests/Engine/CustomizedRule.tests.ps1

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
Import-Module PSScriptAnalyzer
2+
$directory = Split-Path -Parent $MyInvocation.MyCommand.Path
3+
$message = "this is help"
4+
$measure = "Measure-RequiresRunAsAdministrator"
5+
6+
Describe "Test importing customized rules with null return results" {
7+
Context "Test Get-ScriptAnalyzer with customized rules" {
8+
It "will not terminate the engine" {
9+
$customizedRulePath = Get-ScriptAnalyzerRule -CustomizedRulePath $directory\samplerule\SampleRulesWithErrors.psm1 | Where-Object {$_.RuleName -eq $measure}
10+
$customizedRulePath.Count | Should Be 1
11+
}
12+
13+
}
14+
15+
Context "Test Invoke-ScriptAnalyzer with customized rules" {
16+
It "will not terminate the engine" {
17+
$customizedRulePath = Invoke-ScriptAnalyzer $directory\TestScript.ps1 -CustomizedRulePath $directory\samplerule\SampleRulesWithErrors.psm1 | Where-Object {$_.RuleName -eq $measure}
18+
$customizedRulePath.Count | Should Be 0
19+
}
20+
}
21+
22+
}
23+
24+
Describe "Test importing correct customized rules" {
25+
Context "Test Get-ScriptAnalyzer with customized rules" {
26+
It "will show the customized rule" {
27+
$customizedRulePath = Get-ScriptAnalyzerRule -CustomizedRulePath $directory\samplerule\samplerule.psm1 | Where-Object {$_.RuleName -eq $measure}
28+
$customizedRulePath.Count | Should Be 1
29+
}
30+
31+
}
32+
33+
Context "Test Invoke-ScriptAnalyzer with customized rules" {
34+
It "will show the customized rule in the results" {
35+
$customizedRulePath = Invoke-ScriptAnalyzer $directory\TestScript.ps1 -CustomizedRulePath $directory\samplerule\samplerule.psm1 | Where-Object {$_.Message -eq $message}
36+
$customizedRulePath.Count | Should Be 1
37+
}
38+
}
39+
40+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#Requires -Version 3.0
2+
3+
<#
4+
.SYNOPSIS
5+
Uses #Requires -RunAsAdministrator instead of your own methods.
6+
.DESCRIPTION
7+
The #Requires statement prevents a script from running unless the Windows PowerShell version, modules, snap-ins, and module and snap-in version prerequisites are met.
8+
From Windows PowerShell 4.0, the #Requires statement let script developers require that sessions be run with elevated user rights (run as Administrator).
9+
Script developers does not need to write their own methods any more.
10+
To fix a violation of this rule, please consider to use #Requires -RunAsAdministrator instead of your own methods.
11+
.EXAMPLE
12+
Measure-RequiresRunAsAdministrator -ScriptBlockAst $ScriptBlockAst
13+
.INPUTS
14+
[System.Management.Automation.Language.ScriptBlockAst]
15+
.OUTPUTS
16+
[OutputType([PSCustomObject[])]
17+
.NOTES
18+
None
19+
#>
20+
function Measure-RequiresRunAsAdministrator
21+
{
22+
[CmdletBinding()]
23+
[OutputType([Microsoft.Windows.Powershell.ScriptAnalyzer.Generic.DiagnosticRecord[]])]
24+
Param
25+
(
26+
[Parameter(Mandatory = $true)]
27+
[ValidateNotNullOrEmpty()]
28+
[System.Management.Automation.Language.ScriptBlockAst]
29+
$ScriptBlockAst
30+
)
31+
32+
33+
$results = @()
34+
35+
$results += $null
36+
return $results
37+
38+
39+
}
40+
Export-ModuleMember -Function Measure*

Tests/Engine/samplerule/samplerule.psm1

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,20 @@ function Measure-RequiresRunAsAdministrator
2626
[Parameter(Mandatory = $true)]
2727
[ValidateNotNullOrEmpty()]
2828
[System.Management.Automation.Language.ScriptBlockAst]
29-
$Ast
29+
$testAst
3030
)
3131

3232

3333
$results = @()
34-
34+
3535
$result = [Microsoft.Windows.Powershell.ScriptAnalyzer.Generic.DiagnosticRecord[]]@{"Message" = "this is help";
36-
"Extent" = $FunctionDefinitionAst.Extent;
36+
"Extent" = $ast.Extent;
3737
"RuleName" = $PSCmdlet.MyInvocation.InvocationName;
3838
"Severity" = "Warning"}
3939

40-
$results += $result
40+
$results += $result
41+
42+
4143
return $results
4244

4345

0 commit comments

Comments
 (0)