diff --git a/CHANGELOG.md b/CHANGELOG.md index 028da4b..067c506 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,8 +9,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added -- Added cmdlet `ConvertFrom-DscResourceInstance` wich can be used to convert any - object to in another format. It accepts objects from pipeline. [issue #71](https://github.com/dsccommunity/DscResource.Common/issues/71). +- Added cmdlet `ConvertFrom-DscResourceInstance` which can be used to convert any + object to in another format. It accepts objects from pipeline. [issue #71](https://github.com/dsccommunity/DscResource.Common/issues/71). +- Now code coverage is uploaded to codecov.io. + +### Changed + +- Unit tests are now running using Pester 5 ([issue #40](https://github.com/dsccommunity/DscResource.Common/issues/40)). +- Excludes the PowerShell module script file _DscResource.Common.psm1_ located + in folder _source_ from the HQRM testing. ## [0.10.2] - 2021-03-24 diff --git a/RequiredModules.psd1 b/RequiredModules.psd1 index 59dd5ff..d1bf087 100644 --- a/RequiredModules.psd1 +++ b/RequiredModules.psd1 @@ -9,7 +9,7 @@ InvokeBuild = 'latest' PSScriptAnalyzer = 'latest' - Pester = '4.10.1' + Pester = 'latest' Plaster = 'latest' ModuleBuilder = 'latest' ChangelogManagement = 'latest' diff --git a/build.yaml b/build.yaml index d136bbf..d48e5b1 100644 --- a/build.yaml +++ b/build.yaml @@ -28,10 +28,11 @@ BuildWorkflow: - package_module_nupkg hqrmtest: - - DscResource_Tests_Stop_On_Fail + - Invoke_HQRM_Tests_Stop_On_Fail test: - Pester_Tests_Stop_On_Fail + - Convert_Pester_Coverage - Pester_if_Code_Coverage_Under_Threshold publish: @@ -44,26 +45,39 @@ BuildWorkflow: #################################################### Pester: - OutputFormat: NUnitXML + Configuration: + Run: + Path: + - tests/QA + - tests/Unit + - tests/Integration + CodeCoverage: + CoveragePercentTarget: 78 + OutputPath: JaCoCo_coverage.xml + OutputEncoding: ascii ExcludeFromCodeCoverage: - Script: - - tests/QA - - tests/Unit - - tests/Integration - ExcludeTag: - Tag: - CodeCoverageThreshold: 50 - CodeCoverageOutputFile: JaCoCo_coverage.xml - CodeCoverageOutputFileEncoding: ascii DscTest: - ExcludeTag: - - 'Common Tests - New Error-Level Script Analyzer Rules' - Tag: - ExcludeSourceFile: - - output - ExcludeModuleFile: - MainGitBranch: main + Pester: + Configuration: + Filter: + Tag: + ExcludeTag: + - Common Tests - New Error-Level Script Analyzer Rules + Output: + Verbosity: Detailed + TestResult: + Enabled: true + OutputFormat: NUnitXML + OutputPath: ./output/testResults/NUnitXml_DscResourceCommon_HQRM.xml + OutputEncoding: UTF-8 + TestSuiteName: DscResourceCommonHQRM + Script: + ExcludeSourceFile: + - output + - DscResource.Common + ExcludeModuleFile: + MainGitBranch: main Resolve-Dependency: Gallery: 'PSGallery' @@ -75,6 +89,8 @@ ModuleBuildTasks: - '*.build.Sampler.ib.tasks' Sampler.GitHubTasks: - '*.ib.tasks' + DscResource.Test: + - 'Task.*' TaskHeader: | param($Path) diff --git a/cocdecov.yml b/cocdecov.yml new file mode 100644 index 0000000..010e707 --- /dev/null +++ b/cocdecov.yml @@ -0,0 +1,25 @@ +codecov: + require_ci_to_pass: no + # main should be the baseline for reporting + branch: main + +comment: + layout: "reach, diff, flags, files" + behavior: default + +coverage: + range: 50..80 + round: down + precision: 0 + + status: + project: + default: + # Set the overall project code coverage requirement to 70% + target: 70 + patch: + default: + # Set the pull request requirement to not regress overall coverage by more than 5% + # and let codecov.io set the goal for the code changed in the patch. + target: auto + threshold: 5 diff --git a/tests/Integration/Public/Set-DscMachineRebootRequired.Tests.ps1 b/tests/Integration/Public/Set-DscMachineRebootRequired.Tests.ps1 index c1ade09..8138354 100644 --- a/tests/Integration/Public/Set-DscMachineRebootRequired.Tests.ps1 +++ b/tests/Integration/Public/Set-DscMachineRebootRequired.Tests.ps1 @@ -1,17 +1,12 @@ -$ProjectPath = "$PSScriptRoot\..\..\.." | Convert-Path -$ProjectName = ((Get-ChildItem -Path $ProjectPath\*\*.psd1).Where{ - ($_.Directory.Name -match 'source|src' -or $_.Directory.Name -eq $_.BaseName) -and - $(try - { - Test-ModuleManifest $_.FullName -ErrorAction Stop - } - catch - { - $false - } ) - }).BaseName +BeforeAll { + $script:moduleName = 'DscResource.Common' -Import-Module $ProjectName -Force + Remove-Module -Name $script:moduleName -Force -ErrorAction 'SilentlyContinue' + + Get-Module -Name $script:moduleName -ListAvailable | + Select-Object -First 1 | + Import-Module -Force -ErrorAction 'Stop' +} Describe 'Set-DscMachineRebootRequired' -Tag 'Set-DscMachineRebootRequired' { BeforeAll { @@ -36,4 +31,8 @@ Describe 'Set-DscMachineRebootRequired' -Tag 'Set-DscMachineRebootRequired' { $global:DSCMachineStatus | Should -Be 1 } } + + It 'Should have reverted the value of $global:DSCMachineStatus' { + $global:DSCMachineStatus | Should -Be $script:currentDSCMachineStatus + } } diff --git a/tests/Integration/Public/Set-PSModulePath.Tests.ps1 b/tests/Integration/Public/Set-PSModulePath.Tests.ps1 index 73341b2..f6c8d8c 100644 --- a/tests/Integration/Public/Set-PSModulePath.Tests.ps1 +++ b/tests/Integration/Public/Set-PSModulePath.Tests.ps1 @@ -1,41 +1,34 @@ -$ProjectPath = "$PSScriptRoot\..\..\.." | Convert-Path -$ProjectName = ((Get-ChildItem -Path $ProjectPath\*\*.psd1).Where{ - ($_.Directory.Name -match 'source|src' -or $_.Directory.Name -eq $_.BaseName) -and - $(try - { - Test-ModuleManifest $_.FullName -ErrorAction Stop - } - catch - { - $false - } ) - }).BaseName +BeforeDiscovery { + # Determines if we should skip tests. + if ($isWindows -or $PSEdition -eq 'Desktop') + { + $currentPrincipal = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent()) -Import-Module $ProjectName -Force + $skipTest = -not $currentPrincipal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator) + } + else + { + $skipTest = $true + } +} -Describe 'Set-PSModulePath' -Tag 'SetPSModulePath' { - BeforeAll { - $currentPSModulePath = $env:PSModulePath +BeforeAll { + $script:moduleName = 'DscResource.Common' - if ($isWindows -or $PSEdition -eq 'Desktop') - { - $currentPrincipal = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent()) + Remove-Module -Name $script:moduleName -Force -ErrorAction 'SilentlyContinue' - $skipTest = -not $currentPrincipal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator) - } - else - { - $skipTest = $true - } + Get-Module -Name $script:moduleName -ListAvailable | + Select-Object -First 1 | + Import-Module -Force -ErrorAction 'Stop' +} - if (-not $skipTest) - { - $currentMachinePSModulePath = [Environment]::GetEnvironmentVariable('PSModulePath', 'Machine') +Describe 'Set-PSModulePath' -Tag 'SetPSModulePath' { + Context 'When updating the session environment variable PSModulePath' { + BeforeAll { + $currentPSModulePath = $env:PSModulePath } - } - Context 'When updating the session environment variable PSModulePath' { - AfterAll { + AfterEach { $env:PSModulePath = $currentPSModulePath } @@ -44,31 +37,29 @@ Describe 'Set-PSModulePath' -Tag 'SetPSModulePath' { $env:PSModulePath | Should -Be 'C:\Module' } + + It 'Should have returned the session PSModulePath to the original value' { + $env:PSModulePath | Should -Be $currentPSModulePath + } } - Context 'When updating the machine environment variable PSModulePath' { - AfterAll { - if (-not $skipTest) - { - [System.Environment]::SetEnvironmentVariable('PSModulePath', $currentMachinePSModulePath, [System.EnvironmentVariableTarget]::Machine) - } + Context 'When updating the machine environment variable PSModulePath' -Skip:$skipTest { + BeforeAll { + $currentMachinePSModulePath = [Environment]::GetEnvironmentVariable('PSModulePath', 'Machine') } - It 'Should not throw an error and have set the correct value' -Skip:$skipTest { + AfterEach { + [System.Environment]::SetEnvironmentVariable('PSModulePath', $currentMachinePSModulePath, [System.EnvironmentVariableTarget]::Machine) + } + + It 'Should not throw an error and have set the correct value' { { Set-PSModulePath -Path 'C:\Module' -Machine } | Should -Not -Throw [Environment]::GetEnvironmentVariable('PSModulePath', 'Machine') | Should -Be 'C:\Module' } - } - - Context 'When the tests have run for Set-PSModulePath' { - It 'Should have returned the session PSModulePath to the original value' { - $env:PSModulePath | Should -Be $currentPSModulePath - } It 'Should have returned the machine PSModulePath to the original value' -Skip:$skipTest { [Environment]::GetEnvironmentVariable('PSModulePath', 'Machine') | Should -Be $currentMachinePSModulePath } } } - diff --git a/tests/QA/module.tests.ps1 b/tests/QA/module.tests.ps1 index 39aa23c..10d67ed 100644 --- a/tests/QA/module.tests.ps1 +++ b/tests/QA/module.tests.ps1 @@ -1,92 +1,182 @@ -$here = Split-Path -Parent $MyInvocation.MyCommand.Path - -# Convert-path required for PS7 or Join-Path fails -$ProjectPath = "$here\..\.." | Convert-Path -$ProjectName = (Get-ChildItem $ProjectPath\*\*.psd1 | Where-Object { - ($_.Directory.Name -match 'source|src' -or $_.Directory.Name -eq $_.BaseName) -and - $(try { Test-ModuleManifest $_.FullName -ErrorAction Stop }catch{$false}) } -).BaseName - -$SourcePath = (Get-ChildItem $ProjectPath\*\*.psd1 | Where-Object { - ($_.Directory.Name -match 'source|src' -or $_.Directory.Name -eq $_.BaseName) -and - $(try { Test-ModuleManifest $_.FullName -ErrorAction Stop }catch { $false }) } - ).Directory.FullName +BeforeDiscovery { + $script:moduleName = 'DscResource.Common' + + Remove-Module -Name $script:moduleName -Force -ErrorAction 'SilentlyContinue' + + $mut = Get-Module -Name $script:moduleName -ListAvailable | + Select-Object -First 1 | + Import-Module -Force -ErrorAction 'Stop' -PassThru +} -$mut = Import-Module -Name $ProjectName -ErrorAction Stop -PassThru -Force -$allModuleFunctions = &$mut {Get-Command -Module $args[0] -CommandType Function } $ProjectName +BeforeAll { + $script:moduleName = 'DscResource.Common' + + # Convert-Path required for PS7 or Join-Path fails + $projectPath = "$($PSScriptRoot)\..\.." | Convert-Path + + $sourcePath = ( + Get-ChildItem -Path $projectPath\*\*.psd1 | + Where-Object -FilterScript { + ($_.Directory.Name -match 'source|src' -or $_.Directory.Name -eq $_.BaseName) ` + -and $( + try + { + Test-ModuleManifest -Path $_.FullName -ErrorAction 'Stop' + } + catch + { + $false + } + ) + } + ).Directory.FullName +} Describe 'General module control' -Tags 'FunctionalQuality' { + It 'Should import without errors' { + { Import-Module -Name $script:moduleName -Force -ErrorAction Stop } | Should -Not -Throw - It 'imports without errors' { - { Import-Module -Name $ProjectName -Force -ErrorAction Stop } | Should -Not -Throw - Get-Module $ProjectName | Should -Not -BeNullOrEmpty + Get-Module -Name $script:moduleName | Should -Not -BeNullOrEmpty } - It 'Removes without error' { - { Remove-Module -Name $ProjectName -ErrorAction Stop } | Should -not -Throw - Get-Module $ProjectName | Should -beNullOrEmpty + It 'Should remove without error' { + { Remove-Module -Name $script:moduleName -ErrorAction Stop } | Should -Not -Throw + + Get-Module $script:moduleName | Should -BeNullOrEmpty } } -if (Get-Command Invoke-ScriptAnalyzer -ErrorAction SilentlyContinue) { - $scriptAnalyzerRules = Get-ScriptAnalyzerRule +BeforeDiscovery { + # Must use the imported module to build test cases. + $allModuleFunctions = & $mut { Get-Command -Module $args[0] -CommandType Function } $script:moduleName + + # Build test cases. + $testCases = @() + + foreach ($function in $allModuleFunctions) + { + $testCases += @{ + Name = $function.Name + } + } } -else { - if ($ErrorActionPreference -ne 'Stop') { - Write-Warning "ScriptAnalyzer not found!" + +Describe 'Quality for module' -Tags 'TestQuality' { + BeforeDiscovery { + if (Get-Command -Name Invoke-ScriptAnalyzer -ErrorAction SilentlyContinue) + { + $scriptAnalyzerRules = Get-ScriptAnalyzerRule + } + else + { + if ($ErrorActionPreference -ne 'Stop') + { + Write-Warning -Message 'ScriptAnalyzer not found!' + } + else + { + throw 'ScriptAnalyzer not found!' + } + } } - else { - Throw "ScriptAnalyzer not found!" + + It 'Should have a unit test for ' -TestCases $testCases { + Get-ChildItem -Path 'tests\' -Recurse -Include "$Name.Tests.ps1" | Should -Not -BeNullOrEmpty + } + + It 'Should pass Script Analyzer for ' -TestCases $testCases -Skip:(-not $scriptAnalyzerRules) { + $functionFile = Get-ChildItem -Path $sourcePath -Recurse -Include "$Name.ps1" + + $pssaResult = (Invoke-ScriptAnalyzer -Path $functionFile.FullName) + $report = $pssaResult | Format-Table -AutoSize | Out-String -Width 110 + $pssaResult | Should -BeNullOrEmpty -Because ` + "some rule triggered.`r`n`r`n $report" } } -foreach ($function in $allModuleFunctions) { - $functionFile = Get-ChildItem -path $SourcePath -Recurse -Include "$($function.Name).ps1" - Describe "Quality for $($function.Name)" -Tags 'TestQuality' { - It "$($function.Name) has a unit test" { - Get-ChildItem "tests\" -recurse -include "$($function.Name).Tests.ps1" | Should Not BeNullOrEmpty - } +Describe 'Help for module' -Tags 'helpQuality' { + It 'Should have .SYNOPSIS for ' -TestCases $testCases { + $functionFile = Get-ChildItem -Path $sourcePath -Recurse -Include "$Name.ps1" + + $scriptFileRawContent = Get-Content -Raw -Path $functionFile.FullName - if ($scriptAnalyzerRules) { - It "Script Analyzer for $($functionFile.FullName)" { - $PSSAResult = (Invoke-ScriptAnalyzer -Path $functionFile.FullName) - $Report = $PSSAResult | Format-Table -AutoSize | Out-String -Width 110 - $PSSAResult | Should -BeNullOrEmpty -Because ` - "some rule triggered.`r`n`r`n $Report" + $abstractSyntaxTree = [System.Management.Automation.Language.Parser]::ParseInput($scriptFileRawContent, [ref] $null, [ref] $null) + + $astSearchDelegate = { $args[0] -is [System.Management.Automation.Language.FunctionDefinitionAst] } + + $parsedFunction = $abstractSyntaxTree.FindAll( $astSearchDelegate, $true ) | + Where-Object -FilterScript { + $_.Name -eq $Name } - } + $functionHelp = $parsedFunction.GetHelpContent() + + $functionHelp.Synopsis | Should -Not -BeNullOrEmpty } - Describe "Help for $($function.Name)" -Tags 'helpQuality' { - $AbstractSyntaxTree = [System.Management.Automation.Language.Parser]:: - ParseInput((Get-Content -raw $functionFile.FullName), [ref]$null, [ref]$null) - $AstSearchDelegate = { $args[0] -is [System.Management.Automation.Language.FunctionDefinitionAst] } - $ParsedFunction = $AbstractSyntaxTree.FindAll( $AstSearchDelegate, $true ) | - ? Name -eq $function.Name + It 'Should have a .DESCRIPTION with length greater than 40 characters for ' -TestCases $testCases { + $functionFile = Get-ChildItem -Path $sourcePath -Recurse -Include "$Name.ps1" - $FunctionHelp = $ParsedFunction.GetHelpContent() + $scriptFileRawContent = Get-Content -Raw -Path $functionFile.FullName - It 'Has a SYNOPSIS' { - $FunctionHelp.Synopsis | should not BeNullOrEmpty - } + $abstractSyntaxTree = [System.Management.Automation.Language.Parser]::ParseInput($scriptFileRawContent, [ref] $null, [ref] $null) - It 'Has a Description, with length > 40' { - $FunctionHelp.Description.Length | Should beGreaterThan 40 - } + $astSearchDelegate = { $args[0] -is [System.Management.Automation.Language.FunctionDefinitionAst] } - It 'Has at least 1 example' { - $FunctionHelp.Examples.Count | Should beGreaterThan 0 - $FunctionHelp.Examples[0] | Should match ([regex]::Escape($function.Name)) - $FunctionHelp.Examples[0].Length | Should BeGreaterThan ($function.Name.Length + 10) - } + $parsedFunction = $abstractSyntaxTree.FindAll( $astSearchDelegate, $true ) | + Where-Object -FilterScript { + $_.Name -eq $Name + } + + $functionHelp = $parsedFunction.GetHelpContent() - $parameters = $ParsedFunction.Body.ParamBlock.Parameters.name.VariablePath.Foreach{ $_.ToString() } - foreach ($parameter in $parameters) { - It "Has help for Parameter: $parameter" { - $FunctionHelp.Parameters.($parameter.ToUpper()) | Should Not BeNullOrEmpty - $FunctionHelp.Parameters.($parameter.ToUpper()).Length | Should BeGreaterThan 25 + $functionHelp.Description.Length | Should -BeGreaterThan 40 + } + + It 'Should have at least one (1) example for ' -TestCases $testCases { + $functionFile = Get-ChildItem -Path $sourcePath -Recurse -Include "$Name.ps1" + + $scriptFileRawContent = Get-Content -Raw -Path $functionFile.FullName + + $abstractSyntaxTree = [System.Management.Automation.Language.Parser]::ParseInput($scriptFileRawContent, [ref] $null, [ref] $null) + + $astSearchDelegate = { $args[0] -is [System.Management.Automation.Language.FunctionDefinitionAst] } + + $parsedFunction = $abstractSyntaxTree.FindAll( $astSearchDelegate, $true ) | + Where-Object -FilterScript { + $_.Name -eq $Name } + + $functionHelp = $parsedFunction.GetHelpContent() + + $functionHelp.Examples.Count | Should -BeGreaterThan 0 + $functionHelp.Examples[0] | Should -Match ([regex]::Escape($function.Name)) + $functionHelp.Examples[0].Length | Should -BeGreaterThan ($function.Name.Length + 10) + + } + + It 'Should have described all parameters for ' -TestCases $testCases { + $functionFile = Get-ChildItem -Path $sourcePath -Recurse -Include "$Name.ps1" + + $scriptFileRawContent = Get-Content -Raw -Path $functionFile.FullName + + $abstractSyntaxTree = [System.Management.Automation.Language.Parser]::ParseInput($scriptFileRawContent, [ref] $null, [ref] $null) + + $astSearchDelegate = { $args[0] -is [System.Management.Automation.Language.FunctionDefinitionAst] } + + $parsedFunction = $abstractSyntaxTree.FindAll( $astSearchDelegate, $true ) | + Where-Object -FilterScript { + $_.Name -eq $Name + } + + $functionHelp = $parsedFunction.GetHelpContent() + + $parameters = $parsedFunction.Body.ParamBlock.Parameters.Name.VariablePath.ForEach({ $_.ToString() }) + + foreach ($parameter in $parameters) + { + $functionHelp.Parameters.($parameter.ToUpper()) | Should -Not -BeNullOrEmpty -Because ('the parameter {0} must have a description' -f $parameter) + $functionHelp.Parameters.($parameter.ToUpper()).Length | Should -BeGreaterThan 25 -Because ('the parameter {0} must have descriptive description' -f $parameter) } } } diff --git a/tests/Unit/Private/Test-DscObjectHasProperty.Tests.ps1 b/tests/Unit/Private/Test-DscObjectHasProperty.Tests.ps1 index 19f9f62..78af8f2 100644 --- a/tests/Unit/Private/Test-DscObjectHasProperty.Tests.ps1 +++ b/tests/Unit/Private/Test-DscObjectHasProperty.Tests.ps1 @@ -1,35 +1,62 @@ -$ProjectPath = "$PSScriptRoot\..\..\.." | Convert-Path -$ProjectName = ((Get-ChildItem -Path $ProjectPath\*\*.psd1).Where{ - ($_.Directory.Name -match 'source|src' -or $_.Directory.Name -eq $_.BaseName) -and - $(try { Test-ModuleManifest -Path $_.FullName -ErrorAction Stop } catch { $false } ) - }).BaseName +BeforeAll { + $script:moduleName = 'DscResource.Common' + Remove-Module -Name $script:moduleName -Force -ErrorAction 'SilentlyContinue' -Import-Module $ProjectName + Get-Module -Name $script:moduleName -ListAvailable | + Select-Object -First 1 | + Import-Module -Force -ErrorAction 'Stop' -InModuleScope $ProjectName { - Describe 'Test-DscObjectHasProperty' { + $PSDefaultParameterValues['InModuleScope:ModuleName'] = $script:moduleName +} - # Use the Get-Verb cmdlet to just get a simple object fast - $testDscObject = (Get-Verb)[0] +AfterAll { + $PSDefaultParameterValues.Remove('InModuleScope:ModuleName') +} - Context 'When the object contains the expected property' { - It 'Should not throw exception' { - { $script:result = Test-DscObjectHasProperty -Object $testDscObject -PropertyName 'Verb' -Verbose } | Should -Not -Throw +Describe 'Test-DscObjectHasProperty' { + Context 'When the object contains the expected property' { + BeforeAll { + # Use the Get-Verb cmdlet to just get a simple object fast + InModuleScope -ScriptBlock { + $script:testDscObject = (Get-Verb)[0] } + } - It 'Should return $true' { - $script:result | Should -Be $true + It 'Should not throw exception' { + InModuleScope -ScriptBlock { + { Test-DscObjectHasProperty -Object $script:testDscObject -PropertyName 'Verb' -Verbose } | Should -Not -Throw } } - Context 'When the object does not contain the expected property' { - It 'Should not throw exception' { - { $script:result = Test-DscObjectHasProperty -Object $testDscObject -PropertyName 'Missing' -Verbose } | Should -Not -Throw + It 'Should return $true' { + InModuleScope -ScriptBlock { + $result = Test-DscObjectHasProperty -Object $script:testDscObject -PropertyName 'Verb' -Verbose + + $result | Should -BeTrue } + } + } + + Context 'When the object does not contain the expected property' { + BeforeAll { + # Use the Get-Verb cmdlet to just get a simple object fast + InModuleScope -ScriptBlock { + $script:testDscObject = (Get-Verb)[0] + } + } + + It 'Should not throw exception' { + InModuleScope -ScriptBlock { + { Test-DscObjectHasProperty -Object $script:testDscObject -PropertyName 'Missing' -Verbose } | Should -Not -Throw + } + } + + It 'Should return $false' { + InModuleScope -ScriptBlock { + $result = Test-DscObjectHasProperty -Object $script:testDscObject -PropertyName 'Missing' -Verbose - It 'Should return $false' { - $script:result | Should -Be $false + $result | Should -BeFalse } } } diff --git a/tests/Unit/Private/Test-DscPropertyState.Tests.ps1 b/tests/Unit/Private/Test-DscPropertyState.Tests.ps1 index 43640a6..d0bbf11 100644 --- a/tests/Unit/Private/Test-DscPropertyState.Tests.ps1 +++ b/tests/Unit/Private/Test-DscPropertyState.Tests.ps1 @@ -1,25 +1,23 @@ -$ProjectPath = "$PSScriptRoot\..\..\.." | Convert-Path -$ProjectName = ((Get-ChildItem -Path $ProjectPath\*\*.psd1).Where{ - ($_.Directory.Name -match 'source|src' -or $_.Directory.Name -eq $_.BaseName) -and - $( - try - { - Test-ModuleManifest -Path $_.FullName -ErrorAction Stop - } - catch - { - $false - } - ) - }).BaseName +BeforeAll { + $script:moduleName = 'DscResource.Common' + + Remove-Module -Name $script:moduleName -Force -ErrorAction 'SilentlyContinue' + + Get-Module -Name $script:moduleName -ListAvailable | + Select-Object -First 1 | + Import-Module -Force -ErrorAction 'Stop' + $PSDefaultParameterValues['InModuleScope:ModuleName'] = $script:moduleName +} -Import-Module $ProjectName +AfterAll { + $PSDefaultParameterValues.Remove('InModuleScope:ModuleName') +} -InModuleScope $ProjectName { - Describe 'Test-DscPropertyState' -Tag 'TestDscPropertyState' { - Context 'When comparing tables' { - It 'Should return true for two identical tables' { +Describe 'Test-DscPropertyState' -Tag 'TestDscPropertyState' { + Context 'When comparing tables' { + It 'Should return true for two identical tables' { + InModuleScope -ScriptBlock { $mockValues = @{ CurrentValue = 'Test' DesiredValue = 'Test' @@ -28,9 +26,11 @@ InModuleScope $ProjectName { Test-DscPropertyState -Values $mockValues | Should -BeTrue } } + } - Context 'When comparing strings' { - It 'Should return false when a value is different for [System.String]' { + Context 'When comparing strings' { + It 'Should return false when a value is different for [System.String]' { + InModuleScope -ScriptBlock { $mockValues = @{ CurrentValue = [System.String] 'something' DesiredValue = [System.String] 'test' @@ -38,8 +38,10 @@ InModuleScope $ProjectName { Test-DscPropertyState -Values $mockValues | Should -BeFalse } + } - It 'Should return false when a String value is missing' { + It 'Should return false when a String value is missing' { + InModuleScope -ScriptBlock { $mockValues = @{ CurrentValue = $null DesiredValue = [System.String] 'Something' @@ -47,8 +49,10 @@ InModuleScope $ProjectName { Test-DscPropertyState -Values $mockValues | Should -BeFalse } + } - It 'Should return true when two strings are equal' { + It 'Should return true when two strings are equal' { + InModuleScope -ScriptBlock { $mockValues = @{ CurrentValue = [System.String] 'Something' DesiredValue = [System.String] 'Something' @@ -57,9 +61,11 @@ InModuleScope $ProjectName { Test-DscPropertyState -Values $mockValues | Should -Be $true } } + } - Context 'When comparing integers' { - It 'Should return false when a value is different for [System.Int32]' { + Context 'When comparing integers' { + It 'Should return false when a value is different for [System.Int32]' { + InModuleScope -ScriptBlock { $mockValues = @{ CurrentValue = [System.Int32] 1 DesiredValue = [System.Int32] 2 @@ -67,8 +73,10 @@ InModuleScope $ProjectName { Test-DscPropertyState -Values $mockValues | Should -BeFalse } + } - It 'Should return true when the values are the same for [System.Int32]' { + It 'Should return true when the values are the same for [System.Int32]' { + InModuleScope -ScriptBlock { $mockValues = @{ CurrentValue = [System.Int32] 2 DesiredValue = [System.Int32] 2 @@ -76,8 +84,10 @@ InModuleScope $ProjectName { Test-DscPropertyState -Values $mockValues | Should -Be $true } + } - It 'Should return false when a value is different for [System.UInt32]' { + It 'Should return false when a value is different for [System.UInt32]' { + InModuleScope -ScriptBlock { $mockValues = @{ CurrentValue = [System.UInt32] 1 DesiredValue = [System.UInt32] 2 @@ -85,8 +95,10 @@ InModuleScope $ProjectName { Test-DscPropertyState -Values $mockValues | Should -Be $false } + } - It 'Should return true when the values are the same for [System.UInt32]' { + It 'Should return true when the values are the same for [System.UInt32]' { + InModuleScope -ScriptBlock { $mockValues = @{ CurrentValue = [System.UInt32] 2 DesiredValue = [System.UInt32] 2 @@ -94,8 +106,10 @@ InModuleScope $ProjectName { Test-DscPropertyState -Values $mockValues | Should -Be $true } + } - It 'Should return false when a value is different for [System.Int16]' { + It 'Should return false when a value is different for [System.Int16]' { + InModuleScope -ScriptBlock { $mockValues = @{ CurrentValue = [System.Int16] 1 DesiredValue = [System.Int16] 2 @@ -103,8 +117,10 @@ InModuleScope $ProjectName { Test-DscPropertyState -Values $mockValues | Should -BeFalse } + } - It 'Should return true when the values are the same for [System.Int16]' { + It 'Should return true when the values are the same for [System.Int16]' { + InModuleScope -ScriptBlock { $mockValues = @{ CurrentValue = [System.Int16] 2 DesiredValue = [System.Int16] 2 @@ -112,8 +128,10 @@ InModuleScope $ProjectName { Test-DscPropertyState -Values $mockValues | Should -Be $true } + } - It 'Should return false when a value is different for [System.UInt16]' { + It 'Should return false when a value is different for [System.UInt16]' { + InModuleScope -ScriptBlock { $mockValues = @{ CurrentValue = [System.UInt16] 1 DesiredValue = [System.UInt16] 2 @@ -121,8 +139,10 @@ InModuleScope $ProjectName { Test-DscPropertyState -Values $mockValues | Should -BeFalse } + } - It 'Should return true when the values are the same for [System.UInt16]' { + It 'Should return true when the values are the same for [System.UInt16]' { + InModuleScope -ScriptBlock { $mockValues = @{ CurrentValue = [System.UInt16] 2 DesiredValue = [System.UInt16] 2 @@ -130,8 +150,10 @@ InModuleScope $ProjectName { Test-DscPropertyState -Values $mockValues | Should -Be $true } + } - It 'Should return false when a Integer value is missing' { + It 'Should return false when a Integer value is missing' { + InModuleScope -ScriptBlock { $mockValues = @{ CurrentValue = $null DesiredValue = [System.Int32] 1 @@ -140,9 +162,11 @@ InModuleScope $ProjectName { Test-DscPropertyState -Values $mockValues | Should -BeFalse } } + } - Context 'When comparing booleans' { - It 'Should return false when a value is different for [System.Boolean]' { + Context 'When comparing booleans' { + It 'Should return false when a value is different for [System.Boolean]' { + InModuleScope -ScriptBlock { $mockValues = @{ CurrentValue = [System.Boolean] $true DesiredValue = [System.Boolean] $false @@ -150,8 +174,10 @@ InModuleScope $ProjectName { Test-DscPropertyState -Values $mockValues | Should -BeFalse } + } - It 'Should return false when a Boolean value is missing' { + It 'Should return false when a Boolean value is missing' { + InModuleScope -ScriptBlock { $mockValues = @{ CurrentValue = $null DesiredValue = [System.Boolean] $true @@ -160,9 +186,11 @@ InModuleScope $ProjectName { Test-DscPropertyState -Values $mockValues | Should -BeFalse } } + } - Context 'When comparing arrays' { - It 'Should return true when evaluating an array' { + Context 'When comparing arrays' { + It 'Should return true when evaluating an array' { + InModuleScope -ScriptBlock { $mockValues = @{ CurrentValue = @('1', '2') DesiredValue = @('1', '2') @@ -170,8 +198,10 @@ InModuleScope $ProjectName { Test-DscPropertyState -Values $mockValues | Should -BeTrue } + } - It 'Should return false when evaluating an array with wrong values' { + It 'Should return false when evaluating an array with wrong values' { + InModuleScope -ScriptBlock { $mockValues = @{ CurrentValue = @('CurrentValueA', 'CurrentValueB') DesiredValue = @('DesiredValue1', 'DesiredValue2') @@ -179,8 +209,10 @@ InModuleScope $ProjectName { Test-DscPropertyState -Values $mockValues | Should -BeFalse } + } - It 'Should return false when evaluating an array, but the current value is $null' { + It 'Should return false when evaluating an array, but the current value is $null' { + InModuleScope -ScriptBlock { $mockValues = @{ CurrentValue = $null DesiredValue = @('1', '2') @@ -188,8 +220,10 @@ InModuleScope $ProjectName { Test-DscPropertyState -Values $mockValues | Should -BeFalse } + } - It 'Should return false when evaluating an array, but the desired value is $null' { + It 'Should return false when evaluating an array, but the desired value is $null' { + InModuleScope -ScriptBlock { $mockValues = @{ CurrentValue = @('1', '2') DesiredValue = $null @@ -197,8 +231,10 @@ InModuleScope $ProjectName { Test-DscPropertyState -Values $mockValues | Should -BeFalse } + } - It 'Should return false when evaluating an array, but the current value is an empty array' { + It 'Should return false when evaluating an array, but the current value is an empty array' { + InModuleScope -ScriptBlock { $mockValues = @{ CurrentValue = @() DesiredValue = @('1', '2') @@ -206,8 +242,10 @@ InModuleScope $ProjectName { Test-DscPropertyState -Values $mockValues | Should -BeFalse } + } - It 'Should return false when evaluating an array, but the desired value is an empty array' { + It 'Should return false when evaluating an array, but the desired value is an empty array' { + InModuleScope -ScriptBlock { $mockValues = @{ CurrentValue = @('1', '2') DesiredValue = @() @@ -215,8 +253,10 @@ InModuleScope $ProjectName { Test-DscPropertyState -Values $mockValues | Should -BeFalse } + } - It 'Should return true when evaluating an array, when both values are $null' { + It 'Should return true when evaluating an array, when both values are $null' { + InModuleScope -ScriptBlock { $mockValues = @{ CurrentValue = $null DesiredValue = $null @@ -224,8 +264,10 @@ InModuleScope $ProjectName { Test-DscPropertyState -Values $mockValues | Should -BeTrue } + } - It 'Should return true when evaluating an array, when both values are an empty array' { + It 'Should return true when evaluating an array, when both values are an empty array' { + InModuleScope -ScriptBlock { $mockValues = @{ CurrentValue = @() DesiredValue = @() @@ -234,22 +276,258 @@ InModuleScope $ProjectName { Test-DscPropertyState -Values $mockValues | Should -BeTrue } } + } + + # Skip on macOS and Linux. macOS and Linux does not support CimInstance. + Context 'When comparing a CIM instance' -Skip:(-not ($IsWindows -or $PSEdition -eq 'Desktop')) { + BeforeAll { + InModuleScope -ScriptBlock { + $script:mockClassName = 'DSC_MockResourceClassName' + $script:mockNamespace = 'root/microsoft/Windows/DesiredStateConfiguration' + } + } + + It 'Should return true when evaluating properties of a CIM instance' { + InModuleScope -ScriptBlock { + $currentCimInstanceParameters = @{ + ClassName = $mockClassName + Namespace = $mockNamespace + Property = @{ + State = 'Deny' + Permission = @('Delete', 'Select') + Ensure = 'Present' + } + ClientOnly = $true + } + + $desiredCimInstanceParameters = @{ + ClassName = $mockClassName + Namespace = $mockNamespace + Property = @{ + State = 'Deny' + Permission = @('Delete', 'Select') + Ensure = 'Present' + } + ClientOnly = $true + } + + $mockValues = @{ + CurrentValue = New-CimInstance @currentCimInstanceParameters + DesiredValue = New-CimInstance @desiredCimInstanceParameters + } + + Test-DscPropertyState -Values $mockValues | Should -BeTrue + } + } + + It 'Should return false when evaluating a CIM instance property that is an array with wrong values' { + InModuleScope -ScriptBlock { + $currentCimInstanceParameters = @{ + ClassName = $mockClassName + Namespace = $mockNamespace + Property = @{ + State = 'Deny' + Permission = @('Delete', 'Select') + Ensure = 'Present' + } + ClientOnly = $true + } + + $desiredCimInstanceParameters = @{ + ClassName = $mockClassName + Namespace = $mockNamespace + Property = @{ + State = 'Deny' + Permission = @('Delete', 'Update') + Ensure = 'Present' + } + ClientOnly = $true + } + + $mockValues = @{ + CurrentValue = New-CimInstance @currentCimInstanceParameters + DesiredValue = New-CimInstance @desiredCimInstanceParameters + } + + Test-DscPropertyState -Values $mockValues | Should -BeFalse + } + } + + It 'Should return false when evaluating a CIM instance property that is a string with wrong value' { + InModuleScope -ScriptBlock { + $currentCimInstanceParameters = @{ + ClassName = $mockClassName + Namespace = $mockNamespace + Property = @{ + State = 'Deny' + Permission = @('Delete', 'Select') + Ensure = 'Present' + } + ClientOnly = $true + } + + $desiredCimInstanceParameters = @{ + ClassName = $mockClassName + Namespace = $mockNamespace + Property = @{ + State = 'Grant' + Permission = @('Delete', 'Select') + Ensure = 'Present' + } + ClientOnly = $true + } + + $mockValues = @{ + CurrentValue = New-CimInstance @currentCimInstanceParameters + DesiredValue = New-CimInstance @desiredCimInstanceParameters + } + + Test-DscPropertyState -Values $mockValues | Should -BeFalse + } + } + + It 'Should return false when evaluating a CIM instance, but the current value is $null' { + InModuleScope -ScriptBlock { + $desiredCimInstanceParameters = @{ + ClassName = $mockClassName + Namespace = $mockNamespace + Property = @{ + State = 'Grant' + Permission = @('Delete', 'Select') + Ensure = 'Present' + } + ClientOnly = $true + } + + $mockValues = @{ + CurrentValue = $null + DesiredValue = New-CimInstance @desiredCimInstanceParameters + } + + Test-DscPropertyState -Values $mockValues | Should -BeFalse + } + } + + It 'Should return false when evaluating a CIM instance, but the desired value is $null' { + InModuleScope -ScriptBlock { + $currentCimInstanceParameters = @{ + ClassName = $mockClassName + Namespace = $mockNamespace + Property = @{ + State = 'Deny' + Permission = @('Delete', 'Select') + Ensure = 'Present' + } + ClientOnly = $true + } + + $mockValues = @{ + CurrentValue = New-CimInstance @currentCimInstanceParameters + DesiredValue = $null + } + + Test-DscPropertyState -Values $mockValues | Should -BeFalse + } + } + + It 'Should return false when evaluating a CIM instance, but the current CIM instance does not have any properties' { + InModuleScope -ScriptBlock { + $currentCimInstanceParameters = @{ + ClassName = $mockClassName + Namespace = $mockNamespace + Property = @{} + ClientOnly = $true + } + + $desiredCimInstanceParameters = @{ + ClassName = $mockClassName + Namespace = $mockNamespace + Property = @{ + State = 'Grant' + Permission = @('Delete', 'Select') + Ensure = 'Present' + } + ClientOnly = $true + } + + $mockValues = @{ + CurrentValue = New-CimInstance @currentCimInstanceParameters + DesiredValue = New-CimInstance @desiredCimInstanceParameters + } + + Test-DscPropertyState -Values $mockValues | Should -BeFalse + } + } + + It 'Should return false when evaluating a CIM instance, but the desired CIM instance does not have any properties' { + InModuleScope -ScriptBlock { + $currentCimInstanceParameters = @{ + ClassName = $mockClassName + Namespace = $mockNamespace + Property = @{ + State = 'Grant' + Permission = @('Delete', 'Select') + Ensure = 'Present' + } + ClientOnly = $true + } + + $desiredCimInstanceParameters = @{ + ClassName = $mockClassName + Namespace = $mockNamespace + Property = @{} + ClientOnly = $true + } + + $mockValues = @{ + CurrentValue = New-CimInstance @currentCimInstanceParameters + DesiredValue = New-CimInstance @desiredCimInstanceParameters + } + + Test-DscPropertyState -Values $mockValues | Should -BeFalse + } + } - # macOS and Linux does not support CimInstance. - if ($IsWindows -or $PSEdition -eq 'Desktop') - { - Context 'When comparing a CIM instance' { - BeforeAll { - $mockClassName = 'DSC_MockResourceClassName' - $mockNamespace = 'root/microsoft/Windows/DesiredStateConfiguration' + It 'Should return false when evaluating a CIM instance, but the current CIM instance is missing a property' { + InModuleScope -ScriptBlock { + $currentCimInstanceParameters = @{ + ClassName = $mockClassName + Namespace = $mockNamespace + Property = @{ + State = 'Grant' + Ensure = 'Present' + } + ClientOnly = $true + } + + $desiredCimInstanceParameters = @{ + ClassName = $mockClassName + Namespace = $mockNamespace + Property = @{ + State = 'Grant' + Permission = @('Delete', 'Select') + Ensure = 'Present' + } + ClientOnly = $true } - It 'Should return true when evaluating properties of a CIM instance' { + $mockValues = @{ + CurrentValue = New-CimInstance @currentCimInstanceParameters + DesiredValue = New-CimInstance @desiredCimInstanceParameters + } + + Test-DscPropertyState -Values $mockValues | Should -BeFalse + } + } + + Context 'When the desired CIM instance has less properties than the current CIM properties' { + It 'Should return true when evaluating a CIM instance where all CIM properties are in desired state' { + InModuleScope -ScriptBlock { $currentCimInstanceParameters = @{ ClassName = $mockClassName Namespace = $mockNamespace Property = @{ - State = 'Deny' + State = 'Grant' Permission = @('Delete', 'Select') Ensure = 'Present' } @@ -260,9 +538,8 @@ InModuleScope $ProjectName { ClassName = $mockClassName Namespace = $mockNamespace Property = @{ - State = 'Deny' - Permission = @('Delete', 'Select') - Ensure = 'Present' + State = 'Grant' + Ensure = 'Present' } ClientOnly = $true } @@ -274,13 +551,15 @@ InModuleScope $ProjectName { Test-DscPropertyState -Values $mockValues | Should -BeTrue } + } - It 'Should return false when evaluating a CIM instance property that is an array with wrong values' { + It 'Should return false when evaluating a CIM instance when a CIM property is not in desired state' { + InModuleScope -ScriptBlock { $currentCimInstanceParameters = @{ ClassName = $mockClassName Namespace = $mockNamespace Property = @{ - State = 'Deny' + State = 'Grant' Permission = @('Delete', 'Select') Ensure = 'Present' } @@ -291,9 +570,8 @@ InModuleScope $ProjectName { ClassName = $mockClassName Namespace = $mockNamespace Property = @{ - State = 'Deny' - Permission = @('Delete', 'Update') - Ensure = 'Present' + State = 'Deny' + Ensure = 'Present' } ClientOnly = $true } @@ -305,8 +583,54 @@ InModuleScope $ProjectName { Test-DscPropertyState -Values $mockValues | Should -BeFalse } + } + } + + It 'Should return true when evaluating a CIM instance, when both current and desired value does not have any CIM properties' { + InModuleScope -ScriptBlock { + $currentCimInstanceParameters = @{ + ClassName = $mockClassName + Namespace = $mockNamespace + Property = @{} + ClientOnly = $true + } + + $desiredCimInstanceParameters = @{ + ClassName = $mockClassName + Namespace = $mockNamespace + Property = @{} + ClientOnly = $true + } - It 'Should return false when evaluating a CIM instance property that is a string with wrong value' { + $mockValues = @{ + CurrentValue = New-CimInstance @currentCimInstanceParameters + DesiredValue = New-CimInstance @desiredCimInstanceParameters + } + + Test-DscPropertyState -Values $mockValues | Should -BeTrue + } + } + } + + Context 'When comparing a CIM instance collection' -Skip:(-not ($IsWindows -or $PSEdition -eq 'Desktop')) { + BeforeAll { + InModuleScope -ScriptBlock { + $script:mockTypeName = 'System.Collections.ObjectModel.Collection`1[Microsoft.Management.Infrastructure.CimInstance]' + $script:mockClassName = 'DSC_MockResourceClassName' + $script:mockNamespace = 'root/microsoft/Windows/DesiredStateConfiguration' + } + } + + BeforeEach { + InModuleScope -ScriptBlock { + $script:currentCimInstancePermissionCollection = New-Object -TypeName $script:mockTypeName + $script:desiredCimInstancePermissionCollection = New-Object -TypeName $script:mockTypeName + } + } + + Context 'When not passing the key ''KeyProperties'' in the hashtable' { + It 'Should throw the correct error message' { + InModuleScope -ScriptBlock { $currentCimInstanceParameters = @{ ClassName = $mockClassName Namespace = $mockNamespace @@ -322,43 +646,43 @@ InModuleScope $ProjectName { ClassName = $mockClassName Namespace = $mockNamespace Property = @{ - State = 'Grant' + State = 'Deny' Permission = @('Delete', 'Select') Ensure = 'Present' } ClientOnly = $true } + $script:currentCimInstancePermissionCollection += New-CimInstance @currentCimInstanceParameters + $script:desiredCimInstancePermissionCollection += New-CimInstance @desiredCimInstanceParameters + $mockValues = @{ - CurrentValue = New-CimInstance @currentCimInstanceParameters - DesiredValue = New-CimInstance @desiredCimInstanceParameters + CurrentValue = $currentCimInstancePermissionCollection + DesiredValue = $desiredCimInstancePermissionCollection } - Test-DscPropertyState -Values $mockValues | Should -BeFalse + $mockErrorRecord = Get-InvalidOperationRecord -Message $script:localizedData.KeyPropertiesMissing + + { Test-DscPropertyState -Values $mockValues } | Should -Throw -ExpectedMessage $mockErrorRecord.Exception.Message } + } + } - It 'Should return false when evaluating a CIM instance, but the current value is $null' { - $desiredCimInstanceParameters = @{ + Context 'When current value contain two CIM instances that have the same ''KeyProperties''' { + It 'Should throw the correct error message' { + InModuleScope -ScriptBlock { + $currentCimInstanceParameters = @{ ClassName = $mockClassName Namespace = $mockNamespace Property = @{ - State = 'Grant' + State = 'Deny' Permission = @('Delete', 'Select') Ensure = 'Present' } ClientOnly = $true } - $mockValues = @{ - CurrentValue = $null - DesiredValue = New-CimInstance @desiredCimInstanceParameters - } - - Test-DscPropertyState -Values $mockValues | Should -BeFalse - } - - It 'Should return false when evaluating a CIM instance, but the desired value is $null' { - $currentCimInstanceParameters = @{ + $desiredCimInstanceParameters = @{ ClassName = $mockClassName Namespace = $mockNamespace Property = @{ @@ -369,19 +693,36 @@ InModuleScope $ProjectName { ClientOnly = $true } + $script:currentCimInstancePermissionCollection += New-CimInstance @currentCimInstanceParameters + $script:currentCimInstancePermissionCollection += New-CimInstance @currentCimInstanceParameters + + $script:desiredCimInstancePermissionCollection += New-CimInstance @desiredCimInstanceParameters + $script:desiredCimInstancePermissionCollection += New-CimInstance @desiredCimInstanceParameters + $mockValues = @{ - CurrentValue = New-CimInstance @currentCimInstanceParameters - DesiredValue = $null + CurrentValue = $currentCimInstancePermissionCollection + DesiredValue = $desiredCimInstancePermissionCollection + KeyProperties = @('State') } - Test-DscPropertyState -Values $mockValues | Should -BeFalse + $mockErrorRecord = Get-InvalidOperationRecord -Message $script:localizedData.TooManyCimInstances + + { Test-DscPropertyState -Values $mockValues } | Should -Throw -ExpectedMessage $mockErrorRecord.Exception.Message } + } + } - It 'Should return false when evaluating a CIM instance, but the current CIM instance does not have any properties' { + Context 'When current and desired collection contain one CIM instance each with equally property values' { + It 'Should return true when evaluating properties of each CIM instance of a collection of CIM instances' { + InModuleScope -ScriptBlock { $currentCimInstanceParameters = @{ ClassName = $mockClassName Namespace = $mockNamespace - Property = @{} + Property = @{ + State = 'Deny' + Permission = @('Delete', 'Select') + Ensure = 'Present' + } ClientOnly = $true } @@ -389,23 +730,31 @@ InModuleScope $ProjectName { ClassName = $mockClassName Namespace = $mockNamespace Property = @{ - State = 'Grant' + State = 'Deny' Permission = @('Delete', 'Select') Ensure = 'Present' } ClientOnly = $true } + $script:currentCimInstancePermissionCollection += New-CimInstance @currentCimInstanceParameters + $script:desiredCimInstancePermissionCollection += New-CimInstance @desiredCimInstanceParameters + $mockValues = @{ - CurrentValue = New-CimInstance @currentCimInstanceParameters - DesiredValue = New-CimInstance @desiredCimInstanceParameters + CurrentValue = $currentCimInstancePermissionCollection + DesiredValue = $desiredCimInstancePermissionCollection + KeyProperties = @('State') } - Test-DscPropertyState -Values $mockValues | Should -BeFalse + Test-DscPropertyState -Values $mockValues | Should -BeTrue } + } + } - It 'Should return false when evaluating a CIM instance, but the desired CIM instance does not have any properties' { - $currentCimInstanceParameters = @{ + Context 'When current and desired collection contain two CIM instance each with equally property values' { + It 'Should return true when evaluating properties of each CIM instance of a collection of CIM instances' { + InModuleScope -ScriptBlock { + $currentCimInstanceParameters1 = @{ ClassName = $mockClassName Namespace = $mockNamespace Property = @{ @@ -416,639 +765,383 @@ InModuleScope $ProjectName { ClientOnly = $true } - $desiredCimInstanceParameters = @{ + $currentCimInstanceParameters2 = @{ ClassName = $mockClassName Namespace = $mockNamespace - Property = @{} + Property = @{ + State = 'Deny' + Permission = @('Drop') + Ensure = 'Present' + } ClientOnly = $true } - $mockValues = @{ - CurrentValue = New-CimInstance @currentCimInstanceParameters - DesiredValue = New-CimInstance @desiredCimInstanceParameters - } - - Test-DscPropertyState -Values $mockValues | Should -BeFalse - } - - It 'Should return false when evaluating a CIM instance, but the current CIM instance is missing a property' { - $currentCimInstanceParameters = @{ + $desiredCimInstanceParameters1 = @{ ClassName = $mockClassName Namespace = $mockNamespace Property = @{ - State = 'Grant' - Ensure = 'Present' + State = 'Grant' + Permission = @('Delete', 'Select') + Ensure = 'Present' } ClientOnly = $true } - $desiredCimInstanceParameters = @{ + $desiredCimInstanceParameters2 = @{ ClassName = $mockClassName Namespace = $mockNamespace Property = @{ - State = 'Grant' - Permission = @('Delete', 'Select') + State = 'Deny' + Permission = @('Drop') Ensure = 'Present' } ClientOnly = $true } - $mockValues = @{ - CurrentValue = New-CimInstance @currentCimInstanceParameters - DesiredValue = New-CimInstance @desiredCimInstanceParameters - } + $script:currentCimInstancePermissionCollection += New-CimInstance @currentCimInstanceParameters1 + $script:currentCimInstancePermissionCollection += New-CimInstance @currentCimInstanceParameters2 - Test-DscPropertyState -Values $mockValues | Should -BeFalse - } + $script:desiredCimInstancePermissionCollection += New-CimInstance @desiredCimInstanceParameters1 + $script:desiredCimInstancePermissionCollection += New-CimInstance @desiredCimInstanceParameters2 - Context 'When the desired CIM instance has less properties than the current CIM properties' { - It 'Should return true when evaluating a CIM instance where all CIM properties are in desired state' { - $currentCimInstanceParameters = @{ - ClassName = $mockClassName - Namespace = $mockNamespace - Property = @{ - State = 'Grant' - Permission = @('Delete', 'Select') - Ensure = 'Present' - } - ClientOnly = $true - } - - $desiredCimInstanceParameters = @{ - ClassName = $mockClassName - Namespace = $mockNamespace - Property = @{ - State = 'Grant' - Ensure = 'Present' - } - ClientOnly = $true - } - - $mockValues = @{ - CurrentValue = New-CimInstance @currentCimInstanceParameters - DesiredValue = New-CimInstance @desiredCimInstanceParameters - } - - Test-DscPropertyState -Values $mockValues | Should -BeTrue + $mockValues = @{ + CurrentValue = $currentCimInstancePermissionCollection + DesiredValue = $desiredCimInstancePermissionCollection + KeyProperties = @('State') } - It 'Should return false when evaluating a CIM instance when a CIM property is not in desired state' { - $currentCimInstanceParameters = @{ - ClassName = $mockClassName - Namespace = $mockNamespace - Property = @{ - State = 'Grant' - Permission = @('Delete', 'Select') - Ensure = 'Present' - } - ClientOnly = $true - } - - $desiredCimInstanceParameters = @{ - ClassName = $mockClassName - Namespace = $mockNamespace - Property = @{ - State = 'Deny' - Ensure = 'Present' - } - ClientOnly = $true - } + Test-DscPropertyState -Values $mockValues | Should -BeTrue + } + } + } - $mockValues = @{ - CurrentValue = New-CimInstance @currentCimInstanceParameters - DesiredValue = New-CimInstance @desiredCimInstanceParameters + Context 'When current CIM instance collection have more CIM instance than the desired state, but with equally property values' { + It 'Should return true when evaluating properties of each CIM instance of a collection of CIM instances' { + InModuleScope -ScriptBlock { + $currentCimInstanceParameters1 = @{ + ClassName = $mockClassName + Namespace = $mockNamespace + Property = @{ + State = 'Grant' + Permission = @('Delete', 'Select') + Ensure = 'Present' } - - Test-DscPropertyState -Values $mockValues | Should -BeFalse + ClientOnly = $true } - } - It 'Should return true when evaluating a CIM instance, when both current and desired value does not have any CIM properties' { - $currentCimInstanceParameters = @{ + $currentCimInstanceParameters2 = @{ ClassName = $mockClassName Namespace = $mockNamespace - Property = @{} + Property = @{ + State = 'Deny' + Permission = @('Drop') + Ensure = 'Present' + } ClientOnly = $true } - $desiredCimInstanceParameters = @{ + $desiredCimInstanceParameters1 = @{ ClassName = $mockClassName Namespace = $mockNamespace - Property = @{} + Property = @{ + State = 'Grant' + Permission = @('Delete', 'Select') + Ensure = 'Present' + } ClientOnly = $true } + $script:currentCimInstancePermissionCollection += New-CimInstance @currentCimInstanceParameters1 + $script:currentCimInstancePermissionCollection += New-CimInstance @currentCimInstanceParameters2 + + $script:desiredCimInstancePermissionCollection += New-CimInstance @desiredCimInstanceParameters1 + $mockValues = @{ - CurrentValue = New-CimInstance @currentCimInstanceParameters - DesiredValue = New-CimInstance @desiredCimInstanceParameters + CurrentValue = $currentCimInstancePermissionCollection + DesiredValue = $desiredCimInstancePermissionCollection + KeyProperties = @('State') } Test-DscPropertyState -Values $mockValues | Should -BeTrue } } + } - Context 'When comparing a CIM instance collection' { - BeforeAll { - $mockTypeName = 'System.Collections.ObjectModel.Collection`1[Microsoft.Management.Infrastructure.CimInstance]' - $mockClassName = 'DSC_MockResourceClassName' - $mockNamespace = 'root/microsoft/Windows/DesiredStateConfiguration' - } - - BeforeEach { - $currentCimInstancePermissionCollection = New-Object -TypeName $mockTypeName - $desiredCimInstancePermissionCollection = New-Object -TypeName $mockTypeName - } - - Context 'When not passing the key ''KeyProperties'' in the hashtable' { - It 'Should throw the correct error message' { - $currentCimInstanceParameters = @{ - ClassName = $mockClassName - Namespace = $mockNamespace - Property = @{ - State = 'Deny' - Permission = @('Delete', 'Select') - Ensure = 'Present' - } - ClientOnly = $true - } - - $desiredCimInstanceParameters = @{ - ClassName = $mockClassName - Namespace = $mockNamespace - Property = @{ - State = 'Deny' - Permission = @('Delete', 'Select') - Ensure = 'Present' - } - ClientOnly = $true - } - - $currentCimInstancePermissionCollection += New-CimInstance @currentCimInstanceParameters - $desiredCimInstancePermissionCollection += New-CimInstance @desiredCimInstanceParameters - - $mockValues = @{ - CurrentValue = $currentCimInstancePermissionCollection - DesiredValue = $desiredCimInstancePermissionCollection + Context 'When desired CIM instance collection have more CIM instance than the current state' { + It 'Should return false when evaluating properties of each CIM instance of a collection of CIM instances' { + InModuleScope -ScriptBlock { + $currentCimInstanceParameters1 = @{ + ClassName = $mockClassName + Namespace = $mockNamespace + Property = @{ + State = 'Grant' + Permission = @('Delete', 'Select') + Ensure = 'Present' } - - { Test-DscPropertyState -Values $mockValues } | Should -Throw $script:localizedData.KeyPropertiesMissing + ClientOnly = $true } - } - Context 'When current value contain two CIM instances that have the same ''KeyProperties''' { - It 'Should throw the correct error message' { - $currentCimInstanceParameters = @{ - ClassName = $mockClassName - Namespace = $mockNamespace - Property = @{ - State = 'Deny' - Permission = @('Delete', 'Select') - Ensure = 'Present' - } - ClientOnly = $true - } - $desiredCimInstanceParameters = @{ - ClassName = $mockClassName - Namespace = $mockNamespace - Property = @{ - State = 'Deny' - Permission = @('Delete', 'Select') - Ensure = 'Present' - } - ClientOnly = $true - } - - $currentCimInstancePermissionCollection += New-CimInstance @currentCimInstanceParameters - $currentCimInstancePermissionCollection += New-CimInstance @currentCimInstanceParameters - - $desiredCimInstancePermissionCollection += New-CimInstance @desiredCimInstanceParameters - $desiredCimInstancePermissionCollection += New-CimInstance @desiredCimInstanceParameters - - $mockValues = @{ - CurrentValue = $currentCimInstancePermissionCollection - DesiredValue = $desiredCimInstancePermissionCollection - KeyProperties = @('State') + $desiredCimInstanceParameters1 = @{ + ClassName = $mockClassName + Namespace = $mockNamespace + Property = @{ + State = 'Grant' + Permission = @('Delete', 'Select') + Ensure = 'Present' } - - { Test-DscPropertyState -Values $mockValues } | Should -Throw $script:localizedData.TooManyCimInstances + ClientOnly = $true } - } - Context 'When current and desired collection contain one CIM instance each with equally property values' { - It 'Should return true when evaluating properties of each CIM instance of a collection of CIM instances' { - $currentCimInstanceParameters = @{ - ClassName = $mockClassName - Namespace = $mockNamespace - Property = @{ - State = 'Deny' - Permission = @('Delete', 'Select') - Ensure = 'Present' - } - ClientOnly = $true - } - - $desiredCimInstanceParameters = @{ - ClassName = $mockClassName - Namespace = $mockNamespace - Property = @{ - State = 'Deny' - Permission = @('Delete', 'Select') - Ensure = 'Present' - } - ClientOnly = $true - } - - $currentCimInstancePermissionCollection += New-CimInstance @currentCimInstanceParameters - $desiredCimInstancePermissionCollection += New-CimInstance @desiredCimInstanceParameters - - $mockValues = @{ - CurrentValue = $currentCimInstancePermissionCollection - DesiredValue = $desiredCimInstancePermissionCollection - KeyProperties = @('State') + $desiredCimInstanceParameters2 = @{ + ClassName = $mockClassName + Namespace = $mockNamespace + Property = @{ + State = 'Deny' + Permission = @('Drop') + Ensure = 'Present' } - - Test-DscPropertyState -Values $mockValues | Should -BeTrue + ClientOnly = $true } - } - - Context 'When current and desired collection contain two CIM instance each with equally property values' { - It 'Should return true when evaluating properties of each CIM instance of a collection of CIM instances' { - $currentCimInstanceParameters1 = @{ - ClassName = $mockClassName - Namespace = $mockNamespace - Property = @{ - State = 'Grant' - Permission = @('Delete', 'Select') - Ensure = 'Present' - } - ClientOnly = $true - } - - $currentCimInstanceParameters2 = @{ - ClassName = $mockClassName - Namespace = $mockNamespace - Property = @{ - State = 'Deny' - Permission = @('Drop') - Ensure = 'Present' - } - ClientOnly = $true - } - $desiredCimInstanceParameters1 = @{ - ClassName = $mockClassName - Namespace = $mockNamespace - Property = @{ - State = 'Grant' - Permission = @('Delete', 'Select') - Ensure = 'Present' - } - ClientOnly = $true - } - - $desiredCimInstanceParameters2 = @{ - ClassName = $mockClassName - Namespace = $mockNamespace - Property = @{ - State = 'Deny' - Permission = @('Drop') - Ensure = 'Present' - } - ClientOnly = $true - } - - $currentCimInstancePermissionCollection += New-CimInstance @currentCimInstanceParameters1 - $currentCimInstancePermissionCollection += New-CimInstance @currentCimInstanceParameters2 + $script:currentCimInstancePermissionCollection += New-CimInstance @currentCimInstanceParameters1 - $desiredCimInstancePermissionCollection += New-CimInstance @desiredCimInstanceParameters1 - $desiredCimInstancePermissionCollection += New-CimInstance @desiredCimInstanceParameters2 + $script:desiredCimInstancePermissionCollection += New-CimInstance @desiredCimInstanceParameters1 + $script:desiredCimInstancePermissionCollection += New-CimInstance @desiredCimInstanceParameters2 - $mockValues = @{ - CurrentValue = $currentCimInstancePermissionCollection - DesiredValue = $desiredCimInstancePermissionCollection - KeyProperties = @('State') - } - - Test-DscPropertyState -Values $mockValues | Should -BeTrue + $mockValues = @{ + CurrentValue = $currentCimInstancePermissionCollection + DesiredValue = $desiredCimInstancePermissionCollection + KeyProperties = @('State') } - } - - Context 'When current CIM instance collection have more CIM instance than the desired state, but with equally property values' { - It 'Should return true when evaluating properties of each CIM instance of a collection of CIM instances' { - $currentCimInstanceParameters1 = @{ - ClassName = $mockClassName - Namespace = $mockNamespace - Property = @{ - State = 'Grant' - Permission = @('Delete', 'Select') - Ensure = 'Present' - } - ClientOnly = $true - } - - $currentCimInstanceParameters2 = @{ - ClassName = $mockClassName - Namespace = $mockNamespace - Property = @{ - State = 'Deny' - Permission = @('Drop') - Ensure = 'Present' - } - ClientOnly = $true - } - - $desiredCimInstanceParameters1 = @{ - ClassName = $mockClassName - Namespace = $mockNamespace - Property = @{ - State = 'Grant' - Permission = @('Delete', 'Select') - Ensure = 'Present' - } - ClientOnly = $true - } - - $currentCimInstancePermissionCollection += New-CimInstance @currentCimInstanceParameters1 - $currentCimInstancePermissionCollection += New-CimInstance @currentCimInstanceParameters2 - $desiredCimInstancePermissionCollection += New-CimInstance @desiredCimInstanceParameters1 - - $mockValues = @{ - CurrentValue = $currentCimInstancePermissionCollection - DesiredValue = $desiredCimInstancePermissionCollection - KeyProperties = @('State') - } - - Test-DscPropertyState -Values $mockValues | Should -BeTrue - } + Test-DscPropertyState -Values $mockValues | Should -BeFalse } + } + } - Context 'When desired CIM instance collection have more CIM instance than the current state' { - It 'Should return false when evaluating properties of each CIM instance of a collection of CIM instances' { - $currentCimInstanceParameters1 = @{ - ClassName = $mockClassName - Namespace = $mockNamespace - Property = @{ - State = 'Grant' - Permission = @('Delete', 'Select') - Ensure = 'Present' - } - ClientOnly = $true - } - - - $desiredCimInstanceParameters1 = @{ - ClassName = $mockClassName - Namespace = $mockNamespace - Property = @{ - State = 'Grant' - Permission = @('Delete', 'Select') - Ensure = 'Present' - } - ClientOnly = $true - } - - $desiredCimInstanceParameters2 = @{ - ClassName = $mockClassName - Namespace = $mockNamespace - Property = @{ - State = 'Deny' - Permission = @('Drop') - Ensure = 'Present' - } - ClientOnly = $true - } - - $currentCimInstancePermissionCollection += New-CimInstance @currentCimInstanceParameters1 - - $desiredCimInstancePermissionCollection += New-CimInstance @desiredCimInstanceParameters1 - $desiredCimInstancePermissionCollection += New-CimInstance @desiredCimInstanceParameters2 - - $mockValues = @{ - CurrentValue = $currentCimInstancePermissionCollection - DesiredValue = $desiredCimInstancePermissionCollection - KeyProperties = @('State') + Context 'When desired CIM instance collection have more CIM instance than the current state, and the CIM instances in the current state is not in desired state' { + It 'Should return false when evaluating properties of each CIM instance of a collection of CIM instances' { + InModuleScope -ScriptBlock { + $currentCimInstanceParameters1 = @{ + ClassName = $mockClassName + Namespace = $mockNamespace + Property = @{ + State = 'Grant' + Permission = @('Delete', 'Select') + Ensure = 'Present' } - - Test-DscPropertyState -Values $mockValues | Should -BeFalse + ClientOnly = $true } - } - Context 'When desired CIM instance collection have more CIM instance than the current state, and the CIM instances in the current state is not in desired state' { - It 'Should return false when evaluating properties of each CIM instance of a collection of CIM instances' { - $currentCimInstanceParameters1 = @{ - ClassName = $mockClassName - Namespace = $mockNamespace - Property = @{ - State = 'Grant' - Permission = @('Delete', 'Select') - Ensure = 'Present' - } - ClientOnly = $true - } - - $desiredCimInstanceParameters1 = @{ - ClassName = $mockClassName - Namespace = $mockNamespace - Property = @{ - State = 'Grant' - Permission = @('Delete', 'Select', 'Update') - Ensure = 'Present' - } - ClientOnly = $true + $desiredCimInstanceParameters1 = @{ + ClassName = $mockClassName + Namespace = $mockNamespace + Property = @{ + State = 'Grant' + Permission = @('Delete', 'Select', 'Update') + Ensure = 'Present' } + ClientOnly = $true + } - $desiredCimInstanceParameters2 = @{ - ClassName = $mockClassName - Namespace = $mockNamespace - Property = @{ - State = 'Deny' - Permission = @('Drop') - Ensure = 'Present' - } - ClientOnly = $true + $desiredCimInstanceParameters2 = @{ + ClassName = $mockClassName + Namespace = $mockNamespace + Property = @{ + State = 'Deny' + Permission = @('Drop') + Ensure = 'Present' } + ClientOnly = $true + } - $currentCimInstancePermissionCollection += New-CimInstance @currentCimInstanceParameters1 - - $desiredCimInstancePermissionCollection += New-CimInstance @desiredCimInstanceParameters1 - $desiredCimInstancePermissionCollection += New-CimInstance @desiredCimInstanceParameters2 + $script:currentCimInstancePermissionCollection += New-CimInstance @currentCimInstanceParameters1 - $mockValues = @{ - CurrentValue = $currentCimInstancePermissionCollection - DesiredValue = $desiredCimInstancePermissionCollection - KeyProperties = @('State') - } + $script:desiredCimInstancePermissionCollection += New-CimInstance @desiredCimInstanceParameters1 + $script:desiredCimInstancePermissionCollection += New-CimInstance @desiredCimInstanceParameters2 - Test-DscPropertyState -Values $mockValues | Should -BeFalse + $mockValues = @{ + CurrentValue = $currentCimInstancePermissionCollection + DesiredValue = $desiredCimInstancePermissionCollection + KeyProperties = @('State') } + + Test-DscPropertyState -Values $mockValues | Should -BeFalse } + } + } - <# - There is only need to test empty collection in the current state - because the desired state must always provide at least one item. - #> - Context 'When current CIM instance collection have no CIM instances' { - It 'Should return false when evaluating properties of each CIM instance of a collection of CIM instances' { - $desiredCimInstanceParameters = @{ - ClassName = $mockClassName - Namespace = $mockNamespace - Property = @{ - State = 'Grant' - Permission = @('Delete', 'Select', 'Update') - Ensure = 'Present' - } - ClientOnly = $true + <# + There is only need to test empty collection in the current state + because the desired state must always provide at least one item. + #> + Context 'When current CIM instance collection have no CIM instances' { + It 'Should return false when evaluating properties of each CIM instance of a collection of CIM instances' { + InModuleScope -ScriptBlock { + $desiredCimInstanceParameters = @{ + ClassName = $mockClassName + Namespace = $mockNamespace + Property = @{ + State = 'Grant' + Permission = @('Delete', 'Select', 'Update') + Ensure = 'Present' } + ClientOnly = $true + } - $desiredCimInstancePermissionCollection += New-CimInstance @desiredCimInstanceParameters - - $mockValues = @{ - CurrentValue = $currentCimInstancePermissionCollection - DesiredValue = $desiredCimInstancePermissionCollection - KeyProperties = @('State') - } + $script:desiredCimInstancePermissionCollection += New-CimInstance @desiredCimInstanceParameters - Test-DscPropertyState -Values $mockValues | Should -BeFalse + $mockValues = @{ + CurrentValue = $currentCimInstancePermissionCollection + DesiredValue = $desiredCimInstancePermissionCollection + KeyProperties = @('State') } + + Test-DscPropertyState -Values $mockValues | Should -BeFalse } + } + } - Context 'When the CIM instance are using two key properties' { - It 'Should return true when evaluating properties of one CIM instance that matches the key properties' { - $currentCimInstanceParameters1 = @{ - ClassName = $mockClassName - Namespace = $mockNamespace - Property = @{ - State = 'Grant' - Ensure = 'Present' - Permission = @('Delete', 'Select') - } - ClientOnly = $true + Context 'When the CIM instance are using two key properties' { + It 'Should return true when evaluating properties of one CIM instance that matches the key properties' { + InModuleScope -ScriptBlock { + $currentCimInstanceParameters1 = @{ + ClassName = $mockClassName + Namespace = $mockNamespace + Property = @{ + State = 'Grant' + Ensure = 'Present' + Permission = @('Delete', 'Select') } + ClientOnly = $true + } - $currentCimInstanceParameters2 = @{ - ClassName = $mockClassName - Namespace = $mockNamespace - Property = @{ - State = 'Grant' - Ensure = 'Absent' - Permission = @('Drop') - } - ClientOnly = $true + $currentCimInstanceParameters2 = @{ + ClassName = $mockClassName + Namespace = $mockNamespace + Property = @{ + State = 'Grant' + Ensure = 'Absent' + Permission = @('Drop') } + ClientOnly = $true + } - $desiredCimInstanceParameters1 = @{ - ClassName = $mockClassName - Namespace = $mockNamespace - Property = @{ - State = 'Grant' - Ensure = 'Present' - Permission = @('Delete', 'Select') - } - ClientOnly = $true + $desiredCimInstanceParameters1 = @{ + ClassName = $mockClassName + Namespace = $mockNamespace + Property = @{ + State = 'Grant' + Ensure = 'Present' + Permission = @('Delete', 'Select') } + ClientOnly = $true + } - $desiredCimInstanceParameters2 = @{ - ClassName = $mockClassName - Namespace = $mockNamespace - Property = @{ - State = 'Grant' - Ensure = 'Absent' - Permission = @('Drop') - } - ClientOnly = $true + $desiredCimInstanceParameters2 = @{ + ClassName = $mockClassName + Namespace = $mockNamespace + Property = @{ + State = 'Grant' + Ensure = 'Absent' + Permission = @('Drop') } + ClientOnly = $true + } - $currentCimInstancePermissionCollection += New-CimInstance @currentCimInstanceParameters1 - $currentCimInstancePermissionCollection += New-CimInstance @currentCimInstanceParameters2 - - $desiredCimInstancePermissionCollection += New-CimInstance @desiredCimInstanceParameters1 - $desiredCimInstancePermissionCollection += New-CimInstance @desiredCimInstanceParameters2 + $script:currentCimInstancePermissionCollection += New-CimInstance @currentCimInstanceParameters1 + $script:currentCimInstancePermissionCollection += New-CimInstance @currentCimInstanceParameters2 - $mockValues = @{ - CurrentValue = $currentCimInstancePermissionCollection - DesiredValue = $desiredCimInstancePermissionCollection - KeyProperties = @( - 'State' - 'Ensure' - ) - } + $script:desiredCimInstancePermissionCollection += New-CimInstance @desiredCimInstanceParameters1 + $script:desiredCimInstancePermissionCollection += New-CimInstance @desiredCimInstanceParameters2 - Test-DscPropertyState -Values $mockValues | Should -BeTrue + $mockValues = @{ + CurrentValue = $currentCimInstancePermissionCollection + DesiredValue = $desiredCimInstancePermissionCollection + KeyProperties = @( + 'State' + 'Ensure' + ) } + + Test-DscPropertyState -Values $mockValues | Should -BeTrue } + } + } - Context 'When the CIM instance are using two key properties, and one CIM instance is not in desired state' { - It 'Should return false when evaluating properties of one CIM instance that matches the key properties' { - $currentCimInstanceParameters1 = @{ - ClassName = $mockClassName - Namespace = $mockNamespace - Property = @{ - State = 'Grant' - Ensure = 'Present' - Permission = @('Select') - } - ClientOnly = $true + Context 'When the CIM instance are using two key properties, and one CIM instance is not in desired state' { + It 'Should return false when evaluating properties of one CIM instance that matches the key properties' { + InModuleScope -ScriptBlock { + $currentCimInstanceParameters1 = @{ + ClassName = $mockClassName + Namespace = $mockNamespace + Property = @{ + State = 'Grant' + Ensure = 'Present' + Permission = @('Select') } + ClientOnly = $true + } - $currentCimInstanceParameters2 = @{ - ClassName = $mockClassName - Namespace = $mockNamespace - Property = @{ - State = 'Grant' - Ensure = 'Absent' - Permission = @('Drop') - } - ClientOnly = $true + $currentCimInstanceParameters2 = @{ + ClassName = $mockClassName + Namespace = $mockNamespace + Property = @{ + State = 'Grant' + Ensure = 'Absent' + Permission = @('Drop') } + ClientOnly = $true + } - $desiredCimInstanceParameters1 = @{ - ClassName = $mockClassName - Namespace = $mockNamespace - Property = @{ - State = 'Grant' - Ensure = 'Present' - Permission = @('Select') - } - ClientOnly = $true + $desiredCimInstanceParameters1 = @{ + ClassName = $mockClassName + Namespace = $mockNamespace + Property = @{ + State = 'Grant' + Ensure = 'Present' + Permission = @('Select') } + ClientOnly = $true + } - $desiredCimInstanceParameters2 = @{ - ClassName = $mockClassName - Namespace = $mockNamespace - Property = @{ - State = 'Grant' - Ensure = 'Absent' - Permission = @('Delete') - } - ClientOnly = $true + $desiredCimInstanceParameters2 = @{ + ClassName = $mockClassName + Namespace = $mockNamespace + Property = @{ + State = 'Grant' + Ensure = 'Absent' + Permission = @('Delete') } + ClientOnly = $true + } - $currentCimInstancePermissionCollection += New-CimInstance @currentCimInstanceParameters1 - $currentCimInstancePermissionCollection += New-CimInstance @currentCimInstanceParameters2 - - $desiredCimInstancePermissionCollection += New-CimInstance @desiredCimInstanceParameters1 - $desiredCimInstancePermissionCollection += New-CimInstance @desiredCimInstanceParameters2 + $script:currentCimInstancePermissionCollection += New-CimInstance @currentCimInstanceParameters1 + $script:currentCimInstancePermissionCollection += New-CimInstance @currentCimInstanceParameters2 - $mockValues = @{ - CurrentValue = $currentCimInstancePermissionCollection - DesiredValue = $desiredCimInstancePermissionCollection - KeyProperties = @( - 'State' - 'Ensure' - ) - } + $script:desiredCimInstancePermissionCollection += New-CimInstance @desiredCimInstanceParameters1 + $script:desiredCimInstancePermissionCollection += New-CimInstance @desiredCimInstanceParameters2 - Test-DscPropertyState -Values $mockValues | Should -BeFalse + $mockValues = @{ + CurrentValue = $currentCimInstancePermissionCollection + DesiredValue = $desiredCimInstancePermissionCollection + KeyProperties = @( + 'State' + 'Ensure' + ) } + + Test-DscPropertyState -Values $mockValues | Should -BeFalse } } } + } - Context -Name 'When passing invalid types for DesiredValue' { - It 'Should write a warning when DesiredValue contain an unsupported type' { + Context 'When passing invalid types for DesiredValue' { + It 'Should write a warning when DesiredValue contain an unsupported type' { + InModuleScope -ScriptBlock { Mock -CommandName Write-Warning # This is a dummy type to test with a type that could never be a correct one. @@ -1074,7 +1167,7 @@ InModuleScope $ProjectName { Test-DscPropertyState -Values $mockValues | Should -BeFalse - Assert-MockCalled -CommandName Write-Warning -Exactly -Times 1 -Scope It + Should -Invoke -CommandName Write-Warning -Exactly -Times 1 -Scope It } } } diff --git a/tests/Unit/Public/Assert-BoundParameter.Tests.ps1 b/tests/Unit/Public/Assert-BoundParameter.Tests.ps1 index ea891a8..abe3e9a 100644 --- a/tests/Unit/Public/Assert-BoundParameter.Tests.ps1 +++ b/tests/Unit/Public/Assert-BoundParameter.Tests.ps1 @@ -1,122 +1,131 @@ -$ProjectPath = "$PSScriptRoot\..\..\.." | Convert-Path -$ProjectName = ((Get-ChildItem -Path $ProjectPath\*\*.psd1).Where{ - ($_.Directory.Name -match 'source|src' -or $_.Directory.Name -eq $_.BaseName) -and - $(try { Test-ModuleManifest -Path $_.FullName -ErrorAction Stop } catch { $false } ) - }).BaseName - - -Import-Module $ProjectName - -InModuleScope $ProjectName { - Describe 'ComputerManagementDsc.Common\Assert-BoundParameter' -Tag 'AssertBoundParameter' { - Context 'When the assert is successful' { - Context 'When there are no bound parameters' { - It 'Should not throw an error' { - { - $assertBoundParameterParameters = @{ - BoundParameterList = @{} - MutuallyExclusiveList1 = @('a') - MutuallyExclusiveList2 = @('b') - } +BeforeAll { + $script:moduleName = 'DscResource.Common' - Assert-BoundParameter @assertBoundParameterParameters - } | Should -Not -Throw - } + Remove-Module -Name $script:moduleName -Force -ErrorAction 'SilentlyContinue' + + Get-Module -Name $script:moduleName -ListAvailable | + Select-Object -First 1 | + Import-Module -Force -ErrorAction 'Stop' + + $PSDefaultParameterValues['InModuleScope:ModuleName'] = $script:moduleName +} + +AfterAll { + $PSDefaultParameterValues.Remove('InModuleScope:ModuleName') +} + +Describe 'Assert-BoundParameter' -Tag 'AssertBoundParameter' { + Context 'When the assert is successful' { + Context 'When there are no bound parameters' { + It 'Should not throw an error' { + { + $assertBoundParameterParameters = @{ + BoundParameterList = @{} + MutuallyExclusiveList1 = @('a') + MutuallyExclusiveList2 = @('b') + } + + Assert-BoundParameter @assertBoundParameterParameters + } | Should -Not -Throw } + } - Context 'When there are one bound parameters' { - It 'Should not throw an error' { - { - $assertBoundParameterParameters = @{ - BoundParameterList = @{ - param1 = 'value1' - } - MutuallyExclusiveList1 = @('a') - MutuallyExclusiveList2 = @('b') + Context 'When there are one bound parameters' { + It 'Should not throw an error' { + { + $assertBoundParameterParameters = @{ + BoundParameterList = @{ + param1 = 'value1' } + MutuallyExclusiveList1 = @('a') + MutuallyExclusiveList2 = @('b') + } - Assert-BoundParameter @assertBoundParameterParameters - } | Should -Not -Throw - } + Assert-BoundParameter @assertBoundParameterParameters + } | Should -Not -Throw } + } - Context 'When there are two bound parameters' { - It 'Should not throw an error' { - { - $assertBoundParameterParameters = @{ - BoundParameterList = @{ - param1 = 'value1' - param2 = 'value2' - } - MutuallyExclusiveList1 = @('a', 'b') - MutuallyExclusiveList2 = @('c', 'd') + Context 'When there are two bound parameters' { + It 'Should not throw an error' { + { + $assertBoundParameterParameters = @{ + BoundParameterList = @{ + param1 = 'value1' + param2 = 'value2' } + MutuallyExclusiveList1 = @('a', 'b') + MutuallyExclusiveList2 = @('c', 'd') + } - Assert-BoundParameter @assertBoundParameterParameters - } | Should -Not -Throw - } + Assert-BoundParameter @assertBoundParameterParameters + } | Should -Not -Throw } + } - Context 'When there are only one parameter matching a value in an exclusive list' { - It 'Should not throw an error' { - { - $assertBoundParameterParameters = @{ - BoundParameterList = @{ - param1 = 'value1' - } - MutuallyExclusiveList1 = @('param1') - MutuallyExclusiveList2 = @('param2') + Context 'When there are only one parameter matching a value in an exclusive list' { + It 'Should not throw an error' { + { + $assertBoundParameterParameters = @{ + BoundParameterList = @{ + param1 = 'value1' } + MutuallyExclusiveList1 = @('param1') + MutuallyExclusiveList2 = @('param2') + } - Assert-BoundParameter @assertBoundParameterParameters - } | Should -Not -Throw - } + Assert-BoundParameter @assertBoundParameterParameters + } | Should -Not -Throw } } + } + + Context 'When the assert fails' { + Context 'When using parameters that are mutually exclusive' { + It 'Should throw an error' { + $errorMessage = InModuleScope -ScriptBlock { + $script:localizedData.ParameterUsageWrong + } + + $errorMessage = $errorMessage -f 'param1', 'param2' - Context 'When the assert fails' { - Context 'When using parameters that are mutually exclusive' { - It 'Should throw an error' { - $errorMessage = ` - $script:localizedData.ParameterUsageWrong ` - -f 'param1', 'param2' - - { - $assertBoundParameterParameters = @{ - BoundParameterList = @{ - param1 = 'value1' - param2 = 'value1' - } - MutuallyExclusiveList1 = @('param1') - MutuallyExclusiveList2 = @('param2') + { + $assertBoundParameterParameters = @{ + BoundParameterList = @{ + param1 = 'value1' + param2 = 'value1' } + MutuallyExclusiveList1 = @('param1') + MutuallyExclusiveList2 = @('param2') + } - Assert-BoundParameter @assertBoundParameterParameters - } | Should -Throw $errorMessage - } + Assert-BoundParameter @assertBoundParameterParameters + } | Should -Throw -ExpectedMessage "$errorMessage*" } + } + + Context 'When using several parameters that are mutually exclusive' { + It 'Should throw an error' { + $errorMessage = InModuleScope -ScriptBlock { + $script:localizedData.ParameterUsageWrong + } + + $errorMessage = $errorMessage -f "param1','param2", "param3','param4" - Context 'When using several parameters that are mutually exclusive' { - It 'Should throw an error' { - $errorMessage = ` - $script:localizedData.ParameterUsageWrong ` - -f "param1','param2", "param3','param4" - - { - $assertBoundParameterParameters = @{ - BoundParameterList = @{ - param1 = 'value1' - param2 = 'value2' - param3 = 'value3' - param4 = 'value4' - } - MutuallyExclusiveList1 = @('param1','param2') - MutuallyExclusiveList2 = @('param3','param4') + { + $assertBoundParameterParameters = @{ + BoundParameterList = @{ + param1 = 'value1' + param2 = 'value2' + param3 = 'value3' + param4 = 'value4' } + MutuallyExclusiveList1 = @('param1','param2') + MutuallyExclusiveList2 = @('param3','param4') + } - Assert-BoundParameter @assertBoundParameterParameters - } | Should -Throw $errorMessage - } + Assert-BoundParameter @assertBoundParameterParameters + } | Should -Throw -ExpectedMessage "$errorMessage*" } } } diff --git a/tests/Unit/Public/Assert-IPAddress.Tests.ps1 b/tests/Unit/Public/Assert-IPAddress.Tests.ps1 index 277970e..42d5cbb 100644 --- a/tests/Unit/Public/Assert-IPAddress.Tests.ps1 +++ b/tests/Unit/Public/Assert-IPAddress.Tests.ps1 @@ -1,103 +1,133 @@ -$ProjectPath = "$PSScriptRoot\..\..\.." | Convert-Path -$ProjectName = ((Get-ChildItem -Path $ProjectPath\*\*.psd1).Where{ - ($_.Directory.Name -match 'source|src' -or $_.Directory.Name -eq $_.BaseName) -and - $(try { Test-ModuleManifest -Path $_.FullName -ErrorAction Stop } catch { $false } ) - }).BaseName +BeforeAll { + $script:moduleName = 'DscResource.Common' + Remove-Module -Name $script:moduleName -Force -ErrorAction 'SilentlyContinue' -Import-Module $ProjectName + Get-Module -Name $script:moduleName -ListAvailable | + Select-Object -First 1 | + Import-Module -Force -ErrorAction 'Stop' -InModuleScope $ProjectName { - Describe 'ComputerManagementDsc.Common\Assert-IPAddress' -Tag 'AssertIPAddress' { - Context 'When invoking with valid IPv4 Address' { - It 'Should not throw an error' { - $testIPAddressParameters = @{ - Address = '192.168.0.1' - AddressFamily = 'IPv4' - } + $PSDefaultParameterValues['InModuleScope:ModuleName'] = $script:moduleName +} + +AfterAll { + $PSDefaultParameterValues.Remove('InModuleScope:ModuleName') +} - { Assert-IPAddress @testIPAddressParameters } | Should -Not -Throw + +Describe 'Assert-IPAddress' -Tag 'AssertIPAddress' { + Context 'When invoking with valid IPv4 Address' { + It 'Should not throw an error' { + $testIPAddressParameters = @{ + Address = '192.168.0.1' + AddressFamily = 'IPv4' } - } - Context 'When invoking with valid IPv6 Address' { - It 'Should not throw an error' { - $testIPAddressParameters = @{ - Address = 'fe80:ab04:30F5:002b::1' - AddressFamily = 'IPv6' - } + { Assert-IPAddress @testIPAddressParameters } | Should -Not -Throw + } + } - { Assert-IPAddress @testIPAddressParameters } | Should -Not -Throw + Context 'When invoking with valid IPv6 Address' { + It 'Should not throw an error' { + $testIPAddressParameters = @{ + Address = 'fe80:ab04:30F5:002b::1' + AddressFamily = 'IPv6' } + + { Assert-IPAddress @testIPAddressParameters } | Should -Not -Throw } + } - Context 'When invoking with invalid IP Address' { - Context 'When address family is supplied' { - It 'Should throw an AddressFormatError error' { - $testIPAddressParameters = @{ - Address = 'NotReal' - AddressFamily = 'IPv4' - } + Context 'When invoking with invalid IP Address' { + Context 'When address family is supplied' { + It 'Should throw an AddressFormatError error' { + $testIPAddressParameters = @{ + Address = 'NotReal' + AddressFamily = 'IPv4' + } - { Assert-IPAddress @testIPAddressParameters } | ` - Should -Throw ($script:localizedData.AddressFormatError -f $testIPAddressParameters.Address) + $errorMessage = InModuleScope -ScriptBlock { + $script:localizedData.AddressFormatError } - } - Context 'When address family is not supplied' { - It 'Should throw an AddressFormatError error' { - $testIPAddressParameters = @{ - Address = 'NotReal' - } + $errorMessage = $errorMessage -f $testIPAddressParameters.Address - { Assert-IPAddress @testIPAddressParameters } | ` - Should -Throw ($script:localizedData.AddressFormatError -f $testIPAddressParameters.Address) - } + { Assert-IPAddress @testIPAddressParameters } | + Should -Throw -ExpectedMessage "$errorMessage*" } } - Context 'When invoking with IPv4 Address and family mismatch' { - It 'Should throw an AddressMismatchError error' { + Context 'When address family is not supplied' { + It 'Should throw an AddressFormatError error' { $testIPAddressParameters = @{ - Address = '192.168.0.1' - AddressFamily = 'IPv6' + Address = 'NotReal' } - { Assert-IPAddress @testIPAddressParameters } | ` - Should -Throw ($script:localizedData.AddressIPv4MismatchError -f $testIPAddressParameters.Address, $testIPAddressParameters.AddressFamily) + $errorMessage = InModuleScope -ScriptBlock { + $script:localizedData.AddressFormatError + } + + $errorMessage = $errorMessage -f $testIPAddressParameters.Address + + { Assert-IPAddress @testIPAddressParameters } | + Should -Throw -ExpectedMessage "$errorMessage*" } } + } - Context 'When invoking with IPv6 Address and family mismatch' { - It 'Should throw an AddressMismatchError error' { - $testIPAddressParameters = @{ - Address = 'fe80::' - AddressFamily = 'IPv4' - } + Context 'When invoking with IPv4 Address and family mismatch' { + It 'Should throw an AddressMismatchError error' { + $testIPAddressParameters = @{ + Address = '192.168.0.1' + AddressFamily = 'IPv6' + } - { Assert-IPAddress @testIPAddressParameters } | ` - Should -Throw ($script:localizedData.AddressIPv6MismatchError -f $testIPAddressParameters.Address, $testIPAddressParameters.AddressFamily) + $errorMessage = InModuleScope -ScriptBlock { + $script:localizedData.AddressIPv4MismatchError } + + $errorMessage = $errorMessage -f $testIPAddressParameters.Address, $testIPAddressParameters.AddressFamily + + { Assert-IPAddress @testIPAddressParameters } | ` + Should -Throw -ExpectedMessage "$errorMessage*" } + } - Context 'When invoking with valid IPv4 Address with no address family' { - It 'Should not throw an error' { - $testIPAddressParameters = @{ - Address = '192.168.0.1' - } + Context 'When invoking with IPv6 Address and family mismatch' { + It 'Should throw an AddressMismatchError error' { + $testIPAddressParameters = @{ + Address = 'fe80::' + AddressFamily = 'IPv4' + } - { Assert-IPAddress @testIPAddressParameters } | Should -Not -Throw + $errorMessage = InModuleScope -ScriptBlock { + $script:localizedData.AddressIPv6MismatchError } + + $errorMessage = $errorMessage -f $testIPAddressParameters.Address, $testIPAddressParameters.AddressFamily + + { Assert-IPAddress @testIPAddressParameters } | ` + Should -Throw -ExpectedMessage "$errorMessage*" } + } - Context 'When invoking with valid IPv6 Address with no address family' { - It 'Should not throw an error' { - $testIPAddressParameters = @{ - Address = 'fe80:ab04:30F5:002b::1' - } + Context 'When invoking with valid IPv4 Address with no address family' { + It 'Should not throw an error' { + $testIPAddressParameters = @{ + Address = '192.168.0.1' + } - { Assert-IPAddress @testIPAddressParameters } | Should -Not -Throw + { Assert-IPAddress @testIPAddressParameters } | Should -Not -Throw + } + } + + Context 'When invoking with valid IPv6 Address with no address family' { + It 'Should not throw an error' { + $testIPAddressParameters = @{ + Address = 'fe80:ab04:30F5:002b::1' } + + { Assert-IPAddress @testIPAddressParameters } | Should -Not -Throw } } } diff --git a/tests/Unit/Public/Assert-Module.Tests.ps1 b/tests/Unit/Public/Assert-Module.Tests.ps1 index fb982a1..074d290 100644 --- a/tests/Unit/Public/Assert-Module.Tests.ps1 +++ b/tests/Unit/Public/Assert-Module.Tests.ps1 @@ -1,99 +1,108 @@ -$ProjectPath = "$PSScriptRoot\..\..\.." | Convert-Path -$ProjectName = ((Get-ChildItem -Path $ProjectPath\*\*.psd1).Where{ - ($_.Directory.Name -match 'source|src' -or $_.Directory.Name -eq $_.BaseName) -and - $(try { Test-ModuleManifest -Path $_.FullName -ErrorAction Stop } catch { $false } ) - }).BaseName +BeforeAll { + $script:moduleName = 'DscResource.Common' + Remove-Module -Name $script:moduleName -Force -ErrorAction 'SilentlyContinue' -Import-Module $ProjectName + Get-Module -Name $script:moduleName -ListAvailable | + Select-Object -First 1 | + Import-Module -Force -ErrorAction 'Stop' -InModuleScope $ProjectName { - Describe 'Assert-Module' { + $PSDefaultParameterValues['InModuleScope:ModuleName'] = $script:moduleName + $PSDefaultParameterValues['Mock:ModuleName'] = $script:moduleName + $PSDefaultParameterValues['Should:ModuleName'] = $script:moduleName +} + +AfterAll { + $PSDefaultParameterValues.Remove('InModuleScope:ModuleName') + $PSDefaultParameterValues.Remove('Mock:ModuleName') + $PSDefaultParameterValues.Remove('Should:ModuleName') +} + +Describe 'Assert-Module' { + Context 'When module is not installed' { BeforeAll { - $testModuleName = 'TestModule' + Mock -CommandName Get-Module } - Context 'When module is not installed' { - BeforeAll { - Mock -CommandName Get-Module + It 'Should throw the correct error' { + $mockErrorMessage = InModuleScope -ScriptBlock { + $script:localizedData.RoleNotFound -f 'TestModule' } - It 'Should throw the correct error' { - { Assert-Module -ModuleName $testModuleName -Verbose } | ` - Should -Throw ($script:localizedData.RoleNotFound -f $testModuleName) - } + { Assert-Module -ModuleName 'TestModule' -Verbose } | ` + Should -Throw -ExpectedMessage $mockErrorMessage } + } - Context 'When module is installed' { - Context 'When module is already present in session' { - BeforeAll { - Mock -CommandName Import-Module - Mock -CommandName Get-Module -MockWith { - return @{ - Name = $testModuleName - } + Context 'When module is installed' { + Context 'When module is already present in session' { + BeforeAll { + Mock -CommandName Import-Module + Mock -CommandName Get-Module -MockWith { + return @{ + Name = 'TestModule' } } + } - Context 'When asserting that module is available' { - It 'Should not throw an error' { - { Assert-Module -ModuleName $testModuleName } | Should -Not -Throw - } + Context 'When asserting that module is available' { + It 'Should not throw an error' { + { Assert-Module -ModuleName 'TestModule' } | Should -Not -Throw + } - It 'Should call the expected mocks' { - Assert-MockCalled -CommandName Import-Module -Exactly -Times 0 - } + It 'Should call the expected mocks' { + Should -Invoke -CommandName Import-Module -Exactly -Times 0 -Scope Context } + } - Context 'When using ImportModule but module is already imported' { - It 'Should not throw an error' { - { Assert-Module -ModuleName $testModuleName -ImportModule } | Should -Not -Throw - } + Context 'When using ImportModule but module is already imported' { + It 'Should not throw an error' { + { Assert-Module -ModuleName 'TestModule' -ImportModule } | Should -Not -Throw + } - It 'Should call the expected mocks' { - Assert-MockCalled -CommandName Import-Module -Exactly -Times 0 - } + It 'Should call the expected mocks' { + Should -Invoke -CommandName Import-Module -Exactly -Times 0 -Scope Context } + } - Context 'When module should be forcibly imported' { - It 'Should not throw an error' { - { Assert-Module -ModuleName $testModuleName -ImportModule -Force } | Should -Not -Throw - } + Context 'When module should be forcibly imported' { + It 'Should not throw an error' { + { Assert-Module -ModuleName 'TestModule' -ImportModule -Force } | Should -Not -Throw + } - It 'Should call the expected mocks' { - Assert-MockCalled -CommandName Import-Module -ParameterFilter { - $Force -eq $true - } -Exactly -Times 1 - } + It 'Should call the expected mocks' { + Should -Invoke -CommandName Import-Module -ParameterFilter { + $Force -eq $true + } -Exactly -Times 1 -Scope Context } } + } - Context 'When module is not present in the session' { - BeforeAll { - Mock -CommandName Import-Module - Mock -CommandName Get-Module -MockWith { - return $null - } -ParameterFilter { - $ListAvailable -eq $false - } + Context 'When module is not present in the session' { + BeforeAll { + Mock -CommandName Import-Module + Mock -CommandName Get-Module -MockWith { + return $null + } -ParameterFilter { + $ListAvailable -eq $false + } - Mock -CommandName Get-Module -MockWith { - return @{ - Name = $testModuleName - } - } -ParameterFilter { - $ListAvailable -eq $true + Mock -CommandName Get-Module -MockWith { + return @{ + Name = 'TestModule' } + } -ParameterFilter { + $ListAvailable -eq $true } + } - Context 'When module should be imported' { - It 'Should not throw an error' { - { Assert-Module -ModuleName $testModuleName -ImportModule } | Should -Not -Throw - } + Context 'When module should be imported' { + It 'Should not throw an error' { + { Assert-Module -ModuleName 'TestModule' -ImportModule } | Should -Not -Throw + } - It 'Should call the expected mocks' { - Assert-MockCalled -CommandName Import-Module -Exactly -Times 1 - } + It 'Should call the expected mocks' { + Should -Invoke -CommandName Import-Module -Exactly -Times 1 -Scope Context } } } diff --git a/tests/Unit/Public/Compare-DscParameterState.Tests.ps1 b/tests/Unit/Public/Compare-DscParameterState.Tests.ps1 index fc77ab0..2544068 100644 --- a/tests/Unit/Public/Compare-DscParameterState.Tests.ps1 +++ b/tests/Unit/Public/Compare-DscParameterState.Tests.ps1 @@ -1,27 +1,37 @@ -$ProjectPath = "$PSScriptRoot\..\..\.." | Convert-Path -$ProjectName = ((Get-ChildItem -Path $ProjectPath\*\*.psd1).Where{ - ($_.Directory.Name -match 'source|src' -or $_.Directory.Name -eq $_.BaseName) -and - $(try - { - Test-ModuleManifest $_.FullName -ErrorAction Stop - } - catch - { - $false - } ) - }).BaseName - -Import-Module $ProjectName -Force - -InModuleScope $ProjectName { - Describe 'ComputerManagementDsc.Common\Compare-DscParameterState' { +BeforeAll { + $script:moduleName = 'DscResource.Common' + + Remove-Module -Name $script:moduleName -Force -ErrorAction 'SilentlyContinue' + + Get-Module -Name $script:moduleName -ListAvailable | + Select-Object -First 1 | + Import-Module -Force -ErrorAction 'Stop' +} + +Describe 'ComputerManagementDsc.Common\Compare-DscParameterState' { + BeforeAll { + $verbose = $true + } + + Context 'When testing single values' { BeforeAll { - $verbose = $true + $currentValues = @{ + String = 'a string' + Bool = $true + Int = 99 + Array = 'a', 'b', 'c' + Hashtable = @{ + k1 = 'Test' + k2 = 123 + k3 = 'v1', 'v2', 'v3' + } + ScriptBlock = { Get-Date } + } } - Context 'When testing single values' { - BeforeAll{ - $currentValues = @{ + Context 'When all values match' { + BeforeAll { + $desiredValues = [PSObject] @{ String = 'a string' Bool = $true Int = 99 @@ -34,252 +44,359 @@ InModuleScope $ProjectName { ScriptBlock = { Get-Date } } } - Context 'When all values match' { - BeforeAll{ - $desiredValues = [PSObject] @{ - String = 'a string' - Bool = $true - Int = 99 - Array = 'a', 'b', 'c' - Hashtable = @{ - k1 = 'Test' - k2 = 123 - k3 = 'v1', 'v2', 'v3' - } - ScriptBlock = { Get-Date } - } - } - It 'Should not throw exception' { - { $script:result = Compare-DscParameterState ` - -CurrentValues $currentValues ` - -DesiredValues $desiredValues ` - -Verbose:$verbose } | Should -Not -Throw - } - It 'Should return null' { - $script:result | Should -BeNullOrEmpty - } + It 'Should not throw exception' { + { $script:result = Compare-DscParameterState ` + -CurrentValues $currentValues ` + -DesiredValues $desiredValues ` + -Verbose:$verbose } | Should -Not -Throw } - Context 'When a string is mismatched' { - BeforeAll { - $desiredValues = [PSObject] @{ - String = 'different string' - Bool = $true - Int = 99 - Array = 'a', 'b', 'c' - Hashtable = @{ - k1 = 'Test' - k2 = 123 - k3 = 'v1', 'v2', 'v3' - } + It 'Should return null' { + $script:result | Should -BeNullOrEmpty + } + } + + Context 'When a string is mismatched' { + BeforeAll { + $desiredValues = [PSObject] @{ + String = 'different string' + Bool = $true + Int = 99 + Array = 'a', 'b', 'c' + Hashtable = @{ + k1 = 'Test' + k2 = 123 + k3 = 'v1', 'v2', 'v3' } } - It 'Should not throw exception' { - { $script:result = Compare-DscParameterState ` - -CurrentValues $currentValues ` - -DesiredValues $desiredValues ` - -Verbose:$verbose } | Should -Not -Throw - } + } - It 'Should return $false for String InDesiredState' { - $script:result.where({$_.Property -eq 'String'}).InDesiredState | Should -BeFalse - } + It 'Should not throw exception' { + { $script:result = Compare-DscParameterState ` + -CurrentValues $currentValues ` + -DesiredValues $desiredValues ` + -Verbose:$verbose } | Should -Not -Throw + } - It 'Should return same type in values' { - $script:result.where({$_.Property -eq 'String'}).ActualType | Should -Be $script:result.where({$_.Property -eq 'String'}).ExpectedType - } + It 'Should return $false for String InDesiredState' { + $script:result.Where({ + $_.Property -eq 'String' + }).InDesiredState | Should -BeFalse + } - It 'Should not return other property by default' { - $script:result.where({$_.Property -ne 'String'}).InDesiredState | Should -BeNullOrEmpty - } + It 'Should return same type in values' { + $script:result.Where({ + $_.Property -eq 'String' + }).ActualType | Should -Be $script:result.Where({ + $_.Property -eq 'String' + }).ExpectedType } - Context 'When a boolean is mismatched' { - BeforeAll{ - $desiredValues = [PSObject] @{ - String = 'a string' - Bool = $false - Int = 99 - Array = 'a', 'b', 'c' - Hashtable = @{ - k1 = 'Test' - k2 = 123 - k3 = 'v1', 'v2', 'v3' - } + It 'Should not return other property by default' { + $script:result.Where({ + $_.Property -ne 'String' + }).InDesiredState | Should -BeNullOrEmpty + } + } + + Context 'When a boolean is mismatched' { + BeforeAll { + $desiredValues = [PSObject] @{ + String = 'a string' + Bool = $false + Int = 99 + Array = 'a', 'b', 'c' + Hashtable = @{ + k1 = 'Test' + k2 = 123 + k3 = 'v1', 'v2', 'v3' } } + } - It 'Should not throw exception' { - { $script:result = Compare-DscParameterState ` - -CurrentValues $currentValues ` - -DesiredValues $desiredValues ` - -Verbose:$verbose } | Should -Not -Throw - } + It 'Should not throw exception' { + { $script:result = Compare-DscParameterState ` + -CurrentValues $currentValues ` + -DesiredValues $desiredValues ` + -Verbose:$verbose } | Should -Not -Throw + } - It 'Should return $false for Bool InDesiredState' { - $script:result.where({$_.Property -eq 'Bool'}).InDesiredState | Should -BeFalse - } + It 'Should return $false for Bool InDesiredState' { + $script:result.Where({ + $_.Property -eq 'Bool' + }).InDesiredState | Should -BeFalse + } - It 'Should return same type in values' { - $script:result.where({$_.Property -eq 'Bool'}).ActualType | Should -Be $script:result.where({$_.Property -eq 'Bool'}).ExpectedType - } + It 'Should return same type in values' { + $script:result.Where({ + $_.Property -eq 'Bool' + }).ActualType | Should -Be $script:result.Where({ + $_.Property -eq 'Bool' + }).ExpectedType + } - It 'Should not return other property by default' { - $script:result.where({$_.Property -ne 'Bool'}).InDesiredState | Should -BeNullOrEmpty - } + It 'Should not return other property by default' { + $script:result.Where({ + $_.Property -ne 'Bool' + }).InDesiredState | Should -BeNullOrEmpty } + } - Context 'When an int is mismatched' { - BeforeAll { - $desiredValues = [PSObject] @{ - String = 'a string' - Bool = $true - Int = 1 - Array = 'a', 'b', 'c' - Hashtable = @{ - k1 = 'Test' - k2 = 123 - k3 = 'v1', 'v2', 'v3' - } + Context 'When an int is mismatched' { + BeforeAll { + $desiredValues = [PSObject] @{ + String = 'a string' + Bool = $true + Int = 1 + Array = 'a', 'b', 'c' + Hashtable = @{ + k1 = 'Test' + k2 = 123 + k3 = 'v1', 'v2', 'v3' } } - It 'Should not throw exception' { - { $script:result = Compare-DscParameterState ` - -CurrentValues $currentValues ` - -DesiredValues $desiredValues ` - -Verbose:$verbose } | Should -Not -Throw - } + } - It 'Should return $false for Int InDesiredState' { - $script:result.where({$_.Property -eq 'Int'}).InDesiredState | Should -BeFalse - } + It 'Should not throw exception' { + { $script:result = Compare-DscParameterState ` + -CurrentValues $currentValues ` + -DesiredValues $desiredValues ` + -Verbose:$verbose } | Should -Not -Throw + } - It 'Should return same type in values' { - $script:result.where({$_.Property -eq 'Int'}).ActualType | Should -Be $script:result.where({$_.Property -eq 'Int'}).ExpectedType - } + It 'Should return $false for Int InDesiredState' { + $script:result.Where({ + $_.Property -eq 'Int' + }).InDesiredState | Should -BeFalse + } - It 'Should not return other property by default' { - $script:result.where({$_.Property -ne 'Int'}).InDesiredState | Should -BeNullOrEmpty - } + It 'Should return same type in values' { + $script:result.Where({ + $_.Property -eq 'Int' + }).ActualType | Should -Be $script:result.Where({ + $_.Property -eq 'Int' + }).ExpectedType } - Context 'When an scriptblock is mismatched' { - BeforeAll { - $desiredValues = [PSObject] @{ - String = 'a string' - Bool = $true - Int = 99 - Array = 'a', 'b', 'c' - Hashtable = @{ - k1 = 'Test' - k2 = 123 - k3 = 'v1', 'v2', 'v3' - } - ScriptBlock = { Get-Process } + It 'Should not return other property by default' { + $script:result.Where({ + $_.Property -ne 'Int' + }).InDesiredState | Should -BeNullOrEmpty + } + } + + Context 'When an scriptblock is mismatched' { + BeforeAll { + $desiredValues = [PSObject] @{ + String = 'a string' + Bool = $true + Int = 99 + Array = 'a', 'b', 'c' + Hashtable = @{ + k1 = 'Test' + k2 = 123 + k3 = 'v1', 'v2', 'v3' } + ScriptBlock = { Get-Process } } + } - It 'Should not throw exception' { - { $script:result = Compare-DscParameterState ` - -CurrentValues $currentValues ` - -DesiredValues $desiredValues ` - -Verbose:$verbose } | Should -Not -Throw - } + It 'Should not throw exception' { + { $script:result = Compare-DscParameterState ` + -CurrentValues $currentValues ` + -DesiredValues $desiredValues ` + -Verbose:$verbose } | Should -Not -Throw + } - It 'Should return $false for ScriptBlock InDesiredState' { - $script:result.where({$_.Property -eq 'ScriptBlock'}).InDesiredState | Should -BeFalse - } + It 'Should return $false for ScriptBlock InDesiredState' { + $script:result.Where({ + $_.Property -eq 'ScriptBlock' + }).InDesiredState | Should -BeFalse + } - It 'Should return same type in values' { - $script:result.where({$_.Property -eq 'ScriptBlock'}).ActualType | Should -Be $script:result.where({$_.Property -eq 'ScriptBlock'}).ExpectedType - } + It 'Should return same type in values' { + $script:result.Where({ + $_.Property -eq 'ScriptBlock' + }).ActualType | Should -Be $script:result.Where({ + $_.Property -eq 'ScriptBlock' + }).ExpectedType + } - It 'Should not return other property by default' { - $script:result.where({$_.Property -ne 'ScriptBlock'}).InDesiredState | Should -BeNullOrEmpty - } + It 'Should not return other property by default' { + $script:result.Where({ + $_.Property -ne 'ScriptBlock' + }).InDesiredState | Should -BeNullOrEmpty } + } - Context 'When an int is mismatched without ScriptBlock' { - BeforeAll { - $desiredValues = [PSObject] @{ - String = 'a string' - Bool = $true - Int = 1 - Array = 'a', 'b', 'c' - Hashtable = @{ - k1 = 'Test' - k2 = 123 - k3 = 'v1', 'v2', 'v3' - } + Context 'When an int is mismatched without ScriptBlock' { + BeforeAll { + $desiredValues = [PSObject] @{ + String = 'a string' + Bool = $true + Int = 1 + Array = 'a', 'b', 'c' + Hashtable = @{ + k1 = 'Test' + k2 = 123 + k3 = 'v1', 'v2', 'v3' } } + } - It 'Should not throw exception' { - { $script:result = Compare-DscParameterState ` - -CurrentValues $currentValues ` - -DesiredValues $desiredValues ` - -Verbose:$verbose } | Should -Not -Throw - } + It 'Should not throw exception' { + { $script:result = Compare-DscParameterState ` + -CurrentValues $currentValues ` + -DesiredValues $desiredValues ` + -Verbose:$verbose } | Should -Not -Throw + } - It 'Should return $false for Int InDesiredState' { - $script:result.where({$_.Property -eq 'Int'}).InDesiredState | Should -BeFalse - } + It 'Should return $false for Int InDesiredState' { + $script:result.Where({ + $_.Property -eq 'Int' + }).InDesiredState | Should -BeFalse + } - It 'Should return same type in values' { - $script:result.where({$_.Property -eq 'Int'}).ActualType | Should -Be $script:result.where({$_.Property -eq 'Int'}).ExpectedType - } + It 'Should return same type in values' { + $script:result.Where({ + $_.Property -eq 'Int' + }).ActualType | Should -Be $script:result.Where({ + $_.Property -eq 'Int' + }).ExpectedType + } - It 'Should not return other property by default' { - $script:result.where({$_.Property -ne 'Int'}).InDesiredState | Should -BeNullOrEmpty - } + It 'Should not return other property by default' { + $script:result.Where({ + $_.Property -ne 'Int' + }).InDesiredState | Should -BeNullOrEmpty } + } - Context 'When a type is mismatched' { - BeforeAll{ - $desiredValues = [PSObject] @{ - String = 'a string' - Bool = $true - Int = '99' - Array = 'a', 'b', 'c' - } + Context 'When a type is mismatched' { + BeforeAll { + $desiredValues = [PSObject] @{ + String = 'a string' + Bool = $true + Int = '99' + Array = 'a', 'b', 'c' } + } - It 'Should not throw exception' { - { $script:result = Compare-DscParameterState ` - -CurrentValues $currentValues ` - -DesiredValues $desiredValues ` - -Verbose:$verbose } | Should -Not -Throw - } + It 'Should not throw exception' { + { $script:result = Compare-DscParameterState ` + -CurrentValues $currentValues ` + -DesiredValues $desiredValues ` + -Verbose:$verbose } | Should -Not -Throw + } - It 'Should return $false for Int InDesiredState' { - $script:result.where({$_.Property -eq 'Int'}).InDesiredState | Should -BeFalse - } + It 'Should return $false for Int InDesiredState' { + $script:result.Where({ + $_.Property -eq 'Int' + }).InDesiredState | Should -BeFalse + } - It 'Should not return same type in values' { - $script:result.where({$_.Property -eq 'Int'}).ActualType | Should -Not -Be $script:result.where({$_.Property -eq 'Int'}).ExpectedType + It 'Should not return same type in values' { + $script:result.Where({ + $_.Property -eq 'Int' + }).ActualType | Should -Not -Be $script:result.Where({ + $_.Property -eq 'Int' + }).ExpectedType + } + + It 'Should not return other property by default' { + $script:result.Where({ + $_.Property -ne 'Int' + }).InDesiredState | Should -BeNullOrEmpty + } + } + + Context 'When a type is mismatched but TurnOffTypeChecking is used' { + BeforeAll { + $desiredValues = [PSObject] @{ + String = 'a string' + Bool = $true + Int = '99' + Array = 'a', 'b', 'c' } + } - It 'Should not return other property by default' { - $script:result.where({$_.Property -ne 'Int'}).InDesiredState | Should -BeNullOrEmpty + It 'Should not throw exception' { + { $script:result = Compare-DscParameterState ` + -CurrentValues $currentValues ` + -DesiredValues $desiredValues ` + -TurnOffTypeChecking ` + -Verbose:$verbose } | Should -Not -Throw + } + + It 'Should return null result' { + $script:result | Should -BeNullOrEmpty + } + } + + Context 'When a value is mismatched but ExcludeProperties is used to exclude then' { + BeforeAll { + $desiredValues = @{ + String = 'some other string' + Bool = $true + Int = 99 + Array = 'a', 'b', 'c' + Hashtable = @{ + k1 = 'Test' + k2 = 123 + k3 = 'v1', 'v2', 'v3' + } + ScriptBlock = { Get-Date } } + + $excludeProperties = @( + 'String' + ) } - Context 'When a type is mismatched but TurnOffTypeChecking is used' { - BeforeAll{ - $desiredValues = [PSObject] @{ - String = 'a string' - Bool = $true - Int = '99' - Array = 'a', 'b', 'c' + It 'Should not throw exception' { + { $script:result = Compare-DscParameterState ` + -CurrentValues $currentValues ` + -DesiredValues $desiredValues ` + -ExcludeProperties $excludeProperties ` + -Verbose:$verbose } | Should -Not -Throw + } + + It 'Should return null result' { + $script:result | Should -BeNullOrEmpty + } + } + + Context 'When a value is mismatched but it is not in Properties then' { + BeforeAll { + $desiredValues = @{ + String = 'some other string' + Bool = $true + Int = 99 + Array = 'a', 'b', 'c' + Hashtable = @{ + k1 = 'Test' + k2 = 123 + k3 = 'v1', 'v2', 'v3' } + ScriptBlock = { Get-Date } } + $properties = @( + 'Bool' + 'Int' + 'Array' + 'Hashtable' + 'ScriptBlock' + ) + } + + Context 'When using the alias ValuesToCheck' { It 'Should not throw exception' { { $script:result = Compare-DscParameterState ` -CurrentValues $currentValues ` -DesiredValues $desiredValues ` - -TurnOffTypeChecking ` + -ValuesToCheck $properties ` -Verbose:$verbose } | Should -Not -Throw } @@ -288,77 +405,29 @@ InModuleScope $ProjectName { } } - Context 'When a value is mismatched but ExcludeProperties is used to exclude then' { - BeforeAll{ - $desiredValues = @{ - String = 'some other string' - Bool = $true - Int = 99 - Array = 'a', 'b', 'c' - Hashtable = @{ - k1 = 'Test' - k2 = 123 - k3 = 'v1', 'v2', 'v3' - } - ScriptBlock = { Get-Date } - } - } - - $excludeProperties = @( - 'String' - ) - - It 'Should not throw exception' { - { $script:result = Compare-DscParameterState ` - -CurrentValues $currentValues ` - -DesiredValues $desiredValues ` - -ExcludeProperties $excludeProperties ` - -Verbose:$verbose } | Should -Not -Throw - } - - It 'Should return null result' { - $script:result | Should -BeNullOrEmpty - } + It 'Should not throw exception' { + { $script:result = Compare-DscParameterState ` + -CurrentValues $currentValues ` + -DesiredValues $desiredValues ` + -Properties $properties ` + -Verbose:$verbose } | Should -Not -Throw } - Context 'When a value is mismatched but it is not in Properties then' { - BeforeAll{ - $desiredValues = @{ - String = 'some other string' - Bool = $true - Int = 99 - Array = 'a', 'b', 'c' - Hashtable = @{ - k1 = 'Test' - k2 = 123 - k3 = 'v1', 'v2', 'v3' - } - ScriptBlock = { Get-Date } - } + It 'Should return null result' { + $script:result | Should -BeNullOrEmpty + } + Context 'When a value is mismatched but it is in Properties then' { + BeforeAll { $properties = @( + 'String' 'Bool' 'Int' 'Array' 'Hashtable' - 'ScriptBlock' ) } - Context 'When using the alias ValuesToCheck' { - It 'Should not throw exception' { - { $script:result = Compare-DscParameterState ` - -CurrentValues $currentValues ` - -DesiredValues $desiredValues ` - -ValuesToCheck $properties ` - -Verbose:$verbose } | Should -Not -Throw - } - - It 'Should return null result' { - $script:result | Should -BeNullOrEmpty - } - } - It 'Should not throw exception' { { $script:result = Compare-DscParameterState ` -CurrentValues $currentValues ` @@ -367,46 +436,48 @@ InModuleScope $ProjectName { -Verbose:$verbose } | Should -Not -Throw } - It 'Should return null result' { - $script:result | Should -BeNullOrEmpty + It 'Should return $false for String InDesiredState' { + $script:result.Where({ + $_.Property -eq 'String' + }).InDesiredState | Should -BeFalse } - Context 'When a value is mismatched but it is in Properties then' { - BeforeAll{ - $properties = @( - 'String' - 'Bool' - 'Int' - 'Array' - 'Hashtable' - ) - } - It 'Should not throw exception' { - { $script:result = Compare-DscParameterState ` - -CurrentValues $currentValues ` - -DesiredValues $desiredValues ` - -Properties $properties ` - -Verbose:$verbose } | Should -Not -Throw - } - - It 'Should return $false for String InDesiredState' { - $script:result.where({$_.Property -eq 'String'}).InDesiredState | Should -BeFalse - } - - It 'Should return same type in values' { - $script:result.where({$_.Property -eq 'String'}).ActualType | Should -Be $script:result.where({$_.Property -eq 'String'}).ExpectedType - } + It 'Should return same type in values' { + $script:result.Where({ + $_.Property -eq 'String' + }).ActualType | Should -Be $script:result.Where({ + $_.Property -eq 'String' + }).ExpectedType + } - It 'Should not return other property by default' { - $script:result.where({$_.Property -ne 'String'}).InDesiredState | Should -BeNullOrEmpty - } + It 'Should not return other property by default' { + $script:result.Where({ + $_.Property -ne 'String' + }).InDesiredState | Should -BeNullOrEmpty } } } + } - Context 'When testing single values and use IncludeInDesiredState parameter' { - BeforeAll{ - $currentValues = @{ + Context 'When testing single values and use IncludeInDesiredState parameter' { + BeforeAll { + $currentValues = @{ + String = 'a string' + Bool = $true + Int = 99 + Array = 'a', 'b', 'c' + Hashtable = @{ + k1 = 'Test' + k2 = 123 + k3 = 'v1', 'v2', 'v3' + } + ScriptBlock = { Get-Date } + } + } + + Context 'When all values match' { + BeforeAll { + $desiredValues = [PSObject] @{ String = 'a string' Bool = $true Int = 99 @@ -419,277 +490,338 @@ InModuleScope $ProjectName { ScriptBlock = { Get-Date } } } - Context 'When all values match' { - BeforeAll{ - $desiredValues = [PSObject] @{ - String = 'a string' - Bool = $true - Int = 99 - Array = 'a', 'b', 'c' - Hashtable = @{ - k1 = 'Test' - k2 = 123 - k3 = 'v1', 'v2', 'v3' - } - ScriptBlock = { Get-Date } - } - } - It 'Should not throw exception' { - { $script:result = Compare-DscParameterState ` - -CurrentValues $currentValues ` - -DesiredValues $desiredValues ` - -IncludeInDesiredState ` - -Verbose:$verbose } | Should -Not -Throw - } - It 'Should return all InDesiredState in $true' { - $script:result.InDesiredState | Should -Not -Contain $false - } + It 'Should not throw exception' { + { $script:result = Compare-DscParameterState ` + -CurrentValues $currentValues ` + -DesiredValues $desiredValues ` + -IncludeInDesiredState ` + -Verbose:$verbose } | Should -Not -Throw } - Context 'When a string is mismatched' { - BeforeAll { - $desiredValues = [PSObject] @{ - String = 'different string' - Bool = $true - Int = 99 - Array = 'a', 'b', 'c' - Hashtable = @{ - k1 = 'Test' - k2 = 123 - k3 = 'v1', 'v2', 'v3' - } + It 'Should return all InDesiredState in $true' { + $script:result.InDesiredState | Should -Not -Contain $false + } + } + + Context 'When a string is mismatched' { + BeforeAll { + $desiredValues = [PSObject] @{ + String = 'different string' + Bool = $true + Int = 99 + Array = 'a', 'b', 'c' + Hashtable = @{ + k1 = 'Test' + k2 = 123 + k3 = 'v1', 'v2', 'v3' } } - It 'Should not throw exception' { - { $script:result = Compare-DscParameterState ` - -CurrentValues $currentValues ` - -DesiredValues $desiredValues ` - -IncludeInDesiredState ` - -Verbose:$verbose } | Should -Not -Throw - } + } - It 'Should return $false for String InDesiredState' { - $script:result.where({$_.Property -eq 'String'}).InDesiredState | Should -BeFalse - } + It 'Should not throw exception' { + { $script:result = Compare-DscParameterState ` + -CurrentValues $currentValues ` + -DesiredValues $desiredValues ` + -IncludeInDesiredState ` + -Verbose:$verbose } | Should -Not -Throw + } - It 'Should return all InDesiredState (without String property) in $true' { - $script:result.where({$_.Property -ne 'String'}).InDesiredState | Should -Not -Contain $false - } + It 'Should return $false for String InDesiredState' { + $script:result.Where({ + $_.Property -eq 'String' + }).InDesiredState | Should -BeFalse } - Context 'When a boolean is mismatched' { - BeforeAll{ - $desiredValues = [PSObject] @{ - String = 'a string' - Bool = $false - Int = 99 - Array = 'a', 'b', 'c' - Hashtable = @{ - k1 = 'Test' - k2 = 123 - k3 = 'v1', 'v2', 'v3' - } + It 'Should return all InDesiredState (without String property) in $true' { + $script:result.Where({ + $_.Property -ne 'String' + }).InDesiredState | Should -Not -Contain $false + } + } + + Context 'When a boolean is mismatched' { + BeforeAll { + $desiredValues = [PSObject] @{ + String = 'a string' + Bool = $false + Int = 99 + Array = 'a', 'b', 'c' + Hashtable = @{ + k1 = 'Test' + k2 = 123 + k3 = 'v1', 'v2', 'v3' } } + } - It 'Should not throw exception' { - { $script:result = Compare-DscParameterState ` - -CurrentValues $currentValues ` - -DesiredValues $desiredValues ` - -IncludeInDesiredState ` - -Verbose:$verbose } | Should -Not -Throw - } + It 'Should not throw exception' { + { $script:result = Compare-DscParameterState ` + -CurrentValues $currentValues ` + -DesiredValues $desiredValues ` + -IncludeInDesiredState ` + -Verbose:$verbose } | Should -Not -Throw + } - It 'Should return $false for Bool InDesiredState' { - $script:result.where({$_.Property -eq 'Bool'}).InDesiredState | Should -BeFalse - } + It 'Should return $false for Bool InDesiredState' { + $script:result.Where({ + $_.Property -eq 'Bool' + }).InDesiredState | Should -BeFalse + } - It 'Should return all InDesiredState (without Bool property) in $true' { - $script:result.where({$_.Property -ne 'Bool'}).InDesiredState | Should -Not -Contain $false - } + It 'Should return all InDesiredState (without Bool property) in $true' { + $script:result.Where({ + $_.Property -ne 'Bool' + }).InDesiredState | Should -Not -Contain $false } + } - Context 'When an int is mismatched' { - BeforeAll { - $desiredValues = [PSObject] @{ - String = 'a string' - Bool = $true - Int = 1 - Array = 'a', 'b', 'c' - Hashtable = @{ - k1 = 'Test' - k2 = 123 - k3 = 'v1', 'v2', 'v3' - } + Context 'When an int is mismatched' { + BeforeAll { + $desiredValues = [PSObject] @{ + String = 'a string' + Bool = $true + Int = 1 + Array = 'a', 'b', 'c' + Hashtable = @{ + k1 = 'Test' + k2 = 123 + k3 = 'v1', 'v2', 'v3' } } - It 'Should not throw exception' { - { $script:result = Compare-DscParameterState ` - -CurrentValues $currentValues ` - -DesiredValues $desiredValues ` - -IncludeInDesiredState ` - -Verbose:$verbose } | Should -Not -Throw - } + } - It 'Should return $false for Int InDesiredState' { - $script:result.where({$_.Property -eq 'Int'}).InDesiredState | Should -BeFalse - } + It 'Should not throw exception' { + { $script:result = Compare-DscParameterState ` + -CurrentValues $currentValues ` + -DesiredValues $desiredValues ` + -IncludeInDesiredState ` + -Verbose:$verbose } | Should -Not -Throw + } - It 'Should return all InDesiredState (without Int property) in $true' { - $script:result.where({$_.Property -ne 'Int'}).InDesiredState | Should -Not -Contain $false - } + It 'Should return $false for Int InDesiredState' { + $script:result.Where({ + $_.Property -eq 'Int' + }).InDesiredState | Should -BeFalse } - Context 'When an scriptblock is mismatched' { - BeforeAll { - $desiredValues = [PSObject] @{ - String = 'a string' - Bool = $true - Int = 99 - Array = 'a', 'b', 'c' - Hashtable = @{ - k1 = 'Test' - k2 = 123 - k3 = 'v1', 'v2', 'v3' - } - ScriptBlock = { Get-Process } + It 'Should return all InDesiredState (without Int property) in $true' { + $script:result.Where({ + $_.Property -ne 'Int' + }).InDesiredState | Should -Not -Contain $false + } + } + + Context 'When an scriptblock is mismatched' { + BeforeAll { + $desiredValues = [PSObject] @{ + String = 'a string' + Bool = $true + Int = 99 + Array = 'a', 'b', 'c' + Hashtable = @{ + k1 = 'Test' + k2 = 123 + k3 = 'v1', 'v2', 'v3' } + ScriptBlock = { Get-Process } } + } - It 'Should not throw exception' { - { $script:result = Compare-DscParameterState ` - -CurrentValues $currentValues ` - -DesiredValues $desiredValues ` - -IncludeInDesiredState ` - -Verbose:$verbose } | Should -Not -Throw - } + It 'Should not throw exception' { + { $script:result = Compare-DscParameterState ` + -CurrentValues $currentValues ` + -DesiredValues $desiredValues ` + -IncludeInDesiredState ` + -Verbose:$verbose } | Should -Not -Throw + } - It 'Should return $false for ScriptBlock InDesiredState' { - $script:result.where({$_.Property -eq 'ScriptBlock'}).InDesiredState | Should -BeFalse - } + It 'Should return $false for ScriptBlock InDesiredState' { + $script:result.Where({ + $_.Property -eq 'ScriptBlock' + }).InDesiredState | Should -BeFalse + } - It 'Should return all InDesiredState (without ScriptBlock property) in $true' { - $script:result.where({$_.Property -ne 'ScriptBlock'}).InDesiredState | Should -Not -Contain $false - } + It 'Should return all InDesiredState (without ScriptBlock property) in $true' { + $script:result.Where({ + $_.Property -ne 'ScriptBlock' + }).InDesiredState | Should -Not -Contain $false } + } - Context 'When an int is mismatched without ScriptBlock' { - BeforeAll { - $desiredValues = [PSObject] @{ - String = 'a string' - Bool = $true - Int = 1 - Array = 'a', 'b', 'c' - Hashtable = @{ - k1 = 'Test' - k2 = 123 - k3 = 'v1', 'v2', 'v3' - } + Context 'When an int is mismatched without ScriptBlock' { + BeforeAll { + $desiredValues = [PSObject] @{ + String = 'a string' + Bool = $true + Int = 1 + Array = 'a', 'b', 'c' + Hashtable = @{ + k1 = 'Test' + k2 = 123 + k3 = 'v1', 'v2', 'v3' } } + } - It 'Should not throw exception' { - { $script:result = Compare-DscParameterState ` - -CurrentValues $currentValues ` - -DesiredValues $desiredValues ` - -IncludeInDesiredState ` - -Verbose:$verbose } | Should -Not -Throw - } + It 'Should not throw exception' { + { $script:result = Compare-DscParameterState ` + -CurrentValues $currentValues ` + -DesiredValues $desiredValues ` + -IncludeInDesiredState ` + -Verbose:$verbose } | Should -Not -Throw + } - It 'Should return $false for Int InDesiredState' { - $script:result.where({$_.Property -eq 'Int'}).InDesiredState | Should -BeFalse - } + It 'Should return $false for Int InDesiredState' { + $script:result.Where({ + $_.Property -eq 'Int' + }).InDesiredState | Should -BeFalse + } - It 'Should return all InDesiredState (without Int property) in $true' { - $script:result.where({$_.Property -ne 'Int'}).InDesiredState | Should -Not -Contain $false - } + It 'Should return all InDesiredState (without Int property) in $true' { + $script:result.Where({ + $_.Property -ne 'Int' + }).InDesiredState | Should -Not -Contain $false + } - It 'Should not return property with ScriptBlock in value' { - $script:result.where({$_.Property -eq 'ScriptBlock'}) | Should -BeNullOrEmpty - } + It 'Should not return property with ScriptBlock in value' { + $script:result.Where({ + $_.Property -eq 'ScriptBlock' + }) | Should -BeNullOrEmpty } + } - Context 'When a type is mismatched' { - BeforeAll{ - $desiredValues = [PSObject] @{ - String = 'a string' - Bool = $true - Int = '99' - Array = 'a', 'b', 'c' - } + Context 'When a type is mismatched' { + BeforeAll { + $desiredValues = [PSObject] @{ + String = 'a string' + Bool = $true + Int = '99' + Array = 'a', 'b', 'c' } + } - It 'Should not throw exception' { - { $script:result = Compare-DscParameterState ` - -CurrentValues $currentValues ` - -DesiredValues $desiredValues ` - -IncludeInDesiredState ` - -Verbose:$verbose } | Should -Not -Throw - } + It 'Should not throw exception' { + { $script:result = Compare-DscParameterState ` + -CurrentValues $currentValues ` + -DesiredValues $desiredValues ` + -IncludeInDesiredState ` + -Verbose:$verbose } | Should -Not -Throw + } - It 'Should return $false for Int InDesiredState' { - $script:result.where({$_.Property -eq 'Int'}).InDesiredState | Should -BeFalse - } + It 'Should return $false for Int InDesiredState' { + $script:result.Where({ + $_.Property -eq 'Int' + }).InDesiredState | Should -BeFalse + } - It 'Should return all InDesiredState (without Int property) in $true' { - $script:result.where({$_.Property -ne 'Int'}).InDesiredState | Should -Not -Contain $false - } + It 'Should return all InDesiredState (without Int property) in $true' { + $script:result.Where({ + $_.Property -ne 'Int' + }).InDesiredState | Should -Not -Contain $false } + } - Context 'When a type is mismatched but TurnOffTypeChecking is used' { - BeforeAll{ - $desiredValues = [PSObject] @{ - String = 'a string' - Bool = $true - Int = '99' - Array = 'a', 'b', 'c' - } + Context 'When a type is mismatched but TurnOffTypeChecking is used' { + BeforeAll { + $desiredValues = [PSObject] @{ + String = 'a string' + Bool = $true + Int = '99' + Array = 'a', 'b', 'c' } + } - It 'Should not throw exception' { - { $script:result = Compare-DscParameterState ` - -CurrentValues $currentValues ` - -DesiredValues $desiredValues ` - -IncludeInDesiredState ` - -TurnOffTypeChecking ` - -Verbose:$verbose } | Should -Not -Throw - } + It 'Should not throw exception' { + { $script:result = Compare-DscParameterState ` + -CurrentValues $currentValues ` + -DesiredValues $desiredValues ` + -IncludeInDesiredState ` + -TurnOffTypeChecking ` + -Verbose:$verbose } | Should -Not -Throw + } - It 'Should return $true for Int InDesiredState' { - $script:result.where({$_.Property -eq 'Int'}).InDesiredState | Should -BeTrue - } + It 'Should return $true for Int InDesiredState' { + $script:result.Where({ + $_.Property -eq 'Int' + }).InDesiredState | Should -BeTrue + } - It 'Should return all InDesiredState in $true' { - $script:result.InDesiredState | Should -Not -Contain $false - } + It 'Should return all InDesiredState in $true' { + $script:result.InDesiredState | Should -Not -Contain $false } + } - Context 'When a value is mismatched but ExcludeProperties is used to exclude then' { - BeforeAll{ - $desiredValues = @{ - String = 'some other string' - Bool = $true - Int = 99 - Array = 'a', 'b', 'c' - Hashtable = @{ - k1 = 'Test' - k2 = 123 - k3 = 'v1', 'v2', 'v3' - } - ScriptBlock = { Get-Date } + Context 'When a value is mismatched but ExcludeProperties is used to exclude then' { + BeforeAll { + $desiredValues = @{ + String = 'some other string' + Bool = $true + Int = 99 + Array = 'a', 'b', 'c' + Hashtable = @{ + k1 = 'Test' + k2 = 123 + k3 = 'v1', 'v2', 'v3' } + ScriptBlock = { Get-Date } } $excludeProperties = @( 'String' ) + } + + It 'Should not throw exception' { + { $script:result = Compare-DscParameterState ` + -CurrentValues $currentValues ` + -DesiredValues $desiredValues ` + -ExcludeProperties $excludeProperties ` + -IncludeInDesiredState ` + -Verbose:$verbose } | Should -Not -Throw + } + + It 'Should return all InDesiredState in $true' { + $script:result.InDesiredState | Should -Not -Contain $false + } + + It 'Should not return property with String in value' { + $script:result.Where({ + $_.Property -eq 'String' + }) | Should -BeNullOrEmpty + } + } + + Context 'When a value is mismatched but it is not in Properties then' { + BeforeAll { + $desiredValues = @{ + String = 'some other string' + Bool = $true + Int = 99 + Array = 'a', 'b', 'c' + Hashtable = @{ + k1 = 'Test' + k2 = 123 + k3 = 'v1', 'v2', 'v3' + } + ScriptBlock = { Get-Date } + } + + $properties = @( + 'Bool' + 'Int' + 'Array' + 'Hashtable' + 'ScriptBlock' + ) + } + Context 'When using the alias ValuesToCheck' { It 'Should not throw exception' { { $script:result = Compare-DscParameterState ` -CurrentValues $currentValues ` -DesiredValues $desiredValues ` - -ExcludeProperties $excludeProperties ` + -ValuesToCheck $properties ` -IncludeInDesiredState ` -Verbose:$verbose } | Should -Not -Throw } @@ -698,58 +830,48 @@ InModuleScope $ProjectName { $script:result.InDesiredState | Should -Not -Contain $false } - It 'Should not return property with String in value' { - $script:result.where({$_.Property -eq 'String'}) | Should -BeNullOrEmpty + It 'Should return all property in $properties' { + $script:result.Property.Count | Should -Be $properties.Count + foreach ($property in $properties) + { + $property | Should -BeIn $script:result.Property + } } } - Context 'When a value is mismatched but it is not in Properties then' { - BeforeAll{ - $desiredValues = @{ - String = 'some other string' - Bool = $true - Int = 99 - Array = 'a', 'b', 'c' - Hashtable = @{ - k1 = 'Test' - k2 = 123 - k3 = 'v1', 'v2', 'v3' - } - ScriptBlock = { Get-Date } - } + It 'Should not throw exception' { + { $script:result = Compare-DscParameterState ` + -CurrentValues $currentValues ` + -DesiredValues $desiredValues ` + -Properties $properties ` + -IncludeInDesiredState ` + -Verbose:$verbose } | Should -Not -Throw + } + + It 'Should return all InDesiredState in $true' { + $script:result.InDesiredState | Should -Not -Contain $false + } + + It 'Should return all property in $properties' { + $script:result.Property.Count | Should -Be $properties.Count + foreach ($property in $properties) + { + $property | Should -BeIn $script:result.Property + } + } + + Context 'When a value is mismatched but it is in Properties then' { + BeforeAll { $properties = @( + 'String' 'Bool' 'Int' 'Array' 'Hashtable' - 'ScriptBlock' ) } - Context 'When using the alias ValuesToCheck' { - It 'Should not throw exception' { - { $script:result = Compare-DscParameterState ` - -CurrentValues $currentValues ` - -DesiredValues $desiredValues ` - -ValuesToCheck $properties ` - -IncludeInDesiredState ` - -Verbose:$verbose } | Should -Not -Throw - } - - It 'Should return all InDesiredState in $true' { - $script:result.InDesiredState | Should -Not -Contain $false - } - - It 'Should return all property in $properties' { - $script:result.Property.Count | Should -Be $Properties.Count - foreach ($Property in $Properties) - { - $property | Should -BeIn $script:result.Property - } - } - } - It 'Should not throw exception' { { $script:result = Compare-DscParameterState ` -CurrentValues $currentValues ` @@ -759,65 +881,61 @@ InModuleScope $ProjectName { -Verbose:$verbose } | Should -Not -Throw } - It 'Should return all InDesiredState in $true' { - $script:result.InDesiredState | Should -Not -Contain $false + It 'Should return $false for String InDesiredState' { + $script:result.Where({ + $_.Property -eq 'String' + }).InDesiredState | Should -BeFalse + } + + It 'Should return all InDesiredState (without String property) in $true' { + $script:result.Where({ + $_.Property -ne 'String' + }).InDesiredState | Should -Not -Contain $false } It 'Should return all property in $properties' { - $script:result.Property.Count | Should -Be $Properties.Count - foreach ($Property in $Properties) + $script:result.Property.Count | Should -Be $properties.Count + + foreach ($property in $properties) { $property | Should -BeIn $script:result.Property } } - Context 'When a value is mismatched but it is in Properties then' { - BeforeAll{ - $properties = @( - 'String' - 'Bool' - 'Int' - 'Array' - 'Hashtable' - ) - } - It 'Should not throw exception' { - { $script:result = Compare-DscParameterState ` - -CurrentValues $currentValues ` - -DesiredValues $desiredValues ` - -Properties $properties ` - -IncludeInDesiredState ` - -Verbose:$verbose } | Should -Not -Throw - } - - It 'Should return $false for String InDesiredState' { - $script:result.where({$_.Property -eq 'String'}).InDesiredState | Should -BeFalse - } - - It 'Should return all InDesiredState (without String property) in $true' { - $script:result.where({$_.Property -ne 'String'}).InDesiredState | Should -Not -Contain $false - } + It 'Should not return property with ScriptBlock in value' { + $script:result.Where({ + $_.Property -eq 'ScriptBlock' + }) | Should -BeNullOrEmpty + } + } + } + } - It 'Should return all property in $properties' { - $script:result.Property.Count | Should -Be $Properties.Count - foreach ($Property in $Properties) - { - $property | Should -BeIn $script:result.Property - } - } + Context 'When testing PSCredential' { - It 'Should not return property with ScriptBlock in value' { - $script:result.where({$_.Property -eq 'ScriptBlock'}) | Should -BeNullOrEmpty + Context 'When currentValue is PSCredential type' { + BeforeAll { + $currentValues = @{ + String = 'a string' + Bool = $true + Int = 99 + Array = 'a', 'b', 'c' + Hashtable = @{ + k1 = 'Test' + k2 = 123 + k3 = 'v1', 'v2', 'v3' } + ScriptBlock = { Get-Date } + PSCredential = [System.Management.Automation.PSCredential]::new( + 'UserName', + $(ConvertTo-SecureString -String 'Pa55w.rd' -AsPlainText -Force) + ) } } - } - Context 'When testing pscredential' { - - Context 'When currentValue is pscredential type' { + Context 'When all values match' { BeforeAll { - $currentValues = @{ + $desiredValues = [PSObject] @{ String = 'a string' Bool = $true Int = 99 @@ -828,86 +946,28 @@ InModuleScope $ProjectName { k3 = 'v1', 'v2', 'v3' } ScriptBlock = { Get-Date } - PScredential = [pscredential]::new( + PSCredential = [System.Management.Automation.PSCredential]::new( 'UserName', $(ConvertTo-SecureString -String 'Pa55w.rd' -AsPlainText -Force) ) } } - - Context 'When all values match' { - BeforeAll{ - $desiredValues = [PSObject] @{ - String = 'a string' - Bool = $true - Int = 99 - Array = 'a', 'b', 'c' - Hashtable = @{ - k1 = 'Test' - k2 = 123 - k3 = 'v1', 'v2', 'v3' - } - ScriptBlock = { Get-Date } - PScredential = [pscredential]::new( - 'UserName', - $(ConvertTo-SecureString -String 'Pa55w.rd' -AsPlainText -Force) - ) - } - } - It 'Should not throw exception' { - { $script:result = Compare-DscParameterState ` - -CurrentValues $currentValues ` - -DesiredValues $desiredValues ` - -IncludeInDesiredState ` - -Verbose:$verbose } | Should -Not -Throw - } - - It 'Should return all InDesiredState in $true' { - $script:result.InDesiredState | Should -Not -Contain $false - } + It 'Should not throw exception' { + { $script:result = Compare-DscParameterState ` + -CurrentValues $currentValues ` + -DesiredValues $desiredValues ` + -IncludeInDesiredState ` + -Verbose:$verbose } | Should -Not -Throw } - Context 'When an pscredential is mismatched' { - BeforeAll { - $desiredValues = [PSObject] @{ - String = 'a string' - Bool = $true - Int = 99 - Array = 'a', 'b', 'c' - Hashtable = @{ - k1 = 'Test' - k2 = 123 - k3 = 'v1', 'v2', 'v3' - } - ScriptBlock = { Get-Date } - PScredential = [pscredential]::new( - 'SurName', - $(ConvertTo-SecureString -String 'Pa55w.rd' -AsPlainText -Force) - ) - } - } - - It 'Should not throw exception' { - { $script:result = Compare-DscParameterState ` - -CurrentValues $currentValues ` - -DesiredValues $desiredValues ` - -IncludeInDesiredState ` - -Verbose:$verbose } | Should -Not -Throw - } - - It 'Should return $false for PScredential InDesiredState' { - $script:result.where({$_.Property -eq 'PScredential'}).InDesiredState | Should -BeFalse - } - - It 'Should return all InDesiredState (without PScredential property) in $true' { - $script:result.where({$_.Property -ne 'PScredential'}).InDesiredState | Should -Not -Contain $false - } + It 'Should return all InDesiredState in $true' { + $script:result.InDesiredState | Should -Not -Contain $false } } - Context 'When currentValue is string type' { + Context 'When an PSCredential is mismatched' { BeforeAll { - $currentValues = @{ + $desiredValues = [PSObject] @{ String = 'a string' Bool = $true Int = 99 @@ -918,108 +978,69 @@ InModuleScope $ProjectName { k3 = 'v1', 'v2', 'v3' } ScriptBlock = { Get-Date } - PScredential = 'UserName' + PSCredential = [System.Management.Automation.PSCredential]::new( + 'SurName', + $(ConvertTo-SecureString -String 'Pa55w.rd' -AsPlainText -Force) + ) } } - Context 'When all values match' { - BeforeAll{ - $desiredValues = [PSObject] @{ - String = 'a string' - Bool = $true - Int = 99 - Array = 'a', 'b', 'c' - Hashtable = @{ - k1 = 'Test' - k2 = 123 - k3 = 'v1', 'v2', 'v3' - } - ScriptBlock = { Get-Date } - PScredential = [pscredential]::new( - 'UserName', - $(ConvertTo-SecureString -String 'Pa55w.rd' -AsPlainText -Force) - ) - } - } - It 'Should not throw exception' { - { $script:result = Compare-DscParameterState ` - -CurrentValues $currentValues ` - -DesiredValues $desiredValues ` - -IncludeInDesiredState ` - -Verbose:$verbose } | Should -Not -Throw - } - - It 'Should return all InDesiredState in $true' { - $script:result.InDesiredState | Should -Not -Contain $false - } + It 'Should not throw exception' { + { $script:result = Compare-DscParameterState ` + -CurrentValues $currentValues ` + -DesiredValues $desiredValues ` + -IncludeInDesiredState ` + -Verbose:$verbose } | Should -Not -Throw } - Context 'When an pscredential is mismatched' { - BeforeAll { - $desiredValues = [PSObject] @{ - String = 'a string' - Bool = $true - Int = 99 - Array = 'a', 'b', 'c' - Hashtable = @{ - k1 = 'Test' - k2 = 123 - k3 = 'v1', 'v2', 'v3' - } - ScriptBlock = { Get-Date } - PScredential = [pscredential]::new( - 'SurName', - $(ConvertTo-SecureString -String 'Pa55w.rd' -AsPlainText -Force) - ) - } - } - - It 'Should not throw exception' { - { $script:result = Compare-DscParameterState ` - -CurrentValues $currentValues ` - -DesiredValues $desiredValues ` - -IncludeInDesiredState ` - -Verbose:$verbose } | Should -Not -Throw - } - - It 'Should return $false for PScredential InDesiredState' { - $script:result.where({$_.Property -eq 'PScredential'}).InDesiredState | Should -BeFalse - } + It 'Should return $false for PSCredential InDesiredState' { + $script:result.Where({ + $_.Property -eq 'PSCredential' + }).InDesiredState | Should -BeFalse + } - It 'Should return all InDesiredState (without PScredential property) in $true' { - $script:result.where({$_.Property -ne 'PScredential'}).InDesiredState | Should -Not -Contain $false - } + It 'Should return all InDesiredState (without PSCredential property) in $true' { + $script:result.Where({ + $_.Property -ne 'PSCredential' + }).InDesiredState | Should -Not -Contain $false } } } - Context 'When testing array values' { + Context 'When currentValue is string type' { BeforeAll { $currentValues = @{ - String = 'a string' - Bool = $true - Int = 99 - Array = 'a', 'b', 'c', 1 - Hashtable = @{ + String = 'a string' + Bool = $true + Int = 99 + Array = 'a', 'b', 'c' + Hashtable = @{ k1 = 'Test' k2 = 123 k3 = 'v1', 'v2', 'v3' } + ScriptBlock = { Get-Date } + PScredential = 'UserName' } } - Context 'When array is missing a value' { + Context 'When all values match' { BeforeAll { $desiredValues = [PSObject] @{ - String = 'a string' - Bool = $true - Int = 99 - Array = 'a', 'b', 'c' - Hashtable = @{ + String = 'a string' + Bool = $true + Int = 99 + Array = 'a', 'b', 'c' + Hashtable = @{ k1 = 'Test' k2 = 123 k3 = 'v1', 'v2', 'v3' } + ScriptBlock = { Get-Date } + PSCredential = [System.Management.Automation.PSCredential]::new( + 'UserName', + $(ConvertTo-SecureString -String 'Pa55w.rd' -AsPlainText -Force) + ) } } @@ -1031,22 +1052,28 @@ InModuleScope $ProjectName { -Verbose:$verbose } | Should -Not -Throw } - It 'Should return $false for Array InDesiredState' { - $script:result.where({$_.Property -eq 'Array'}).InDesiredState | Should -BeFalse - } - - It 'Should return all InDesiredState (without Array property) in $true' { - $script:result.where({$_.Property -ne 'Array'}).InDesiredState | Should -Not -Contain $false + It 'Should return all InDesiredState in $true' { + $script:result.InDesiredState | Should -Not -Contain $false } } - Context 'When array has an additional value' { + Context 'When an PSCredential is mismatched' { BeforeAll { $desiredValues = [PSObject] @{ - String = 'a string' - Bool = $true - Int = 99 - Array = 'a', 'b', 'c', 1, 2 + String = 'a string' + Bool = $true + Int = 99 + Array = 'a', 'b', 'c' + Hashtable = @{ + k1 = 'Test' + k2 = 123 + k3 = 'v1', 'v2', 'v3' + } + ScriptBlock = { Get-Date } + PSCredential = [System.Management.Automation.PSCredential]::new( + 'SurName', + $(ConvertTo-SecureString -String 'Pa55w.rd' -AsPlainText -Force) + ) } } @@ -1058,533 +1085,689 @@ InModuleScope $ProjectName { -Verbose:$verbose } | Should -Not -Throw } - It 'Should return $false for Array InDesiredState' { - $script:result.where({$_.Property -eq 'Array'}).InDesiredState | Should -BeFalse + It 'Should return $false for PSCredential InDesiredState' { + $script:result.Where({ + $_.Property -eq 'PSCredential' + }).InDesiredState | Should -BeFalse } - It 'Should return all InDesiredState (without Array property) in $true' { - $script:result.where({$_.Property -ne 'Array'}).InDesiredState | Should -Not -Contain $false + It 'Should return all InDesiredState (without PSCredential property) in $true' { + $script:result.Where({ + $_.Property -ne 'PSCredential' + }).InDesiredState | Should -Not -Contain $false } } + } + } - Context 'When array has a different value' { - BeforeAll { - $desiredValues = [PSObject] @{ - String = 'a string' - Bool = $true - Int = 99 - Array = 'a', 'x', 'c', 1 - } + Context 'When testing array values' { + BeforeAll { + $currentValues = @{ + String = 'a string' + Bool = $true + Int = 99 + Array = 'a', 'b', 'c', 1 + Hashtable = @{ + k1 = 'Test' + k2 = 123 + k3 = 'v1', 'v2', 'v3' } + } + } - It 'Should not throw exception' { - { $script:result = Compare-DscParameterState ` - -CurrentValues $currentValues ` - -DesiredValues $desiredValues ` - -IncludeInDesiredState ` - -Verbose:$verbose } | Should -Not -Throw + Context 'When array is missing a value' { + BeforeAll { + $desiredValues = [PSObject] @{ + String = 'a string' + Bool = $true + Int = 99 + Array = 'a', 'b', 'c' + Hashtable = @{ + k1 = 'Test' + k2 = 123 + k3 = 'v1', 'v2', 'v3' + } } + } - It 'Should return $false for Array InDesiredState' { - $script:result.where({$_.Property -eq 'Array'}).InDesiredState | Should -BeFalse - } + It 'Should not throw exception' { + { $script:result = Compare-DscParameterState ` + -CurrentValues $currentValues ` + -DesiredValues $desiredValues ` + -IncludeInDesiredState ` + -Verbose:$verbose } | Should -Not -Throw + } - It 'Should return all InDesiredState (without Array property) in $true' { - $script:result.where({$_.Property -ne 'Array'}).InDesiredState | Should -Not -Contain $false - } + It 'Should return $false for Array InDesiredState' { + $script:result.Where({ + $_.Property -eq 'Array' + }).InDesiredState | Should -BeFalse } - Context 'When array has different order' { - BeforeAll { - $desiredValues = [PSObject] @{ - String = 'a string' - Bool = $true - Int = 99 - Array = 'c', 'b', 'a', 1 - } + It 'Should return all InDesiredState (without Array property) in $true' { + $script:result.Where({ + $_.Property -ne 'Array' + }).InDesiredState | Should -Not -Contain $false + } + } + + Context 'When array has an additional value' { + BeforeAll { + $desiredValues = [PSObject] @{ + String = 'a string' + Bool = $true + Int = 99 + Array = 'a', 'b', 'c', 1, 2 } + } - It 'Should not throw exception' { - { $script:result = Compare-DscParameterState ` - -CurrentValues $currentValues ` - -DesiredValues $desiredValues ` - -IncludeInDesiredState ` - -Verbose:$verbose } | Should -Not -Throw - } + It 'Should not throw exception' { + { $script:result = Compare-DscParameterState ` + -CurrentValues $currentValues ` + -DesiredValues $desiredValues ` + -IncludeInDesiredState ` + -Verbose:$verbose } | Should -Not -Throw + } - It 'Should return $false for Array InDesiredState' { - $script:result.where({$_.Property -eq 'Array'}).InDesiredState | Should -BeFalse - } + It 'Should return $false for Array InDesiredState' { + $script:result.Where({ + $_.Property -eq 'Array' + }).InDesiredState | Should -BeFalse + } - It 'Should return all InDesiredState (without Array property) in $true' { - $script:result.where({$_.Property -ne 'Array'}).InDesiredState | Should -Not -Contain $false - } + It 'Should return all InDesiredState (without Array property) in $true' { + $script:result.Where({ + $_.Property -ne 'Array' + }).InDesiredState | Should -Not -Contain $false } + } - Context 'When array has different order but SortArrayValues is used' { - BeforeAll { - $desiredValues = [PSObject] @{ - String = 'a string' - Bool = $true - Int = 99 - Array = 'c', 'b', 'a', 1 - } + Context 'When array has a different value' { + BeforeAll { + $desiredValues = [PSObject] @{ + String = 'a string' + Bool = $true + Int = 99 + Array = 'a', 'x', 'c', 1 } + } - It 'Should not throw exception' { - { $script:result = Compare-DscParameterState ` - -CurrentValues $currentValues ` - -DesiredValues $desiredValues ` - -SortArrayValues ` - -IncludeInDesiredState ` - -Verbose:$verbose } | Should -Not -Throw - } + It 'Should not throw exception' { + { $script:result = Compare-DscParameterState ` + -CurrentValues $currentValues ` + -DesiredValues $desiredValues ` + -IncludeInDesiredState ` + -Verbose:$verbose } | Should -Not -Throw + } - It 'Should return $true for Array InDesiredState' { - $script:result.where({$_.Property -eq 'Array'}).InDesiredState | Should -BeTrue - } + It 'Should return $false for Array InDesiredState' { + $script:result.Where({ + $_.Property -eq 'Array' + }).InDesiredState | Should -BeFalse + } - It 'Should return all InDesiredState (without Array property) in $true' { - $script:result.where({$_.Property -ne 'Array'}).InDesiredState | Should -Not -Contain $false - } + It 'Should return all InDesiredState (without Array property) in $true' { + $script:result.Where({$_.Property -ne 'Array'}).InDesiredState | Should -Not -Contain $false } + } - Context 'When array has a value with a different type' { - BeforeAll { - $desiredValues = [PSObject] @{ - String = 'a string' - Bool = $true - Int = 99 - Array = 'a', 'b', 'c', '1' - } + Context 'When array has different order' { + BeforeAll { + $desiredValues = [PSObject] @{ + String = 'a string' + Bool = $true + Int = 99 + Array = 'c', 'b', 'a', 1 } + } - It 'Should not throw exception' { - { $script:result = Compare-DscParameterState ` - -CurrentValues $currentValues ` - -DesiredValues $desiredValues ` - -IncludeInDesiredState ` - -Verbose:$verbose } | Should -Not -Throw - } + It 'Should not throw exception' { + { $script:result = Compare-DscParameterState ` + -CurrentValues $currentValues ` + -DesiredValues $desiredValues ` + -IncludeInDesiredState ` + -Verbose:$verbose } | Should -Not -Throw + } - It 'Should return $false for Array InDesiredState' { - $script:result.where({$_.Property -eq 'Array'}).InDesiredState | Should -BeFalse - } + It 'Should return $false for Array InDesiredState' { + $script:result.Where({ + $_.Property -eq 'Array' + }).InDesiredState | Should -BeFalse + } - It 'Should return all InDesiredState (without Array property) in $true' { - $script:result.where({$_.Property -ne 'Array'}).InDesiredState | Should -Not -Contain $false - } + It 'Should return all InDesiredState (without Array property) in $true' { + $script:result.Where({$_.Property -ne 'Array'}).InDesiredState | Should -Not -Contain $false } + } - Context 'When array has a value with a different type but TurnOffTypeChecking is used' { - BeforeAll { - $desiredValues = [PSObject] @{ - String = 'a string' - Bool = $true - Int = 99 - Array = 'a', 'b', 'c', '1' - } + Context 'When array has different order but SortArrayValues is used' { + BeforeAll { + $desiredValues = [PSObject] @{ + String = 'a string' + Bool = $true + Int = 99 + Array = 'c', 'b', 'a', 1 } + } - It 'Should not throw exception' { - { $script:result = Compare-DscParameterState ` - -CurrentValues $currentValues ` - -DesiredValues $desiredValues ` - -TurnOffTypeChecking ` - -IncludeInDesiredState ` - -Verbose:$verbose } | Should -Not -Throw - } + It 'Should not throw exception' { + { $script:result = Compare-DscParameterState ` + -CurrentValues $currentValues ` + -DesiredValues $desiredValues ` + -SortArrayValues ` + -IncludeInDesiredState ` + -Verbose:$verbose } | Should -Not -Throw + } - It 'Should return $true for Array InDesiredState' { - $script:result.where({$_.Property -eq 'Array'}).InDesiredState | Should -BeTrue - } + It 'Should return $true for Array InDesiredState' { + $script:result.Where({ + $_.Property -eq 'Array' + }).InDesiredState | Should -BeTrue + } + + It 'Should return all InDesiredState (without Array property) in $true' { + $script:result.Where({ + $_.Property -ne 'Array' + }).InDesiredState | Should -Not -Contain $false + } + } - It 'Should return all InDesiredState (without Array property) in $true' { - $script:result.where({$_.Property -ne 'Array'}).InDesiredState | Should -Not -Contain $false + Context 'When array has a value with a different type' { + BeforeAll { + $desiredValues = [PSObject] @{ + String = 'a string' + Bool = $true + Int = 99 + Array = 'a', 'b', 'c', '1' } } - Context 'When both arrays are empty' { - BeforeAll { - $currentValues = @{ - String = 'a string' - Bool = $true - Int = 99 - Array = @() - Hashtable = @{ - k1 = 'Test' - k2 = 123 - k3 = @() - } - } + It 'Should not throw exception' { + { $script:result = Compare-DscParameterState ` + -CurrentValues $currentValues ` + -DesiredValues $desiredValues ` + -IncludeInDesiredState ` + -Verbose:$verbose } | Should -Not -Throw + } - $desiredValues = [PSObject] @{ - String = 'a string' - Bool = $true - Int = 99 - Array = @() - Hashtable = @{ - k1 = 'Test' - k2 = 123 - k3 = @() - } - } - } + It 'Should return $false for Array InDesiredState' { + $script:result.Where({ + $_.Property -eq 'Array' + }).InDesiredState | Should -BeFalse + } - It 'Should not throw exception' { - { $script:result = Compare-DscParameterState ` - -CurrentValues $currentValues ` - -DesiredValues $desiredValues ` - -IncludeInDesiredState ` - -Verbose:$verbose } | Should -Not -Throw - } + It 'Should return all InDesiredState (without Array property) in $true' { + $script:result.Where({ + $_.Property -ne 'Array' + }).InDesiredState | Should -Not -Contain $false + } + } - It 'Should return $true for Array InDesiredState' { - $script:result.where({$_.Property -eq 'Array'}).InDesiredState | Should -BeTrue + Context 'When array has a value with a different type but TurnOffTypeChecking is used' { + BeforeAll { + $desiredValues = [PSObject] @{ + String = 'a string' + Bool = $true + Int = 99 + Array = 'a', 'b', 'c', '1' } + } - It 'Should return all InDesiredState (without Array property) in $true' { - $script:result.where({$_.Property -ne 'Array'}).InDesiredState | Should -Not -Contain $false - } + It 'Should not throw exception' { + { $script:result = Compare-DscParameterState ` + -CurrentValues $currentValues ` + -DesiredValues $desiredValues ` + -TurnOffTypeChecking ` + -IncludeInDesiredState ` + -Verbose:$verbose } | Should -Not -Throw } - Context 'When a current value array is empty' { - BeforeAll { - $currentValues = @{ - String = 'a string' - Bool = $true - Int = 99 - Array = @('a','b','c') - Hashtable = @{ - k1 = 'Test' - k2 = 123 - k3 = @() - } - } + It 'Should return $true for Array InDesiredState' { + $script:result.Where({ + $_.Property -eq 'Array' + }).InDesiredState | Should -BeTrue + } - $desiredValues = [PSObject] @{ - String = 'a string' - Bool = $true - Int = 99 - Array = @() - Hashtable = @{ - k1 = 'Test' - k2 = 123 - k3 = @() - } + It 'Should return all InDesiredState (without Array property) in $true' { + $script:result.Where({ + $_.Property -ne 'Array' + }).InDesiredState | Should -Not -Contain $false + } + } + + Context 'When both arrays are empty' { + BeforeAll { + $currentValues = @{ + String = 'a string' + Bool = $true + Int = 99 + Array = @() + Hashtable = @{ + k1 = 'Test' + k2 = 123 + k3 = @() } } - It 'Should not throw exception' { - { $script:result = Compare-DscParameterState ` - -CurrentValues $currentValues ` - -DesiredValues $desiredValues ` - -IncludeInDesiredState ` - -Verbose:$verbose } | Should -Not -Throw + $desiredValues = [PSObject] @{ + String = 'a string' + Bool = $true + Int = 99 + Array = @() + Hashtable = @{ + k1 = 'Test' + k2 = 123 + k3 = @() + } } + } - It 'Should return $false for Array InDesiredState' { - $script:result.where({$_.Property -eq 'Array'}).InDesiredState | Should -BeFalse - } + It 'Should not throw exception' { + { $script:result = Compare-DscParameterState ` + -CurrentValues $currentValues ` + -DesiredValues $desiredValues ` + -IncludeInDesiredState ` + -Verbose:$verbose } | Should -Not -Throw + } - It 'Should return all InDesiredState (without Array property) in $true' { - $script:result.where({$_.Property -ne 'Array'}).InDesiredState | Should -Not -Contain $false - } + It 'Should return $true for Array InDesiredState' { + $script:result.Where({ + $_.Property -eq 'Array' + }).InDesiredState | Should -BeTrue } - Context 'When a desired value array is empty' { - BeforeAll { - $currentValues = @{ - String = 'a string' - Bool = $true - Int = 99 - Array = @() - Hashtable = @{ - k1 = 'Test' - k2 = 123 - k3 = @() - } - } + It 'Should return all InDesiredState (without Array property) in $true' { + $script:result.Where({ + $_.Property -ne 'Array' + }).InDesiredState | Should -Not -Contain $false + } + } - $desiredValues = [PSObject] @{ - String = 'a string' - Bool = $true - Int = 99 - Array = @('a','b','c') - Hashtable = @{ - k1 = 'Test' - k2 = 123 - k3 = @() - } + Context 'When a current value array is empty' { + BeforeAll { + $currentValues = @{ + String = 'a string' + Bool = $true + Int = 99 + Array = @('a','b','c') + Hashtable = @{ + k1 = 'Test' + k2 = 123 + k3 = @() } } - It 'Should not throw exception' { - { $script:result = Compare-DscParameterState ` - -CurrentValues $currentValues ` - -DesiredValues $desiredValues ` - -IncludeInDesiredState ` - -Verbose:$verbose } | Should -Not -Throw + $desiredValues = [PSObject] @{ + String = 'a string' + Bool = $true + Int = 99 + Array = @() + Hashtable = @{ + k1 = 'Test' + k2 = 123 + k3 = @() + } } + } - It 'Should return $false for Array InDesiredState' { - $script:result.where({$_.Property -eq 'Array'}).InDesiredState | Should -BeFalse - } + It 'Should not throw exception' { + { $script:result = Compare-DscParameterState ` + -CurrentValues $currentValues ` + -DesiredValues $desiredValues ` + -IncludeInDesiredState ` + -Verbose:$verbose } | Should -Not -Throw + } - It 'Should return all InDesiredState (without Array property) in $true' { - $script:result.where({$_.Property -ne 'Array'}).InDesiredState | Should -Not -Contain $false - } + It 'Should return $false for Array InDesiredState' { + $script:result.Where({ + $_.Property -eq 'Array' + }).InDesiredState | Should -BeFalse + } + + It 'Should return all InDesiredState (without Array property) in $true' { + $script:result.Where({ + $_.Property -ne 'Array' + }).InDesiredState | Should -Not -Contain $false } } - Context 'When testing hashtables' { + Context 'When a desired value array is empty' { BeforeAll { $currentValues = @{ String = 'a string' Bool = $true Int = 99 - Array = 'a', 'b', 'c' + Array = @() Hashtable = @{ k1 = 'Test' k2 = 123 - k3 = 'v1', 'v2', 'v3', 99 + k3 = @() } } - } - Context 'When hashtable is missing a value' { - BeforeAll { - $desiredValues = [PSObject] @{ - String = 'a string' - Bool = $true - Int = 99 - Array = 'a', 'b', 'c' - Hashtable = @{ - k1 = 'Test' - k2 = 123 - k3 = 'v1', 'v2', 'v3' - } + $desiredValues = [PSObject] @{ + String = 'a string' + Bool = $true + Int = 99 + Array = @('a','b','c') + Hashtable = @{ + k1 = 'Test' + k2 = 123 + k3 = @() } } + } - It 'Should not throw exception' { - { $script:result = Compare-DscParameterState ` - -CurrentValues $currentValues ` - -DesiredValues $desiredValues ` - -IncludeInDesiredState ` - -Verbose:$verbose } | Should -Not -Throw - } + It 'Should not throw exception' { + { $script:result = Compare-DscParameterState ` + -CurrentValues $currentValues ` + -DesiredValues $desiredValues ` + -IncludeInDesiredState ` + -Verbose:$verbose } | Should -Not -Throw + } - It 'Should return $false for Hashtable InDesiredState' { - $script:result.where({$_.Property -eq 'Hashtable'}).InDesiredState | Should -BeFalse - } + It 'Should return $false for Array InDesiredState' { + $script:result.Where({ + $_.Property -eq 'Array' + }).InDesiredState | Should -BeFalse + } + + It 'Should return all InDesiredState (without Array property) in $true' { + $script:result.Where({ + $_.Property -ne 'Array' + }).InDesiredState | Should -Not -Contain $false + } + } + } - It 'Should return all InDesiredState (without Hashtable property) in $true' { - $script:result.where({$_.Property -ne 'Hashtable'}).InDesiredState | Should -Not -Contain $false + Context 'When testing hashtables' { + BeforeAll { + $currentValues = @{ + String = 'a string' + Bool = $true + Int = 99 + Array = 'a', 'b', 'c' + Hashtable = @{ + k1 = 'Test' + k2 = 123 + k3 = 'v1', 'v2', 'v3', 99 } } + } - Context 'When hashtable has an additional value' { - BeforeAll { - $desiredValues = [PSObject] @{ - String = 'a string' - Bool = $true - Int = 99 - Array = 'a', 'b', 'c' - Hashtable = @{ - k1 = 'Test' - k2 = 123 - k3 = 'v1', 'v2', 'v3', 99, 100 - } + Context 'When hashtable is missing a value' { + BeforeAll { + $desiredValues = [PSObject] @{ + String = 'a string' + Bool = $true + Int = 99 + Array = 'a', 'b', 'c' + Hashtable = @{ + k1 = 'Test' + k2 = 123 + k3 = 'v1', 'v2', 'v3' } } + } - It 'Should not throw exception' { - { $script:result = Compare-DscParameterState ` - -CurrentValues $currentValues ` - -DesiredValues $desiredValues ` - -IncludeInDesiredState ` - -Verbose:$verbose } | Should -Not -Throw - } + It 'Should not throw exception' { + { $script:result = Compare-DscParameterState ` + -CurrentValues $currentValues ` + -DesiredValues $desiredValues ` + -IncludeInDesiredState ` + -Verbose:$verbose } | Should -Not -Throw + } - It 'Should return $false for Hashtable InDesiredState' { - $script:result.where({$_.Property -eq 'Hashtable'}).InDesiredState | Should -BeFalse - } + It 'Should return $false for Hashtable InDesiredState' { + $script:result.Where({ + $_.Property -eq 'Hashtable' + }).InDesiredState | Should -BeFalse + } - It 'Should return all InDesiredState (without Hashtable property) in $true' { - $script:result.where({$_.Property -ne 'Hashtable'}).InDesiredState | Should -Not -Contain $false - } + It 'Should return all InDesiredState (without Hashtable property) in $true' { + $script:result.Where({ + $_.Property -ne 'Hashtable' + }).InDesiredState | Should -Not -Contain $false } + } - Context 'When hashtable has a different value' { - BeforeAll { - $desiredValues = [PSObject] @{ - String = 'a string' - Bool = $true - Int = 99 - Array = 'a', 'b', 'c' - Hashtable = @{ - k1 = 'Test' - k2 = 123 - k3 = 'xx', 'v2', 'v3', 99 - } + Context 'When hashtable has an additional value' { + BeforeAll { + $desiredValues = [PSObject] @{ + String = 'a string' + Bool = $true + Int = 99 + Array = 'a', 'b', 'c' + Hashtable = @{ + k1 = 'Test' + k2 = 123 + k3 = 'v1', 'v2', 'v3', 99, 100 } } + } - It 'Should not throw exception' { - { $script:result = Compare-DscParameterState ` - -CurrentValues $currentValues ` - -DesiredValues $desiredValues ` - -IncludeInDesiredState ` - -Verbose:$verbose } | Should -Not -Throw - } + It 'Should not throw exception' { + { $script:result = Compare-DscParameterState ` + -CurrentValues $currentValues ` + -DesiredValues $desiredValues ` + -IncludeInDesiredState ` + -Verbose:$verbose } | Should -Not -Throw + } - It 'Should return $false for Hashtable InDesiredState' { - $script:result.where({$_.Property -eq 'Hashtable'}).InDesiredState | Should -BeFalse - } + It 'Should return $false for Hashtable InDesiredState' { + $script:result.Where({ + $_.Property -eq 'Hashtable' + }).InDesiredState | Should -BeFalse + } - It 'Should return all InDesiredState (without Hashtable property) in $true' { - $script:result.where({$_.Property -ne 'Hashtable'}).InDesiredState | Should -Not -Contain $false - } + It 'Should return all InDesiredState (without Hashtable property) in $true' { + $script:result.Where({ + $_.Property -ne 'Hashtable' + }).InDesiredState | Should -Not -Contain $false } + } - Context 'When an array in hashtable has different order' { - BeforeAll { - $desiredValues = [PSObject] @{ - String = 'a string' - Bool = $true - Int = 99 - Array = 'a', 'b', 'c' - Hashtable = @{ - k1 = 'Test' - k2 = 123 - k3 = 'v3', 'v2', 'v1', 99 - } + Context 'When hashtable has a different value' { + BeforeAll { + $desiredValues = [PSObject] @{ + String = 'a string' + Bool = $true + Int = 99 + Array = 'a', 'b', 'c' + Hashtable = @{ + k1 = 'Test' + k2 = 123 + k3 = 'xx', 'v2', 'v3', 99 } } + } - It 'Should not throw exception' { - { $script:result = Compare-DscParameterState ` - -CurrentValues $currentValues ` - -DesiredValues $desiredValues ` - -IncludeInDesiredState ` - -Verbose:$verbose } | Should -Not -Throw - } + It 'Should not throw exception' { + { $script:result = Compare-DscParameterState ` + -CurrentValues $currentValues ` + -DesiredValues $desiredValues ` + -IncludeInDesiredState ` + -Verbose:$verbose } | Should -Not -Throw + } - It 'Should return $false for Hashtable InDesiredState' { - $script:result.where({$_.Property -eq 'Hashtable'}).InDesiredState | Should -BeFalse - } + It 'Should return $false for Hashtable InDesiredState' { + $script:result.Where({ + $_.Property -eq 'Hashtable' + }).InDesiredState | Should -BeFalse + } - It 'Should return all InDesiredState (without Hashtable property) in $true' { - $script:result.where({$_.Property -ne 'Hashtable'}).InDesiredState | Should -Not -Contain $false - } + It 'Should return all InDesiredState (without Hashtable property) in $true' { + $script:result.Where({ + $_.Property -ne 'Hashtable' + }).InDesiredState | Should -Not -Contain $false } + } - Context 'When an array in hashtable has different order but SortArrayValues is used' { - BeforeAll { - $desiredValues = [PSObject] @{ - String = 'a string' - Bool = $true - Int = 99 - Array = 'a', 'b', 'c' - Hashtable = @{ - k1 = 'Test' - k2 = 123 - k3 = 'v3', 'v2', 'v1', 99 - } + Context 'When an array in hashtable has different order' { + BeforeAll { + $desiredValues = [PSObject] @{ + String = 'a string' + Bool = $true + Int = 99 + Array = 'a', 'b', 'c' + Hashtable = @{ + k1 = 'Test' + k2 = 123 + k3 = 'v3', 'v2', 'v1', 99 } } + } - It 'Should not throw exception' { - { $script:result = Compare-DscParameterState ` - -CurrentValues $currentValues ` - -DesiredValues $desiredValues ` - -SortArrayValues ` - -IncludeInDesiredState ` - -Verbose:$verbose } | Should -Not -Throw + It 'Should not throw exception' { + { $script:result = Compare-DscParameterState ` + -CurrentValues $currentValues ` + -DesiredValues $desiredValues ` + -IncludeInDesiredState ` + -Verbose:$verbose } | Should -Not -Throw + } + + It 'Should return $false for Hashtable InDesiredState' { + $script:result.Where({ + $_.Property -eq 'Hashtable' + }).InDesiredState | Should -BeFalse + } + + It 'Should return all InDesiredState (without Hashtable property) in $true' { + $script:result.Where({ + $_.Property -ne 'Hashtable' + }).InDesiredState | Should -Not -Contain $false + } + } + + Context 'When an array in hashtable has different order but SortArrayValues is used' { + BeforeAll { + $desiredValues = [PSObject] @{ + String = 'a string' + Bool = $true + Int = 99 + Array = 'a', 'b', 'c' + Hashtable = @{ + k1 = 'Test' + k2 = 123 + k3 = 'v3', 'v2', 'v1', 99 + } } + } - It 'Should return $true for Hashtable InDesiredState' { - $script:result.where({$_.Property -eq 'Hashtable'}).InDesiredState | Should -BeTrue - } + It 'Should not throw exception' { + { $script:result = Compare-DscParameterState ` + -CurrentValues $currentValues ` + -DesiredValues $desiredValues ` + -SortArrayValues ` + -IncludeInDesiredState ` + -Verbose:$verbose } | Should -Not -Throw + } - It 'Should return all InDesiredState (without Hashtable property) in $true' { - $script:result.where({$_.Property -ne 'Hashtable'}).InDesiredState | Should -Not -Contain $false - } + It 'Should return $true for Hashtable InDesiredState' { + $script:result.Where({ + $_.Property -eq 'Hashtable' + }).InDesiredState | Should -BeTrue } - Context 'When hashtable has a value with a different type' { - BeforeAll { - $desiredValues = [PSObject] @{ - String = 'a string' - Bool = $true - Int = 99 - Array = 'a', 'b', 'c' - Hashtable = @{ - k1 = 'Test' - k2 = 123 - k3 = 'v1', 'v2', 'v3', '99' - } + It 'Should return all InDesiredState (without Hashtable property) in $true' { + $script:result.Where({ + $_.Property -ne 'Hashtable' + }).InDesiredState | Should -Not -Contain $false + } + } + + Context 'When hashtable has a value with a different type' { + BeforeAll { + $desiredValues = [PSObject] @{ + String = 'a string' + Bool = $true + Int = 99 + Array = 'a', 'b', 'c' + Hashtable = @{ + k1 = 'Test' + k2 = 123 + k3 = 'v1', 'v2', 'v3', '99' } } + } - It 'Should not throw exception' { - { $script:result = Compare-DscParameterState ` - -CurrentValues $currentValues ` - -DesiredValues $desiredValues ` - -IncludeInDesiredState ` - -Verbose:$verbose } | Should -Not -Throw - } + It 'Should not throw exception' { + { $script:result = Compare-DscParameterState ` + -CurrentValues $currentValues ` + -DesiredValues $desiredValues ` + -IncludeInDesiredState ` + -Verbose:$verbose } | Should -Not -Throw + } - It 'Should return $false for Hashtable InDesiredState' { - $script:result.where({$_.Property -eq 'Hashtable'}).InDesiredState | Should -BeFalse - } + It 'Should return $false for Hashtable InDesiredState' { + $script:result.Where({ + $_.Property -eq 'Hashtable' + }).InDesiredState | Should -BeFalse + } - It 'Should return all InDesiredState (without Hashtable property) in $true' { - $script:result.where({$_.Property -ne 'Hashtable'}).InDesiredState | Should -Not -Contain $false - } + It 'Should return all InDesiredState (without Hashtable property) in $true' { + $script:result.Where({ + $_.Property -ne 'Hashtable' + }).InDesiredState | Should -Not -Contain $false } + } - Context 'When hashtable has a value with a different type but TurnOffTypeChecking is used' { - BeforeAll { - $desiredValues = [PSObject] @{ - String = 'a string' - Bool = $true - Int = 99 - Array = 'a', 'b', 'c' - Hashtable = @{ - k1 = 'Test' - k2 = 123 - k3 = 'v1', 'v2', 'v3', 99 - } + Context 'When hashtable has a value with a different type but TurnOffTypeChecking is used' { + BeforeAll { + $desiredValues = [PSObject] @{ + String = 'a string' + Bool = $true + Int = 99 + Array = 'a', 'b', 'c' + Hashtable = @{ + k1 = 'Test' + k2 = 123 + k3 = 'v1', 'v2', 'v3', 99 } } + } - It 'Should not throw exception' { - { $script:result = Compare-DscParameterState ` - -CurrentValues $currentValues ` - -DesiredValues $desiredValues ` - -TurnOffTypeChecking ` - -IncludeInDesiredState ` - -Verbose:$verbose } | Should -Not -Throw - } + It 'Should not throw exception' { + { $script:result = Compare-DscParameterState ` + -CurrentValues $currentValues ` + -DesiredValues $desiredValues ` + -TurnOffTypeChecking ` + -IncludeInDesiredState ` + -Verbose:$verbose } | Should -Not -Throw + } - It 'Should return $true for Hashtable InDesiredState' { - $script:result.where({$_.Property -eq 'Hashtable'}).InDesiredState | Should -BeTrue - } + It 'Should return $true for Hashtable InDesiredState' { + $script:result.Where({ + $_.Property -eq 'Hashtable' + }).InDesiredState | Should -BeTrue + } + + It 'Should return all InDesiredState (without Hashtable property) in $true' { + $script:result.Where({ + $_.Property -ne 'Hashtable' + }).InDesiredState | Should -Not -Contain $false + } + } + } - It 'Should return all InDesiredState (without Hashtable property) in $true' { - $script:result.where({$_.Property -ne 'Hashtable'}).InDesiredState | Should -Not -Contain $false + Context 'When reverse checking' { + BeforeAll { + $currentValues = @{ + String = 'a string' + Bool = $true + Int = 99 + Array = 'a', 'b', 'c', 1 + Hashtable = @{ + k1 = 'Test' + k2 = 123 + k3 = 'v1', 'v2', 'v3' } } } - Context 'When reverse checking' { + Context 'When even if missing property in the desired state' { BeforeAll { - $currentValues = @{ - String = 'a string' - Bool = $true - Int = 99 + $desiredValues = [PSObject] @{ Array = 'a', 'b', 'c', 1 Hashtable = @{ k1 = 'Test' @@ -1594,389 +1777,397 @@ InModuleScope $ProjectName { } } - Context 'When even if missing property in the desired state' { - BeforeAll { - $desiredValues = [PSObject] @{ - Array = 'a', 'b', 'c', 1 - Hashtable = @{ - k1 = 'Test' - k2 = 123 - k3 = 'v1', 'v2', 'v3' - } - } - } + It 'Should not throw exception' { + { $script:result = Compare-DscParameterState ` + -CurrentValues $currentValues ` + -DesiredValues $desiredValues ` + -IncludeInDesiredState ` + -Verbose:$verbose } | Should -Not -Throw + } - It 'Should not throw exception' { - { $script:result = Compare-DscParameterState ` - -CurrentValues $currentValues ` - -DesiredValues $desiredValues ` - -IncludeInDesiredState ` - -Verbose:$verbose } | Should -Not -Throw + It 'Should return $false for all InDesiredState' { + $script:result.InDesiredState | Should -Not -Contain $false + } + } + + Context 'When missing property in the desired state' { + BeforeAll { + $currentValues = @{ + String = 'a string' + Bool = $true } - It 'Should return $false for all InDesiredState' { - $script:result.InDesiredState | Should -Not -Contain $false + $desiredValues = [PSObject] @{ + String = 'a string' } } - Context 'When missing property in the desired state' { - BeforeAll { - $currentValues = @{ - String = 'a string' - Bool = $true - } + It 'Should not throw exception' { + { $script:result = Compare-DscParameterState ` + -CurrentValues $currentValues ` + -DesiredValues $desiredValues ` + -ReverseCheck ` + -IncludeInDesiredState ` + -Verbose:$verbose } | Should -Not -Throw + } - $desiredValues = [PSObject] @{ - String = 'a string' - } - } + It 'Should return $false for missed property (Bool)' { + $script:result.Where({ + $_.Property -eq 'Bool' + }).InDesiredState | Should -BeFalse + } - It 'Should not throw exception' { - { $script:result = Compare-DscParameterState ` - -CurrentValues $currentValues ` - -DesiredValues $desiredValues ` - -ReverseCheck ` - -IncludeInDesiredState ` - -Verbose:$verbose } | Should -Not -Throw - } + It 'Should return all InDesiredState (without Bool property) in $true' { + $script:result.Where({ + $_.Property -ne 'Bool' + }).InDesiredState | Should -Not -Contain $false + } + } + } - It 'Should return $false for missed property (Bool)' { - $script:result.where({$_.Property -eq 'Bool'}).InDesiredState | Should -BeFalse + Context 'When testing parameter types' { + Context 'When desired value is of the wrong type' { + BeforeAll { + $currentValues = @{ + String = 'a string' } - It 'Should return all InDesiredState (without Bool property) in $true' { - $script:result.where({$_.Property -ne 'Bool'}).InDesiredState | Should -Not -Contain $false - } + $desiredValues = 1, 2, 3 } - } - Context 'When testing parameter types' { - Context 'When desired value is of the wrong type' { - BeforeAll { - $currentValues = @{ - String = 'a string' - } + It 'Should throw exception' { + { $script:result = Compare-DscParameterState ` + -CurrentValues $currentValues ` + -DesiredValues $desiredValues ` + -IncludeInDesiredState ` + -Verbose:$verbose } | Should -Throw + } + } - $desiredValues = 1, 2, 3 - } + Context 'When current value is of the wrong type' { + BeforeAll { + $currentValues = 1, 2, 3 - It 'Should throw exception' { - { $script:result = Compare-DscParameterState ` - -CurrentValues $currentValues ` - -DesiredValues $desiredValues ` - -IncludeInDesiredState ` - -Verbose:$verbose } | Should -Throw + $desiredValues = @{ + String = 'a string' } } - Context 'When current value is of the wrong type' { - BeforeAll { - $currentValues = 1, 2, 3 + It 'Should throw exception' { + { $script:result = Compare-DscParameterState ` + -CurrentValues $currentValues ` + -DesiredValues $desiredValues ` + -IncludeInDesiredState ` + -Verbose:$verbose } | Should -Throw + } + } + } - $desiredValues = @{ + # Skip on macOS and Linux. macOS and Linux does not support CimInstance. + Context 'When testing CimInstances / hashtables' -Skip:(-not ($IsWindows -or $PSEdition -eq 'Desktop')) { + BeforeAll { + $currentValues = @{ + String = 'a string' + Bool = $true + Int = 99 + Array = 'a', 'b', 'c' + Hashtable = @{ + k1 = 'Test' + k2 = 123 + k3 = 'v1', 'v2', 'v3', 99 + } + + CimInstances = [Microsoft.Management.Infrastructure.CimInstance[]] ( + ConvertTo-CimInstance -Hashtable @{ String = 'a string' + Bool = $true + Int = 99 + Array = 'a, b, c' } - } - - It 'Should throw exception' { - { $script:result = Compare-DscParameterState ` - -CurrentValues $currentValues ` - -DesiredValues $desiredValues ` - -IncludeInDesiredState ` - -Verbose:$verbose } | Should -Throw - } + ) } } - # macOS and Linux does not support CimInstance. - if ($isWindows -or $PSEdition -eq 'Desktop') - { - Context 'When testing CimInstances / hashtables' { - BeforeAll { - $currentValues = @{ - String = 'a string' - Bool = $true - Int = 99 - Array = 'a', 'b', 'c' - Hashtable = @{ - k1 = 'Test' - k2 = 123 - k3 = 'v1', 'v2', 'v3', 99 - } - - CimInstances = [Microsoft.Management.Infrastructure.CimInstance[]] ( - ConvertTo-CimInstance -Hashtable @{ - String = 'a string' - Bool = $true - Int = 99 - Array = 'a, b, c' - } - ) + Context 'When everything matches' { + BeforeAll { + $desiredValues = [PSObject]@{ + String = 'a string' + Bool = $true + Int = 99 + Array = 'a', 'b', 'c' + Hashtable = @{ + k1 = 'Test' + k2 = 123 + k3 = 'v1', 'v2', 'v3', 99 } - } - Context 'When everything matches' { - BeforeAll { - $desiredValues = [PSObject]@{ - String = 'a string' - Bool = $true - Int = 99 - Array = 'a', 'b', 'c' - Hashtable = @{ - k1 = 'Test' - k2 = 123 - k3 = 'v1', 'v2', 'v3', 99 - } - - CimInstances = [Microsoft.Management.Infrastructure.CimInstance[]] ( - ConvertTo-CimInstance -Hashtable @{ - String = 'a string' - Bool = $true - Int = 99 - Array = 'a, b, c' - } - ) + CimInstances = [Microsoft.Management.Infrastructure.CimInstance[]] ( + ConvertTo-CimInstance -Hashtable @{ + String = 'a string' + Bool = $true + Int = 99 + Array = 'a, b, c' } - } - - It 'Should not throw exception' { - { $script:result = Compare-DscParameterState ` - -CurrentValues $currentValues ` - -DesiredValues $desiredValues ` - -IncludeInDesiredState ` - -Verbose:$verbose } | Should -Not -Throw - } - - It 'Should return all InDesiredState in $true' { - $script:result.InDesiredState | Should -Not -Contain $false - } + ) } + } - Context 'When CimInstances missing a value in the desired state (not recognized)' { - BeforeAll { - $desiredValues = [PSObject]@{ - String = 'a string' - Bool = $true - Int = 99 - Array = 'a', 'b', 'c' - Hashtable = @{ - k1 = 'Test' - k2 = 123 - k3 = 'v1', 'v2', 'v3', 99 - } - CimInstances = @{ - String = 'a string' - Bool = $true - Array = 'a, b, c' - } - } - } + It 'Should not throw exception' { + { $script:result = Compare-DscParameterState ` + -CurrentValues $currentValues ` + -DesiredValues $desiredValues ` + -IncludeInDesiredState ` + -Verbose:$verbose } | Should -Not -Throw + } - It 'Should not throw exception' { - { $script:result = Compare-DscParameterState ` - -CurrentValues $currentValues ` - -DesiredValues $desiredValues ` - -IncludeInDesiredState ` - -Verbose:$verbose } | Should -Not -Throw - } + It 'Should return all InDesiredState in $true' { + $script:result.InDesiredState | Should -Not -Contain $false + } + } - It 'Should return all InDesiredState in $true' { - $script:result.InDesiredState | Should -Not -Contain $false + Context 'When CimInstances missing a value in the desired state (not recognized)' { + BeforeAll { + $desiredValues = [PSObject]@{ + String = 'a string' + Bool = $true + Int = 99 + Array = 'a', 'b', 'c' + Hashtable = @{ + k1 = 'Test' + k2 = 123 + k3 = 'v1', 'v2', 'v3', 99 + } + CimInstances = @{ + String = 'a string' + Bool = $true + Array = 'a, b, c' } } + } - Context 'When CimInstances missing a value in the desired state (recognized using ReverseCheck)' { - BeforeAll { - $desiredValues = [PSObject] @{ - String = 'a string' - Bool = $true - Int = 99 - Array = 'a', 'b', 'c' - Hashtable = @{ - k1 = 'Test' - k2 = 123 - k3 = 'v1', 'v2', 'v3', 99 - } - CimInstances = @{ - String = 'a string' - Bool = $true - Array = 'a, b, c' - } - } - } + It 'Should not throw exception' { + { $script:result = Compare-DscParameterState ` + -CurrentValues $currentValues ` + -DesiredValues $desiredValues ` + -IncludeInDesiredState ` + -Verbose:$verbose } | Should -Not -Throw + } - It 'Should not throw exception' { - { $script:result = Compare-DscParameterState ` - -CurrentValues $currentValues ` - -DesiredValues $desiredValues ` - -ReverseCheck ` - -IncludeInDesiredState ` - -Verbose:$verbose } | Should -Not -Throw - } + It 'Should return all InDesiredState in $true' { + $script:result.InDesiredState | Should -Not -Contain $false + } + } - It 'Should return $false for CimInstances InDesiredState' { - $script:result.where({$_.Property -eq 'CimInstances'}).InDesiredState | Should -BeFalse + Context 'When CimInstances missing a value in the desired state (recognized using ReverseCheck)' { + BeforeAll { + $desiredValues = [PSObject] @{ + String = 'a string' + Bool = $true + Int = 99 + Array = 'a', 'b', 'c' + Hashtable = @{ + k1 = 'Test' + k2 = 123 + k3 = 'v1', 'v2', 'v3', 99 } - - It 'Should return all InDesiredState (without CimInstances property) in $true' { - $script:result.where({$_.Property -ne 'CimInstances'}).InDesiredState | Should -Not -Contain $false + CimInstances = @{ + String = 'a string' + Bool = $true + Array = 'a, b, c' } } + } - Context 'When CimInstances have an additional value' { - BeforeAll { - $desiredValues = [PSObject] @{ - String = 'a string' - Bool = $true - Int = 99 - Array = 'a', 'b', 'c' - Hashtable = @{ - k1 = 'Test' - k2 = 123 - k3 = 'v1', 'v2', 'v3', 99 - } - CimInstances = @{ - String = 'a string' - Bool = $true - Int = 99 - Array = 'a, b, c' - Test = 'Some string' - } - } - } + It 'Should not throw exception' { + { $script:result = Compare-DscParameterState ` + -CurrentValues $currentValues ` + -DesiredValues $desiredValues ` + -ReverseCheck ` + -IncludeInDesiredState ` + -Verbose:$verbose } | Should -Not -Throw + } - It 'Should not throw exception' { - { $script:result = Compare-DscParameterState ` - -CurrentValues $currentValues ` - -DesiredValues $desiredValues ` - -IncludeInDesiredState ` - -Verbose:$verbose } | Should -Not -Throw - } + It 'Should return $false for CimInstances InDesiredState' { + $script:result.Where({ + $_.Property -eq 'CimInstances' + }).InDesiredState | Should -BeFalse + } - It 'Should return $false for CimInstances InDesiredState' { - $script:result.where({$_.Property -eq 'CimInstances'}).InDesiredState | Should -BeFalse - } + It 'Should return all InDesiredState (without CimInstances property) in $true' { + $script:result.Where({ + $_.Property -ne 'CimInstances' + }).InDesiredState | Should -Not -Contain $false + } + } - It 'Should return all InDesiredState (without CimInstances property) in $true' { - $script:result.where({$_.Property -ne 'CimInstances'}).InDesiredState | Should -Not -Contain $false + Context 'When CimInstances have an additional value' { + BeforeAll { + $desiredValues = [PSObject] @{ + String = 'a string' + Bool = $true + Int = 99 + Array = 'a', 'b', 'c' + Hashtable = @{ + k1 = 'Test' + k2 = 123 + k3 = 'v1', 'v2', 'v3', 99 + } + CimInstances = @{ + String = 'a string' + Bool = $true + Int = 99 + Array = 'a, b, c' + Test = 'Some string' } } + } - Context 'When CimInstances have a different value' { - BeforeAll { - $desiredValues = [PSObject] @{ - String = 'a string' - Bool = $true - Int = 99 - Array = 'a', 'b', 'c' - Hashtable = @{ - k1 = 'Test' - k2 = 123 - k3 = 'v1', 'v2', 'v3', 99 - } - CimInstances = @{ - String = 'some other string' - Bool = $true - Int = 99 - Array = 'a, b, c' - } - } - } + It 'Should not throw exception' { + { $script:result = Compare-DscParameterState ` + -CurrentValues $currentValues ` + -DesiredValues $desiredValues ` + -IncludeInDesiredState ` + -Verbose:$verbose } | Should -Not -Throw + } - It 'Should not throw exception' { - { $script:result = Compare-DscParameterState ` - -CurrentValues $currentValues ` - -DesiredValues $desiredValues ` - -IncludeInDesiredState ` - -Verbose:$verbose } | Should -Not -Throw - } + It 'Should return $false for CimInstances InDesiredState' { + $script:result.Where({ + $_.Property -eq 'CimInstances' + }).InDesiredState | Should -BeFalse + } - It 'Should return $false for CimInstances InDesiredState' { - $script:result.where({$_.Property -eq 'CimInstances'}).InDesiredState | Should -BeFalse - } + It 'Should return all InDesiredState (without CimInstances property) in $true' { + $script:result.Where({ + $_.Property -ne 'CimInstances' + }).InDesiredState | Should -Not -Contain $false + } + } - It 'Should return all InDesiredState (without CimInstances property) in $true' { - $script:result.where({$_.Property -ne 'CimInstances'}).InDesiredState | Should -Not -Contain $false + Context 'When CimInstances have a different value' { + BeforeAll { + $desiredValues = [PSObject] @{ + String = 'a string' + Bool = $true + Int = 99 + Array = 'a', 'b', 'c' + Hashtable = @{ + k1 = 'Test' + k2 = 123 + k3 = 'v1', 'v2', 'v3', 99 + } + CimInstances = @{ + String = 'some other string' + Bool = $true + Int = 99 + Array = 'a, b, c' } } + } - Context 'When CimInstances have a value with a different type' { - BeforeAll { - $desiredValues = [PSObject] @{ - String = 'a string' - Bool = $true - Int = 99 - Array = 'a', 'b', 'c' - Hashtable = @{ - k1 = 'Test' - k2 = 123 - k3 = 'v1', 'v2', 'v3', 99 - } - CimInstances = @{ - String = 'a string' - Bool = $true - Int = '99' - Array = 'a, b, c' - } - } - } + It 'Should not throw exception' { + { $script:result = Compare-DscParameterState ` + -CurrentValues $currentValues ` + -DesiredValues $desiredValues ` + -IncludeInDesiredState ` + -Verbose:$verbose } | Should -Not -Throw + } - It 'Should not throw exception' { - { $script:result = Compare-DscParameterState ` - -CurrentValues $currentValues ` - -DesiredValues $desiredValues ` - -IncludeInDesiredState ` - -Verbose:$verbose } | Should -Not -Throw - } + It 'Should return $false for CimInstances InDesiredState' { + $script:result.Where({ + $_.Property -eq 'CimInstances' + }).InDesiredState | Should -BeFalse + } - It 'Should return $false for CimInstances InDesiredState' { - $script:result.where({$_.Property -eq 'CimInstances'}).InDesiredState | Should -BeFalse - } + It 'Should return all InDesiredState (without CimInstances property) in $true' { + $script:result.Where({ + $_.Property -ne 'CimInstances' + }).InDesiredState | Should -Not -Contain $false + } + } - It 'Should return all InDesiredState (without CimInstances property) in $true' { - $script:result.where({$_.Property -ne 'CimInstances'}).InDesiredState | Should -Not -Contain $false + Context 'When CimInstances have a value with a different type' { + BeforeAll { + $desiredValues = [PSObject] @{ + String = 'a string' + Bool = $true + Int = 99 + Array = 'a', 'b', 'c' + Hashtable = @{ + k1 = 'Test' + k2 = 123 + k3 = 'v1', 'v2', 'v3', 99 + } + CimInstances = @{ + String = 'a string' + Bool = $true + Int = '99' + Array = 'a, b, c' } } + } - Context 'When CimInstances have a value with a different type but TurnOffTypeChecking is used' { - BeforeAll { - $desiredValues = [PSObject] @{ - String = 'a string' - Bool = $true - Int = 99 - Array = 'a', 'b', 'c' - Hashtable = @{ - k1 = 'Test' - k2 = 123 - k3 = 'v1', 'v2', 'v3', 99 - } - CimInstances = @{ - String = 'a string' - Bool = $true - Int = '99' - Array = 'a, b, c' - } - } - } + It 'Should not throw exception' { + { $script:result = Compare-DscParameterState ` + -CurrentValues $currentValues ` + -DesiredValues $desiredValues ` + -IncludeInDesiredState ` + -Verbose:$verbose } | Should -Not -Throw + } - It 'Should not throw exception' { - { $script:result = Compare-DscParameterState ` - -CurrentValues $currentValues ` - -DesiredValues $desiredValues ` - -TurnOffTypeChecking ` - -IncludeInDesiredState ` - -Verbose:$verbose } | Should -Not -Throw - } + It 'Should return $false for CimInstances InDesiredState' { + $script:result.Where({ + $_.Property -eq 'CimInstances' + }).InDesiredState | Should -BeFalse + } - It 'Should return $true for CimInstances InDesiredState' { - $script:result.where({$_.Property -eq 'CimInstances'}).InDesiredState | Should -BeTrue - } + It 'Should return all InDesiredState (without CimInstances property) in $true' { + $script:result.Where({ + $_.Property -ne 'CimInstances' + }).InDesiredState | Should -Not -Contain $false + } + } - It 'Should return all InDesiredState (without CimInstances property) in $true' { - $script:result.where({$_.Property -ne 'CimInstances'}).InDesiredState | Should -Not -Contain $false + Context 'When CimInstances have a value with a different type but TurnOffTypeChecking is used' { + BeforeAll { + $desiredValues = [PSObject] @{ + String = 'a string' + Bool = $true + Int = 99 + Array = 'a', 'b', 'c' + Hashtable = @{ + k1 = 'Test' + k2 = 123 + k3 = 'v1', 'v2', 'v3', 99 + } + CimInstances = @{ + String = 'a string' + Bool = $true + Int = '99' + Array = 'a, b, c' } } } + + It 'Should not throw exception' { + { $script:result = Compare-DscParameterState ` + -CurrentValues $currentValues ` + -DesiredValues $desiredValues ` + -TurnOffTypeChecking ` + -IncludeInDesiredState ` + -Verbose:$verbose } | Should -Not -Throw + } + + It 'Should return $true for CimInstances InDesiredState' { + $script:result.Where({ + $_.Property -eq 'CimInstances' + }).InDesiredState | Should -BeTrue + } + + It 'Should return all InDesiredState (without CimInstances property) in $true' { + $script:result.Where({ + $_.Property -ne 'CimInstances' + }).InDesiredState | Should -Not -Contain $false + } } } } diff --git a/tests/Unit/Public/Compare-ResourcePropertyState.Tests.ps1 b/tests/Unit/Public/Compare-ResourcePropertyState.Tests.ps1 index 3a8a82e..9e0f336 100644 --- a/tests/Unit/Public/Compare-ResourcePropertyState.Tests.ps1 +++ b/tests/Unit/Public/Compare-ResourcePropertyState.Tests.ps1 @@ -1,288 +1,278 @@ -$ProjectPath = "$PSScriptRoot\..\..\.." | Convert-Path -$ProjectName = ((Get-ChildItem -Path $ProjectPath\*\*.psd1).Where{ - ($_.Directory.Name -match 'source|src' -or $_.Directory.Name -eq $_.BaseName) -and - $( - try - { - Test-ModuleManifest -Path $_.FullName -ErrorAction Stop +BeforeAll { + $script:moduleName = 'DscResource.Common' + + Remove-Module -Name $script:moduleName -Force -ErrorAction 'SilentlyContinue' + + Get-Module -Name $script:moduleName -ListAvailable | + Select-Object -First 1 | + Import-Module -Force -ErrorAction 'Stop' +} + +Describe 'Compare-ResourcePropertyState' -Tag 'CompareResourcePropertyState' { + Context 'When one property is in desired state' { + BeforeAll { + $mockCurrentValues = @{ + ComputerName = 'DC01' } - catch - { - $false + + $mockDesiredValues = @{ + ComputerName = 'DC01' } - ) - }).BaseName + } + It 'Should return the correct values' { + $compareTargetResourceStateParameters = @{ + CurrentValues = $mockCurrentValues + DesiredValues = $mockDesiredValues + } -Import-Module $ProjectName + $compareTargetResourceStateResult = Compare-ResourcePropertyState @compareTargetResourceStateParameters + $compareTargetResourceStateResult | Should -HaveCount 1 -InModuleScope $ProjectName { - Describe 'Compare-ResourcePropertyState' -Tag 'CompareResourcePropertyState' { - Context 'When one property is in desired state' { - BeforeAll { - $mockCurrentValues = @{ - ComputerName = 'DC01' - } + $property = $compareTargetResourceStateResult.Where({$_.ParameterName -eq 'ComputerName'}) + $property | Should -Not -BeNulLorEmpty + $property.Expected | Should -Be 'DC01' + $property.Actual | Should -Be 'DC01' + $property.InDesiredState | Should -BeTrue + } + } - $mockDesiredValues = @{ - ComputerName = 'DC01' - } + Context 'When two properties are in desired state' { + BeforeAll { + $mockCurrentValues = @{ + ComputerName = 'DC01' + Location = 'Sweden' } - It 'Should return the correct values' { - $compareTargetResourceStateParameters = @{ - CurrentValues = $mockCurrentValues - DesiredValues = $mockDesiredValues - } - - $compareTargetResourceStateResult = Compare-ResourcePropertyState @compareTargetResourceStateParameters - $compareTargetResourceStateResult | Should -HaveCount 1 + $mockDesiredValues = @{ + ComputerName = 'DC01' + Location = 'Sweden' + <# + This is used to increase code coverage so that the code + that removes common parameters are hit. + #> + ErrorAction = 'Stop' + } + } - $property = $compareTargetResourceStateResult.Where({$_.ParameterName -eq 'ComputerName'}) - $property | Should -Not -BeNulLorEmpty - $property.Expected | Should -Be 'DC01' - $property.Actual | Should -Be 'DC01' - $property.InDesiredState | Should -BeTrue + It 'Should return the correct values' { + $compareTargetResourceStateParameters = @{ + CurrentValues = $mockCurrentValues + DesiredValues = $mockDesiredValues } + + $compareTargetResourceStateResult = Compare-ResourcePropertyState @compareTargetResourceStateParameters + $compareTargetResourceStateResult | Should -HaveCount 2 + + $property = $compareTargetResourceStateResult.Where({$_.ParameterName -eq 'ComputerName'}) + $property | Should -Not -BeNulLorEmpty + $property.Expected | Should -Be 'DC01' + $property.Actual | Should -Be 'DC01' + $property.InDesiredState | Should -BeTrue + + $property = $compareTargetResourceStateResult.Where({$_.ParameterName -eq 'Location'}) + $property | Should -Not -BeNulLorEmpty + $property.Expected | Should -Be 'Sweden' + $property.Actual | Should -Be 'Sweden' + $property.InDesiredState | Should -BeTrue } + } - Context 'When two properties are in desired state' { - BeforeAll { - $mockCurrentValues = @{ - ComputerName = 'DC01' - Location = 'Sweden' - } - - $mockDesiredValues = @{ - ComputerName = 'DC01' - Location = 'Sweden' - <# - This is used to increase code coverage so that the code - that removes common parameters are hit. - #> - ErrorAction = 'Stop' - } + Context 'When passing just one property and that property is not in desired state' { + BeforeAll { + $mockCurrentValues = @{ + ComputerName = 'DC01' } - It 'Should return the correct values' { - $compareTargetResourceStateParameters = @{ - CurrentValues = $mockCurrentValues - DesiredValues = $mockDesiredValues - } - - $compareTargetResourceStateResult = Compare-ResourcePropertyState @compareTargetResourceStateParameters - $compareTargetResourceStateResult | Should -HaveCount 2 - - $property = $compareTargetResourceStateResult.Where({$_.ParameterName -eq 'ComputerName'}) - $property | Should -Not -BeNulLorEmpty - $property.Expected | Should -Be 'DC01' - $property.Actual | Should -Be 'DC01' - $property.InDesiredState | Should -BeTrue - - $property = $compareTargetResourceStateResult.Where({$_.ParameterName -eq 'Location'}) - $property | Should -Not -BeNulLorEmpty - $property.Expected | Should -Be 'Sweden' - $property.Actual | Should -Be 'Sweden' - $property.InDesiredState | Should -BeTrue + $mockDesiredValues = @{ + ComputerName = 'APP01' } } - Context 'When passing just one property and that property is not in desired state' { - BeforeAll { - $mockCurrentValues = @{ - ComputerName = 'DC01' - } - - $mockDesiredValues = @{ - ComputerName = 'APP01' - } + It 'Should return the correct values' { + $compareTargetResourceStateParameters = @{ + CurrentValues = $mockCurrentValues + DesiredValues = $mockDesiredValues } - It 'Should return the correct values' { - $compareTargetResourceStateParameters = @{ - CurrentValues = $mockCurrentValues - DesiredValues = $mockDesiredValues - } + $compareTargetResourceStateResult = Compare-ResourcePropertyState @compareTargetResourceStateParameters + $compareTargetResourceStateResult | Should -HaveCount 1 + + $property = $compareTargetResourceStateResult.Where({$_.ParameterName -eq 'ComputerName'}) + $property | Should -Not -BeNulLorEmpty + $property.Expected | Should -Be 'APP01' + $property.Actual | Should -Be 'DC01' + $property.InDesiredState | Should -BeFalse + } + } - $compareTargetResourceStateResult = Compare-ResourcePropertyState @compareTargetResourceStateParameters - $compareTargetResourceStateResult | Should -HaveCount 1 + Context 'When passing two properties and one property is not in desired state' { + BeforeAll { + $mockCurrentValues = @{ + ComputerName = 'DC01' + Location = 'Sweden' + } - $property = $compareTargetResourceStateResult.Where({$_.ParameterName -eq 'ComputerName'}) - $property | Should -Not -BeNulLorEmpty - $property.Expected | Should -Be 'APP01' - $property.Actual | Should -Be 'DC01' - $property.InDesiredState | Should -BeFalse + $mockDesiredValues = @{ + ComputerName = 'DC01' + Location = 'Europe' } } - Context 'When passing two properties and one property is not in desired state' { - BeforeAll { - $mockCurrentValues = @{ - ComputerName = 'DC01' - Location = 'Sweden' - } - - $mockDesiredValues = @{ - ComputerName = 'DC01' - Location = 'Europe' - } + It 'Should return the correct values' { + $compareTargetResourceStateParameters = @{ + CurrentValues = $mockCurrentValues + DesiredValues = $mockDesiredValues } - It 'Should return the correct values' { - $compareTargetResourceStateParameters = @{ - CurrentValues = $mockCurrentValues - DesiredValues = $mockDesiredValues - } - - $compareTargetResourceStateResult = Compare-ResourcePropertyState @compareTargetResourceStateParameters - $compareTargetResourceStateResult | Should -HaveCount 2 - - $property = $compareTargetResourceStateResult.Where({$_.ParameterName -eq 'ComputerName'}) - $property | Should -Not -BeNulLorEmpty - $property.Expected | Should -Be 'DC01' - $property.Actual | Should -Be 'DC01' - $property.InDesiredState | Should -BeTrue - - $property = $compareTargetResourceStateResult.Where({$_.ParameterName -eq 'Location'}) - $property | Should -Not -BeNulLorEmpty - $property.Expected | Should -Be 'Europe' - $property.Actual | Should -Be 'Sweden' - $property.InDesiredState | Should -BeFalse - } + $compareTargetResourceStateResult = Compare-ResourcePropertyState @compareTargetResourceStateParameters + $compareTargetResourceStateResult | Should -HaveCount 2 + + $property = $compareTargetResourceStateResult.Where({$_.ParameterName -eq 'ComputerName'}) + $property | Should -Not -BeNulLorEmpty + $property.Expected | Should -Be 'DC01' + $property.Actual | Should -Be 'DC01' + $property.InDesiredState | Should -BeTrue + + $property = $compareTargetResourceStateResult.Where({$_.ParameterName -eq 'Location'}) + $property | Should -Not -BeNulLorEmpty + $property.Expected | Should -Be 'Europe' + $property.Actual | Should -Be 'Sweden' + $property.InDesiredState | Should -BeFalse } + } - Context 'When passing a common parameter set to desired value' { - BeforeAll { - $mockCurrentValues = @{ - ComputerName = 'DC01' - } + Context 'When passing a common parameter set to desired value' { + BeforeAll { + $mockCurrentValues = @{ + ComputerName = 'DC01' + } - $mockDesiredValues = @{ - ComputerName = 'DC01' - } + $mockDesiredValues = @{ + ComputerName = 'DC01' } + } - It 'Should return the correct values' { - $compareTargetResourceStateParameters = @{ - CurrentValues = $mockCurrentValues - DesiredValues = $mockDesiredValues - } + It 'Should return the correct values' { + $compareTargetResourceStateParameters = @{ + CurrentValues = $mockCurrentValues + DesiredValues = $mockDesiredValues + } - $compareTargetResourceStateResult = Compare-ResourcePropertyState @compareTargetResourceStateParameters - $compareTargetResourceStateResult | Should -HaveCount 1 + $compareTargetResourceStateResult = Compare-ResourcePropertyState @compareTargetResourceStateParameters + $compareTargetResourceStateResult | Should -HaveCount 1 - $property = $compareTargetResourceStateResult.Where({$_.ParameterName -eq 'ComputerName'}) - $property | Should -Not -BeNulLorEmpty - $property.Expected | Should -Be 'DC01' - $property.Actual | Should -Be 'DC01' - $property.InDesiredState | Should -BeTrue - } + $property = $compareTargetResourceStateResult.Where({$_.ParameterName -eq 'ComputerName'}) + $property | Should -Not -BeNulLorEmpty + $property.Expected | Should -Be 'DC01' + $property.Actual | Should -Be 'DC01' + $property.InDesiredState | Should -BeTrue } + } - Context 'When using parameter Properties to compare desired values' { - BeforeAll { - $mockCurrentValues = @{ - ComputerName = 'DC01' - Location = 'Sweden' - } - - $mockDesiredValues = @{ - ComputerName = 'DC01' - Location = 'Europe' - } + Context 'When using parameter Properties to compare desired values' { + BeforeAll { + $mockCurrentValues = @{ + ComputerName = 'DC01' + Location = 'Sweden' } - It 'Should return the correct values' { - $compareTargetResourceStateParameters = @{ - CurrentValues = $mockCurrentValues - DesiredValues = $mockDesiredValues - Properties = @( - 'ComputerName' - ) - } - - $compareTargetResourceStateResult = Compare-ResourcePropertyState @compareTargetResourceStateParameters - $compareTargetResourceStateResult | Should -HaveCount 1 - - $property = $compareTargetResourceStateResult.Where({$_.ParameterName -eq 'ComputerName'}) - $property | Should -Not -BeNulLorEmpty - $property.Expected | Should -Be 'DC01' - $property.Actual | Should -Be 'DC01' - $property.InDesiredState | Should -BeTrue + $mockDesiredValues = @{ + ComputerName = 'DC01' + Location = 'Europe' } } - Context 'When using parameter Properties and IgnoreProperties to compare desired values' { - BeforeAll { - $mockCurrentValues = @{ - ComputerName = 'DC01' - Location = 'Sweden' - Ensure = 'Present' - } - - $mockDesiredValues = @{ - ComputerName = 'DC01' - Location = 'Europe' - Ensure = 'Absent' - } + It 'Should return the correct values' { + $compareTargetResourceStateParameters = @{ + CurrentValues = $mockCurrentValues + DesiredValues = $mockDesiredValues + Properties = @( + 'ComputerName' + ) + } + + $compareTargetResourceStateResult = Compare-ResourcePropertyState @compareTargetResourceStateParameters + $compareTargetResourceStateResult | Should -HaveCount 1 + + $property = $compareTargetResourceStateResult.Where({$_.ParameterName -eq 'ComputerName'}) + $property | Should -Not -BeNulLorEmpty + $property.Expected | Should -Be 'DC01' + $property.Actual | Should -Be 'DC01' + $property.InDesiredState | Should -BeTrue + } + } + + Context 'When using parameter Properties and IgnoreProperties to compare desired values' { + BeforeAll { + $mockCurrentValues = @{ + ComputerName = 'DC01' + Location = 'Sweden' + Ensure = 'Present' } - It 'Should return the correct values' { - $compareTargetResourceStateParameters = @{ - CurrentValues = $mockCurrentValues - DesiredValues = $mockDesiredValues - IgnoreProperties = @( - 'Ensure' - ) - } - - $compareTargetResourceStateResult = Compare-ResourcePropertyState @compareTargetResourceStateParameters - $compareTargetResourceStateResult | Should -HaveCount 2 - - $property = $compareTargetResourceStateResult.Where({$_.ParameterName -eq 'ComputerName'}) - $property | Should -Not -BeNulLorEmpty - $property.Expected | Should -Be 'DC01' - $property.Actual | Should -Be 'DC01' - $property.InDesiredState | Should -BeTrue - - $property = $compareTargetResourceStateResult.Where({$_.ParameterName -eq 'Location'}) - $property | Should -Not -BeNulLorEmpty - $property.Expected | Should -Be 'Europe' - $property.Actual | Should -Be 'Sweden' - $property.InDesiredState | Should -BeFalse + $mockDesiredValues = @{ + ComputerName = 'DC01' + Location = 'Europe' + Ensure = 'Absent' } } - Context 'When using parameter Properties and IgnoreProperties to compare desired values' { - BeforeAll { - $mockCurrentValues = @{ - ComputerName = 'DC01' - Location = 'Sweden' - Ensure = 'Present' - } - - $mockDesiredValues = @{ - ComputerName = 'DC01' - Location = 'Europe' - Ensure = 'Absent' - } + It 'Should return the correct values' { + $compareTargetResourceStateParameters = @{ + CurrentValues = $mockCurrentValues + DesiredValues = $mockDesiredValues + IgnoreProperties = @( + 'Ensure' + ) } - It 'Should return and empty array' { - $compareTargetResourceStateParameters = @{ - CurrentValues = $mockCurrentValues - DesiredValues = $mockDesiredValues - Properties = @( - 'ComputerName' - ) - IgnoreProperties = @( - 'ComputerName' - ) - } - - $compareTargetResourceStateResult = Compare-ResourcePropertyState @compareTargetResourceStateParameters - $compareTargetResourceStateResult | Should -BeNullOrEmpty + $compareTargetResourceStateResult = Compare-ResourcePropertyState @compareTargetResourceStateParameters + $compareTargetResourceStateResult | Should -HaveCount 2 + + $property = $compareTargetResourceStateResult.Where({$_.ParameterName -eq 'ComputerName'}) + $property | Should -Not -BeNulLorEmpty + $property.Expected | Should -Be 'DC01' + $property.Actual | Should -Be 'DC01' + $property.InDesiredState | Should -BeTrue + + $property = $compareTargetResourceStateResult.Where({$_.ParameterName -eq 'Location'}) + $property | Should -Not -BeNulLorEmpty + $property.Expected | Should -Be 'Europe' + $property.Actual | Should -Be 'Sweden' + $property.InDesiredState | Should -BeFalse + } + } + + Context 'When using parameter Properties and IgnoreProperties to compare desired values' { + BeforeAll { + $mockCurrentValues = @{ + ComputerName = 'DC01' + Location = 'Sweden' + Ensure = 'Present' } + + $mockDesiredValues = @{ + ComputerName = 'DC01' + Location = 'Europe' + Ensure = 'Absent' + } + } + + It 'Should return and empty array' { + $compareTargetResourceStateParameters = @{ + CurrentValues = $mockCurrentValues + DesiredValues = $mockDesiredValues + Properties = @( + 'ComputerName' + ) + IgnoreProperties = @( + 'ComputerName' + ) + } + + $compareTargetResourceStateResult = Compare-ResourcePropertyState @compareTargetResourceStateParameters + $compareTargetResourceStateResult | Should -BeNullOrEmpty } } } diff --git a/tests/Unit/Public/ConvertFrom-DscResourceInstance.Tests.ps1 b/tests/Unit/Public/ConvertFrom-DscResourceInstance.Tests.ps1 index 68f2930..cc25c60 100644 --- a/tests/Unit/Public/ConvertFrom-DscResourceInstance.Tests.ps1 +++ b/tests/Unit/Public/ConvertFrom-DscResourceInstance.Tests.ps1 @@ -1,107 +1,105 @@ -$ProjectPath = "$PSScriptRoot\..\..\.." | Convert-Path -$ProjectName = ((Get-ChildItem -Path $ProjectPath\*\*.psd1).Where{ - ($_.Directory.Name -match 'source|src' -or $_.Directory.Name -eq $_.BaseName) -and - $(try - { - Test-ModuleManifest $_.FullName -ErrorAction Stop - } - catch - { - $false - } ) - }).BaseName - -Import-Module $ProjectName -Force - -InModuleScope $ProjectName { - Describe 'ConvertFrom-DscResourceInstance' { - $psObject = [pscustomobject]@{ - k1 = 'v1' - k2 = 100 - k3 = 1, 2, 3 - },[pscustomobject]@{ - k1 = 'v2' - k2 = 101 - k3 = 1, 2, 3, 4 - } +BeforeAll { + $script:moduleName = 'DscResource.Common' - Context 'When the array contains the expected record count through the pipeline' { - It 'Should not throw exception' { - { $script:result = $psObject | ConvertFrom-DscResourceInstance } | Should -Not -Throw - } + Remove-Module -Name $script:moduleName -Force -ErrorAction 'SilentlyContinue' - It "Should return record count of $($psObject.Count)" { - $script:result.Count | Should -Be $psObject.Count - } + Get-Module -Name $script:moduleName -ListAvailable | + Select-Object -First 1 | + Import-Module -Force -ErrorAction 'Stop' +} - It 'Should return result of type [System.Collections.Hashtable]' { - $script:result | Should -BeOfType [System.Collections.Hashtable] +Describe 'ConvertFrom-DscResourceInstance' { + BeforeAll { + $psObject = @( + [PSCustomObject] @{ + k1 = 'v1' + k2 = 100 + k3 = 1, 2, 3 } + [PSCustomObject] @{ + k1 = 'v2' + k2 = 101 + k3 = 1, 2, 3, 4 + } + ) + } - Context 'When the first object of array contains the expected record' { - - It 'Should return value "k1" in the hashtable should be "v1"' { - $script:result[0].k1 | Should -Be 'v1' - } + Context 'When the array contains the expected record count through the pipeline' { + It 'Should not throw exception' { + { $script:result = $psObject | ConvertFrom-DscResourceInstance } | Should -Not -Throw + } - It 'Should return value "k2" in the hashtable should be "100"' { - $script:result[0].k2 | Should -Be 100 - } + It "Should return record count of $($psObject.Count)" { + $script:result.Count | Should -Be $psObject.Count + } - It 'Should return value "k3" in the hashtable should be "1,2,3"' { - $script:result[0].k3 -join ',' | Should -Be '1,2,3' - } - } + It 'Should return result of type [System.Collections.Hashtable]' { + $script:result | Should -BeOfType [System.Collections.Hashtable] + } - Context 'When the second object of array contains the expected record' { + Context 'When the first object of array contains the expected record' { - It 'Should return value "k1" in the hashtable should be "v2"' { - $script:result[1].k1 | Should -Be 'v2' - } + It 'Should return value "k1" in the hashtable should be "v1"' { + $script:result[0].k1 | Should -Be 'v1' + } - It 'Should return value "k2" in the hashtable should be "101"' { - $script:result[1].k2 | Should -Be 101 - } + It 'Should return value "k2" in the hashtable should be "100"' { + $script:result[0].k2 | Should -Be 100 + } - It 'Should return value "k3" in the hashtable should be "1,2,3,4"' { - $script:result[1].k3 -join ',' | Should -Be '1,2,3,4' - } + It 'Should return value "k3" in the hashtable should be "1,2,3"' { + $script:result[0].k3 -join ',' | Should -Be '1,2,3' } } - Context 'When the array contains the expected record count' { - It 'Should not throw exception' { - { $script:result = ConvertFrom-DscResourceInstance -InputObject $psObject[0] } | Should -Not -Throw + Context 'When the second object of array contains the expected record' { + + It 'Should return value "k1" in the hashtable should be "v2"' { + $script:result[1].k1 | Should -Be 'v2' } - It "Should return record count of $($psObject.Count)" { - ($script:result | Measure-Object).Count | Should -Be 1 + It 'Should return value "k2" in the hashtable should be "101"' { + $script:result[1].k2 | Should -Be 101 } - It 'Should return result of type [System.Collections.Hashtable]' { - $script:result | Should -BeOfType [System.Collections.Hashtable] + It 'Should return value "k3" in the hashtable should be "1,2,3,4"' { + $script:result[1].k3 -join ',' | Should -Be '1,2,3,4' } + } + } - Context 'When the object contains the expected record' { + Context 'When the array contains the expected record count' { + It 'Should not throw exception' { + { $script:result = ConvertFrom-DscResourceInstance -InputObject $psObject[0] } | Should -Not -Throw + } - It 'Should return value "k1" in the hashtable should be "v1"' { - $script:result.k1 | Should -Be 'v1' - } + It "Should return record count of $($psObject.Count)" { + ($script:result | Measure-Object).Count | Should -Be 1 + } + + It 'Should return result of type [System.Collections.Hashtable]' { + $script:result | Should -BeOfType [System.Collections.Hashtable] + } - It 'Should return value "k2" in the hashtable should be "100"' { - $script:result.k2 | Should -Be 100 - } + Context 'When the object contains the expected record' { - It 'Should return value "k3" in the hashtable should be "1,2,3"' { - $script:result.k3 -join ',' | Should -Be '1,2,3' - } + It 'Should return value "k1" in the hashtable should be "v1"' { + $script:result.k1 | Should -Be 'v1' + } + + It 'Should return value "k2" in the hashtable should be "100"' { + $script:result.k2 | Should -Be 100 } - } - Context 'When a wrong output format is set' { - It 'Should throw exception' { - { $script:result = $psObject | ConvertFrom-DscResourceInstance -OutputFormat 'DummyFormat' } | Should -Throw + It 'Should return value "k3" in the hashtable should be "1,2,3"' { + $script:result.k3 -join ',' | Should -Be '1,2,3' } } } + + Context 'When a wrong output format is set' { + It 'Should throw exception' { + { $script:result = $psObject | ConvertFrom-DscResourceInstance -OutputFormat 'DummyFormat' } | Should -Throw + } + } } diff --git a/tests/Unit/Public/ConvertTo-CimInstance.Tests.ps1 b/tests/Unit/Public/ConvertTo-CimInstance.Tests.ps1 index a31e4f9..3777792 100644 --- a/tests/Unit/Public/ConvertTo-CimInstance.Tests.ps1 +++ b/tests/Unit/Public/ConvertTo-CimInstance.Tests.ps1 @@ -1,60 +1,50 @@ -# macOS and Linux does not support CimInstance. -if (-not ($isWindows -or $PSEdition -eq 'Desktop')) -{ - return -} +BeforeAll { + $script:moduleName = 'DscResource.Common' -$ProjectPath = "$PSScriptRoot\..\..\.." | Convert-Path -$ProjectName = ((Get-ChildItem -Path $ProjectPath\*\*.psd1).Where{ - ($_.Directory.Name -match 'source|src' -or $_.Directory.Name -eq $_.BaseName) -and - $(try - { - Test-ModuleManifest $_.FullName -ErrorAction Stop - } - catch - { - $false - } ) - }).BaseName + Remove-Module -Name $script:moduleName -Force -ErrorAction 'SilentlyContinue' -Import-Module $ProjectName -Force + Get-Module -Name $script:moduleName -ListAvailable | + Select-Object -First 1 | + Import-Module -Force -ErrorAction 'Stop' +} -InModuleScope $ProjectName { - Describe 'NetworkingDsc.Common\ConvertTo-CimInstance' { +# macOS and Linux does not support CimInstance. +Describe 'ConvertTo-CimInstance' -Skip:(-not ($IsWindows -or $PSEdition -eq 'Desktop')) { + BeforeAll { $hashtable = @{ k1 = 'v1' k2 = 100 k3 = 1, 2, 3 } + } + + Context 'When the array contains the expected record count' { + It 'Should not throw exception' { + { + $script:result = [Microsoft.Management.Infrastructure.CimInstance[]] ( + $hashtable | ConvertTo-CimInstance + ) + } | Should -Not -Throw + } + + It "Should record count should be $($hashTable.Count)" { + $script:result.Count | Should -Be $hashtable.Count + } + + It 'Should return result of type CimInstance[]' { + $script:result.GetType().Name | Should -Be 'CimInstance[]' + } + + It 'Should return value "k1" in the CimInstance array should be "v1"' { + ($script:result | Where-Object Key -eq k1).Value | Should -Be 'v1' + } + + It 'Should return value "k2" in the CimInstance array should be "100"' { + ($script:result | Where-Object Key -eq k2).Value | Should -Be 100 + } - Context 'When the array contains the expected record count' { - It 'Should not throw exception' { - { - $script:result = [Microsoft.Management.Infrastructure.CimInstance[]] ( - $hashtable | ConvertTo-CimInstance - ) - } | Should -Not -Throw - } - - It "Should record count should be $($hashTable.Count)" { - $script:result.Count | Should -Be $hashtable.Count - } - - It 'Should return result of type CimInstance[]' { - $script:result.GetType().Name | Should -Be 'CimInstance[]' - } - - It 'Should return value "k1" in the CimInstance array should be "v1"' { - ($script:result | Where-Object Key -eq k1).Value | Should -Be 'v1' - } - - It 'Should return value "k2" in the CimInstance array should be "100"' { - ($script:result | Where-Object Key -eq k2).Value | Should -Be 100 - } - - It 'Should return value "k3" in the CimInstance array should be "1,2,3"' { - ($script:result | Where-Object Key -eq k3).Value | Should -Be '1,2,3' - } + It 'Should return value "k3" in the CimInstance array should be "1,2,3"' { + ($script:result | Where-Object Key -eq k3).Value | Should -Be '1,2,3' } } } diff --git a/tests/Unit/Public/ConvertTo-Hashtable.Tests.ps1 b/tests/Unit/Public/ConvertTo-Hashtable.Tests.ps1 index c353703..c6a4d81 100644 --- a/tests/Unit/Public/ConvertTo-Hashtable.Tests.ps1 +++ b/tests/Unit/Public/ConvertTo-Hashtable.Tests.ps1 @@ -1,56 +1,46 @@ -# macOS and Linux does not support CimInstance. -if (-not ($isWindows -or $PSEdition -eq 'Desktop')) -{ - return +BeforeAll { + $script:moduleName = 'DscResource.Common' + + Remove-Module -Name $script:moduleName -Force -ErrorAction 'SilentlyContinue' + + Get-Module -Name $script:moduleName -ListAvailable | + Select-Object -First 1 | + Import-Module -Force -ErrorAction 'Stop' } -$ProjectPath = "$PSScriptRoot\..\..\.." | Convert-Path -$ProjectName = ((Get-ChildItem -Path $ProjectPath\*\*.psd1).Where{ - ($_.Directory.Name -match 'source|src' -or $_.Directory.Name -eq $_.BaseName) -and - $(try - { - Test-ModuleManifest $_.FullName -ErrorAction Stop - } - catch - { - $false - } ) - }).BaseName - -Import-Module $ProjectName -Force - -InModuleScope $ProjectName { - Describe 'NetworkingDsc.Common\ConvertTo-HashTable' { +# macOS and Linux does not support CimInstance. +Describe 'ConvertTo-HashTable' -Skip:(-not ($IsWindows -or $PSEdition -eq 'Desktop')) { + BeforeAll { [Microsoft.Management.Infrastructure.CimInstance[]] $cimInstances = ConvertTo-CimInstance -Hashtable @{ k1 = 'v1' k2 = 100 k3 = 1, 2, 3 } + } - Context 'When the array contains the expected record count' { - It 'Should not throw exception' { - { $script:result = $cimInstances | ConvertTo-HashTable } | Should -Not -Throw - } + Context 'When the array contains the expected record count' { + It 'Should not throw exception' { + { $script:result = $cimInstances | ConvertTo-HashTable } | Should -Not -Throw + } - It "Should return record count of $($cimInstances.Count)" { - $script:result.Count | Should -Be $cimInstances.Count - } + It "Should return record count of $($cimInstances.Count)" { + $script:result.Count | Should -Be $cimInstances.Count + } - It 'Should return result of type [System.Collections.Hashtable]' { - $script:result | Should -BeOfType [System.Collections.Hashtable] - } + It 'Should return result of type [System.Collections.Hashtable]' { + $script:result | Should -BeOfType [System.Collections.Hashtable] + } - It 'Should return value "k1" in the hashtable should be "v1"' { - $script:result.k1 | Should -Be 'v1' - } + It 'Should return value "k1" in the hashtable should be "v1"' { + $script:result.k1 | Should -Be 'v1' + } - It 'Should return value "k2" in the hashtable should be "100"' { - $script:result.k2 | Should -Be 100 - } + It 'Should return value "k2" in the hashtable should be "100"' { + $script:result.k2 | Should -Be 100 + } - It 'Should return value "k3" in the hashtable should be "1,2,3"' { - $script:result.k3 | Should -Be '1,2,3' - } + It 'Should return value "k3" in the hashtable should be "1,2,3"' { + $script:result.k3 | Should -Be '1,2,3' } } } diff --git a/tests/Unit/Public/Get-ComputerName.Tests.ps1 b/tests/Unit/Public/Get-ComputerName.Tests.ps1 index bdf641f..04b0d43 100644 --- a/tests/Unit/Public/Get-ComputerName.Tests.ps1 +++ b/tests/Unit/Public/Get-ComputerName.Tests.ps1 @@ -1,37 +1,36 @@ -$ProjectPath = "$PSScriptRoot\..\..\.." | Convert-Path -$ProjectName = ((Get-ChildItem -Path $ProjectPath\*\*.psd1).Where{ - ($_.Directory.Name -match 'source|src' -or $_.Directory.Name -eq $_.BaseName) -and - $(try { Test-ModuleManifest -Path $_.FullName -ErrorAction Stop } catch { $false } ) - }).BaseName +BeforeAll { + $script:moduleName = 'DscResource.Common' + Remove-Module -Name $script:moduleName -Force -ErrorAction 'SilentlyContinue' -Import-Module $ProjectName - -InModuleScope $ProjectName { - Describe 'Get-ComputerName' { - BeforeAll { - $mockComputerName = 'MyComputer' + Get-Module -Name $script:moduleName -ListAvailable | + Select-Object -First 1 | + Import-Module -Force -ErrorAction 'Stop' +} - if ($IsLinux -or $IsMacOs) - { - function hostname - { - } +Describe 'Get-ComputerName' { + BeforeAll { + $mockComputerName = 'MyComputer' - Mock -CommandName 'hostname' -MockWith { - return $mockComputerName - } - } - else + if ($IsLinux -or $IsMacOs) + { + function hostname { - $mockComputerName = $env:COMPUTERNAME } + + Mock -CommandName 'hostname' -MockWith { + return $mockComputerName + } -ModuleName 'DscResource.Common' + } + else + { + $mockComputerName = $env:COMPUTERNAME } + } - Context 'When getting computer name' { - It 'Should return the correct computer name' { - Get-ComputerName | Should -Be $mockComputerName - } + Context 'When getting computer name' { + It 'Should return the correct computer name' { + Get-ComputerName | Should -Be $mockComputerName } } } diff --git a/tests/Unit/Public/Get-LocalizedData.Tests.ps1 b/tests/Unit/Public/Get-LocalizedData.Tests.ps1 index 08fb0a4..ddb9d26 100644 --- a/tests/Unit/Public/Get-LocalizedData.Tests.ps1 +++ b/tests/Unit/Public/Get-LocalizedData.Tests.ps1 @@ -1,155 +1,154 @@ -$ProjectPath = "$PSScriptRoot\..\..\.." | Convert-Path -$ProjectName = ((Get-ChildItem -Path $ProjectPath\*\*.psd1).Where{ - ($_.Directory.Name -match 'source|src' -or $_.Directory.Name -eq $_.BaseName) -and - $(try { Test-ModuleManifest -Path $_.FullName -ErrorAction Stop } catch { $false } ) - }).BaseName +BeforeAll { + $script:moduleName = 'DscResource.Common' + Remove-Module -Name $script:moduleName -Force -ErrorAction 'SilentlyContinue' -Import-Module $ProjectName + Get-Module -Name $script:moduleName -ListAvailable | + Select-Object -First 1 | + Import-Module -Force -ErrorAction 'Stop' +} -InModuleScope $ProjectName { - Describe 'Get-LocalizedData' -Tag 'GetLocalizedData' { - Context 'When specifying a specific filename' { - BeforeAll { - New-Item -Force -Path 'TestDrive:\ar-SA' -ItemType Directory +Describe 'Get-LocalizedData' -Tag 'GetLocalizedData' { + Context 'When specifying a specific filename' { + BeforeAll { + New-Item -Force -Path 'TestDrive:\ar-SA' -ItemType Directory - $null = " - ConvertFrom-StringData @`' - # English strings - ParameterBlockParameterAttributeMissing = A [Parameter()] attribute must be the first attribute of each parameter and be on its own line. See https://github.com/PowerShell/DscResources/blob/master/StyleGuidelines.md#correct-format-for-parameter-block - '@ - " | Out-File -Force -FilePath 'TestDrive:\ar-SA\Strings.psd1' - - "Get-LocalizedData -FileName 'Strings' -EA Stop" | - Out-File -Force -FilePath 'TestDrive:\execute.ps1' - } + $null = " + ConvertFrom-StringData @`' + # English strings + ParameterBlockParameterAttributeMissing = A [Parameter()] attribute must be the first attribute of each parameter and be on its own line. See https://github.com/PowerShell/DscResources/blob/master/StyleGuidelines.md#correct-format-for-parameter-block + '@ + " | Out-File -Force -FilePath 'TestDrive:\ar-SA\Strings.psd1' - It 'Should fail finding a Strings file in different locale' { - { $null = &'TestDrive:\execute.ps1' } | Should -Throw - } + "Get-LocalizedData -FileName 'Strings' -EA Stop" | + Out-File -Force -FilePath 'TestDrive:\execute.ps1' } - Context 'When specifying a specific filename and falling back to a DefaultUICulture' { - BeforeAll { - New-Item -Force -Path 'TestDrive:\ar-SA' -ItemType Directory + It 'Should fail finding a Strings file in different locale' { + { $null = &'TestDrive:\execute.ps1' } | Should -Throw + } + } - $null = " + Context 'When specifying a specific filename and falling back to a DefaultUICulture' { + BeforeAll { + New-Item -Force -Path 'TestDrive:\ar-SA' -ItemType Directory + + $null = " ConvertFrom-StringData @`' # ar-SA strings ParameterBlockParameterAttributeMissing = A [Parameter()] attribute must be the first attribute of each parameter and be on its own line. See https://github.com/PowerShell/DscResources/blob/master/StyleGuidelines.md#correct-format-for-parameter-block '@ - " | Out-File -Force -FilePath 'TestDrive:\ar-SA\Strings.psd1' + " | Out-File -Force -FilePath 'TestDrive:\ar-SA\Strings.psd1' - "Get-LocalizedData -FileName 'Strings' -DefaultUICulture 'ar-SA' -EA Stop" | - Out-File -Force -FilePath 'TestDrive:\execute.ps1' - } + "Get-LocalizedData -FileName 'Strings' -DefaultUICulture 'ar-SA' -EA Stop" | + Out-File -Force -FilePath 'TestDrive:\execute.ps1' + } - It 'Should retrieve the data' { - { $null = &'TestDrive:\execute.ps1' } | Should -Not -Throw - &'TestDrive:\execute.ps1' | Should -Not -BeNullOrEmpty - } + It 'Should retrieve the data' { + { $null = &'TestDrive:\execute.ps1' } | Should -Not -Throw + &'TestDrive:\execute.ps1' | Should -Not -BeNullOrEmpty } + } - Context 'When a filename is not specified' { - BeforeAll { - <# - We need to mock the test using the current OS UI culture so - that the tests passes. - #> - $mockCurrentUICulture = Get-UICulture - } + Context 'When a filename is not specified' { + BeforeAll { + <# + We need to mock the test using the current OS UI culture so + that the tests passes. + #> + $mockCurrentUICulture = 'en-US' + } - Context 'When no localized string file is found' { - BeforeAll { - New-Item -Force -Path ('TestDrive:\{0}' -f $mockCurrentUICulture) -ItemType Directory + Context 'When no localized string file is found' { + BeforeAll { + New-Item -Force -Path ('TestDrive:\{0}' -f $mockCurrentUICulture) -ItemType Directory - "Get-LocalizedData -DefaultUICulture 'en-US' -EA Stop" | - Out-File -Force -FilePath 'TestDrive:\DSC_Resource.psm1' - } + "Get-LocalizedData -DefaultUICulture 'en-US' -EA Stop" | + Out-File -Force -FilePath 'TestDrive:\DSC_Resource.psm1' + } - It 'Should throw if no file is found' { - { Import-Module -Name 'TestDrive:\DSC_Resource.psm1' -ErrorAction 'Stop' } | Should -Throw - } + It 'Should throw if no file is found' { + { Import-Module -Name 'TestDrive:\DSC_Resource.psm1' -ErrorAction 'Stop' } | Should -Throw } + } - Context 'When expecting to find a localized string filename without suffix' { - BeforeAll { - New-Item -Force -Path ('TestDrive:\{0}' -f $mockCurrentUICulture) -ItemType Directory + Context 'When expecting to find a localized string filename without suffix' { + BeforeAll { + New-Item -Force -Path ('TestDrive:\{0}' -f $mockCurrentUICulture) -ItemType Directory - $null = " + $null = " ConvertFrom-StringData @`' # en-US strings StringKey = String value '@ - " | Out-File -Force -FilePath ('TestDrive:\{0}\DSC_Resource1.psd1' -f $mockCurrentUICulture) + " | Out-File -Force -FilePath ('TestDrive:\{0}\DSC_Resource1.psd1' -f $mockCurrentUICulture) - "Get-LocalizedData -DefaultUICulture 'en-US' -EA Stop" | - Out-File -Force -FilePath 'TestDrive:\DSC_Resource1.psm1' - } + "Get-LocalizedData -DefaultUICulture 'en-US' -EA Stop" | + Out-File -Force -FilePath 'TestDrive:\DSC_Resource1.psm1' + } - It 'Should retrieve the data' { - { Import-Module -Name 'TestDrive:\DSC_Resource1.psm1' -ErrorAction 'Stop' } | Should -Not -Throw - } + It 'Should retrieve the data' { + { Import-Module -Name 'TestDrive:\DSC_Resource1.psm1' -ErrorAction 'Stop' } | Should -Not -Throw } + } - Context 'When expecting to find the default culture''s localized string filename without suffix' { - BeforeAll { - New-Item -Force -Path 'TestDrive:\sv-SE' -ItemType Directory + Context 'When expecting to find the default culture''s localized string filename without suffix' { + BeforeAll { + New-Item -Force -Path 'TestDrive:\sv-SE' -ItemType Directory - $null = " + $null = " ConvertFrom-StringData @`' # sv-SE strings StringKey = Sträng värde '@ - " | Out-File -Force -FilePath 'TestDrive:\sv-SE\DSC_Resource1.psd1' + " | Out-File -Force -FilePath 'TestDrive:\sv-SE\DSC_Resource1.psd1' - "Get-LocalizedData -DefaultUICulture 'sv-SE' -EA Stop" | - Out-File -Force -FilePath 'TestDrive:\DSC_Resource1.psm1' - } + "Get-LocalizedData -DefaultUICulture 'sv-SE' -EA Stop" | + Out-File -Force -FilePath 'TestDrive:\DSC_Resource1.psm1' + } - It 'Should retrieve the data' { - { Import-Module -Name 'TestDrive:\DSC_Resource1.psm1' -ErrorAction 'Stop' } | Should -Not -Throw - } + It 'Should retrieve the data' { + { Import-Module -Name 'TestDrive:\DSC_Resource1.psm1' -ErrorAction 'Stop' } | Should -Not -Throw } + } - Context "When expecting to find a localized string filename using the suffix '.strings'" { - BeforeAll { - New-Item -Force -Path ('TestDrive:\{0}' -f $mockCurrentUICulture) -ItemType Directory + Context "When expecting to find a localized string filename using the suffix '.strings'" { + BeforeAll { + New-Item -Force -Path ('TestDrive:\{0}' -f $mockCurrentUICulture) -ItemType Directory - $null = " + $null = " ConvertFrom-StringData @`' # en-US strings StringKey = String value '@ - " | Out-File -Force -FilePath ('TestDrive:\{0}\DSC_Resource2.strings.psd1' -f $mockCurrentUICulture) + " | Out-File -Force -FilePath ('TestDrive:\{0}\DSC_Resource2.strings.psd1' -f $mockCurrentUICulture) - "Get-LocalizedData -DefaultUICulture 'en-US' -EA Stop" | - Out-File -Force -FilePath 'TestDrive:\DSC_Resource2.psm1' - } + "Get-LocalizedData -DefaultUICulture 'en-US' -EA Stop" | + Out-File -Force -FilePath 'TestDrive:\DSC_Resource2.psm1' + } - It 'Should retrieve the data' { - { Import-Module -Name 'TestDrive:\DSC_Resource2.psm1' -ErrorAction 'Stop' } | Should -Not -Throw - } + It 'Should retrieve the data' { + { Import-Module -Name 'TestDrive:\DSC_Resource2.psm1' -ErrorAction 'Stop' } | Should -Not -Throw } + } - Context "When expecting to find the default culture's localized string filename with the suffix '.strings'" { - BeforeAll { - New-Item -Force -Path 'TestDrive:\sv-SE' -ItemType Directory + Context "When expecting to find the default culture's localized string filename with the suffix '.strings'" { + BeforeAll { + New-Item -Force -Path 'TestDrive:\sv-SE' -ItemType Directory - $null = " + $null = " ConvertFrom-StringData @`' # sv-SE strings StringKey = Sträng värde '@ - " | Out-File -Force -FilePath 'TestDrive:\sv-SE\DSC_Resource2.strings.psd1' + " | Out-File -Force -FilePath 'TestDrive:\sv-SE\DSC_Resource2.strings.psd1' - "Get-LocalizedData -DefaultUICulture 'sv-SE' -EA Stop" | - Out-File -Force -FilePath 'TestDrive:\DSC_Resource2.psm1' - } + "Get-LocalizedData -DefaultUICulture 'sv-SE' -EA Stop" | + Out-File -Force -FilePath 'TestDrive:\DSC_Resource2.psm1' + } - It 'Should retrieve the data' { - { Import-Module -Name 'TestDrive:\DSC_Resource2.psm1' -ErrorAction 'Stop' } | Should -Not -Throw - } + It 'Should retrieve the data' { + { Import-Module -Name 'TestDrive:\DSC_Resource2.psm1' -ErrorAction 'Stop' } | Should -Not -Throw } <# @@ -194,7 +193,7 @@ StringKey = String value ThreeLetterISOLanguageName = 'deu' ThreeLetterWindowsLanguageName = 'DEU' } - } + } -ModuleName 'DscResource.Common' } It 'Should retrieve the data' { diff --git a/tests/Unit/Public/Get-TemporaryFolder.Tests.ps1 b/tests/Unit/Public/Get-TemporaryFolder.Tests.ps1 index 9ea57cf..4ea7245 100644 --- a/tests/Unit/Public/Get-TemporaryFolder.Tests.ps1 +++ b/tests/Unit/Public/Get-TemporaryFolder.Tests.ps1 @@ -1,57 +1,58 @@ -$ProjectPath = "$PSScriptRoot\..\..\.." | Convert-Path -$ProjectName = ((Get-ChildItem -Path $ProjectPath\*\*.psd1).Where{ - ($_.Directory.Name -match 'source|src' -or $_.Directory.Name -eq $_.BaseName) -and - $(try { Test-ModuleManifest -Path $_.FullName -ErrorAction Stop } catch { $false } ) - }).BaseName +BeforeAll { + $script:moduleName = 'DscResource.Common' + Remove-Module -Name $script:moduleName -Force -ErrorAction 'SilentlyContinue' -Import-Module $ProjectName - -InModuleScope $ProjectName { - Describe 'SqlServerDsc.Common\Get-TemporaryFolder' -Tag 'GetTemporaryFolder' { - Context 'When getting the current temporary path' { - BeforeAll { - $mockExpectedTempPath = [IO.Path]::GetTempPath() - } + Get-Module -Name $script:moduleName -ListAvailable | + Select-Object -First 1 | + Import-Module -Force -ErrorAction 'Stop' +} - It 'Should return the expected temporary path' { - Get-TemporaryFolder | Should -BeExactly $mockExpectedTempPath - } +Describe 'Get-TemporaryFolder' -Tag 'GetTemporaryFolder' { + Context 'When getting the current temporary path' { + BeforeAll { + $mockExpectedTempPath = [IO.Path]::GetTempPath() } - switch ($true) - { - # Windows PowerShell or PowerShell 6+ on Windows - (-not (Test-Path -Path variable:IsWindows) -or $IsWindows) - { - <# - $env:TEMP used short filename, Get-Item expands it then we - need to add a backslash to the path. Because $env:TEMP - is the only one not ending the path with a backslash. - #> - $mockTemporaryPath = (Get-Item -Path $env:TEMP).FullName + '\' - } - - $IsMacOS - { - $mockTemporaryPath = $env:TMPDIR - } - - $IsLinux - { - $mockTemporaryPath = '/tmp/' - } + It 'Should return the expected temporary path' { + Get-TemporaryFolder | Should -BeExactly $mockExpectedTempPath + } + } - Default + Context 'When comparing returned temporary folder to other method of getting temporary folder' { + BeforeAll { + switch ($true) { - throw 'Cannot set the temporary path. Unknown operating system.' + # Windows PowerShell or PowerShell 6+ on Windows + (-not (Test-Path -Path variable:IsWindows) -or $IsWindows) + { + <# + $env:TEMP used short filename, Get-Item expands it then we + need to add a backslash to the path. Because $env:TEMP + is the only one not ending the path with a backslash. + #> + $mockTemporaryPath = (Get-Item -Path $env:TEMP).FullName + '\' + } + + $IsMacOS + { + $mockTemporaryPath = $env:TMPDIR + } + + $IsLinux + { + $mockTemporaryPath = '/tmp/' + } + + Default + { + throw 'Cannot set the temporary path. Unknown operating system.' + } } } - Context 'When comparing returned temporary folder to other method of getting temporary folder' { - It 'Should return the same temporary path' { - Get-TemporaryFolder | Should -BeExactly $mockTemporaryPath - } + It 'Should return the same temporary path' { + Get-TemporaryFolder | Should -BeExactly $mockTemporaryPath } } } diff --git a/tests/Unit/Public/New-InvalidArgumentException.Tests.ps1 b/tests/Unit/Public/New-InvalidArgumentException.Tests.ps1 index 4a0bb79..af110ba 100644 --- a/tests/Unit/Public/New-InvalidArgumentException.Tests.ps1 +++ b/tests/Unit/Public/New-InvalidArgumentException.Tests.ps1 @@ -1,10 +1,12 @@ -$ProjectPath = "$PSScriptRoot\..\..\.." | Convert-Path -$ProjectName = ((Get-ChildItem -Path $ProjectPath\*\*.psd1).Where{ - ($_.Directory.Name -match 'source|src' -or $_.Directory.Name -eq $_.BaseName) -and - $(try { Test-ModuleManifest $_.FullName -ErrorAction Stop } catch { $false } ) - }).BaseName +BeforeAll { + $script:moduleName = 'DscResource.Common' -Import-Module $ProjectName -Force + Remove-Module -Name $script:moduleName -Force -ErrorAction 'SilentlyContinue' + + Get-Module -Name $script:moduleName -ListAvailable | + Select-Object -First 1 | + Import-Module -Force -ErrorAction 'Stop' +} Describe 'New-InvalidArgumentException' { Context 'When calling with both the Message and ArgumentName parameter' { @@ -18,6 +20,4 @@ Describe 'New-InvalidArgumentException' { Should -BeLike ('{0}*Parameter*{1}*' -f $mockErrorMesssage, $mockArgumentName) } } - - Assert-VerifiableMock } diff --git a/tests/Unit/Public/New-InvalidDataException.Tests.ps1 b/tests/Unit/Public/New-InvalidDataException.Tests.ps1 index da143ed..c7e14a7 100644 --- a/tests/Unit/Public/New-InvalidDataException.Tests.ps1 +++ b/tests/Unit/Public/New-InvalidDataException.Tests.ps1 @@ -1,10 +1,12 @@ -$ProjectPath = "$PSScriptRoot\..\..\.." | Convert-Path -$ProjectName = ((Get-ChildItem -Path $ProjectPath\*\*.psd1).Where{ - ($_.Directory.Name -match 'source|src' -or $_.Directory.Name -eq $_.BaseName) -and - $(try { Test-ModuleManifest $_.FullName -ErrorAction Stop } catch { $false } ) - }).BaseName +BeforeAll { + $script:moduleName = 'DscResource.Common' -Import-Module $ProjectName -Force + Remove-Module -Name $script:moduleName -Force -ErrorAction 'SilentlyContinue' + + Get-Module -Name $script:moduleName -ListAvailable | + Select-Object -First 1 | + Import-Module -Force -ErrorAction 'Stop' +} Describe 'New-InvalidDataException' { Context 'When calling with both the ErrorId and ErrorMessage parameter' { @@ -20,6 +22,4 @@ Describe 'New-InvalidDataException' { $exception.Exception.Message | Should -Be $mockErrorMessage } } - - Assert-VerifiableMock } diff --git a/tests/Unit/Public/New-InvalidOperationException.Tests.ps1 b/tests/Unit/Public/New-InvalidOperationException.Tests.ps1 index 0cfc069..8d1bc91 100644 --- a/tests/Unit/Public/New-InvalidOperationException.Tests.ps1 +++ b/tests/Unit/Public/New-InvalidOperationException.Tests.ps1 @@ -1,17 +1,20 @@ -$ProjectPath = "$PSScriptRoot\..\..\.." | Convert-Path -$ProjectName = ((Get-ChildItem -Path $ProjectPath\*\*.psd1).Where{ - ($_.Directory.Name -match 'source|src' -or $_.Directory.Name -eq $_.BaseName) -and - $(try { Test-ModuleManifest $_.FullName -ErrorAction Stop } catch { $false } ) - }).BaseName +BeforeAll { + $script:moduleName = 'DscResource.Common' -Import-Module $ProjectName -Force + Remove-Module -Name $script:moduleName -Force -ErrorAction 'SilentlyContinue' + + Get-Module -Name $script:moduleName -ListAvailable | + Select-Object -First 1 | + Import-Module -Force -ErrorAction 'Stop' +} Describe 'New-InvalidOperationException' { Context 'When calling with Message parameter only' { It 'Should throw the correct error' { $mockErrorMessage = 'Mocked error' + $mockExpectedErrorMessage = 'System.InvalidOperationException: Mocked error' - { New-InvalidOperationException -Message $mockErrorMessage } | Should -Throw $mockErrorMessage + { New-InvalidOperationException -Message $mockErrorMessage } | Should -Throw $mockExpectedErrorMessage } } @@ -26,11 +29,9 @@ Describe 'New-InvalidOperationException' { # Wildcard processing needed to handle differing Powershell 5/6/7 exception output { New-InvalidOperationException -Message $mockErrorMessage -ErrorRecord $mockErrorRecord } | - Should -Throw -Passthru | Select-Object -ExpandProperty Exception | + Should -Throw -PassThru | Select-Object -ExpandProperty Exception | Should -BeLike ('System.Exception: System.InvalidOperationException: {0}*System.Exception: {1}*' -f $mockErrorMessage, $mockExceptionErrorMessage) } } - - Assert-VerifiableMock } diff --git a/tests/Unit/Public/New-InvalidResultException.Tests.ps1 b/tests/Unit/Public/New-InvalidResultException.Tests.ps1 index 385d85a..93e52bd 100644 --- a/tests/Unit/Public/New-InvalidResultException.Tests.ps1 +++ b/tests/Unit/Public/New-InvalidResultException.Tests.ps1 @@ -1,17 +1,20 @@ -$ProjectPath = "$PSScriptRoot\..\..\.." | Convert-Path -$ProjectName = ((Get-ChildItem -Path $ProjectPath\*\*.psd1).Where{ - ($_.Directory.Name -match 'source|src' -or $_.Directory.Name -eq $_.BaseName) -and - $(try { Test-ModuleManifest $_.FullName -ErrorAction Stop } catch { $false } ) - }).BaseName +BeforeAll { + $script:moduleName = 'DscResource.Common' -Import-Module $ProjectName -Force + Remove-Module -Name $script:moduleName -Force -ErrorAction 'SilentlyContinue' + + Get-Module -Name $script:moduleName -ListAvailable | + Select-Object -First 1 | + Import-Module -Force -ErrorAction 'Stop' +} Describe 'New-InvalidResultException' { Context 'When calling with Message parameter only' { It 'Should throw the correct error' { $mockErrorMessage = 'Mocked error' + $mockExpectedErrorMessage = 'System.Exception: Mocked error' - { New-InvalidResultException -Message $mockErrorMessage } | Should -Throw $mockErrorMessage + { New-InvalidResultException -Message $mockErrorMessage } | Should -Throw $mockExpectedErrorMessage } } @@ -26,11 +29,9 @@ Describe 'New-InvalidResultException' { # Wildcard processing needed to handle differing Powershell 5/6/7 exception output { New-InvalidResultException -Message $mockErrorMessage -ErrorRecord $mockErrorRecord } | - Should -Throw -Passthru | Select-Object -ExpandProperty Exception | + Should -Throw -PassThru | Select-Object -ExpandProperty Exception | Should -BeLike ('System.Exception: System.Exception: {0}*System.Exception: {1}*' -f $mockErrorMessage, $mockExceptionErrorMessage) } } - - Assert-VerifiableMock } diff --git a/tests/Unit/Public/New-NotImplementedException.Tests.ps1 b/tests/Unit/Public/New-NotImplementedException.Tests.ps1 index 9e8d696..472f5b6 100644 --- a/tests/Unit/Public/New-NotImplementedException.Tests.ps1 +++ b/tests/Unit/Public/New-NotImplementedException.Tests.ps1 @@ -1,17 +1,20 @@ -$ProjectPath = "$PSScriptRoot\..\..\.." | Convert-Path -$ProjectName = ((Get-ChildItem -Path $ProjectPath\*\*.psd1).Where{ - ($_.Directory.Name -match 'source|src' -or $_.Directory.Name -eq $_.BaseName) -and - $(try { Test-ModuleManifest $_.FullName -ErrorAction Stop } catch { $false } ) - }).BaseName +BeforeAll { + $script:moduleName = 'DscResource.Common' -Import-Module $ProjectName -Force + Remove-Module -Name $script:moduleName -Force -ErrorAction 'SilentlyContinue' + + Get-Module -Name $script:moduleName -ListAvailable | + Select-Object -First 1 | + Import-Module -Force -ErrorAction 'Stop' +} Describe 'New-NotImplementedException' { Context 'When called with Message parameter only' { It 'Should throw the correct error' { $mockErrorMessage = 'Mocked error' + $mockExpectedErrorMessage = 'System.NotImplementedException: Mocked error' - { New-NotImplementedException -Message $mockErrorMessage } | Should -Throw $mockErrorMessage + { New-NotImplementedException -Message $mockErrorMessage } | Should -Throw $mockExpectedErrorMessage } } @@ -26,11 +29,9 @@ Describe 'New-NotImplementedException' { # Wildcard processing needed to handle differing Powershell 5/6/7 exception output { New-NotImplementedException -Message $mockErrorMessage -ErrorRecord $mockErrorRecord } | - Should -Throw -Passthru | Select-Object -ExpandProperty Exception | + Should -Throw -PassThru | Select-Object -ExpandProperty Exception | Should -BeLike ('System.Exception: System.NotImplementedException: {0}*System.Exception: {1}*' -f $mockErrorMessage, $mockExceptionErrorMessage) } } - - Assert-VerifiableMock } diff --git a/tests/Unit/Public/New-ObjectNotFoundException.Tests.ps1 b/tests/Unit/Public/New-ObjectNotFoundException.Tests.ps1 index d09c5aa..401c1f7 100644 --- a/tests/Unit/Public/New-ObjectNotFoundException.Tests.ps1 +++ b/tests/Unit/Public/New-ObjectNotFoundException.Tests.ps1 @@ -1,17 +1,20 @@ -$ProjectPath = "$PSScriptRoot\..\..\.." | Convert-Path -$ProjectName = ((Get-ChildItem -Path $ProjectPath\*\*.psd1).Where{ - ($_.Directory.Name -match 'source|src' -or $_.Directory.Name -eq $_.BaseName) -and - $(try { Test-ModuleManifest $_.FullName -ErrorAction Stop } catch { $false } ) - }).BaseName +BeforeAll { + $script:moduleName = 'DscResource.Common' -Import-Module $ProjectName -Force + Remove-Module -Name $script:moduleName -Force -ErrorAction 'SilentlyContinue' + + Get-Module -Name $script:moduleName -ListAvailable | + Select-Object -First 1 | + Import-Module -Force -ErrorAction 'Stop' +} Describe 'New-ObjectNotFoundException' { Context 'When calling with Message parameter only' { It 'Should throw the correct error' { $mockErrorMessage = 'Mocked error' + $mockExpectedErrorMessage = 'System.Exception: Mocked error' - { New-ObjectNotFoundException -Message $mockErrorMessage } | Should -Throw $mockErrorMessage + { New-ObjectNotFoundException -Message $mockErrorMessage } | Should -Throw $mockExpectedErrorMessage } } @@ -25,11 +28,9 @@ Describe 'New-ObjectNotFoundException' { -ArgumentList $mockException, $null, 'InvalidResult', $null { New-ObjectNotFoundException -Message $mockErrorMessage -ErrorRecord $mockErrorRecord } | - Should -Throw -Passthru | Select-Object -ExpandProperty Exception | + Should -Throw -PassThru | Select-Object -ExpandProperty Exception | Should -BeLike ('System.Exception: System.Exception: {0}*System.Exception: {1}*' -f $mockErrorMessage, $mockExceptionErrorMessage) } } - - Assert-VerifiableMock } diff --git a/tests/Unit/Public/Remove-CommonParameter.Tests.ps1 b/tests/Unit/Public/Remove-CommonParameter.Tests.ps1 index d53228b..aba9886 100644 --- a/tests/Unit/Public/Remove-CommonParameter.Tests.ps1 +++ b/tests/Unit/Public/Remove-CommonParameter.Tests.ps1 @@ -1,67 +1,62 @@ -$ProjectPath = "$PSScriptRoot\..\..\.." | Convert-Path -$ProjectName = ((Get-ChildItem -Path $ProjectPath\*\*.psd1).Where{ - ($_.Directory.Name -match 'source|src' -or $_.Directory.Name -eq $_.BaseName) -and - $(try - { - Test-ModuleManifest $_.FullName -ErrorAction Stop - } - catch - { - $false - } ) - }).BaseName +BeforeAll { + $script:moduleName = 'DscResource.Common' -Import-Module $ProjectName -Force + Remove-Module -Name $script:moduleName -Force -ErrorAction 'SilentlyContinue' -InModuleScope $ProjectName { - Describe 'NetworkingDsc.Common\Remove-CommonParameter' { - $removeCommonParameter = @{ - Parameter1 = 'value1' - Parameter2 = 'value2' - Verbose = $true - Debug = $true - ErrorAction = 'Stop' - WarningAction = 'Stop' - InformationAction = 'Stop' - ErrorVariable = 'errorVariable' - WarningVariable = 'warningVariable' - OutVariable = 'outVariable' - OutBuffer = 'outBuffer' - PipelineVariable = 'pipelineVariable' - InformationVariable = 'informationVariable' - WhatIf = $true - Confirm = $true - UseTransaction = $true - } + Get-Module -Name $script:moduleName -ListAvailable | + Select-Object -First 1 | + Import-Module -Force -ErrorAction 'Stop' +} - Context 'Hashtable contains all common parameters' { - It 'Should not throw exception' { - { - $script:result = Remove-CommonParameter -Hashtable $removeCommonParameter -Verbose - } | Should -Not -Throw +Describe 'Remove-CommonParameter' { + Context 'Hashtable contains all common parameters' { + BeforeAll { + $removeCommonParameter = @{ + Parameter1 = 'value1' + Parameter2 = 'value2' + Verbose = $true + Debug = $true + ErrorAction = 'Stop' + WarningAction = 'Stop' + InformationAction = 'Stop' + ErrorVariable = 'errorVariable' + WarningVariable = 'warningVariable' + OutVariable = 'outVariable' + OutBuffer = 'outBuffer' + PipelineVariable = 'pipelineVariable' + InformationVariable = 'informationVariable' + WhatIf = $true + Confirm = $true + UseTransaction = $true } + } - It 'Should have retained parameters in the hashtable' { - $script:result.Contains('Parameter1') | Should -BeTrue - $script:result.Contains('Parameter2') | Should -BeTrue - } + It 'Should not throw exception' { + { + $script:result = Remove-CommonParameter -Hashtable $removeCommonParameter -Verbose + } | Should -Not -Throw + } - It 'Should have removed the common parameters from the hashtable' { - $script:result.Contains('Verbose') | Should -BeFalse - $script:result.Contains('Debug') | Should -BeFalse - $script:result.Contains('ErrorAction') | Should -BeFalse - $script:result.Contains('WarningAction') | Should -BeFalse - $script:result.Contains('InformationAction') | Should -BeFalse - $script:result.Contains('ErrorVariable') | Should -BeFalse - $script:result.Contains('WarningVariable') | Should -BeFalse - $script:result.Contains('OutVariable') | Should -BeFalse - $script:result.Contains('OutBuffer') | Should -BeFalse - $script:result.Contains('PipelineVariable') | Should -BeFalse - $script:result.Contains('InformationVariable') | Should -BeFalse - $script:result.Contains('WhatIf') | Should -BeFalse - $script:result.Contains('Confirm') | Should -BeFalse - $script:result.Contains('UseTransaction') | Should -BeFalse - } + It 'Should have retained parameters in the hashtable' { + $script:result.Contains('Parameter1') | Should -BeTrue + $script:result.Contains('Parameter2') | Should -BeTrue + } + + It 'Should have removed the common parameters from the hashtable' { + $script:result.Contains('Verbose') | Should -BeFalse + $script:result.Contains('Debug') | Should -BeFalse + $script:result.Contains('ErrorAction') | Should -BeFalse + $script:result.Contains('WarningAction') | Should -BeFalse + $script:result.Contains('InformationAction') | Should -BeFalse + $script:result.Contains('ErrorVariable') | Should -BeFalse + $script:result.Contains('WarningVariable') | Should -BeFalse + $script:result.Contains('OutVariable') | Should -BeFalse + $script:result.Contains('OutBuffer') | Should -BeFalse + $script:result.Contains('PipelineVariable') | Should -BeFalse + $script:result.Contains('InformationVariable') | Should -BeFalse + $script:result.Contains('WhatIf') | Should -BeFalse + $script:result.Contains('Confirm') | Should -BeFalse + $script:result.Contains('UseTransaction') | Should -BeFalse } } } diff --git a/tests/Unit/Public/Test-DscParameterState.Tests.ps1 b/tests/Unit/Public/Test-DscParameterState.Tests.ps1 index 5fee8e3..2da71e4 100644 --- a/tests/Unit/Public/Test-DscParameterState.Tests.ps1 +++ b/tests/Unit/Public/Test-DscParameterState.Tests.ps1 @@ -1,23 +1,20 @@ -$ProjectPath = "$PSScriptRoot\..\..\.." | Convert-Path -$ProjectName = ((Get-ChildItem -Path $ProjectPath\*\*.psd1).Where{ - ($_.Directory.Name -match 'source|src' -or $_.Directory.Name -eq $_.BaseName) -and - $(try - { - Test-ModuleManifest $_.FullName -ErrorAction Stop - } - catch - { - $false - } ) - }).BaseName - -Import-Module $ProjectName -Force - -InModuleScope $ProjectName { - Describe 'ComputerManagementDsc.Common\Test-DscParameterState' { +BeforeAll { + $script:moduleName = 'DscResource.Common' + + Remove-Module -Name $script:moduleName -Force -ErrorAction 'SilentlyContinue' + + Get-Module -Name $script:moduleName -ListAvailable | + Select-Object -First 1 | + Import-Module -Force -ErrorAction 'Stop' +} + +Describe 'Test-DscParameterState' { + BeforeAll { $verbose = $true + } - Context 'When testing single values' { + Context 'When testing single values' { + BeforeAll { $currentValues = @{ String = 'a string' Bool = $true @@ -30,8 +27,10 @@ InModuleScope $ProjectName { } ScriptBlock = { Get-Date } } + } - Context 'When all values match' { + Context 'When all values match' { + BeforeAll { $desiredValues = [PSObject] @{ String = 'a string' Bool = $true @@ -44,20 +43,25 @@ InModuleScope $ProjectName { } ScriptBlock = { Get-Date } } + } - It 'Should not throw exception' { - { $script:result = Test-DscParameterState ` - -CurrentValues $currentValues ` - -DesiredValues $desiredValues ` - -Verbose:$verbose } | Should -Not -Throw - } + It 'Should not throw exception' { + { Test-DscParameterState ` + -CurrentValues $currentValues ` + -DesiredValues $desiredValues ` + -Verbose:$verbose } | Should -Not -Throw + } - It 'Should return $true' { - $script:result | Should -BeTrue - } + It 'Should return $true' { + Test-DscParameterState ` + -CurrentValues $currentValues ` + -DesiredValues $desiredValues ` + -Verbose:$verbose | Should -BeTrue } + } - Context 'When a string is mismatched' { + Context 'When a string is mismatched' { + BeforeAll { $desiredValues = [PSObject] @{ String = 'different string' Bool = $true @@ -69,20 +73,25 @@ InModuleScope $ProjectName { k3 = 'v1', 'v2', 'v3' } } + } - It 'Should not throw exception' { - { $script:result = Test-DscParameterState ` - -CurrentValues $currentValues ` - -DesiredValues $desiredValues ` - -Verbose:$verbose } | Should -Not -Throw - } + It 'Should not throw exception' { + { Test-DscParameterState ` + -CurrentValues $currentValues ` + -DesiredValues $desiredValues ` + -Verbose:$verbose } | Should -Not -Throw + } - It 'Should return $false' { - $script:result | Should -BeFalse - } + It 'Should return $false' { + Test-DscParameterState ` + -CurrentValues $currentValues ` + -DesiredValues $desiredValues ` + -Verbose:$verbose | Should -BeFalse } + } - Context 'When a boolean is mismatched' { + Context 'When a boolean is mismatched' { + BeforeAll { $desiredValues = [PSObject] @{ String = 'a string' Bool = $false @@ -94,20 +103,25 @@ InModuleScope $ProjectName { k3 = 'v1', 'v2', 'v3' } } + } - It 'Should not throw exception' { - { $script:result = Test-DscParameterState ` - -CurrentValues $currentValues ` - -DesiredValues $desiredValues ` - -Verbose:$verbose } | Should -Not -Throw - } + It 'Should not throw exception' { + { Test-DscParameterState ` + -CurrentValues $currentValues ` + -DesiredValues $desiredValues ` + -Verbose:$verbose } | Should -Not -Throw + } - It 'Should return $false' { - $script:result | Should -BeFalse - } + It 'Should return $false' { + Test-DscParameterState ` + -CurrentValues $currentValues ` + -DesiredValues $desiredValues ` + -Verbose:$verbose | Should -BeFalse } + } - Context 'When an int is mismatched' { + Context 'When an int is mismatched' { + BeforeAll { $desiredValues = [PSObject] @{ String = 'a string' Bool = $true @@ -119,20 +133,25 @@ InModuleScope $ProjectName { k3 = 'v1', 'v2', 'v3' } } + } - It 'Should not throw exception' { - { $script:result = Test-DscParameterState ` - -CurrentValues $currentValues ` - -DesiredValues $desiredValues ` - -Verbose:$verbose } | Should -Not -Throw - } + It 'Should not throw exception' { + { Test-DscParameterState ` + -CurrentValues $currentValues ` + -DesiredValues $desiredValues ` + -Verbose:$verbose } | Should -Not -Throw + } - It 'Should return $false' { - $script:result | Should -BeFalse - } + It 'Should return $false' { + Test-DscParameterState ` + -CurrentValues $currentValues ` + -DesiredValues $desiredValues ` + -Verbose:$verbose | Should -BeFalse } + } - Context 'When an scriptblock is mismatched' { + Context 'When an scriptblock is mismatched' { + BeforeAll { $desiredValues = [PSObject] @{ String = 'a string' Bool = $true @@ -145,20 +164,22 @@ InModuleScope $ProjectName { } ScriptBlock = { Get-Process } } + } - It 'Should not throw exception' { - { $script:result = Test-DscParameterState ` - -CurrentValues $currentValues ` - -DesiredValues $desiredValues ` - -Verbose:$verbose } | Should -Not -Throw - } + It 'Should not throw exception' { + { $script:result = Test-DscParameterState ` + -CurrentValues $currentValues ` + -DesiredValues $desiredValues ` + -Verbose:$verbose } | Should -Not -Throw + } - It 'Should return $false' { - $script:result | Should -BeFalse - } + It 'Should return $false' { + $script:result | Should -BeFalse } + } - Context 'When an int is mismatched' { + Context 'When an int is mismatched' { + BeforeAll { $desiredValues = [PSObject] @{ String = 'a string' Bool = $true @@ -170,61 +191,74 @@ InModuleScope $ProjectName { k3 = 'v1', 'v2', 'v3' } } + } - It 'Should not throw exception' { - { $script:result = Test-DscParameterState ` - -CurrentValues $currentValues ` - -DesiredValues $desiredValues ` - -Verbose:$verbose } | Should -Not -Throw - } + It 'Should not throw exception' { + { $script:result = Test-DscParameterState ` + -CurrentValues $currentValues ` + -DesiredValues $desiredValues ` + -Verbose:$verbose } | Should -Not -Throw + } - It 'Should return $false' { - $script:result | Should -BeFalse - } + It 'Should return $false' { + $script:result | Should -BeFalse } + } - Context 'When a type is mismatched' { + Context 'When a type is mismatched' { + BeforeAll { $desiredValues = [PSObject] @{ String = 'a string' Bool = $true Int = '99' Array = 'a', 'b', 'c' } + } - It 'Should not throw exception' { - { $script:result = Test-DscParameterState ` - -CurrentValues $currentValues ` - -DesiredValues $desiredValues ` - -Verbose:$verbose } | Should -Not -Throw - } + It 'Should not throw exception' { + { Test-DscParameterState ` + -CurrentValues $currentValues ` + -DesiredValues $desiredValues ` + -Verbose:$verbose } | Should -Not -Throw + } - It 'Should return $false' { - $script:result | Should -BeFalse - } + It 'Should return $false' { + Test-DscParameterState ` + -CurrentValues $currentValues ` + -DesiredValues $desiredValues ` + -Verbose:$verbose | Should -BeFalse } + } - Context 'When a type is mismatched but TurnOffTypeChecking is used' { + Context 'When a type is mismatched but TurnOffTypeChecking is used' { + BeforeAll { $desiredValues = [PSObject] @{ String = 'a string' Bool = $true Int = '99' Array = 'a', 'b', 'c' } + } - It 'Should not throw exception' { - { $script:result = Test-DscParameterState ` - -CurrentValues $currentValues ` - -DesiredValues $desiredValues ` - -TurnOffTypeChecking ` - -Verbose:$verbose } | Should -Not -Throw - } + It 'Should not throw exception' { + { Test-DscParameterState ` + -CurrentValues $currentValues ` + -DesiredValues $desiredValues ` + -TurnOffTypeChecking ` + -Verbose:$verbose } | Should -Not -Throw + } - It 'Should return $true' { - $script:result | Should -BeTrue - } + It 'Should return $true' { + Test-DscParameterState ` + -CurrentValues $currentValues ` + -DesiredValues $desiredValues ` + -TurnOffTypeChecking ` + -Verbose:$verbose | Should -BeTrue } + } - Context 'When a value is mismatched but ExcludeProperties is used to exclude then' { + Context 'When a value is mismatched but ExcludeProperties is used to exclude then' { + BeforeAll { $desiredValues = @{ String = 'some other string' Bool = $true @@ -241,21 +275,23 @@ InModuleScope $ProjectName { $excludeProperties = @( 'String' ) + } - It 'Should not throw exception' { - { $script:result = Test-DscParameterState ` - -CurrentValues $currentValues ` - -DesiredValues $desiredValues ` - -ExcludeProperties $excludeProperties ` - -Verbose:$verbose } | Should -Not -Throw - } + It 'Should not throw exception' { + { $script:result = Test-DscParameterState ` + -CurrentValues $currentValues ` + -DesiredValues $desiredValues ` + -ExcludeProperties $excludeProperties ` + -Verbose:$verbose } | Should -Not -Throw + } - It 'Should return $true' { - $script:result | Should -BeTrue - } + It 'Should return $true' { + $script:result | Should -BeTrue } + } - Context 'When a value is mismatched but it is not in Properties then' { + Context 'When a value is mismatched but it is not in Properties' { + BeforeAll { $desiredValues = @{ String = 'some other string' Bool = $true @@ -268,48 +304,42 @@ InModuleScope $ProjectName { } ScriptBlock = { Get-Date } } + } - $properties = @( - 'Bool' - 'Int' - 'Array' - 'Hashtable' - 'ScriptBlock' - ) - - Context 'When using the alias ValuesToCheck' { - It 'Should not throw exception' { - { $script:result = Test-DscParameterState ` - -CurrentValues $currentValues ` - -DesiredValues $desiredValues ` - -ValuesToCheck $properties ` - -Verbose:$verbose } | Should -Not -Throw - } - - It 'Should return $true' { - $script:result | Should -BeTrue - } + Context 'When using the parameter alias ValuesToCheck' { + BeforeAll { + $properties = @( + 'Bool' + 'Int' + 'Array' + 'Hashtable' + 'ScriptBlock' + ) } It 'Should not throw exception' { { $script:result = Test-DscParameterState ` -CurrentValues $currentValues ` -DesiredValues $desiredValues ` - -Properties $properties ` + -ValuesToCheck $properties ` -Verbose:$verbose } | Should -Not -Throw } It 'Should return $true' { $script:result | Should -BeTrue } + } - $properties = @( - 'String' - 'Bool' - 'Int' - 'Array' - 'Hashtable' - ) + Context 'When using parameter Properties' { + BeforeAll { + $properties = @( + 'String' + 'Bool' + 'Int' + 'Array' + 'Hashtable' + ) + } It 'Should not throw exception' { { $script:result = Test-DscParameterState ` @@ -324,23 +354,25 @@ InModuleScope $ProjectName { } } } + } - Context 'When testing array values' { - BeforeAll { - $currentValues = @{ - String = 'a string' - Bool = $true - Int = 99 - Array = 'a', 'b', 'c', 1 - Hashtable = @{ - k1 = 'Test' - k2 = 123 - k3 = 'v1', 'v2', 'v3' - } + Context 'When testing array values' { + BeforeAll { + $currentValues = @{ + String = 'a string' + Bool = $true + Int = 99 + Array = 'a', 'b', 'c', 1 + Hashtable = @{ + k1 = 'Test' + k2 = 123 + k3 = 'v1', 'v2', 'v3' } } + } - Context 'When array is missing a value' { + Context 'When array is missing a value' { + BeforeAll { $desiredValues = [PSObject] @{ String = 'a string' Bool = $true @@ -352,143 +384,180 @@ InModuleScope $ProjectName { k3 = 'v1', 'v2', 'v3' } } + } - It 'Should not throw exception' { - { $script:result = Test-DscParameterState ` - -CurrentValues $currentValues ` - -DesiredValues $desiredValues ` - -Verbose:$verbose } | Should -Not -Throw - } + It 'Should not throw exception' { + { Test-DscParameterState ` + -CurrentValues $currentValues ` + -DesiredValues $desiredValues ` + -Verbose:$verbose } | Should -Not -Throw + } - It 'Should return $false' { - $script:result | Should -BeFalse - } + It 'Should return $false' { + Test-DscParameterState ` + -CurrentValues $currentValues ` + -DesiredValues $desiredValues ` + -Verbose:$verbose | Should -BeFalse } + } - Context 'When array has an additional value' { + Context 'When array has an additional value' { + BeforeAll { $desiredValues = [PSObject] @{ String = 'a string' Bool = $true Int = 1 Array = 'a', 'b', 'c', 1, 2 } + } - It 'Should not throw exception' { - { $script:result = Test-DscParameterState ` - -CurrentValues $currentValues ` - -DesiredValues $desiredValues ` - -Verbose:$verbose } | Should -Not -Throw - } + It 'Should not throw exception' { + { Test-DscParameterState ` + -CurrentValues $currentValues ` + -DesiredValues $desiredValues ` + -Verbose:$verbose } | Should -Not -Throw + } - It 'Should return $false' { - $script:result | Should -BeFalse - } + It 'Should return $false' { + Test-DscParameterState ` + -CurrentValues $currentValues ` + -DesiredValues $desiredValues ` + -Verbose:$verbose | Should -BeFalse } + } - Context 'When array has a different value' { + Context 'When array has a different value' { + BeforeAll { $desiredValues = [PSObject] @{ String = 'a string' Bool = $true Int = 1 Array = 'a', 'x', 'c', 1 } + } - It 'Should not throw exception' { - { $script:result = Test-DscParameterState ` - -CurrentValues $currentValues ` - -DesiredValues $desiredValues ` - -Verbose:$verbose } | Should -Not -Throw - } + It 'Should not throw exception' { + { Test-DscParameterState ` + -CurrentValues $currentValues ` + -DesiredValues $desiredValues ` + -Verbose:$verbose } | Should -Not -Throw + } - It 'Should return $false' { - $script:result | Should -BeFalse - } + It 'Should return $false' { + Test-DscParameterState ` + -CurrentValues $currentValues ` + -DesiredValues $desiredValues ` + -Verbose:$verbose | Should -BeFalse } + } - Context 'When array has different order' { + Context 'When array has different order' { + BeforeAll { $desiredValues = [PSObject] @{ String = 'a string' Bool = $true Int = 1 Array = 'c', 'b', 'a', 1 } + } - It 'Should not throw exception' { - { $script:result = Test-DscParameterState ` - -CurrentValues $currentValues ` - -DesiredValues $desiredValues ` - -Verbose:$verbose } | Should -Not -Throw - } + It 'Should not throw exception' { + { Test-DscParameterState ` + -CurrentValues $currentValues ` + -DesiredValues $desiredValues ` + -Verbose:$verbose } | Should -Not -Throw + } - It 'Should return $false' { - $script:result | Should -BeFalse - } + It 'Should return $false' { + Test-DscParameterState ` + -CurrentValues $currentValues ` + -DesiredValues $desiredValues ` + -Verbose:$verbose | Should -BeFalse } + } - Context 'When array has different order but SortArrayValues is used' { + Context 'When array has different order but SortArrayValues is used' { + BeforeAll { $desiredValues = [PSObject] @{ String = 'a string' Bool = $true Int = 1 Array = 'c', 'b', 'a', 1 } + } - It 'Should not throw exception' { - { $script:result = Test-DscParameterState ` - -CurrentValues $currentValues ` - -DesiredValues $desiredValues ` - -SortArrayValues ` - -Verbose:$verbose } | Should -Not -Throw - } + It 'Should not throw exception' { + { Test-DscParameterState ` + -CurrentValues $currentValues ` + -DesiredValues $desiredValues ` + -SortArrayValues ` + -Verbose:$verbose } | Should -Not -Throw + } - It 'Should return $false' { - $script:result | Should -BeFalse - } + It 'Should return $false' { + Test-DscParameterState ` + -CurrentValues $currentValues ` + -DesiredValues $desiredValues ` + -SortArrayValues ` + -Verbose:$verbose | Should -BeFalse } + } - Context 'When array has a value with a different type' { + Context 'When array has a value with a different type' { + BeforeAll { $desiredValues = [PSObject] @{ String = 'a string' Bool = $true Int = 99 Array = 'a', 'b', 'c', '1' } + } - It 'Should not throw exception' { - { $script:result = Test-DscParameterState ` - -CurrentValues $currentValues ` - -DesiredValues $desiredValues ` - -Verbose:$verbose } | Should -Not -Throw - } + It 'Should not throw exception' { + { Test-DscParameterState ` + -CurrentValues $currentValues ` + -DesiredValues $desiredValues ` + -Verbose:$verbose } | Should -Not -Throw + } - It 'Should return $false' { - $script:result | Should -BeFalse - } + It 'Should return $false' { + Test-DscParameterState ` + -CurrentValues $currentValues ` + -DesiredValues $desiredValues ` + -Verbose:$verbose | Should -BeFalse } + } - Context 'When array has a value with a different type but TurnOffTypeChecking is used' { + Context 'When array has a value with a different type but TurnOffTypeChecking is used' { + BeforeAll { $desiredValues = [PSObject] @{ String = 'a string' Bool = $true Int = 99 Array = 'a', 'b', 'c', '1' } + } - It 'Should not throw exception' { - { $script:result = Test-DscParameterState ` - -CurrentValues $currentValues ` - -DesiredValues $desiredValues ` - -TurnOffTypeChecking ` - -Verbose:$verbose } | Should -Not -Throw - } + It 'Should not throw exception' { + { Test-DscParameterState ` + -CurrentValues $currentValues ` + -DesiredValues $desiredValues ` + -TurnOffTypeChecking ` + -Verbose:$verbose } | Should -Not -Throw + } - It 'Should return $true' { - $script:result | Should -BeTrue - } + It 'Should return $true' { + Test-DscParameterState ` + -CurrentValues $currentValues ` + -DesiredValues $desiredValues ` + -TurnOffTypeChecking ` + -Verbose:$verbose | Should -BeTrue } + } - Context 'When both arrays are empty' { + Context 'When both arrays are empty' { + BeforeAll { $currentValues = @{ String = 'a string' Bool = $true @@ -512,41 +581,46 @@ InModuleScope $ProjectName { k3 = @() } } + } - It 'Should not throw exception' { - { $script:result = Test-DscParameterState ` - -CurrentValues $currentValues ` - -DesiredValues $desiredValues ` - -Verbose:$verbose } | Should -Not -Throw - } + It 'Should not throw exception' { + { Test-DscParameterState ` + -CurrentValues $currentValues ` + -DesiredValues $desiredValues ` + -Verbose:$verbose } | Should -Not -Throw + } - It 'Should return $true' { - $script:result | Should -BeTrue - } + It 'Should return $true' { + Test-DscParameterState ` + -CurrentValues $currentValues ` + -DesiredValues $desiredValues ` + -Verbose:$verbose | Should -BeTrue } Context 'When a current value array is empty' { - $currentValues = @{ - String = 'a string' - Bool = $true - Int = 99 - Array = @('a','b','c') - Hashtable = @{ - k1 = 'Test' - k2 = 123 - k3 = @() + BeforeAll { + $currentValues = @{ + String = 'a string' + Bool = $true + Int = 99 + Array = @('a','b','c') + Hashtable = @{ + k1 = 'Test' + k2 = 123 + k3 = @() + } } - } - $desiredValues = [PSObject] @{ - String = 'a string' - Bool = $true - Int = 99 - Array = @() - Hashtable = @{ - k1 = 'Test' - k2 = 123 - k3 = @() + $desiredValues = [PSObject] @{ + String = 'a string' + Bool = $true + Int = 99 + Array = @() + Hashtable = @{ + k1 = 'Test' + k2 = 123 + k3 = @() + } } } @@ -563,27 +637,29 @@ InModuleScope $ProjectName { } Context 'When a desired value array is empty' { - $currentValues = @{ - String = 'a string' - Bool = $true - Int = 99 - Array = @() - Hashtable = @{ - k1 = 'Test' - k2 = 123 - k3 = @() + BeforeAll { + $currentValues = @{ + String = 'a string' + Bool = $true + Int = 99 + Array = @() + Hashtable = @{ + k1 = 'Test' + k2 = 123 + k3 = @() + } } - } - $desiredValues = [PSObject] @{ - String = 'a string' - Bool = $true - Int = 99 - Array = @('a','b','c') - Hashtable = @{ - k1 = 'Test' - k2 = 123 - k3 = @() + $desiredValues = [PSObject] @{ + String = 'a string' + Bool = $true + Int = 99 + Array = @('a','b','c') + Hashtable = @{ + k1 = 'Test' + k2 = 123 + k3 = @() + } } } @@ -599,8 +675,10 @@ InModuleScope $ProjectName { } } } + } - Context 'When testing hashtables' { + Context 'When testing hashtables' { + BeforeAll { $currentValues = @{ String = 'a string' Bool = $true @@ -612,8 +690,10 @@ InModuleScope $ProjectName { k3 = 'v1', 'v2', 'v3', 99 } } + } - Context 'When hashtable is missing a value' { + Context 'When hashtable is missing a value' { + BeforeAll { $desiredValues = [PSObject] @{ String = 'a string' Bool = $true @@ -625,20 +705,25 @@ InModuleScope $ProjectName { k3 = 'v1', 'v2', 'v3' } } + } - It 'Should not throw exception' { - { $script:result = Test-DscParameterState ` - -CurrentValues $currentValues ` - -DesiredValues $desiredValues ` - -Verbose:$verbose } | Should -Not -Throw - } + It 'Should not throw exception' { + { Test-DscParameterState ` + -CurrentValues $currentValues ` + -DesiredValues $desiredValues ` + -Verbose:$verbose } | Should -Not -Throw + } - It 'Should return $false' { - $script:result | Should -BeFalse - } + It 'Should return $false' { + Test-DscParameterState ` + -CurrentValues $currentValues ` + -DesiredValues $desiredValues ` + -Verbose:$verbose | Should -BeFalse } + } - Context 'When hashtable has an additional value' { + Context 'When hashtable has an additional value' { + BeforeAll { $desiredValues = [PSObject] @{ String = 'a string' Bool = $true @@ -650,20 +735,25 @@ InModuleScope $ProjectName { k3 = 'v1', 'v2', 'v3', 99, 100 } } + } - It 'Should not throw exception' { - { $script:result = Test-DscParameterState ` - -CurrentValues $currentValues ` - -DesiredValues $desiredValues ` - -Verbose:$verbose } | Should -Not -Throw - } + It 'Should not throw exception' { + { Test-DscParameterState ` + -CurrentValues $currentValues ` + -DesiredValues $desiredValues ` + -Verbose:$verbose } | Should -Not -Throw + } - It 'Should return $false' { - $script:result | Should -BeFalse - } + It 'Should return $false' { + Test-DscParameterState ` + -CurrentValues $currentValues ` + -DesiredValues $desiredValues ` + -Verbose:$verbose | Should -BeFalse } + } - Context 'When hashtable has a different value' { + Context 'When hashtable has a different value' { + BeforeAll { $desiredValues = [PSObject] @{ String = 'a string' Bool = $true @@ -675,20 +765,25 @@ InModuleScope $ProjectName { k3 = 'xx', 'v2', 'v3', 99 } } + } - It 'Should not throw exception' { - { $script:result = Test-DscParameterState ` - -CurrentValues $currentValues ` - -DesiredValues $desiredValues ` - -Verbose:$verbose } | Should -Not -Throw - } + It 'Should not throw exception' { + { Test-DscParameterState ` + -CurrentValues $currentValues ` + -DesiredValues $desiredValues ` + -Verbose:$verbose } | Should -Not -Throw + } - It 'Should return $false' { - $script:result | Should -BeFalse - } + It 'Should return $false' { + Test-DscParameterState ` + -CurrentValues $currentValues ` + -DesiredValues $desiredValues ` + -Verbose:$verbose | Should -BeFalse } + } - Context 'When an array in hashtable has different order' { + Context 'When an array in hashtable has different order' { + BeforeAll { $desiredValues = [PSObject] @{ String = 'a string' Bool = $true @@ -700,20 +795,25 @@ InModuleScope $ProjectName { k3 = 'v3', 'v2', 'v1', 99 } } + } - It 'Should not throw exception' { - { $script:result = Test-DscParameterState ` - -CurrentValues $currentValues ` - -DesiredValues $desiredValues ` - -Verbose:$verbose } | Should -Not -Throw - } + It 'Should not throw exception' { + { Test-DscParameterState ` + -CurrentValues $currentValues ` + -DesiredValues $desiredValues ` + -Verbose:$verbose } | Should -Not -Throw + } - It 'Should return $false' { - $script:result | Should -BeFalse - } + It 'Should return $false' { + Test-DscParameterState ` + -CurrentValues $currentValues ` + -DesiredValues $desiredValues ` + -Verbose:$verbose | Should -BeFalse } + } - Context 'When an array in hashtable has different order but SortArrayValues is used' { + Context 'When an array in hashtable has different order but SortArrayValues is used' { + BeforeAll { $desiredValues = [PSObject] @{ String = 'a string' Bool = $true @@ -725,22 +825,28 @@ InModuleScope $ProjectName { k3 = 'v3', 'v2', 'v1', 99 } } + } - It 'Should not throw exception' { - { $script:result = Test-DscParameterState ` - -CurrentValues $currentValues ` - -DesiredValues $desiredValues ` - -SortArrayValues ` - -Verbose:$verbose } | Should -Not -Throw - } + It 'Should not throw exception' { + { Test-DscParameterState ` + -CurrentValues $currentValues ` + -DesiredValues $desiredValues ` + -SortArrayValues ` + -Verbose:$verbose } | Should -Not -Throw + } - It 'Should return $true' { - $script:result | Should -BeTrue - } + It 'Should return $true' { + Test-DscParameterState ` + -CurrentValues $currentValues ` + -DesiredValues $desiredValues ` + -SortArrayValues ` + -Verbose:$verbose | Should -BeTrue } + } - Context 'When hashtable has a value with a different type' { + Context 'When hashtable has a value with a different type' { + BeforeAll { $desiredValues = [PSObject] @{ String = 'a string' Bool = $true @@ -752,20 +858,25 @@ InModuleScope $ProjectName { k3 = 'v1', 'v2', 'v3', '99' } } + } - It 'Should not throw exception' { - { $script:result = Test-DscParameterState ` - -CurrentValues $currentValues ` - -DesiredValues $desiredValues ` - -Verbose:$verbose } | Should -Not -Throw - } + It 'Should not throw exception' { + { Test-DscParameterState ` + -CurrentValues $currentValues ` + -DesiredValues $desiredValues ` + -Verbose:$verbose } | Should -Not -Throw + } - It 'Should return $false' { - $script:result | Should -BeFalse - } + It 'Should return $false' { + Test-DscParameterState ` + -CurrentValues $currentValues ` + -DesiredValues $desiredValues ` + -Verbose:$verbose | Should -BeFalse } + } - Context 'When hashtable has a value with a different type but TurnOffTypeChecking is used' { + Context 'When hashtable has a value with a different type but TurnOffTypeChecking is used' { + BeforeAll { $desiredValues = [PSObject] @{ String = 'a string' Bool = $true @@ -777,22 +888,28 @@ InModuleScope $ProjectName { k3 = 'v1', 'v2', 'v3', 99 } } + } - It 'Should not throw exception' { - { $script:result = Test-DscParameterState ` - -CurrentValues $currentValues ` - -DesiredValues $desiredValues ` - -TurnOffTypeChecking ` - -Verbose:$verbose } | Should -Not -Throw - } + It 'Should not throw exception' { + { Test-DscParameterState ` + -CurrentValues $currentValues ` + -DesiredValues $desiredValues ` + -TurnOffTypeChecking ` + -Verbose:$verbose } | Should -Not -Throw + } - It 'Should return $true' { - $script:result | Should -BeTrue - } + It 'Should return $true' { + Test-DscParameterState ` + -CurrentValues $currentValues ` + -DesiredValues $desiredValues ` + -TurnOffTypeChecking ` + -Verbose:$verbose | Should -BeTrue } } + } - Context 'When reverse checking' { + Context 'When reverse checking' { + BeforeAll { $currentValues = @{ String = 'a string' Bool = $true @@ -804,8 +921,10 @@ InModuleScope $ProjectName { k3 = 'v1', 'v2', 'v3' } } + } - Context 'When even if missing property in the desired state' { + Context 'When even if missing property in the desired state' { + BeforeAll { $desiredValues = [PSObject] @{ Array = 'a', 'b', 'c', 1 Hashtable = @{ @@ -814,20 +933,25 @@ InModuleScope $ProjectName { k3 = 'v1', 'v2', 'v3' } } + } - It 'Should not throw exception' { - { $script:result = Test-DscParameterState ` - -CurrentValues $currentValues ` - -DesiredValues $desiredValues ` - -Verbose:$verbose } | Should -Not -Throw - } + It 'Should not throw exception' { + { Test-DscParameterState ` + -CurrentValues $currentValues ` + -DesiredValues $desiredValues ` + -Verbose:$verbose } | Should -Not -Throw + } - It 'Should return $true' { - $script:result | Should -BeTrue - } + It 'Should return $true' { + Test-DscParameterState ` + -CurrentValues $currentValues ` + -DesiredValues $desiredValues ` + -Verbose:$verbose | Should -BeTrue } + } - Context 'When missing property in the desired state' { + Context 'When missing property in the desired state' { + BeforeAll { $currentValues = @{ String = 'a string' Bool = $true @@ -836,58 +960,90 @@ InModuleScope $ProjectName { $desiredValues = [PSObject] @{ String = 'a string' } + } - It 'Should not throw exception' { - { $script:result = Test-DscParameterState ` - -CurrentValues $currentValues ` - -DesiredValues $desiredValues ` - -ReverseCheck ` - -Verbose:$verbose } | Should -Not -Throw - } + It 'Should not throw exception' { + { Test-DscParameterState ` + -CurrentValues $currentValues ` + -DesiredValues $desiredValues ` + -ReverseCheck ` + -Verbose:$verbose } | Should -Not -Throw + } - It 'Should return $false' { - $script:result | Should -BeFalse - } + It 'Should return $false' { + Test-DscParameterState ` + -CurrentValues $currentValues ` + -DesiredValues $desiredValues ` + -ReverseCheck ` + -Verbose:$verbose | Should -BeFalse } } + } - Context 'When testing parameter types' { - Context 'When desired value is of the wrong type' { + Context 'When testing parameter types' { + Context 'When desired value is of the wrong type' { + BeforeAll { $currentValues = @{ String = 'a string' } $desiredValues = 1, 2, 3 + } - It 'Should throw exception' { - { $script:result = Test-DscParameterState ` - -CurrentValues $currentValues ` - -DesiredValues $desiredValues ` - -Verbose:$verbose } | Should -Throw - } + It 'Should throw exception' { + { Test-DscParameterState ` + -CurrentValues $currentValues ` + -DesiredValues $desiredValues ` + -Verbose:$verbose } | Should -Throw } + } - Context 'When current value is of the wrong type' { + Context 'When current value is of the wrong type' { + BeforeAll { $currentValues = 1, 2, 3 $desiredValues = @{ String = 'a string' } + } - It 'Should throw exception' { - { $script:result = Test-DscParameterState ` - -CurrentValues $currentValues ` - -DesiredValues $desiredValues ` - -Verbose:$verbose } | Should -Throw + It 'Should throw exception' { + { Test-DscParameterState ` + -CurrentValues $currentValues ` + -DesiredValues $desiredValues ` + -Verbose:$verbose } | Should -Throw + } + } + } + + # macOS and Linux does not support CimInstance. + Context 'When testing CimInstances / hashtables' -Skip:(-not ($IsWindows -or $PSEdition -eq 'Desktop')) { + BeforeAll { + $currentValues = @{ + String = 'a string' + Bool = $true + Int = 99 + Array = 'a', 'b', 'c' + Hashtable = @{ + k1 = 'Test' + k2 = 123 + k3 = 'v1', 'v2', 'v3', 99 } + + CimInstances = [Microsoft.Management.Infrastructure.CimInstance[]] ( + ConvertTo-CimInstance -Hashtable @{ + String = 'a string' + Bool = $true + Int = 99 + Array = 'a, b, c' + } + ) } } - # macOS and Linux does not support CimInstance. - if ($isWindows -or $PSEdition -eq 'Desktop') - { - Context 'When testing CimInstances / hashtables' { - $currentValues = @{ + Context 'When everything matches' { + BeforeAll { + $desiredValues = [PSObject]@{ String = 'a string' Bool = $true Int = 99 @@ -907,227 +1063,239 @@ InModuleScope $ProjectName { } ) } + } - Context 'When everything matches' { - $desiredValues = [PSObject]@{ - String = 'a string' - Bool = $true - Int = 99 - Array = 'a', 'b', 'c' - Hashtable = @{ - k1 = 'Test' - k2 = 123 - k3 = 'v1', 'v2', 'v3', 99 - } + It 'Should not throw exception' { + { Test-DscParameterState ` + -CurrentValues $currentValues ` + -DesiredValues $desiredValues ` + -Verbose:$verbose } | Should -Not -Throw + } - CimInstances = [Microsoft.Management.Infrastructure.CimInstance[]] ( - ConvertTo-CimInstance -Hashtable @{ - String = 'a string' - Bool = $true - Int = 99 - Array = 'a, b, c' - } - ) - } + It 'Should return $true' { + Test-DscParameterState ` + -CurrentValues $currentValues ` + -DesiredValues $desiredValues ` + -Verbose:$verbose | Should -BeTrue + } + } - It 'Should not throw exception' { - { $script:result = Test-DscParameterState ` - -CurrentValues $currentValues ` - -DesiredValues $desiredValues ` - -Verbose:$verbose } | Should -Not -Throw + Context 'When CimInstances missing a value in the desired state (not recognized)' { + BeforeAll { + $desiredValues = [PSObject]@{ + String = 'a string' + Bool = $true + Int = 99 + Array = 'a', 'b', 'c' + Hashtable = @{ + k1 = 'Test' + k2 = 123 + k3 = 'v1', 'v2', 'v3', 99 } - - It 'Should return $true' { - $script:result | Should -BeTrue + CimInstances = @{ + String = 'a string' + Bool = $true + Array = 'a, b, c' } } + } - Context 'When CimInstances missing a value in the desired state (not recognized)' { - $desiredValues = [PSObject]@{ - String = 'a string' - Bool = $true - Int = 99 - Array = 'a', 'b', 'c' - Hashtable = @{ - k1 = 'Test' - k2 = 123 - k3 = 'v1', 'v2', 'v3', 99 - } - CimInstances = @{ - String = 'a string' - Bool = $true - Array = 'a, b, c' - } - } + It 'Should not throw exception' { + { Test-DscParameterState ` + -CurrentValues $currentValues ` + -DesiredValues $desiredValues ` + -Verbose:$verbose } | Should -Not -Throw + } - It 'Should not throw exception' { - { $script:result = Test-DscParameterState ` - -CurrentValues $currentValues ` - -DesiredValues $desiredValues ` - -Verbose:$verbose } | Should -Not -Throw - } + It 'Should return $true' { + Test-DscParameterState ` + -CurrentValues $currentValues ` + -DesiredValues $desiredValues ` + -Verbose:$verbose | Should -BeTrue + } + } - It 'Should return $true' { - $script:result | Should -BeTrue + Context 'When CimInstances missing a value in the desired state (recognized using ReverseCheck)' { + BeforeAll { + $desiredValues = [PSObject] @{ + String = 'a string' + Bool = $true + Int = 99 + Array = 'a', 'b', 'c' + Hashtable = @{ + k1 = 'Test' + k2 = 123 + k3 = 'v1', 'v2', 'v3', 99 + } + CimInstances = @{ + String = 'a string' + Bool = $true + Array = 'a, b, c' } } + } - Context 'When CimInstances missing a value in the desired state (recognized using ReverseCheck)' { - $desiredValues = [PSObject] @{ - String = 'a string' - Bool = $true - Int = 99 - Array = 'a', 'b', 'c' - Hashtable = @{ - k1 = 'Test' - k2 = 123 - k3 = 'v1', 'v2', 'v3', 99 - } - CimInstances = @{ - String = 'a string' - Bool = $true - Array = 'a, b, c' - } - } + It 'Should not throw exception' { + { Test-DscParameterState ` + -CurrentValues $currentValues ` + -DesiredValues $desiredValues ` + -ReverseCheck ` + -Verbose:$verbose } | Should -Not -Throw + } - It 'Should not throw exception' { - { $script:result = Test-DscParameterState ` - -CurrentValues $currentValues ` - -DesiredValues $desiredValues ` - -ReverseCheck ` - -Verbose:$verbose } | Should -Not -Throw - } + It 'Should return $false' { + Test-DscParameterState ` + -CurrentValues $currentValues ` + -DesiredValues $desiredValues ` + -ReverseCheck ` + -Verbose:$verbose | Should -BeFalse + } + } - It 'Should return $false' { - $script:result | Should -BeFalse + Context 'When CimInstances have an additional value' { + BeforeAll { + $desiredValues = [PSObject] @{ + String = 'a string' + Bool = $true + Int = 99 + Array = 'a', 'b', 'c' + Hashtable = @{ + k1 = 'Test' + k2 = 123 + k3 = 'v1', 'v2', 'v3', 99 + } + CimInstances = @{ + String = 'a string' + Bool = $true + Int = 99 + Array = 'a, b, c' + Test = 'Some string' } } + } - Context 'When CimInstances have an additional value' { - $desiredValues = [PSObject] @{ - String = 'a string' - Bool = $true - Int = 99 - Array = 'a', 'b', 'c' - Hashtable = @{ - k1 = 'Test' - k2 = 123 - k3 = 'v1', 'v2', 'v3', 99 - } - CimInstances = @{ - String = 'a string' - Bool = $true - Int = 99 - Array = 'a, b, c' - Test = 'Some string' - } - } + It 'Should not throw exception' { + { Test-DscParameterState ` + -CurrentValues $currentValues ` + -DesiredValues $desiredValues ` + -Verbose:$verbose } | Should -Not -Throw + } - It 'Should not throw exception' { - { $script:result = Test-DscParameterState ` - -CurrentValues $currentValues ` - -DesiredValues $desiredValues ` - -Verbose:$verbose } | Should -Not -Throw - } + It 'Should return $false' { + Test-DscParameterState ` + -CurrentValues $currentValues ` + -DesiredValues $desiredValues ` + -Verbose:$verbose | Should -BeFalse + } + } - It 'Should return $false' { - $script:result | Should -BeFalse + Context 'When CimInstances have a different value' { + BeforeAll { + $desiredValues = [PSObject] @{ + String = 'a string' + Bool = $true + Int = 99 + Array = 'a', 'b', 'c' + Hashtable = @{ + k1 = 'Test' + k2 = 123 + k3 = 'v1', 'v2', 'v3', 99 + } + CimInstances = @{ + String = 'some other string' + Bool = $true + Int = 99 + Array = 'a, b, c' } } + } - Context 'When CimInstances have a different value' { - $desiredValues = [PSObject] @{ - String = 'a string' - Bool = $true - Int = 99 - Array = 'a', 'b', 'c' - Hashtable = @{ - k1 = 'Test' - k2 = 123 - k3 = 'v1', 'v2', 'v3', 99 - } - CimInstances = @{ - String = 'some other string' - Bool = $true - Int = 99 - Array = 'a, b, c' - } - } + It 'Should not throw exception' { + { Test-DscParameterState ` + -CurrentValues $currentValues ` + -DesiredValues $desiredValues ` + -Verbose:$verbose } | Should -Not -Throw + } - It 'Should not throw exception' { - { $script:result = Test-DscParameterState ` - -CurrentValues $currentValues ` - -DesiredValues $desiredValues ` - -Verbose:$verbose } | Should -Not -Throw - } + It 'Should return $false' { + Test-DscParameterState ` + -CurrentValues $currentValues ` + -DesiredValues $desiredValues ` + -Verbose:$verbose | Should -BeFalse + } + } - It 'Should return $false' { - $script:result | Should -BeFalse + Context 'When CimInstances have a value with a different type' { + BeforeAll { + $desiredValues = [PSObject] @{ + String = 'a string' + Bool = $true + Int = 99 + Array = 'a', 'b', 'c' + Hashtable = @{ + k1 = 'Test' + k2 = 123 + k3 = 'v1', 'v2', 'v3', 99 + } + CimInstances = @{ + String = 'a string' + Bool = $true + Int = '99' + Array = 'a, b, c' } } + } - Context 'When CimInstances have a value with a different type' { - $desiredValues = [PSObject] @{ - String = 'a string' - Bool = $true - Int = 99 - Array = 'a', 'b', 'c' - Hashtable = @{ - k1 = 'Test' - k2 = 123 - k3 = 'v1', 'v2', 'v3', 99 - } - CimInstances = @{ - String = 'a string' - Bool = $true - Int = '99' - Array = 'a, b, c' - } - } + It 'Should not throw exception' { + { Test-DscParameterState ` + -CurrentValues $currentValues ` + -DesiredValues $desiredValues ` + -Verbose:$verbose } | Should -Not -Throw + } - It 'Should not throw exception' { - { $script:result = Test-DscParameterState ` - -CurrentValues $currentValues ` - -DesiredValues $desiredValues ` - -Verbose:$verbose } | Should -Not -Throw - } + It 'Should return $false' { + Test-DscParameterState ` + -CurrentValues $currentValues ` + -DesiredValues $desiredValues ` + -Verbose:$verbose | Should -BeFalse + } + } - It 'Should return $false' { - $script:result | Should -BeFalse + Context 'When CimInstances have a value with a different type but TurnOffTypeChecking is used' { + BeforeAll { + $desiredValues = [PSObject] @{ + String = 'a string' + Bool = $true + Int = 99 + Array = 'a', 'b', 'c' + Hashtable = @{ + k1 = 'Test' + k2 = 123 + k3 = 'v1', 'v2', 'v3', 99 } - } - - Context 'When CimInstances have a value with a different type but TurnOffTypeChecking is used' { - $desiredValues = [PSObject] @{ - String = 'a string' - Bool = $true - Int = 99 - Array = 'a', 'b', 'c' - Hashtable = @{ - k1 = 'Test' - k2 = 123 - k3 = 'v1', 'v2', 'v3', 99 - } - CimInstances = @{ - String = 'a string' - Bool = $true - Int = '99' - Array = 'a, b, c' - } + CimInstances = @{ + String = 'a string' + Bool = $true + Int = '99' + Array = 'a, b, c' } + } + } - It 'Should not throw exception' { - { $script:result = Test-DscParameterState ` - -CurrentValues $currentValues ` - -DesiredValues $desiredValues ` - -TurnOffTypeChecking ` - -Verbose:$verbose } | Should -Not -Throw - } + It 'Should not throw exception' { + { Test-DscParameterState ` + -CurrentValues $currentValues ` + -DesiredValues $desiredValues ` + -TurnOffTypeChecking ` + -Verbose:$verbose } | Should -Not -Throw + } - It 'Should return $true' { - $script:result | Should -BeTrue - } - } + It 'Should return $true' { + Test-DscParameterState ` + -CurrentValues $currentValues ` + -DesiredValues $desiredValues ` + -TurnOffTypeChecking ` + -Verbose:$verbose | Should -BeTrue } } } diff --git a/tests/Unit/Public/Test-IsNanoServer.Tests.ps1 b/tests/Unit/Public/Test-IsNanoServer.Tests.ps1 index a8dc487..1952c57 100644 --- a/tests/Unit/Public/Test-IsNanoServer.Tests.ps1 +++ b/tests/Unit/Public/Test-IsNanoServer.Tests.ps1 @@ -1,67 +1,84 @@ -$ProjectPath = "$PSScriptRoot\..\..\.." | Convert-Path -$ProjectName = ((Get-ChildItem -Path $ProjectPath\*\*.psd1).Where{ - ($_.Directory.Name -match 'source|src' -or $_.Directory.Name -eq $_.BaseName) -and - $(try - { - Test-ModuleManifest $_.FullName -ErrorAction Stop - } - catch - { - $false - } ) - }).BaseName +BeforeAll { + $moduleName = 'DscResource.Common' + $stubModuleName = 'DscResource.Common.Stubs' -Import-Module $ProjectName -Force + Remove-Module -Name $moduleName -Force -ErrorAction 'SilentlyContinue' -Describe 'Test-IsNanoServer' -Tag TestIsNanoServer { - function Get-CimInstance - { - param - ( - [Parameter()] - [System.String] - $ClassName - ) - } + Get-Module -Name $moduleName -ListAvailable | + Select-Object -First 1 | + Import-Module -Force -ErrorAction 'Stop' + <# + This mocks the Get-CimInstance on platforms where the cmdlet does not + exist, like Linux anc macOS. + #> + Remove-Module -Name $stubModuleName -Force -ErrorAction 'SilentlyContinue' + New-Module -Name $stubModuleName -ScriptBlock { + function Get-CimInstance + { + param + ( + [Parameter()] + [System.String] + $ClassName + ) + } + } | Import-Module +} + +AfterAll { + <# + This removes the stub module that was imported in the + BeforeAll-block. + #> + Remove-Module -Name 'DscResource.Common.Stubs' -Force -ErrorAction 'SilentlyContinue' +} + +Describe 'Test-IsNanoServer' -Tag 'TestIsNanoServer' { Context 'When the current computer is a Datacenter Nano server' { - Mock -CommandName Get-CimInstance ` - -ModuleName $ProjectName ` - -MockWith { - [PSCustomObject] @{ - OperatingSystemSKU = 143 + BeforeAll { + Mock -CommandName Get-CimInstance ` + -ModuleName 'DscResource.Common' ` + -MockWith { + [PSCustomObject] @{ + OperatingSystemSKU = 143 + } } - } + } - It 'Should retrun true' { + It 'Should return true' { Test-IsNanoServer -Verbose | Should -BeTrue } } Context 'When the current computer is a Standard Nano server' { - Mock -CommandName Get-CimInstance ` - -ModuleName $ProjectName ` - -MockWith { - [PSCustomObject] @{ - OperatingSystemSKU = 144 + BeforeAll { + Mock -CommandName Get-CimInstance ` + -ModuleName 'DscResource.Common' ` + -MockWith { + [PSCustomObject] @{ + OperatingSystemSKU = 144 + } } - } + } - It 'Should retrun true' { + It 'Should return true' { Test-IsNanoServer -Verbose | Should -BeTrue } } Context 'When the current computer is not a Nano server' { - Mock -CommandName Get-CimInstance ` - -ModuleName $ProjectName ` - -MockWith { - [PSCustomObject] @{ - OperatingSystemSKU = 1 + BeforeAll { + Mock -CommandName Get-CimInstance ` + -ModuleName 'DscResource.Common' ` + -MockWith { + [PSCustomObject] @{ + OperatingSystemSKU = 1 + } } - } + } - It 'Should retrun false' { + It 'Should return false' { Test-IsNanoServer -Verbose | Should -BeFalse } }