From 9bdb4b290fe49add2c01059d26fb0e88331823f9 Mon Sep 17 00:00:00 2001 From: Christoph Bergmeister Date: Thu, 2 May 2019 21:28:15 +0100 Subject: [PATCH 1/2] add test --- ...enticalMandatoryParametersForDSC.tests.ps1 | 71 +++++++++++++++++++ 1 file changed, 71 insertions(+) diff --git a/Tests/Rules/UseIdenticalMandatoryParametersForDSC.tests.ps1 b/Tests/Rules/UseIdenticalMandatoryParametersForDSC.tests.ps1 index 629f7575b..5d9947b52 100644 --- a/Tests/Rules/UseIdenticalMandatoryParametersForDSC.tests.ps1 +++ b/Tests/Rules/UseIdenticalMandatoryParametersForDSC.tests.ps1 @@ -76,6 +76,77 @@ 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 = '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.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("WaitForAny")] +class MSFT_WaitForAnyNoIdenticalMandatoryParameter : OMI_BaseResource +{ + [key, Description("Name of Resource on remote machine")] + string ResourceName; + + [key, Description("dummy variable")] + string Dummy; + + [required, Description("List of remote machines")] + string NodeName[]; + + [required, EmbeddedInstance("MSFT_Credential"), Description("Credential to access all remote machines")] + String Credential; + + [write, Description("dummy subclass variable"), EmbeddedInstance("MSFT_SubClass")] + string Subclass; +}; "@ # Act - run scriptanalyzer From 97d1eedf1761b14eb942c4685d50f72312bf056c Mon Sep 17 00:00:00 2001 From: Christoph Bergmeister Date: Sun, 5 May 2019 10:40:34 +0100 Subject: [PATCH 2/2] update test and minimise it (should fail now) --- ...enticalMandatoryParametersForDSC.tests.ps1 | 45 ++++++------------- 1 file changed, 14 insertions(+), 31 deletions(-) diff --git a/Tests/Rules/UseIdenticalMandatoryParametersForDSC.tests.ps1 b/Tests/Rules/UseIdenticalMandatoryParametersForDSC.tests.ps1 index 5d9947b52..f1a6d3fed 100644 --- a/Tests/Rules/UseIdenticalMandatoryParametersForDSC.tests.ps1 +++ b/Tests/Rules/UseIdenticalMandatoryParametersForDSC.tests.ps1 @@ -86,7 +86,7 @@ class ClassWithNoParent 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) { + 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" @@ -97,28 +97,23 @@ class ClassWithNoParent 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 + 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 'ClassWithNoParent.psm1' - $noparentClassMofFilepath = Join-Path -Path $noParentClassDir -ChildPath 'ClassWithNoParent.schema.mof' + $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" # the file content doesn't much matter + 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")] @@ -129,21 +124,9 @@ Class MSFT_SubClass [Write, Description("Additional non-required parameter")] Boolean Enabled; }; -[ClassVersion("1.0.0"), FriendlyName("WaitForAny")] -class MSFT_WaitForAnyNoIdenticalMandatoryParameter : OMI_BaseResource +[ClassVersion("1.0.0"), FriendlyName("ClassWithNoParent")] +class MSFT_ClassWithNoParent : OMI_BaseResource { - [key, Description("Name of Resource on remote machine")] - string ResourceName; - - [key, Description("dummy variable")] - string Dummy; - - [required, Description("List of remote machines")] - string NodeName[]; - - [required, EmbeddedInstance("MSFT_Credential"), Description("Credential to access all remote machines")] - String Credential; - [write, Description("dummy subclass variable"), EmbeddedInstance("MSFT_SubClass")] string Subclass; };