PoshCode/Azure-Pipelines

Custom PSScriptAnalyzer rules not failing properly

FISHMANPET opened this issue · 1 comments

When PSScriptAnalyzer reports failures for custom rules, it doesn't the rulename doesn't just include the rule name, but the source as well. As an example, I have a custom rule I got from Get-ScriptAnalyzerRule:

RuleName         : Measure-UnicodeSubstitutions
CommonName       : Measure-UnicodeSubstitutions
Description      : When copying code from the internet, sometimes quotes and hyphens are replaced with unicode characters
                   To fix this, replace the characters with "standard" characters
SourceType       : Module
SourceName       : CustomPSSARules
Severity         : Warning
ImplementingType : Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.ExternalRule

The RuleName is simply Measure-UnicodeSubstitutions. However, in the results it looks like this:

RuleName                            Severity     ScriptName Line  Message                                                     
--------                            --------     ---------- ----  -------                                                     
CustomPSSARules\Measure-UnicodeSubs Error        UMN-module.p 256   Bad unicode characters                                      
titutions                                        sm1                                                                          

In the results, the rulename is Source\Rulename (in this case my rules from a module named CustomPSSARules. Because the rule names are in different formats, the code to match results with all the rules fails:

if ($Failures = $ScriptAnalyzer.Results.Where( {$_.RuleName -EQ "$Rule"})) {

In my case $_.RuleName is CustomPSSARules\Measure-UnicodeSubstitutions but $Rule from the list of previously generated Rules is Measure-UnicodeSubstitutions and they aren't equal.

I changed the line to do a like comparison instead:
if ($Failures = $ScriptAnalyzer.Results.Where( {$_.RuleName -like "*$Rule"})) {
And that properly fails the line. I'll make a PR for this shortly.

Yes ... but ... don't you think that's a PSSA bug like this one? I mean, they shouldn't stick the SourceName into the RuleName!

I guess I don't mind changing it to -like since it's unlikely to cause false positives, but it feels like I'm working around a buggy tool.