diff --git a/Rules/UseIdenticalMandatoryParametersDSC.cs b/Rules/UseIdenticalMandatoryParametersDSC.cs index 73f37645f..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? diff --git a/Tests/Rules/UseIdenticalMandatoryParametersForDSC.tests.ps1 b/Tests/Rules/UseIdenticalMandatoryParametersForDSC.tests.ps1 index 358ff6d98..629f7575b 100644 --- a/Tests/Rules/UseIdenticalMandatoryParametersForDSC.tests.ps1 +++ b/Tests/Rules/UseIdenticalMandatoryParametersForDSC.tests.ps1 @@ -33,4 +33,54 @@ 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" -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 = '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 = 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 + + 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 + } + } }