Skip to content

Commit a5571c5

Browse files
edyoungbergmeister
authored andcommitted
Don't crash on CIM classes with no superclass (#1046)
* Don't crash on CIM classes with no superclass * ReTrigger CI by using better linq variable name * add test case * incorporate PR feedback * tweak test for linux * use join-path
1 parent 880b765 commit a5571c5

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

Rules/UseIdenticalMandatoryParametersDSC.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ private IDictionary<string, string> GetKeys(string fileName)
258258

259259
var cimClass = cimClasses?.FirstOrDefault();
260260
var cimSuperClassProperties = new HashSet<string>(
261-
cimClass?.CimSuperClass.CimClassProperties.Select(p => p.Name) ??
261+
cimClass?.CimSuperClass?.CimClassProperties.Select(cimPropertyDeclaration => cimPropertyDeclaration.Name) ??
262262
Enumerable.Empty<string>());
263263

264264
return cimClass?

Tests/Rules/UseIdenticalMandatoryParametersForDSC.tests.ps1

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,54 @@ Describe "UseIdenticalMandatoryParametersForDSC" {
3333
$violations.Count | Should -Be 0
3434
}
3535
}
36+
37+
Context "When a CIM class has no parent" {
38+
# regression test for #982 - just check no uncaught exception
39+
It "Should find no violations, and throw no exceptions" -skip:($IsLinux -or $IsMacOS) {
40+
41+
# Arrange test content in testdrive
42+
$dscResources = Join-Path -Path "TestDrive:" -ChildPath "DSCResources"
43+
$noparentClassDir = Join-Path -Path $dscResources "ClassWithNoParent"
44+
45+
# need a fake module
46+
$fakeModulePath = Join-Path -Path "TestDrive:" -ChildPath "test.psd1"
47+
Set-Content -Path $fakeModulePath -Value @"
48+
@{
49+
ModuleVersion = '1.0'
50+
GUID = 'f5e6cc2a-5500-4592-bbe2-ef033754b56f'
51+
Author = 'test'
52+
53+
FunctionsToExport = @()
54+
CmdletsToExport = @()
55+
VariablesToExport = '*'
56+
AliasesToExport = @()
57+
58+
# 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.
59+
PrivateData = @{
60+
PSData = @{
61+
} # End of PSData hashtable
62+
} # End of PrivateData hashtable
63+
}
64+
"@
65+
# and under it a directory called dscresources\something
66+
New-Item -ItemType Directory -Path $noParentClassDir
67+
$noparentClassFilepath = Join-Path -Path $noParentClassDir -ChildPath 'ClassWithNoParent.psm1'
68+
$noparentClassMofFilepath = Join-Path -Path $noParentClassDir -ChildPath 'ClassWithNoParent.schema.mof'
69+
70+
# containing a .psm1 file and a .schema.mof file with same base name
71+
Set-Content -Path $noParentClassFilepath -Value "#requires -Version 4.0 -Modules CimCmdlets" # the file content doesn't much matter
72+
73+
Set-Content -Path $noParentClassMofFilePath -Value @"
74+
[ClassVersion("1.0.0")]
75+
class ClassWithNoParent
76+
{
77+
[Write] Boolean Anonymous;
78+
};
79+
"@
80+
81+
# Act - run scriptanalyzer
82+
$violations = Invoke-ScriptAnalyzer -Path $noParentClassFilepath -IncludeRule $ruleName -ErrorAction Stop
83+
$violations.Count | Should -Be 0
84+
}
85+
}
3686
}

0 commit comments

Comments
 (0)