From a5cc361e4987a85461b38a5b750f8cac0e5b2e4b Mon Sep 17 00:00:00 2001 From: Christoph Bergmeister Date: Tue, 24 Jul 2018 21:02:57 +0100 Subject: [PATCH 1/3] Make UseCompatibleCmdletsnot throw if default reference desktop-5.1.14393.206-windows is specified in the list of platforms --- Rules/UseCompatibleCmdlets.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Rules/UseCompatibleCmdlets.cs b/Rules/UseCompatibleCmdlets.cs index ea96334da..e86bc863a 100644 --- a/Rules/UseCompatibleCmdlets.cs +++ b/Rules/UseCompatibleCmdlets.cs @@ -326,7 +326,7 @@ private void SetupCmdletsDictionary() return; } - var extentedCompatibilityList = compatibilityList.Concat(Enumerable.Repeat(reference, 1)); + var extentedCompatibilityList = compatibilityList.Union(Enumerable.Repeat(reference, 1)); foreach (var compat in extentedCompatibilityList) { string psedition, psversion, os; From 97d77a9c7efb2db53c9950e957c7b3f5be87757e Mon Sep 17 00:00:00 2001 From: Christoph Bergmeister Date: Tue, 24 Jul 2018 21:40:12 +0100 Subject: [PATCH 2/3] add tests and add alternative default reference if only wps is targeted --- Rules/UseCompatibleCmdlets.cs | 5 +++++ Tests/Rules/UseCompatibleCmdlets.tests.ps1 | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/Rules/UseCompatibleCmdlets.cs b/Rules/UseCompatibleCmdlets.cs index e86bc863a..c7df33764 100644 --- a/Rules/UseCompatibleCmdlets.cs +++ b/Rules/UseCompatibleCmdlets.cs @@ -43,6 +43,7 @@ private struct RuleParameters private bool hasInitializationError; private string reference; private readonly string defaultReference = "desktop-5.1.14393.206-windows"; + private readonly string alternativeDefaultReference = "core-6.0.2-windows"; private RuleParameters ruleParameters; public UseCompatibleCmdlets() @@ -274,6 +275,10 @@ private void SetupCmdletsDictionary() ruleParameters.compatibility = compatibilityList.ToArray(); reference = defaultReference; + if (compatibilityList.Count == 1 && compatibilityList[0] == defaultReference) + { + reference = alternativeDefaultReference; + } #if DEBUG // Setup reference file object referenceObject; diff --git a/Tests/Rules/UseCompatibleCmdlets.tests.ps1 b/Tests/Rules/UseCompatibleCmdlets.tests.ps1 index 62d1e903b..5932289e5 100644 --- a/Tests/Rules/UseCompatibleCmdlets.tests.ps1 +++ b/Tests/Rules/UseCompatibleCmdlets.tests.ps1 @@ -54,4 +54,9 @@ Describe "UseCompatibleCmdlets" { @("Start-VM", "New-SmbShare", "Get-Disk") | ` Test-Command -Settings $settings -ExpectedViolations 1 } + + Context "Default reference can also be used as target platform" { + $settings = @{rules=@{PSUseCompatibleCmdlets=@{compatibility=@("desktop-5.1.14393.206-windows")}}} + @("Remove-Service") | Test-Command -Settings $settings -ExpectedViolations 1 + } } From 3c44cf4d9b3a6760f330f7f565578b0fdf50dffd Mon Sep 17 00:00:00 2001 From: Christoph Bergmeister Date: Sun, 2 Sep 2018 19:38:47 +0100 Subject: [PATCH 3/3] Improve test helper to assert against specific diagnostic type --- Tests/Rules/UseCompatibleCmdlets.tests.ps1 | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/Tests/Rules/UseCompatibleCmdlets.tests.ps1 b/Tests/Rules/UseCompatibleCmdlets.tests.ps1 index 5932289e5..ba39f1524 100644 --- a/Tests/Rules/UseCompatibleCmdlets.tests.ps1 +++ b/Tests/Rules/UseCompatibleCmdlets.tests.ps1 @@ -26,9 +26,11 @@ Describe "UseCompatibleCmdlets" { process { It ("found {0} violations for '{1}'" -f $expectedViolations, $command) { - Invoke-ScriptAnalyzer -ScriptDefinition $command -IncludeRule $ruleName -Settings $settings | ` - Get-Count | ` - Should -Be $expectedViolations + $warnings = Invoke-ScriptAnalyzer -ScriptDefinition $command -IncludeRule $ruleName -Settings $settings + $warnings.Count | Should -Be $expectedViolations + $warnings | ForEach-Object { + $_.RuleName | Should -Be 'PSUseCompatibleCmdlets' + } } } }