Skip to content

Commit 1cdd33c

Browse files
authored
Instead of using the first cimClass and then having no superClass, use the first cimClass that has a non-null superClass (#1200)
* Instead of using the first cimClass and then having to superClass, use the first cimClass that has a non-null superClass * add regression test
1 parent 7a3688e commit 1cdd33c

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed

Diff for: Rules/UseIdenticalMandatoryParametersDSC.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ private IDictionary<string, string> GetKeys(string fileName)
256256
// todo log the error
257257
}
258258

259-
var cimClass = cimClasses?.FirstOrDefault();
259+
var cimClass = cimClasses?.FirstOrDefault(_cimClass => _cimClass.CimSuperClass != null);
260260
var cimSuperClassProperties = new HashSet<string>(
261261
cimClass?.CimSuperClass?.CimClassProperties.Select(cimPropertyDeclaration => cimPropertyDeclaration.Name) ??
262262
Enumerable.Empty<string>());

Diff for: Tests/Rules/UseIdenticalMandatoryParametersForDSC.tests.ps1

+54
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,60 @@ class ClassWithNoParent
7676
{
7777
[Write] Boolean Anonymous;
7878
};
79+
"@
80+
81+
# Act - run scriptanalyzer
82+
$violations = Invoke-ScriptAnalyzer -Path $noParentClassFilepath -IncludeRule $ruleName -ErrorAction Stop
83+
$violations.Count | Should -Be 0
84+
}
85+
}
86+
87+
Context "When a CIM class has no parent, but does contain a subclass which should not be processed" {
88+
# regression test for #1192 - just check no uncaught exception
89+
It "Should find no violations, and throw no exceptions" -Skip:($IsLinux -or $IsMacOS) {
90+
91+
# Arrange test content in testdrive
92+
$dscResources = Join-Path -Path "TestDrive:" -ChildPath "DSCResources"
93+
$noparentClassDir = Join-Path -Path $dscResources "ClassWithNoParent"
94+
95+
# need a fake module
96+
$fakeModulePath = Join-Path -Path "TestDrive:" -ChildPath "test.psd1"
97+
Set-Content -Path $fakeModulePath -Value @"
98+
@{
99+
ModuleVersion = '1.0'
100+
GUID = 'fe2acc06-d9e6-4ca6-b57d-068e8fc5ad57'
101+
Author = 'DummyAuthor'
102+
}
103+
"@
104+
# and under it a directory called dscresources\something
105+
New-Item -ItemType Directory -Path $noParentClassDir
106+
$noparentClassFilepath = Join-Path -Path $noParentClassDir -ChildPath 'MSFT_ClassWithNoParent.psm1'
107+
$noparentClassMofFilepath = Join-Path -Path $noParentClassDir -ChildPath 'MSFT_ClassWithNoParent.schema.mof'
108+
109+
# containing a .psm1 file and a .schema.mof file with same base name
110+
Set-Content -Path $noParentClassFilepath -Value @"
111+
#requires -Version 4.0 -Modules CimCmdlets
112+
function Get-TargetResource { }
113+
function Set-TargetResource { }
114+
function Test-TargetResource { }
115+
Export-ModuleMember -Function *-TargetResource
116+
"@
117+
118+
Set-Content -Path $noParentClassMofFilePath -Value @"
119+
[ClassVersion("1.0.0.0")]
120+
Class MSFT_SubClass
121+
{
122+
[Key, Description("Key of the subclass")] String Name;
123+
[Required, Description("Required parameter of the subclass")] String Description;
124+
[Write, Description("Additional non-required parameter")] Boolean Enabled;
125+
};
126+
127+
[ClassVersion("1.0.0"), FriendlyName("ClassWithNoParent")]
128+
class MSFT_ClassWithNoParent : OMI_BaseResource
129+
{
130+
[write, Description("dummy subclass variable"), EmbeddedInstance("MSFT_SubClass")]
131+
string Subclass;
132+
};
79133
"@
80134

81135
# Act - run scriptanalyzer

0 commit comments

Comments
 (0)