Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't crash on CIM classes with no superclass #1046

Merged
merged 7 commits into from
Sep 13, 2018
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Rules/UseIdenticalMandatoryParametersDSC.cs
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ private IDictionary<string, string> GetKeys(string fileName)

var cimClass = cimClasses?.FirstOrDefault();
var cimSuperClassProperties = new HashSet<string>(
cimClass?.CimSuperClass.CimClassProperties.Select(p => p.Name) ??
cimClass?.CimSuperClass?.CimClassProperties.Select(cimPropertyDeclaration => cimPropertyDeclaration.Name) ??
Enumerable.Empty<string>());

return cimClass?
Expand Down
47 changes: 47 additions & 0 deletions Tests/Rules/UseIdenticalMandatoryParametersForDSC.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,51 @@ 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" {

# 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
}
}
}