Skip to content

Commit 0272c25

Browse files
authored
Merge pull request #1043 from bergmeister/UseCompatibleCmdlets_AllowReferenceTobeUsedAsWell
Make UseCompatibleCmdlets not throw if default reference desktop-5.1.14393.206-windows is specified in the list of platforms and use core-6.0.2-windows as an alternative default reference
2 parents b5a19fa + 3c44cf4 commit 0272c25

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

Rules/UseCompatibleCmdlets.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ private struct RuleParameters
4343
private bool hasInitializationError;
4444
private string reference;
4545
private readonly string defaultReference = "desktop-5.1.14393.206-windows";
46+
private readonly string alternativeDefaultReference = "core-6.0.2-windows";
4647
private RuleParameters ruleParameters;
4748

4849
public UseCompatibleCmdlets()
@@ -274,6 +275,10 @@ private void SetupCmdletsDictionary()
274275

275276
ruleParameters.compatibility = compatibilityList.ToArray();
276277
reference = defaultReference;
278+
if (compatibilityList.Count == 1 && compatibilityList[0] == defaultReference)
279+
{
280+
reference = alternativeDefaultReference;
281+
}
277282
#if DEBUG
278283
// Setup reference file
279284
object referenceObject;
@@ -326,7 +331,7 @@ private void SetupCmdletsDictionary()
326331
return;
327332
}
328333

329-
var extentedCompatibilityList = compatibilityList.Concat(Enumerable.Repeat(reference, 1));
334+
var extentedCompatibilityList = compatibilityList.Union(Enumerable.Repeat(reference, 1));
330335
foreach (var compat in extentedCompatibilityList)
331336
{
332337
string psedition, psversion, os;

Tests/Rules/UseCompatibleCmdlets.tests.ps1

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,11 @@ Describe "UseCompatibleCmdlets" {
2626
process
2727
{
2828
It ("found {0} violations for '{1}'" -f $expectedViolations, $command) {
29-
Invoke-ScriptAnalyzer -ScriptDefinition $command -IncludeRule $ruleName -Settings $settings | `
30-
Get-Count | `
31-
Should -Be $expectedViolations
29+
$warnings = Invoke-ScriptAnalyzer -ScriptDefinition $command -IncludeRule $ruleName -Settings $settings
30+
$warnings.Count | Should -Be $expectedViolations
31+
$warnings | ForEach-Object {
32+
$_.RuleName | Should -Be 'PSUseCompatibleCmdlets'
33+
}
3234
}
3335
}
3436
}
@@ -54,4 +56,9 @@ Describe "UseCompatibleCmdlets" {
5456
@("Start-VM", "New-SmbShare", "Get-Disk") | `
5557
Test-Command -Settings $settings -ExpectedViolations 1
5658
}
59+
60+
Context "Default reference can also be used as target platform" {
61+
$settings = @{rules=@{PSUseCompatibleCmdlets=@{compatibility=@("desktop-5.1.14393.206-windows")}}}
62+
@("Remove-Service") | Test-Command -Settings $settings -ExpectedViolations 1
63+
}
5764
}

0 commit comments

Comments
 (0)