Skip to content
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

Enable suppression of PSAvoidAssignmentToAutomaticVariable for specific variable or parameter #1896

Merged
merged 2 commits into from
Sep 17, 2023
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
12 changes: 6 additions & 6 deletions Rules/AvoidAssignmentToAutomaticVariable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,20 +62,20 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
if (_readOnlyAutomaticVariables.Contains(variableName, StringComparer.OrdinalIgnoreCase))
{
yield return new DiagnosticRecord(DiagnosticRecordHelper.FormatError(Strings.AvoidAssignmentToReadOnlyAutomaticVariableError, variableName),
variableExpressionAst.Extent, GetName(), DiagnosticSeverity.Error, fileName);
variableExpressionAst.Extent, GetName(), DiagnosticSeverity.Error, fileName, variableName);
}

if (_readOnlyAutomaticVariablesIntroducedInVersion6_0.Contains(variableName, StringComparer.OrdinalIgnoreCase))
{
var severity = IsPowerShellVersion6OrGreater() ? DiagnosticSeverity.Error : DiagnosticSeverity.Warning;
yield return new DiagnosticRecord(DiagnosticRecordHelper.FormatError(Strings.AvoidAssignmentToReadOnlyAutomaticVariableIntroducedInPowerShell6_0Error, variableName),
variableExpressionAst.Extent, GetName(), severity, fileName);
variableExpressionAst.Extent, GetName(), severity, fileName, variableName);
}

if (_writableAutomaticVariables.Contains(variableName, StringComparer.OrdinalIgnoreCase))
{
yield return new DiagnosticRecord(DiagnosticRecordHelper.FormatError(Strings.AvoidAssignmentToWritableAutomaticVariableError, variableName),
variableExpressionAst.Extent, GetName(), DiagnosticSeverity.Warning, fileName);
variableExpressionAst.Extent, GetName(), DiagnosticSeverity.Warning, fileName, variableName);
}
}

Expand All @@ -93,20 +93,20 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
if (_readOnlyAutomaticVariables.Contains(variableName, StringComparer.OrdinalIgnoreCase))
{
yield return new DiagnosticRecord(DiagnosticRecordHelper.FormatError(Strings.AvoidAssignmentToReadOnlyAutomaticVariableError, variableName),
variableExpressionAst.Extent, GetName(), DiagnosticSeverity.Error, fileName);
variableExpressionAst.Extent, GetName(), DiagnosticSeverity.Error, fileName, variableName);
}

if (_readOnlyAutomaticVariablesIntroducedInVersion6_0.Contains(variableName, StringComparer.OrdinalIgnoreCase))
{
var severity = IsPowerShellVersion6OrGreater() ? DiagnosticSeverity.Error : DiagnosticSeverity.Warning;
yield return new DiagnosticRecord(DiagnosticRecordHelper.FormatError(Strings.AvoidAssignmentToReadOnlyAutomaticVariableIntroducedInPowerShell6_0Error, variableName),
variableExpressionAst.Extent, GetName(), severity, fileName);
variableExpressionAst.Extent, GetName(), severity, fileName, variableName);
}

