Skip to content

Commit 6d44810

Browse files
author
Kapil Borle
authored
Merge pull request #739 from PowerShell/kapilmb/fix-case-whitelist
Fix whitelisted aliases comparison in PSAvoidUsingCmdletAliases rule
2 parents 95e9287 + aef7ded commit 6d44810

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

Rules/AvoidAlias.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
using System.ComponentModel.Composition;
1919
#endif
2020
using System.Globalization;
21+
using System.Linq;
2122

2223
namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules
2324
{
@@ -119,7 +120,7 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
119120
// You can also review the remark section in following document,
120121
// MSDN: CommandAst.GetCommandName Method
121122
if (aliasName == null
122-
|| whiteList.Contains(aliasName))
123+
|| whiteList.Contains<string>(aliasName, StringComparer.OrdinalIgnoreCase))
123124
{
124125
continue;
125126
}

Tests/Rules/AvoidUsingAlias.tests.ps1

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,16 @@ Configuration MyDscConfiguration {
6161
}
6262

6363
Context "Settings file provides whitelist" {
64-
$whiteListTestScriptDef = 'gci; cd;'
64+
BeforeAll {
65+
$whiteListTestScriptDef = 'gci; cd;'
66+
$settings = @{
67+
'Rules' = @{
68+
'PSAvoidUsingCmdletAliases' = @{
69+
'Whitelist' = @('cd')
70+
}
71+
}
72+
}
73+
}
6574

6675
It "honors the whitelist provided as hashtable" {
6776
$settings = @{
@@ -81,5 +90,10 @@ Configuration MyDscConfiguration {
8190
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $whiteListTestScriptDef -Settings $settingsFilePath -IncludeRule $violationName
8291
$violations.Count | Should be 1
8392
}
93+
94+
It "honors the whitelist in a case-insensitive manner" {
95+
$violations = Invoke-ScriptAnalyzer -ScriptDefinition "CD" -Settings $settings -IncludeRule $violationName
96+
$violations.Count | Should Be 0
97+
}
8498
}
8599
}

0 commit comments

Comments
 (0)