Skip to content

Fix whitelisted aliases comparison in PSAvoidUsingCmdletAliases rule #739

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 2 commits into from
Mar 29, 2017
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
3 changes: 2 additions & 1 deletion Rules/AvoidAlias.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using System.ComponentModel.Composition;
#endif
using System.Globalization;
using System.Linq;

namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules
{
Expand Down Expand Up @@ -119,7 +120,7 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
// You can also review the remark section in following document,
// MSDN: CommandAst.GetCommandName Method
if (aliasName == null
|| whiteList.Contains(aliasName))
|| whiteList.Contains<string>(aliasName, StringComparer.OrdinalIgnoreCase))
{
continue;
}
Expand Down
16 changes: 15 additions & 1 deletion Tests/Rules/AvoidUsingAlias.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,16 @@ Configuration MyDscConfiguration {
}

Context "Settings file provides whitelist" {
$whiteListTestScriptDef = 'gci; cd;'
BeforeAll {
$whiteListTestScriptDef = 'gci; cd;'
$settings = @{
'Rules' = @{
'PSAvoidUsingCmdletAliases' = @{
'Whitelist' = @('cd')
}
}
}
}

It "honors the whitelist provided as hashtable" {
$settings = @{
Expand All @@ -81,5 +90,10 @@ Configuration MyDscConfiguration {
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $whiteListTestScriptDef -Settings $settingsFilePath -IncludeRule $violationName
$violations.Count | Should be 1
}

It "honors the whitelist in a case-insensitive manner" {
$violations = Invoke-ScriptAnalyzer -ScriptDefinition "CD" -Settings $settings -IncludeRule $violationName
$violations.Count | Should Be 0
}
}
}