if (_writableAutomaticVariables.Contains(variableName, StringComparer.OrdinalIgnoreCase))
{
yield return new DiagnosticRecord(DiagnosticRecordHelper.FormatError(Strings.AvoidAssignmentToWritableAutomaticVariableError, variableName),
variableExpressionAst.Extent, GetName(), DiagnosticSeverity.Warning, fileName);
variableExpressionAst.Extent, GetName(), DiagnosticSeverity.Warning, fileName, variableName);
}
}
}
Expand Down
126 changes: 75 additions & 51 deletions Tests/Rules/AvoidAssignmentToAutomaticVariable.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -7,60 +7,61 @@ BeforeAll {

Describe "AvoidAssignmentToAutomaticVariables" {
Context "ReadOnly Variables" {
BeforeDiscovery {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added per best practice in Pester v5. Only cosmetic and more explicit in this case.

$excpectedSeverityForAutomaticVariablesInPowerShell6 = 'Warning'
if ($PSVersionTable.PSVersion.Major -ge 6)
{
$excpectedSeverityForAutomaticVariablesInPowerShell6 = 'Error'
}

$excpectedSeverityForAutomaticVariablesInPowerShell6 = 'Warning'
if ($PSVersionTable.PSVersion.Major -ge 6)
{
$excpectedSeverityForAutomaticVariablesInPowerShell6 = 'Error'
$testCases_AutomaticVariables = @(
@{ VariableName = '?'; ExpectedSeverity = 'Error'; IsReadOnly = $true }
@{ VariableName = 'Error' ; ExpectedSeverity = 'Error'; IsReadOnly = $true }
@{ VariableName = 'ExecutionContext'; ExpectedSeverity = 'Error'; IsReadOnly = $true }
@{ VariableName = 'false'; ExpectedSeverity = 'Error'; IsReadOnly = $true }
@{ VariableName = 'Home'; ExpectedSeverity = 'Error'; IsReadOnly = $true }
@{ VariableName = 'Host'; ExpectedSeverity = 'Error'; IsReadOnly = $true }
@{ VariableName = 'PID'; ExpectedSeverity = 'Error'; IsReadOnly = $true }
@{ VariableName = 'PSCulture'; ExpectedSeverity = 'Error'; IsReadOnly = $true }
@{ VariableName = 'PSEdition'; ExpectedSeverity = 'Error'; IsReadOnly = $true }
@{ VariableName = 'PSHome'; ExpectedSeverity = 'Error'; IsReadOnly = $true }
@{ VariableName = 'PSUICulture'; ExpectedSeverity = 'Error'; IsReadOnly = $true }
@{ VariableName = 'PSVersionTable'; ExpectedSeverity = 'Error'; IsReadOnly = $true }
@{ VariableName = 'ShellId'; ExpectedSeverity = 'Error'; IsReadOnly = $true }
@{ VariableName = 'true'; ExpectedSeverity = 'Error'; IsReadOnly = $true }
# Variables introduced only in PowerShell 6+ have a Severity of Warning only
@{ VariableName = 'IsCoreCLR'; ExpectedSeverity = $excpectedSeverityForAutomaticVariablesInPowerShell6; OnlyPresentInCoreClr = $true }
@{ VariableName = 'IsLinux'; ExpectedSeverity = $excpectedSeverityForAutomaticVariablesInPowerShell6; OnlyPresentInCoreClr = $true }
@{ VariableName = 'IsMacOS'; ExpectedSeverity = $excpectedSeverityForAutomaticVariablesInPowerShell6; OnlyPresentInCoreClr = $true }
@{ VariableName = 'IsWindows'; ExpectedSeverity = $excpectedSeverityForAutomaticVariablesInPowerShell6; OnlyPresentInCoreClr = $true }
@{ VariableName = '_'; ExpectedSeverity = 'Warning' }
@{ VariableName = 'AllNodes'; ExpectedSeverity = 'Warning' }
@{ VariableName = 'Args'; ExpectedSeverity = 'Warning' }
@{ VariableName = 'ConsoleFilename'; ExpectedSeverity = 'Warning' }
@{ VariableName = 'Event'; ExpectedSeverity = 'Warning' }
@{ VariableName = 'EventArgs'; ExpectedSeverity = 'Warning' }
@{ VariableName = 'EventSubscriber'; ExpectedSeverity = 'Warning' }
@{ VariableName = 'ForEach'; ExpectedSeverity = 'Warning' }
@{ VariableName = 'Input'; ExpectedSeverity = 'Warning' }
@{ VariableName = 'Matches'; ExpectedSeverity = 'Warning' }
@{ VariableName = 'MyInvocation'; ExpectedSeverity = 'Warning' }
@{ VariableName = 'NestedPromptLevel'; ExpectedSeverity = 'Warning' }
@{ VariableName = 'Profile'; ExpectedSeverity = 'Warning' }
@{ VariableName = 'PSBoundParameters'; ExpectedSeverity = 'Warning' }
@{ VariableName = 'PsCmdlet'; ExpectedSeverity = 'Warning' }
@{ VariableName = 'PSCommandPath'; ExpectedSeverity = 'Warning' }
@{ VariableName = 'ReportErrorShowExceptionClass'; ExpectedSeverity = 'Warning' }
@{ VariableName = 'ReportErrorShowInnerException'; ExpectedSeverity = 'Warning' }
@{ VariableName = 'ReportErrorShowSource'; ExpectedSeverity = 'Warning' }
@{ VariableName = 'ReportErrorShowStackTrace'; ExpectedSeverity = 'Warning' }
@{ VariableName = 'Sender'; ExpectedSeverity = 'Warning' }
@{ VariableName = 'StackTrace'; ExpectedSeverity = 'Warning' }
@{ VariableName = 'This'; ExpectedSeverity = 'Warning' }
)

$testCases_ReadOnlyAutomaticVariables = $testCases_AutomaticVariables | Where-Object { $_.IsReadonly }
}

$testCases_AutomaticVariables = @(
@{ VariableName = '?'; ExpectedSeverity = 'Error'; IsReadOnly = $true }
@{ VariableName = 'Error' ; ExpectedSeverity = 'Error'; IsReadOnly = $true }
@{ VariableName = 'ExecutionContext'; ExpectedSeverity = 'Error'; IsReadOnly = $true }
@{ VariableName = 'false'; ExpectedSeverity = 'Error'; IsReadOnly = $true }
@{ VariableName = 'Home'; ExpectedSeverity = 'Error'; IsReadOnly = $true }
@{ VariableName = 'Host'; ExpectedSeverity = 'Error'; IsReadOnly = $true }
@{ VariableName = 'PID'; ExpectedSeverity = 'Error'; IsReadOnly = $true }
@{ VariableName = 'PSCulture'; ExpectedSeverity = 'Error'; IsReadOnly = $true }
@{ VariableName = 'PSEdition'; ExpectedSeverity = 'Error'; IsReadOnly = $true }
@{ VariableName = 'PSHome'; ExpectedSeverity = 'Error'; IsReadOnly = $true }
@{ VariableName = 'PSUICulture'; ExpectedSeverity = 'Error'; IsReadOnly = $true }
@{ VariableName = 'PSVersionTable'; ExpectedSeverity = 'Error'; IsReadOnly = $true }
@{ VariableName = 'ShellId'; ExpectedSeverity = 'Error'; IsReadOnly = $true }
@{ VariableName = 'true'; ExpectedSeverity = 'Error'; IsReadOnly = $true }
# Variables introduced only in PowerShell 6+ have a Severity of Warning only
@{ VariableName = 'IsCoreCLR'; ExpectedSeverity = $excpectedSeverityForAutomaticVariablesInPowerShell6; OnlyPresentInCoreClr = $true }
@{ VariableName = 'IsLinux'; ExpectedSeverity = $excpectedSeverityForAutomaticVariablesInPowerShell6; OnlyPresentInCoreClr = $true }
@{ VariableName = 'IsMacOS'; ExpectedSeverity = $excpectedSeverityForAutomaticVariablesInPowerShell6; OnlyPresentInCoreClr = $true }
@{ VariableName = 'IsWindows'; ExpectedSeverity = $excpectedSeverityForAutomaticVariablesInPowerShell6; OnlyPresentInCoreClr = $true }
@{ VariableName = '_'; ExpectedSeverity = 'Warning' }
@{ VariableName = 'AllNodes'; ExpectedSeverity = 'Warning' }
@{ VariableName = 'Args'; ExpectedSeverity = 'Warning' }
@{ VariableName = 'ConsoleFilename'; ExpectedSeverity = 'Warning' }
@{ VariableName = 'Event'; ExpectedSeverity = 'Warning' }
@{ VariableName = 'EventArgs'; ExpectedSeverity = 'Warning' }
@{ VariableName = 'EventSubscriber'; ExpectedSeverity = 'Warning' }
@{ VariableName = 'ForEach'; ExpectedSeverity = 'Warning' }
@{ VariableName = 'Input'; ExpectedSeverity = 'Warning' }
@{ VariableName = 'Matches'; ExpectedSeverity = 'Warning' }
@{ VariableName = 'MyInvocation'; ExpectedSeverity = 'Warning' }
@{ VariableName = 'NestedPromptLevel'; ExpectedSeverity = 'Warning' }
@{ VariableName = 'Profile'; ExpectedSeverity = 'Warning' }
@{ VariableName = 'PSBoundParameters'; ExpectedSeverity = 'Warning' }
@{ VariableName = 'PsCmdlet'; ExpectedSeverity = 'Warning' }
@{ VariableName = 'PSCommandPath'; ExpectedSeverity = 'Warning' }
@{ VariableName = 'ReportErrorShowExceptionClass'; ExpectedSeverity = 'Warning' }
@{ VariableName = 'ReportErrorShowInnerException'; ExpectedSeverity = 'Warning' }
@{ VariableName = 'ReportErrorShowSource'; ExpectedSeverity = 'Warning' }
@{ VariableName = 'ReportErrorShowStackTrace'; ExpectedSeverity = 'Warning' }
@{ VariableName = 'Sender'; ExpectedSeverity = 'Warning' }
@{ VariableName = 'StackTrace'; ExpectedSeverity = 'Warning' }
@{ VariableName = 'This'; ExpectedSeverity = 'Warning' }
)

$testCases_ReadOnlyAutomaticVariables = $testCases_AutomaticVariables | Where-Object { $_.IsReadonly }

It "Variable <VariableName> produces warning of Severity <ExpectedSeverity>" -TestCases $testCases_AutomaticVariables {
param ($VariableName, $ExpectedSeverity)
Copy link
Contributor Author

@fflaten fflaten Feb 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tip: Param-blocks are no longer needed in Pester v5. Some keep them to make the testcase-contract visible so didn't remove them :)


Expand Down Expand Up @@ -133,6 +134,29 @@ Describe "AvoidAssignmentToAutomaticVariables" {
}
}
}
}

Context 'Suppression' {
BeforeDiscovery {
$testCases_RuleSuppression = @(
@{ VariableName = 'true'; Type = 'ReadOnlyAutomaticVariableError' }
@{ VariableName = 'IsWindows'; Type = 'ReadOnlyAutomaticVariableIntroducedInPowerShell6_0Error' }
@{ VariableName = 'ForEach'; Type = 'WritableAutomaticVariableError' }
)
}

It 'Variable <VariableName> of type <Type> can be suppressed by RuleSuppressionId' -TestCases $testCases_RuleSuppression {
$scriptDef = @"
function suppressionTest {
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('$ruleName', '$VariableName')]
param(
`$$VariableName
)
}
"@

$warnings = @(Invoke-ScriptAnalyzer -ScriptDefinition $scriptDef -ExcludeRule PSReviewUnusedParameter)
$warnings.Count | Should -Be 0
}
}
}