From 69fbc5bd3300213fe92587ed540b9ec60ff2cc4f Mon Sep 17 00:00:00 2001 From: Edwin Young Date: Thu, 26 Jul 2018 14:05:20 -0700 Subject: [PATCH 1/6] Don't crash on CIM classes with no superclass --- Rules/UseIdenticalMandatoryParametersDSC.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Rules/UseIdenticalMandatoryParametersDSC.cs b/Rules/UseIdenticalMandatoryParametersDSC.cs index 73f37645f..ec96afe7b 100644 --- a/Rules/UseIdenticalMandatoryParametersDSC.cs +++ b/Rules/UseIdenticalMandatoryParametersDSC.cs @@ -258,7 +258,7 @@ private IDictionary GetKeys(string fileName) var cimClass = cimClasses?.FirstOrDefault(); var cimSuperClassProperties = new HashSet( - cimClass?.CimSuperClass.CimClassProperties.Select(p => p.Name) ?? + cimClass?.CimSuperClass?.CimClassProperties.Select(p => p.Name) ?? Enumerable.Empty()); return cimClass? From b0a89b08e3d158f90e389f40d01397e84c0d292e Mon Sep 17 00:00:00 2001 From: "Christoph Bergmeister [MVP]" Date: Fri, 27 Jul 2018 00:38:51 +0100 Subject: [PATCH 2/6] ReTrigger CI by using better linq variable name --- Rules/UseIdenticalMandatoryParametersDSC.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Rules/UseIdenticalMandatoryParametersDSC.cs b/Rules/UseIdenticalMandatoryParametersDSC.cs index ec96afe7b..34ef8b47f 100644 --- a/Rules/UseIdenticalMandatoryParametersDSC.cs +++ b/Rules/UseIdenticalMandatoryParametersDSC.cs @@ -258,7 +258,7 @@ private IDictionary GetKeys(string fileName) var cimClass = cimClasses?.FirstOrDefault(); var cimSuperClassProperties = new HashSet( - cimClass?.CimSuperClass?.CimClassProperties.Select(p => p.Name) ?? + cimClass?.CimSuperClass?.CimClassProperties.Select(cimPropertyDeclaration => cimPropertyDeclaration.Name) ?? Enumerable.Empty()); return cimClass? From d1cc71bafdaa397083141816816478ba8f59b956 Mon Sep 17 00:00:00 2001 From: Edwin Young Date: Sun, 26 Aug 2018 13:37:14 -0700 Subject: [PATCH 3/6] add test case --- .../ClassWithNoParent/ClassWithNoParent.psm1 | 2 ++ .../ClassWithNoParent/ClassWithNoParent.schema.mof | 6 ++++++ .../UseIdenticalMandatoryParametersForDSC.tests.ps1 | 9 +++++++++ 3 files changed, 17 insertions(+) create mode 100644 Tests/Rules/DSCResourceModule/DSCResources/ClassWithNoParent/ClassWithNoParent.psm1 create mode 100644 Tests/Rules/DSCResourceModule/DSCResources/ClassWithNoParent/ClassWithNoParent.schema.mof diff --git a/Tests/Rules/DSCResourceModule/DSCResources/ClassWithNoParent/ClassWithNoParent.psm1 b/Tests/Rules/DSCResourceModule/DSCResources/ClassWithNoParent/ClassWithNoParent.psm1 new file mode 100644 index 000000000..f13834b52 --- /dev/null +++ b/Tests/Rules/DSCResourceModule/DSCResources/ClassWithNoParent/ClassWithNoParent.psm1 @@ -0,0 +1,2 @@ +#requires -Version 4.0 -Modules CimCmdlets + diff --git a/Tests/Rules/DSCResourceModule/DSCResources/ClassWithNoParent/ClassWithNoParent.schema.mof b/Tests/Rules/DSCResourceModule/DSCResources/ClassWithNoParent/ClassWithNoParent.schema.mof new file mode 100644 index 000000000..5e864024d --- /dev/null +++ b/Tests/Rules/DSCResourceModule/DSCResources/ClassWithNoParent/ClassWithNoParent.schema.mof @@ -0,0 +1,6 @@ +[ClassVersion("1.0.0")] +class ClassWithNoParent +{ + [Write] Boolean Anonymous; +}; + diff --git a/Tests/Rules/UseIdenticalMandatoryParametersForDSC.tests.ps1 b/Tests/Rules/UseIdenticalMandatoryParametersForDSC.tests.ps1 index 358ff6d98..ece559ba7 100644 --- a/Tests/Rules/UseIdenticalMandatoryParametersForDSC.tests.ps1 +++ b/Tests/Rules/UseIdenticalMandatoryParametersForDSC.tests.ps1 @@ -7,6 +7,7 @@ $badResourceFilepath = [System.IO.Path]::Combine( 'MSFT_WaitForAnyNoIdenticalMandatoryParameter.psm1'); $goodResourceFilepath = [System.IO.Path]::Combine($resourceBasepath,'MSFT_WaitForAny','MSFT_WaitForAny.psm1'); +$noparentClassFilepath = [System.IO.Path]::Combine($resourceBasepath,'ClassWithNoParent','ClassWithNoParent.psm1'); Describe "UseIdenticalMandatoryParametersForDSC" { Context "When a mandatory parameters are not present" { @@ -33,4 +34,12 @@ Describe "UseIdenticalMandatoryParametersForDSC" { $violations.Count | Should -Be 0 } } + + Context "When a CIM class has no parent" { + # regression test for #982 - just check no uncaught exception + It "Should find no violations, and throw no exceptions" { + $violations = Invoke-ScriptAnalyzer -Path $noParentClassFilepath -IncludeRule $ruleName + $violations.Count | Should -Be 0 + } + } } From 4561ccce9f1931e939b27da9018ca5fcea23de1a Mon Sep 17 00:00:00 2001 From: Edwin Young Date: Wed, 5 Sep 2018 21:59:08 -0700 Subject: [PATCH 4/6] incorporate PR feedback --- .../ClassWithNoParent/ClassWithNoParent.psm1 | 2 - .../ClassWithNoParent.schema.mof | 6 --- ...enticalMandatoryParametersForDSC.tests.ps1 | 42 ++++++++++++++++++- 3 files changed, 40 insertions(+), 10 deletions(-) delete mode 100644 Tests/Rules/DSCResourceModule/DSCResources/ClassWithNoParent/ClassWithNoParent.psm1 delete mode 100644 Tests/Rules/DSCResourceModule/DSCResources/ClassWithNoParent/ClassWithNoParent.schema.mof diff --git a/Tests/Rules/DSCResourceModule/DSCResources/ClassWithNoParent/ClassWithNoParent.psm1 b/Tests/Rules/DSCResourceModule/DSCResources/ClassWithNoParent/ClassWithNoParent.psm1 deleted file mode 100644 index f13834b52..000000000 --- a/Tests/Rules/DSCResourceModule/DSCResources/ClassWithNoParent/ClassWithNoParent.psm1 +++ /dev/null @@ -1,2 +0,0 @@ -#requires -Version 4.0 -Modules CimCmdlets - diff --git a/Tests/Rules/DSCResourceModule/DSCResources/ClassWithNoParent/ClassWithNoParent.schema.mof b/Tests/Rules/DSCResourceModule/DSCResources/ClassWithNoParent/ClassWithNoParent.schema.mof deleted file mode 100644 index 5e864024d..000000000 --- a/Tests/Rules/DSCResourceModule/DSCResources/ClassWithNoParent/ClassWithNoParent.schema.mof +++ /dev/null @@ -1,6 +0,0 @@ -[ClassVersion("1.0.0")] -class ClassWithNoParent -{ - [Write] Boolean Anonymous; -}; - diff --git a/Tests/Rules/UseIdenticalMandatoryParametersForDSC.tests.ps1 b/Tests/Rules/UseIdenticalMandatoryParametersForDSC.tests.ps1 index ece559ba7..539daae1e 100644 --- a/Tests/Rules/UseIdenticalMandatoryParametersForDSC.tests.ps1 +++ b/Tests/Rules/UseIdenticalMandatoryParametersForDSC.tests.ps1 @@ -7,7 +7,6 @@ $badResourceFilepath = [System.IO.Path]::Combine( 'MSFT_WaitForAnyNoIdenticalMandatoryParameter.psm1'); $goodResourceFilepath = [System.IO.Path]::Combine($resourceBasepath,'MSFT_WaitForAny','MSFT_WaitForAny.psm1'); -$noparentClassFilepath = [System.IO.Path]::Combine($resourceBasepath,'ClassWithNoParent','ClassWithNoParent.psm1'); Describe "UseIdenticalMandatoryParametersForDSC" { Context "When a mandatory parameters are not present" { @@ -38,7 +37,46 @@ Describe "UseIdenticalMandatoryParametersForDSC" { Context "When a CIM class has no parent" { # regression test for #982 - just check no uncaught exception It "Should find no violations, and throw no exceptions" { - $violations = Invoke-ScriptAnalyzer -Path $noParentClassFilepath -IncludeRule $ruleName + + # Arrange test content in testdrive + $noparentClassDir = [System.IO.Path]::Combine("TestDrive:\",'DSCResources\ClassWithNoParent'); + + # need a fake module + Set-Content -Path "TestDrive:\test.psd1" -Value @" +@{ + ModuleVersion = '1.0' + GUID = 'f5e6cc2a-5500-4592-bbe2-ef033754b56f' + Author = 'test' + + FunctionsToExport = @() + CmdletsToExport = @() + VariablesToExport = '*' + AliasesToExport = @() + + # Private data to pass to the module specified in RootModule/ModuleToProcess. This may also contain a PSData hashtable with additional module metadata used by PowerShell. + PrivateData = @{ + PSData = @{ + } # End of PSData hashtable + } # End of PrivateData hashtable +} +"@ + # and under it a directory called dscresources\something + New-Item -ItemType Directory -Path $noParentClassDir + $noparentClassFilepath = [System.IO.Path]::Combine($noParentClassDir,'ClassWithNoParent.psm1'); + $noparentClassMofFilepath = [System.IO.Path]::Combine($noParentClassDir,'ClassWithNoParent.schema.mof'); + + # containing a .psm1 file and a .schema.mof file with same base name + Set-Content -Path $noParentClassFilepath -Value "#requires -Version 4.0 -Modules CimCmdlets" # the file content doesn't much matter + + Set-Content -Path $noParentClassMofFilePath -Value @" +[ClassVersion("1.0.0")] +class ClassWithNoParent +{ + [Write] Boolean Anonymous; +}; +"@ + # Act - run scriptanalyzer + $violations = Invoke-ScriptAnalyzer -Path $noParentClassFilepath -IncludeRule $ruleName -ErrorAction Stop $violations.Count | Should -Be 0 } } From 52dc8cb0dadd40471c10984fea1b7a3e0e204319 Mon Sep 17 00:00:00 2001 From: Edwin Young Date: Mon, 10 Sep 2018 22:51:53 -0700 Subject: [PATCH 5/6] tweak test for linux --- .../UseIdenticalMandatoryParametersForDSC.tests.ps1 | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/Tests/Rules/UseIdenticalMandatoryParametersForDSC.tests.ps1 b/Tests/Rules/UseIdenticalMandatoryParametersForDSC.tests.ps1 index 539daae1e..ca0aa439c 100644 --- a/Tests/Rules/UseIdenticalMandatoryParametersForDSC.tests.ps1 +++ b/Tests/Rules/UseIdenticalMandatoryParametersForDSC.tests.ps1 @@ -36,13 +36,14 @@ Describe "UseIdenticalMandatoryParametersForDSC" { Context "When a CIM class has no parent" { # regression test for #982 - just check no uncaught exception - It "Should find no violations, and throw no exceptions" { + It "Should find no violations, and throw no exceptions" -skip:($IsLinux -or $IsMacOS) { # Arrange test content in testdrive - $noparentClassDir = [System.IO.Path]::Combine("TestDrive:\",'DSCResources\ClassWithNoParent'); + $noparentClassDir = Join-Path "TestDrive:" "DSCResources" "ClassWithNoParent" # need a fake module - Set-Content -Path "TestDrive:\test.psd1" -Value @" + $fakeModulePath = Join-Path "TestDrive:" "test.psd1" + Set-Content -Path $fakeModulePath -Value @" @{ ModuleVersion = '1.0' GUID = 'f5e6cc2a-5500-4592-bbe2-ef033754b56f' @@ -62,8 +63,8 @@ Describe "UseIdenticalMandatoryParametersForDSC" { "@ # and under it a directory called dscresources\something New-Item -ItemType Directory -Path $noParentClassDir - $noparentClassFilepath = [System.IO.Path]::Combine($noParentClassDir,'ClassWithNoParent.psm1'); - $noparentClassMofFilepath = [System.IO.Path]::Combine($noParentClassDir,'ClassWithNoParent.schema.mof'); + $noparentClassFilepath = Join-Path $noParentClassDir 'ClassWithNoParent.psm1' + $noparentClassMofFilepath = Join-Path $noParentClassDir 'ClassWithNoParent.schema.mof' # containing a .psm1 file and a .schema.mof file with same base name Set-Content -Path $noParentClassFilepath -Value "#requires -Version 4.0 -Modules CimCmdlets" # the file content doesn't much matter From 94806423655cab9f402fa47a329df8f166f26874 Mon Sep 17 00:00:00 2001 From: Edwin Young Date: Tue, 11 Sep 2018 22:57:09 -0700 Subject: [PATCH 6/6] use join-path --- .../UseIdenticalMandatoryParametersForDSC.tests.ps1 | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/Tests/Rules/UseIdenticalMandatoryParametersForDSC.tests.ps1 b/Tests/Rules/UseIdenticalMandatoryParametersForDSC.tests.ps1 index ca0aa439c..629f7575b 100644 --- a/Tests/Rules/UseIdenticalMandatoryParametersForDSC.tests.ps1 +++ b/Tests/Rules/UseIdenticalMandatoryParametersForDSC.tests.ps1 @@ -39,10 +39,11 @@ Describe "UseIdenticalMandatoryParametersForDSC" { It "Should find no violations, and throw no exceptions" -skip:($IsLinux -or $IsMacOS) { # Arrange test content in testdrive - $noparentClassDir = Join-Path "TestDrive:" "DSCResources" "ClassWithNoParent" + $dscResources = Join-Path -Path "TestDrive:" -ChildPath "DSCResources" + $noparentClassDir = Join-Path -Path $dscResources "ClassWithNoParent" # need a fake module - $fakeModulePath = Join-Path "TestDrive:" "test.psd1" + $fakeModulePath = Join-Path -Path "TestDrive:" -ChildPath "test.psd1" Set-Content -Path $fakeModulePath -Value @" @{ ModuleVersion = '1.0' @@ -63,8 +64,8 @@ Describe "UseIdenticalMandatoryParametersForDSC" { "@ # and under it a directory called dscresources\something New-Item -ItemType Directory -Path $noParentClassDir - $noparentClassFilepath = Join-Path $noParentClassDir 'ClassWithNoParent.psm1' - $noparentClassMofFilepath = Join-Path $noParentClassDir 'ClassWithNoParent.schema.mof' + $noparentClassFilepath = Join-Path -Path $noParentClassDir -ChildPath 'ClassWithNoParent.psm1' + $noparentClassMofFilepath = Join-Path -Path $noParentClassDir -ChildPath 'ClassWithNoParent.schema.mof' # containing a .psm1 file and a .schema.mof file with same base name Set-Content -Path $noParentClassFilepath -Value "#requires -Version 4.0 -Modules CimCmdlets" # the file content doesn't much matter @@ -76,6 +77,7 @@ class ClassWithNoParent [Write] Boolean Anonymous; }; "@ + # Act - run scriptanalyzer $violations = Invoke-ScriptAnalyzer -Path $noParentClassFilepath -IncludeRule $ruleName -ErrorAction Stop $violations.Count | Should -Be 0