From 061451f2d5a47c1f61e4315e418253151bec06eb Mon Sep 17 00:00:00 2001 From: Christoph Bergmeister Date: Thu, 28 Mar 2019 19:12:45 +0000 Subject: [PATCH 1/2] Instead of using the first cimClass and then having to superClass, use the first cimClass that has a non-null superClass --- Rules/UseIdenticalMandatoryParametersDSC.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Rules/UseIdenticalMandatoryParametersDSC.cs b/Rules/UseIdenticalMandatoryParametersDSC.cs index 34ef8b47f..713b86814 100644 --- a/Rules/UseIdenticalMandatoryParametersDSC.cs +++ b/Rules/UseIdenticalMandatoryParametersDSC.cs @@ -256,7 +256,7 @@ private IDictionary GetKeys(string fileName) // todo log the error } - var cimClass = cimClasses?.FirstOrDefault(); + var cimClass = cimClasses?.FirstOrDefault(_cimClass => _cimClass.CimSuperClass != null); var cimSuperClassProperties = new HashSet( cimClass?.CimSuperClass?.CimClassProperties.Select(cimPropertyDeclaration => cimPropertyDeclaration.Name) ?? Enumerable.Empty()); From 05f3349e2766b57c9f1e3644b3232ce576778116 Mon Sep 17 00:00:00 2001 From: Christoph Bergmeister Date: Sun, 5 May 2019 10:50:13 +0100 Subject: [PATCH 2/2] add regression test --- ...enticalMandatoryParametersForDSC.tests.ps1 | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/Tests/Rules/UseIdenticalMandatoryParametersForDSC.tests.ps1 b/Tests/Rules/UseIdenticalMandatoryParametersForDSC.tests.ps1 index 629f7575b..f1a6d3fed 100644 --- a/Tests/Rules/UseIdenticalMandatoryParametersForDSC.tests.ps1 +++ b/Tests/Rules/UseIdenticalMandatoryParametersForDSC.tests.ps1 @@ -76,6 +76,60 @@ class ClassWithNoParent { [Write] Boolean Anonymous; }; +"@ + + # Act - run scriptanalyzer + $violations = Invoke-ScriptAnalyzer -Path $noParentClassFilepath -IncludeRule $ruleName -ErrorAction Stop + $violations.Count | Should -Be 0 + } + } + + Context "When a CIM class has no parent, but does contain a subclass which should not be processed" { + # regression test for #1192 - just check no uncaught exception + It "Should find no violations, and throw no exceptions" -Skip:($IsLinux -or $IsMacOS) { + + # Arrange test content in testdrive + $dscResources = Join-Path -Path "TestDrive:" -ChildPath "DSCResources" + $noparentClassDir = Join-Path -Path $dscResources "ClassWithNoParent" + + # need a fake module + $fakeModulePath = Join-Path -Path "TestDrive:" -ChildPath "test.psd1" + Set-Content -Path $fakeModulePath -Value @" +@{ + ModuleVersion = '1.0' + GUID = 'fe2acc06-d9e6-4ca6-b57d-068e8fc5ad57' + Author = 'DummyAuthor' +} +"@ + # and under it a directory called dscresources\something + New-Item -ItemType Directory -Path $noParentClassDir + $noparentClassFilepath = Join-Path -Path $noParentClassDir -ChildPath 'MSFT_ClassWithNoParent.psm1' + $noparentClassMofFilepath = Join-Path -Path $noParentClassDir -ChildPath 'MSFT_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 +function Get-TargetResource { } +function Set-TargetResource { } +function Test-TargetResource { } +Export-ModuleMember -Function *-TargetResource +"@ + + Set-Content -Path $noParentClassMofFilePath -Value @" +[ClassVersion("1.0.0.0")] +Class MSFT_SubClass +{ + [Key, Description("Key of the subclass")] String Name; + [Required, Description("Required parameter of the subclass")] String Description; + [Write, Description("Additional non-required parameter")] Boolean Enabled; +}; + +[ClassVersion("1.0.0"), FriendlyName("ClassWithNoParent")] +class MSFT_ClassWithNoParent : OMI_BaseResource +{ + [write, Description("dummy subclass variable"), EmbeddedInstance("MSFT_SubClass")] + string Subclass; +}; "@ # Act - run scriptanalyzer