-
Notifications
You must be signed in to change notification settings - Fork 398
Fix bugs in importing external rule #90
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
53ca175
Fix bugs in importing external rule
yutingc c5f1ecc
Updated string comparison
yutingc 89b4e23
Modified string comparison options
yutingc be781aa
Added unit test
yutingc b73aa4d
Added unit tests
yutingc cdcc4e5
Added changes in ExternalRule
yutingc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
Import-Module PSScriptAnalyzer | ||
$directory = Split-Path -Parent $MyInvocation.MyCommand.Path | ||
$message = "this is help" | ||
$measure = "Measure-RequiresRunAsAdministrator" | ||
|
||
Describe "Test importing customized rules with null return results" { | ||
Context "Test Get-ScriptAnalyzer with customized rules" { | ||
It "will not terminate the engine" { | ||
$customizedRulePath = Get-ScriptAnalyzerRule -CustomizedRulePath $directory\samplerule\SampleRulesWithErrors.psm1 | Where-Object {$_.RuleName -eq $measure} | ||
$customizedRulePath.Count | Should Be 1 | ||
} | ||
|
||
} | ||
|
||
Context "Test Invoke-ScriptAnalyzer with customized rules" { | ||
It "will not terminate the engine" { | ||
$customizedRulePath = Invoke-ScriptAnalyzer $directory\TestScript.ps1 -CustomizedRulePath $directory\samplerule\SampleRulesWithErrors.psm1 | Where-Object {$_.RuleName -eq $measure} | ||
$customizedRulePath.Count | Should Be 0 | ||
} | ||
} | ||
|
||
} | ||
|
||
Describe "Test importing correct customized rules" { | ||
Context "Test Get-ScriptAnalyzer with customized rules" { | ||
It "will show the customized rule" { | ||
$customizedRulePath = Get-ScriptAnalyzerRule -CustomizedRulePath $directory\samplerule\samplerule.psm1 | Where-Object {$_.RuleName -eq $measure} | ||
$customizedRulePath.Count | Should Be 1 | ||
} | ||
|
||
} | ||
|
||
Context "Test Invoke-ScriptAnalyzer with customized rules" { | ||
It "will show the customized rule in the results" { | ||
$customizedRulePath = Invoke-ScriptAnalyzer $directory\TestScript.ps1 -CustomizedRulePath $directory\samplerule\samplerule.psm1 | Where-Object {$_.Message -eq $message} | ||
$customizedRulePath.Count | Should Be 1 | ||
} | ||
} | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#Requires -Version 3.0 | ||
|
||
<# | ||
.SYNOPSIS | ||
Uses #Requires -RunAsAdministrator instead of your own methods. | ||
.DESCRIPTION | ||
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. | ||
From Windows PowerShell 4.0, the #Requires statement let script developers require that sessions be run with elevated user rights (run as Administrator). | ||
Script developers does not need to write their own methods any more. | ||
To fix a violation of this rule, please consider to use #Requires -RunAsAdministrator instead of your own methods. | ||
.EXAMPLE | ||
Measure-RequiresRunAsAdministrator -ScriptBlockAst $ScriptBlockAst | ||
.INPUTS | ||
[System.Management.Automation.Language.ScriptBlockAst] | ||
.OUTPUTS | ||
[OutputType([PSCustomObject[])] | ||
.NOTES | ||
None | ||
#> | ||
function Measure-RequiresRunAsAdministrator | ||
{ | ||
[CmdletBinding()] | ||
[OutputType([Microsoft.Windows.Powershell.ScriptAnalyzer.Generic.DiagnosticRecord[]])] | ||
Param | ||
( | ||
[Parameter(Mandatory = $true)] | ||
[ValidateNotNullOrEmpty()] | ||
[System.Management.Automation.Language.ScriptBlockAst] | ||
$ScriptBlockAst | ||
) | ||
|
||
|
||
$results = @() | ||
|
||
$results += $null | ||
return $results | ||
|
||
|
||
} | ||
Export-ModuleMember -Function Measure* |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
#Requires -Version 3.0 | ||
|
||
<# | ||
.SYNOPSIS | ||
Uses #Requires -RunAsAdministrator instead of your own methods. | ||
.DESCRIPTION | ||
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. | ||
From Windows PowerShell 4.0, the #Requires statement let script developers require that sessions be run with elevated user rights (run as Administrator). | ||
Script developers does not need to write their own methods any more. | ||
To fix a violation of this rule, please consider to use #Requires -RunAsAdministrator instead of your own methods. | ||
.EXAMPLE | ||
Measure-RequiresRunAsAdministrator -ScriptBlockAst $ScriptBlockAst | ||
.INPUTS | ||
[System.Management.Automation.Language.ScriptBlockAst] | ||
.OUTPUTS | ||
[OutputType([PSCustomObject[])] | ||
.NOTES | ||
None | ||
#> | ||
function Measure-RequiresRunAsAdministrator | ||
{ | ||
[CmdletBinding()] | ||
[OutputType([Microsoft.Windows.Powershell.ScriptAnalyzer.Generic.DiagnosticRecord[]])] | ||
Param | ||
( | ||
[Parameter(Mandatory = $true)] | ||
[ValidateNotNullOrEmpty()] | ||
[System.Management.Automation.Language.ScriptBlockAst] | ||
$testAst | ||
) | ||
|
||
|
||
$results = @() | ||
|
||
$result = [Microsoft.Windows.Powershell.ScriptAnalyzer.Generic.DiagnosticRecord[]]@{"Message" = "this is help"; | ||
"Extent" = $ast.Extent; | ||
"RuleName" = $PSCmdlet.MyInvocation.InvocationName; | ||
"Severity" = "Warning"} | ||
|
||
$results += $result | ||
|
||
|
||
return $results | ||
|
||
|
||
} | ||
Export-ModuleMember -Function Measure* |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also the key of this dictionary is still the name of the parameter instead of the type of the parameter.