Skip to content

Commit 065c07e

Browse files
committed
Removed using System.Linq; and added some tests
1 parent b4aa1a7 commit 065c07e

2 files changed

Lines changed: 24 additions & 1 deletion

File tree

Rules/AvoidDynamicVariableNames.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
using System.Collections.Generic;
77
using System.Globalization;
88
using System.Management.Automation.Language;
9-
using System.Linq;
109

1110
#if !CORECLR
1211
using System.ComponentModel.Composition;

Tests/Rules/AvoidDynamicVariableNames.tests.ps1

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ Describe "AvoidDynamicVariableNames" {
1919
$violations.Extent.Text | Should -Be {New-Variable -Name $Test}.ToString()
2020
$violations.Message | Should -Be ($ruleMessage -f '$Test')
2121
}
22+
2223
It "Common dynamic variable iteration" {
2324
$scriptDefinition = {
2425
'One', 'Two', 'Three' | ForEach-Object -Begin { $i = 1 } -Process {
@@ -32,6 +33,20 @@ Describe "AvoidDynamicVariableNames" {
3233
$violations.Extent.Text | Should -Be {New-Variable -Name "My$_" -Value ($i++)}.ToString()
3334
$violations.Message | Should -Be ($ruleMessage -f 'My$_')
3435
}
36+
37+
It "Set-Variable by positional parameter" {
38+
$scriptDefinition = {
39+
'One', 'Two', 'Three' | ForEach-Object -Begin { $i = 1 } -Process {
40+
New-Variable "My$_" ($i++)
41+
}
42+
$MyTwo # returns 2
43+
}.ToString()
44+
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $scriptDefinition -IncludeRule @($ruleName)
45+
$violations.Count | Should -Be 1
46+
$violations.Severity | Should -Be Warning
47+
$violations.Extent.Text | Should -Be {New-Variable "My$_" ($i++)}.ToString()
48+
$violations.Message | Should -Be ($ruleMessage -f 'My$_')
49+
}
3550
}
3651

3752
Context "Compliant" {
@@ -58,6 +73,15 @@ Describe "AvoidDynamicVariableNames" {
5873
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $scriptDefinition -IncludeRule @($ruleName)
5974
$violations | Should -BeNullOrEmpty
6075
}
76+
77+
It "Verbatim (single quoted) name with dollar sign" {
78+
$scriptDefinition = {
79+
New-Variable -Name '$Sign'
80+
Set-Variable -Name '$Sign' -Value 'Dollar'
81+
}.ToString()
82+
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $scriptDefinition -IncludeRule @($ruleName)
83+
$violations | Should -BeNullOrEmpty
84+
}
6185
}
6286

6387
Context "Suppressed" {

0 commit comments

Comments
 (0)