Skip to content

Commit abaa667

Browse files
committed
Add tests for compatible syntax rule
1 parent 7317423 commit abaa667

File tree

3 files changed

+109
-1
lines changed

3 files changed

+109
-1
lines changed

Rules/CompatibilityRules/UseCompatibleSyntax.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,34 @@ public IEnumerable<DiagnosticRecord> GetDiagnosticRecords()
172172
return _diagnosticAccumulator;
173173
}
174174

175+
public override AstVisitAction VisitMemberExpression(MemberExpressionAst memberExpressionAst)
176+
{
177+
if (!_targetVersions.Contains(s_v3))
178+
{
179+
return AstVisitAction.Continue;
180+
}
181+
182+
if (!(memberExpressionAst.Member is StringConstantExpressionAst))
183+
{
184+
string message = string.Format(
185+
CultureInfo.CurrentCulture,
186+
Strings.UseCompatibleSyntaxError,
187+
"dynamic member invocation",
188+
memberExpressionAst.Extent.Text,
189+
"3");
190+
191+
_diagnosticAccumulator.Add(new DiagnosticRecord(
192+
message,
193+
memberExpressionAst.Extent,
194+
_rule.GetName(),
195+
_rule.Severity,
196+
_analyzedFilePath
197+
));
198+
}
199+
200+
return AstVisitAction.Continue;
201+
}
202+
175203
public override AstVisitAction VisitInvokeMemberExpression(InvokeMemberExpressionAst methodCallAst)
176204
{
177205
// Look for [typename]::new(...) and [typename]::$dynamicMethodName syntax

Tests/Rules/UseCompatibleCommands.Tests.ps1

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
$script:RuleName = 'PSUseCompatibleCommands'
55
$script:AnyProfileConfigKey = 'AnyProfilePath'
66
$script:TargetProfileConfigKey = 'TargetProfiles'
7-
$script:AssetDirPath = Join-Path $PSScriptRoot $script:RuleName
87

98
$script:Srv2012_3_profile = 'win-8_x64_6.2.9200.0_3.0_x64_4.0.30319.42000_framework'
109
$script:Srv2012r2_4_profile = 'win-8_x64_6.3.9600.0_4.0_x64_4.0.30319.42000_framework'
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
$script:RuleName = 'PSUseCompatibleSyntax'
2+
3+
$script:ScriptDefinition = @'
4+
class MyClass
5+
{
6+
[string]$Hi = "Hello"
7+
8+
[string]GetString()
9+
{
10+
return $this.Hi
11+
}
12+
}
13+
14+
enum MyEnum
15+
{
16+
One,
17+
Two
18+
}
19+
20+
$x = [MyClass]::new()
21+
22+
$member = 'Hi'
23+
Write-Host $x.$member
24+
25+
Write-Output 'Banana'
26+
27+
$method = 'GetString'
28+
$x.$method()
29+
30+
$enumVal = "One"
31+
[MyEnum]::$enumVal
32+
'@
33+
34+
Describe "PSUseCompatibleSyntax" {
35+
BeforeAll {
36+
$testCases = @(
37+
@{ Script = '$x = [MyClass]::new()'; Versions = @(3,4) }
38+
@{ Script = '$member = "Hi"; $x.$member'; Versions = @(3) }
39+
@{ Script = 'Write-Host "Banana"'; Versions = @() }
40+
@{ Script = '[System.VeryInnocuousType]::RunApiMethod($obj)'; Versions = @() }
41+
@{ Script = '$y.$methodWithAVeryLongName()'; Versions = @(3) }
42+
@{ Script = '$typeExpression::$staticMember'; Versions = @(3) }
43+
@{ Script = '$typeExpression::$dynamicStaticMethodName()'; Versions = @(3) }
44+
)
45+
46+
# PS v3/4 won't parse classes or enums
47+
if ($PSVersionTable.PSVersion.Major -ge 5)
48+
{
49+
$testCases += @(
50+
@{ Script = "class MyClass { }"; Versions = @(3,4) }
51+
@{ Script = "enum MyEnum { One; Two }"; Versions = @(3,4) }
52+
)
53+
}
54+
55+
# PS v6+ won't parse workflows
56+
if ($PSVersionTable.PSVersion.Major -le 5)
57+
{
58+
$testCases += @(
59+
@{ Script = 'workflow Banana { Do-ExpensiveCommandOnAnotherMachine -Argument "Banana" }'; Versions = @(6) }
60+
)
61+
}
62+
}
63+
64+
foreach ($v in 3,4,5,6)
65+
{
66+
It "Finds issues for PSv$v in '<Script>'" -TestCases $testCases {
67+
param([string]$Script, $Versions)
68+
69+
$diagnostics = Invoke-ScriptAnalyzer -ScriptDefinition $Script -IncludeRule PSUseCompatibleSyntax -Settings @{ Rules = @{ PSUseCompatibleSyntax = @{ Enable = $true; TargetVersions = @("$v.0") } } }
70+
71+
if ($Versions -contains $v)
72+
{
73+
$diagnostics.Count | Should -Be 1
74+
}
75+
else
76+
{
77+
$diagnostics.Count | Should -Be 0
78+
}
79+
}
80+
}
81+
}

0 commit comments

Comments
 (0)