From fbd65c32cd98de1c45549c77cdf8870ddb5ba091 Mon Sep 17 00:00:00 2001 From: Johan Ljunggren Date: Mon, 18 Jul 2022 21:15:20 +0200 Subject: [PATCH] Fix missing tasks module (#78) --- CHANGELOG.md | 2 + RequiredModules.psd1 | 1 + build.yaml | 3 ++ tests/QA/module.tests.ps1 | 52 ++++++++++--------- .../Test-DscObjectHasProperty.Tests.ps1 | 18 +++++-- .../Private/Test-DscPropertyState.Tests.ps1 | 18 +++++-- .../Public/Assert-BoundParameter.Tests.ps1 | 18 +++++-- tests/Unit/Public/Assert-IPAddress.Tests.ps1 | 19 +++++-- tests/Unit/Public/Assert-Module.Tests.ps1 | 16 ++++-- .../Compare-DscParameterState.Tests.ps1 | 24 +++++++-- .../Compare-ResourcePropertyState.Tests.ps1 | 24 +++++++-- .../ConvertFrom-DscResourceInstance.Tests.ps1 | 24 +++++++-- .../Public/ConvertTo-CimInstance.Tests.ps1 | 24 +++++++-- .../Unit/Public/ConvertTo-Hashtable.Tests.ps1 | 24 +++++++-- tests/Unit/Public/Get-ComputerName.Tests.ps1 | 24 +++++++-- tests/Unit/Public/Get-LocalizedData.Tests.ps1 | 24 +++++++-- .../Unit/Public/Get-TemporaryFolder.Tests.ps1 | 24 +++++++-- .../New-InvalidArgumentException.Tests.ps1 | 24 +++++++-- .../Public/New-InvalidDataException.Tests.ps1 | 24 +++++++-- .../New-InvalidOperationException.Tests.ps1 | 24 +++++++-- .../New-InvalidResultException.Tests.ps1 | 24 +++++++-- .../New-NotImplementedException.Tests.ps1 | 24 +++++++-- .../New-ObjectNotFoundException.Tests.ps1 | 24 +++++++-- .../Public/Remove-CommonParameter.Tests.ps1 | 24 +++++++-- .../Public/Test-DscParameterState.Tests.ps1 | 24 +++++++-- tests/Unit/Public/Test-IsNanoServer.Tests.ps1 | 26 +++++++--- 26 files changed, 441 insertions(+), 116 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 644df5d..20ca6c5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - DscResource.Common - Update pipeline files to the latest in Sampler. + - Fix missing tasks module. + - Update unit tests to import and remove the module being tested. ### Fixed diff --git a/RequiredModules.psd1 b/RequiredModules.psd1 index d1bf087..0c5d3c0 100644 --- a/RequiredModules.psd1 +++ b/RequiredModules.psd1 @@ -19,4 +19,5 @@ 'DscResource.AnalyzerRules' = 'latest' xDscResourceDesigner = 'latest' 'DscResource.Test' = 'latest' + 'DscResource.DocGenerator' = 'latest' } diff --git a/build.yaml b/build.yaml index d6d5a5d..12c63c0 100644 --- a/build.yaml +++ b/build.yaml @@ -22,6 +22,7 @@ BuildWorkflow: - Build_Module_ModuleBuilder - Build_NestedModules_ModuleBuilder - Create_changelog_release_output + - Generate_Wiki_Content pack: - build @@ -107,6 +108,8 @@ ModuleBuildTasks: - '*.build.Sampler.ib.tasks' Sampler.GitHubTasks: - '*.ib.tasks' + DscResource.DocGenerator: + - 'Task.*' DscResource.Test: - 'Task.*' diff --git a/tests/QA/module.tests.ps1 b/tests/QA/module.tests.ps1 index f736563..d356085 100644 --- a/tests/QA/module.tests.ps1 +++ b/tests/QA/module.tests.ps1 @@ -32,35 +32,39 @@ BeforeAll { ).Directory.FullName } - Describe 'Changelog Management' -Tag 'Changelog' { - It 'Changelog has been updated' -skip:( - !([bool](Get-Command git -EA SilentlyContinue) -and - [bool](&(Get-Process -id $PID).Path -NoProfile -Command 'git rev-parse --is-inside-work-tree 2>$null')) - ) { - # Get the list of changed files compared with branch main - $HeadCommit = &git rev-parse HEAD - $defaultBranchCommit = &git rev-parse origin/main - $filesChanged = &git @('diff', "$defaultBranchCommit...$HeadCommit", '--name-only') - $filesStagedAndUnstaged = &git @('diff', "HEAD", '--name-only') - - $filesChanged += $filesStagedAndUnstaged - - # Only check if there are any changed files. - if ($filesChanged) - { - $filesChanged | Should -Contain 'CHANGELOG.md' -Because 'the CHANGELOG.md must be updated with at least one entry in the Unreleased section for each PR' - } - } +AfterAll { + Get-Module -Name 'DscResource.Common' -All | Remove-Module -Force +} - It 'Changelog format compliant with keepachangelog format' -skip:(![bool](Get-Command git -EA SilentlyContinue)) { - { Get-ChangelogData (Join-Path $ProjectPath 'CHANGELOG.md') -ErrorAction Stop } | Should -Not -Throw +Describe 'Changelog Management' -Tag 'Changelog' { + It 'Changelog has been updated' -skip:( + !([bool](Get-Command git -EA SilentlyContinue) -and + [bool](&(Get-Process -id $PID).Path -NoProfile -Command 'git rev-parse --is-inside-work-tree 2>$null')) + ) { + # Get the list of changed files compared with branch main + $HeadCommit = &git rev-parse HEAD + $defaultBranchCommit = &git rev-parse origin/main + $filesChanged = &git @('diff', "$defaultBranchCommit...$HeadCommit", '--name-only') + $filesStagedAndUnstaged = &git @('diff', "HEAD", '--name-only') + + $filesChanged += $filesStagedAndUnstaged + + # Only check if there are any changed files. + if ($filesChanged) + { + $filesChanged | Should -Contain 'CHANGELOG.md' -Because 'the CHANGELOG.md must be updated with at least one entry in the Unreleased section for each PR' } + } - It 'Changelog should have an Unreleased header' -Skip:$skipTest { - (Get-ChangelogData -Path (Join-Path -Path $ProjectPath -ChildPath 'CHANGELOG.md') -ErrorAction 'Stop').Unreleased.RawData | Should -Not -BeNullOrEmpty - } + It 'Changelog format compliant with keepachangelog format' -skip:(![bool](Get-Command git -EA SilentlyContinue)) { + { Get-ChangelogData (Join-Path $ProjectPath 'CHANGELOG.md') -ErrorAction Stop } | Should -Not -Throw } + It 'Changelog should have an Unreleased header' -Skip:$skipTest { + (Get-ChangelogData -Path (Join-Path -Path $ProjectPath -ChildPath 'CHANGELOG.md') -ErrorAction 'Stop').Unreleased.RawData | Should -Not -BeNullOrEmpty + } +} + Describe 'General module control' -Tags 'FunctionalQuality' { It 'Should import without errors' { { Import-Module -Name $script:moduleName -Force -ErrorAction Stop } | Should -Not -Throw diff --git a/tests/Unit/Private/Test-DscObjectHasProperty.Tests.ps1 b/tests/Unit/Private/Test-DscObjectHasProperty.Tests.ps1 index 78af8f2..693f161 100644 --- a/tests/Unit/Private/Test-DscObjectHasProperty.Tests.ps1 +++ b/tests/Unit/Private/Test-DscObjectHasProperty.Tests.ps1 @@ -1,17 +1,27 @@ BeforeAll { $script:moduleName = 'DscResource.Common' - Remove-Module -Name $script:moduleName -Force -ErrorAction 'SilentlyContinue' + # If the module is not found, run the build task 'noop'. + if (-not (Get-Module -Name $script:moduleName -ListAvailable)) + { + # Redirect all streams to $null, except the error stream (stream 2) + & "$PSScriptRoot/../../build.ps1" -Tasks 'noop' 2>&1 4>&1 5>&1 6>&1 > $null + } - Get-Module -Name $script:moduleName -ListAvailable | - Select-Object -First 1 | - Import-Module -Force -ErrorAction 'Stop' + # Re-import the module using force to get any code changes between runs. + Import-Module -Name $script:moduleName -Force -ErrorAction 'Stop' $PSDefaultParameterValues['InModuleScope:ModuleName'] = $script:moduleName + $PSDefaultParameterValues['Mock:ModuleName'] = $script:moduleName + $PSDefaultParameterValues['Should:ModuleName'] = $script:moduleName } AfterAll { + $PSDefaultParameterValues.Remove('Mock:ModuleName') $PSDefaultParameterValues.Remove('InModuleScope:ModuleName') + $PSDefaultParameterValues.Remove('Should:ModuleName') + + Remove-Module -Name $script:moduleName } Describe 'Test-DscObjectHasProperty' { diff --git a/tests/Unit/Private/Test-DscPropertyState.Tests.ps1 b/tests/Unit/Private/Test-DscPropertyState.Tests.ps1 index d0bbf11..ae0df14 100644 --- a/tests/Unit/Private/Test-DscPropertyState.Tests.ps1 +++ b/tests/Unit/Private/Test-DscPropertyState.Tests.ps1 @@ -1,17 +1,27 @@ BeforeAll { $script:moduleName = 'DscResource.Common' - Remove-Module -Name $script:moduleName -Force -ErrorAction 'SilentlyContinue' + # If the module is not found, run the build task 'noop'. + if (-not (Get-Module -Name $script:moduleName -ListAvailable)) + { + # Redirect all streams to $null, except the error stream (stream 2) + & "$PSScriptRoot/../../build.ps1" -Tasks 'noop' 2>&1 4>&1 5>&1 6>&1 > $null + } - Get-Module -Name $script:moduleName -ListAvailable | - Select-Object -First 1 | - Import-Module -Force -ErrorAction 'Stop' + # Re-import the module using force to get any code changes between runs. + Import-Module -Name $script:moduleName -Force -ErrorAction 'Stop' $PSDefaultParameterValues['InModuleScope:ModuleName'] = $script:moduleName + $PSDefaultParameterValues['Mock:ModuleName'] = $script:moduleName + $PSDefaultParameterValues['Should:ModuleName'] = $script:moduleName } AfterAll { + $PSDefaultParameterValues.Remove('Mock:ModuleName') $PSDefaultParameterValues.Remove('InModuleScope:ModuleName') + $PSDefaultParameterValues.Remove('Should:ModuleName') + + Remove-Module -Name $script:moduleName } Describe 'Test-DscPropertyState' -Tag 'TestDscPropertyState' { diff --git a/tests/Unit/Public/Assert-BoundParameter.Tests.ps1 b/tests/Unit/Public/Assert-BoundParameter.Tests.ps1 index abe3e9a..0bd50d2 100644 --- a/tests/Unit/Public/Assert-BoundParameter.Tests.ps1 +++ b/tests/Unit/Public/Assert-BoundParameter.Tests.ps1 @@ -1,17 +1,27 @@ BeforeAll { $script:moduleName = 'DscResource.Common' - Remove-Module -Name $script:moduleName -Force -ErrorAction 'SilentlyContinue' + # If the module is not found, run the build task 'noop'. + if (-not (Get-Module -Name $script:moduleName -ListAvailable)) + { + # Redirect all streams to $null, except the error stream (stream 2) + & "$PSScriptRoot/../../build.ps1" -Tasks 'noop' 2>&1 4>&1 5>&1 6>&1 > $null + } - Get-Module -Name $script:moduleName -ListAvailable | - Select-Object -First 1 | - Import-Module -Force -ErrorAction 'Stop' + # Re-import the module using force to get any code changes between runs. + Import-Module -Name $script:moduleName -Force -ErrorAction 'Stop' $PSDefaultParameterValues['InModuleScope:ModuleName'] = $script:moduleName + $PSDefaultParameterValues['Mock:ModuleName'] = $script:moduleName + $PSDefaultParameterValues['Should:ModuleName'] = $script:moduleName } AfterAll { + $PSDefaultParameterValues.Remove('Mock:ModuleName') $PSDefaultParameterValues.Remove('InModuleScope:ModuleName') + $PSDefaultParameterValues.Remove('Should:ModuleName') + + Remove-Module -Name $script:moduleName } Describe 'Assert-BoundParameter' -Tag 'AssertBoundParameter' { diff --git a/tests/Unit/Public/Assert-IPAddress.Tests.ps1 b/tests/Unit/Public/Assert-IPAddress.Tests.ps1 index 42d5cbb..442d1fe 100644 --- a/tests/Unit/Public/Assert-IPAddress.Tests.ps1 +++ b/tests/Unit/Public/Assert-IPAddress.Tests.ps1 @@ -1,19 +1,28 @@ BeforeAll { $script:moduleName = 'DscResource.Common' - Remove-Module -Name $script:moduleName -Force -ErrorAction 'SilentlyContinue' + # If the module is not found, run the build task 'noop'. + if (-not (Get-Module -Name $script:moduleName -ListAvailable)) + { + # Redirect all streams to $null, except the error stream (stream 2) + & "$PSScriptRoot/../../build.ps1" -Tasks 'noop' 2>&1 4>&1 5>&1 6>&1 > $null + } - Get-Module -Name $script:moduleName -ListAvailable | - Select-Object -First 1 | - Import-Module -Force -ErrorAction 'Stop' + # Re-import the module using force to get any code changes between runs. + Import-Module -Name $script:moduleName -Force -ErrorAction 'Stop' $PSDefaultParameterValues['InModuleScope:ModuleName'] = $script:moduleName + $PSDefaultParameterValues['Mock:ModuleName'] = $script:moduleName + $PSDefaultParameterValues['Should:ModuleName'] = $script:moduleName } AfterAll { + $PSDefaultParameterValues.Remove('Mock:ModuleName') $PSDefaultParameterValues.Remove('InModuleScope:ModuleName') -} + $PSDefaultParameterValues.Remove('Should:ModuleName') + Remove-Module -Name $script:moduleName +} Describe 'Assert-IPAddress' -Tag 'AssertIPAddress' { Context 'When invoking with valid IPv4 Address' { diff --git a/tests/Unit/Public/Assert-Module.Tests.ps1 b/tests/Unit/Public/Assert-Module.Tests.ps1 index 074d290..2e053e5 100644 --- a/tests/Unit/Public/Assert-Module.Tests.ps1 +++ b/tests/Unit/Public/Assert-Module.Tests.ps1 @@ -1,11 +1,15 @@ BeforeAll { $script:moduleName = 'DscResource.Common' - Remove-Module -Name $script:moduleName -Force -ErrorAction 'SilentlyContinue' + # If the module is not found, run the build task 'noop'. + if (-not (Get-Module -Name $script:moduleName -ListAvailable)) + { + # Redirect all streams to $null, except the error stream (stream 2) + & "$PSScriptRoot/../../build.ps1" -Tasks 'noop' 2>&1 4>&1 5>&1 6>&1 > $null + } - Get-Module -Name $script:moduleName -ListAvailable | - Select-Object -First 1 | - Import-Module -Force -ErrorAction 'Stop' + # Re-import the module using force to get any code changes between runs. + Import-Module -Name $script:moduleName -Force -ErrorAction 'Stop' $PSDefaultParameterValues['InModuleScope:ModuleName'] = $script:moduleName $PSDefaultParameterValues['Mock:ModuleName'] = $script:moduleName @@ -13,9 +17,11 @@ BeforeAll { } AfterAll { - $PSDefaultParameterValues.Remove('InModuleScope:ModuleName') $PSDefaultParameterValues.Remove('Mock:ModuleName') + $PSDefaultParameterValues.Remove('InModuleScope:ModuleName') $PSDefaultParameterValues.Remove('Should:ModuleName') + + Remove-Module -Name $script:moduleName } Describe 'Assert-Module' { diff --git a/tests/Unit/Public/Compare-DscParameterState.Tests.ps1 b/tests/Unit/Public/Compare-DscParameterState.Tests.ps1 index 6b1692d..50ce7a9 100644 --- a/tests/Unit/Public/Compare-DscParameterState.Tests.ps1 +++ b/tests/Unit/Public/Compare-DscParameterState.Tests.ps1 @@ -1,11 +1,27 @@ BeforeAll { $script:moduleName = 'DscResource.Common' - Remove-Module -Name $script:moduleName -Force -ErrorAction 'SilentlyContinue' + # If the module is not found, run the build task 'noop'. + if (-not (Get-Module -Name $script:moduleName -ListAvailable)) + { + # Redirect all streams to $null, except the error stream (stream 2) + & "$PSScriptRoot/../../build.ps1" -Tasks 'noop' 2>&1 4>&1 5>&1 6>&1 > $null + } + + # Re-import the module using force to get any code changes between runs. + Import-Module -Name $script:moduleName -Force -ErrorAction 'Stop' + + $PSDefaultParameterValues['InModuleScope:ModuleName'] = $script:moduleName + $PSDefaultParameterValues['Mock:ModuleName'] = $script:moduleName + $PSDefaultParameterValues['Should:ModuleName'] = $script:moduleName +} + +AfterAll { + $PSDefaultParameterValues.Remove('Mock:ModuleName') + $PSDefaultParameterValues.Remove('InModuleScope:ModuleName') + $PSDefaultParameterValues.Remove('Should:ModuleName') - Get-Module -Name $script:moduleName -ListAvailable | - Select-Object -First 1 | - Import-Module -Force -ErrorAction 'Stop' + Remove-Module -Name $script:moduleName } Describe 'ComputerManagementDsc.Common\Compare-DscParameterState' { diff --git a/tests/Unit/Public/Compare-ResourcePropertyState.Tests.ps1 b/tests/Unit/Public/Compare-ResourcePropertyState.Tests.ps1 index 9e0f336..60d3cd7 100644 --- a/tests/Unit/Public/Compare-ResourcePropertyState.Tests.ps1 +++ b/tests/Unit/Public/Compare-ResourcePropertyState.Tests.ps1 @@ -1,11 +1,27 @@ BeforeAll { $script:moduleName = 'DscResource.Common' - Remove-Module -Name $script:moduleName -Force -ErrorAction 'SilentlyContinue' + # If the module is not found, run the build task 'noop'. + if (-not (Get-Module -Name $script:moduleName -ListAvailable)) + { + # Redirect all streams to $null, except the error stream (stream 2) + & "$PSScriptRoot/../../build.ps1" -Tasks 'noop' 2>&1 4>&1 5>&1 6>&1 > $null + } + + # Re-import the module using force to get any code changes between runs. + Import-Module -Name $script:moduleName -Force -ErrorAction 'Stop' + + $PSDefaultParameterValues['InModuleScope:ModuleName'] = $script:moduleName + $PSDefaultParameterValues['Mock:ModuleName'] = $script:moduleName + $PSDefaultParameterValues['Should:ModuleName'] = $script:moduleName +} + +AfterAll { + $PSDefaultParameterValues.Remove('Mock:ModuleName') + $PSDefaultParameterValues.Remove('InModuleScope:ModuleName') + $PSDefaultParameterValues.Remove('Should:ModuleName') - Get-Module -Name $script:moduleName -ListAvailable | - Select-Object -First 1 | - Import-Module -Force -ErrorAction 'Stop' + Remove-Module -Name $script:moduleName } Describe 'Compare-ResourcePropertyState' -Tag 'CompareResourcePropertyState' { diff --git a/tests/Unit/Public/ConvertFrom-DscResourceInstance.Tests.ps1 b/tests/Unit/Public/ConvertFrom-DscResourceInstance.Tests.ps1 index cc25c60..fe49acf 100644 --- a/tests/Unit/Public/ConvertFrom-DscResourceInstance.Tests.ps1 +++ b/tests/Unit/Public/ConvertFrom-DscResourceInstance.Tests.ps1 @@ -1,11 +1,27 @@ BeforeAll { $script:moduleName = 'DscResource.Common' - Remove-Module -Name $script:moduleName -Force -ErrorAction 'SilentlyContinue' + # If the module is not found, run the build task 'noop'. + if (-not (Get-Module -Name $script:moduleName -ListAvailable)) + { + # Redirect all streams to $null, except the error stream (stream 2) + & "$PSScriptRoot/../../build.ps1" -Tasks 'noop' 2>&1 4>&1 5>&1 6>&1 > $null + } + + # Re-import the module using force to get any code changes between runs. + Import-Module -Name $script:moduleName -Force -ErrorAction 'Stop' + + $PSDefaultParameterValues['InModuleScope:ModuleName'] = $script:moduleName + $PSDefaultParameterValues['Mock:ModuleName'] = $script:moduleName + $PSDefaultParameterValues['Should:ModuleName'] = $script:moduleName +} + +AfterAll { + $PSDefaultParameterValues.Remove('Mock:ModuleName') + $PSDefaultParameterValues.Remove('InModuleScope:ModuleName') + $PSDefaultParameterValues.Remove('Should:ModuleName') - Get-Module -Name $script:moduleName -ListAvailable | - Select-Object -First 1 | - Import-Module -Force -ErrorAction 'Stop' + Remove-Module -Name $script:moduleName } Describe 'ConvertFrom-DscResourceInstance' { diff --git a/tests/Unit/Public/ConvertTo-CimInstance.Tests.ps1 b/tests/Unit/Public/ConvertTo-CimInstance.Tests.ps1 index 3777792..143f4d5 100644 --- a/tests/Unit/Public/ConvertTo-CimInstance.Tests.ps1 +++ b/tests/Unit/Public/ConvertTo-CimInstance.Tests.ps1 @@ -1,11 +1,27 @@ BeforeAll { $script:moduleName = 'DscResource.Common' - Remove-Module -Name $script:moduleName -Force -ErrorAction 'SilentlyContinue' + # If the module is not found, run the build task 'noop'. + if (-not (Get-Module -Name $script:moduleName -ListAvailable)) + { + # Redirect all streams to $null, except the error stream (stream 2) + & "$PSScriptRoot/../../build.ps1" -Tasks 'noop' 2>&1 4>&1 5>&1 6>&1 > $null + } + + # Re-import the module using force to get any code changes between runs. + Import-Module -Name $script:moduleName -Force -ErrorAction 'Stop' + + $PSDefaultParameterValues['InModuleScope:ModuleName'] = $script:moduleName + $PSDefaultParameterValues['Mock:ModuleName'] = $script:moduleName + $PSDefaultParameterValues['Should:ModuleName'] = $script:moduleName +} + +AfterAll { + $PSDefaultParameterValues.Remove('Mock:ModuleName') + $PSDefaultParameterValues.Remove('InModuleScope:ModuleName') + $PSDefaultParameterValues.Remove('Should:ModuleName') - Get-Module -Name $script:moduleName -ListAvailable | - Select-Object -First 1 | - Import-Module -Force -ErrorAction 'Stop' + Remove-Module -Name $script:moduleName } # macOS and Linux does not support CimInstance. diff --git a/tests/Unit/Public/ConvertTo-Hashtable.Tests.ps1 b/tests/Unit/Public/ConvertTo-Hashtable.Tests.ps1 index c6a4d81..1ed47dd 100644 --- a/tests/Unit/Public/ConvertTo-Hashtable.Tests.ps1 +++ b/tests/Unit/Public/ConvertTo-Hashtable.Tests.ps1 @@ -1,11 +1,27 @@ BeforeAll { $script:moduleName = 'DscResource.Common' - Remove-Module -Name $script:moduleName -Force -ErrorAction 'SilentlyContinue' + # If the module is not found, run the build task 'noop'. + if (-not (Get-Module -Name $script:moduleName -ListAvailable)) + { + # Redirect all streams to $null, except the error stream (stream 2) + & "$PSScriptRoot/../../build.ps1" -Tasks 'noop' 2>&1 4>&1 5>&1 6>&1 > $null + } + + # Re-import the module using force to get any code changes between runs. + Import-Module -Name $script:moduleName -Force -ErrorAction 'Stop' + + $PSDefaultParameterValues['InModuleScope:ModuleName'] = $script:moduleName + $PSDefaultParameterValues['Mock:ModuleName'] = $script:moduleName + $PSDefaultParameterValues['Should:ModuleName'] = $script:moduleName +} + +AfterAll { + $PSDefaultParameterValues.Remove('Mock:ModuleName') + $PSDefaultParameterValues.Remove('InModuleScope:ModuleName') + $PSDefaultParameterValues.Remove('Should:ModuleName') - Get-Module -Name $script:moduleName -ListAvailable | - Select-Object -First 1 | - Import-Module -Force -ErrorAction 'Stop' + Remove-Module -Name $script:moduleName } # macOS and Linux does not support CimInstance. diff --git a/tests/Unit/Public/Get-ComputerName.Tests.ps1 b/tests/Unit/Public/Get-ComputerName.Tests.ps1 index 04b0d43..13ebd15 100644 --- a/tests/Unit/Public/Get-ComputerName.Tests.ps1 +++ b/tests/Unit/Public/Get-ComputerName.Tests.ps1 @@ -1,11 +1,27 @@ BeforeAll { $script:moduleName = 'DscResource.Common' - Remove-Module -Name $script:moduleName -Force -ErrorAction 'SilentlyContinue' + # If the module is not found, run the build task 'noop'. + if (-not (Get-Module -Name $script:moduleName -ListAvailable)) + { + # Redirect all streams to $null, except the error stream (stream 2) + & "$PSScriptRoot/../../build.ps1" -Tasks 'noop' 2>&1 4>&1 5>&1 6>&1 > $null + } + + # Re-import the module using force to get any code changes between runs. + Import-Module -Name $script:moduleName -Force -ErrorAction 'Stop' + + $PSDefaultParameterValues['InModuleScope:ModuleName'] = $script:moduleName + $PSDefaultParameterValues['Mock:ModuleName'] = $script:moduleName + $PSDefaultParameterValues['Should:ModuleName'] = $script:moduleName +} + +AfterAll { + $PSDefaultParameterValues.Remove('Mock:ModuleName') + $PSDefaultParameterValues.Remove('InModuleScope:ModuleName') + $PSDefaultParameterValues.Remove('Should:ModuleName') - Get-Module -Name $script:moduleName -ListAvailable | - Select-Object -First 1 | - Import-Module -Force -ErrorAction 'Stop' + Remove-Module -Name $script:moduleName } Describe 'Get-ComputerName' { diff --git a/tests/Unit/Public/Get-LocalizedData.Tests.ps1 b/tests/Unit/Public/Get-LocalizedData.Tests.ps1 index ddb9d26..d31fff2 100644 --- a/tests/Unit/Public/Get-LocalizedData.Tests.ps1 +++ b/tests/Unit/Public/Get-LocalizedData.Tests.ps1 @@ -1,11 +1,27 @@ BeforeAll { $script:moduleName = 'DscResource.Common' - Remove-Module -Name $script:moduleName -Force -ErrorAction 'SilentlyContinue' + # If the module is not found, run the build task 'noop'. + if (-not (Get-Module -Name $script:moduleName -ListAvailable)) + { + # Redirect all streams to $null, except the error stream (stream 2) + & "$PSScriptRoot/../../build.ps1" -Tasks 'noop' 2>&1 4>&1 5>&1 6>&1 > $null + } + + # Re-import the module using force to get any code changes between runs. + Import-Module -Name $script:moduleName -Force -ErrorAction 'Stop' + + $PSDefaultParameterValues['InModuleScope:ModuleName'] = $script:moduleName + $PSDefaultParameterValues['Mock:ModuleName'] = $script:moduleName + $PSDefaultParameterValues['Should:ModuleName'] = $script:moduleName +} + +AfterAll { + $PSDefaultParameterValues.Remove('Mock:ModuleName') + $PSDefaultParameterValues.Remove('InModuleScope:ModuleName') + $PSDefaultParameterValues.Remove('Should:ModuleName') - Get-Module -Name $script:moduleName -ListAvailable | - Select-Object -First 1 | - Import-Module -Force -ErrorAction 'Stop' + Remove-Module -Name $script:moduleName } Describe 'Get-LocalizedData' -Tag 'GetLocalizedData' { diff --git a/tests/Unit/Public/Get-TemporaryFolder.Tests.ps1 b/tests/Unit/Public/Get-TemporaryFolder.Tests.ps1 index 4ea7245..bf9a29c 100644 --- a/tests/Unit/Public/Get-TemporaryFolder.Tests.ps1 +++ b/tests/Unit/Public/Get-TemporaryFolder.Tests.ps1 @@ -1,11 +1,27 @@ BeforeAll { $script:moduleName = 'DscResource.Common' - Remove-Module -Name $script:moduleName -Force -ErrorAction 'SilentlyContinue' + # If the module is not found, run the build task 'noop'. + if (-not (Get-Module -Name $script:moduleName -ListAvailable)) + { + # Redirect all streams to $null, except the error stream (stream 2) + & "$PSScriptRoot/../../build.ps1" -Tasks 'noop' 2>&1 4>&1 5>&1 6>&1 > $null + } + + # Re-import the module using force to get any code changes between runs. + Import-Module -Name $script:moduleName -Force -ErrorAction 'Stop' + + $PSDefaultParameterValues['InModuleScope:ModuleName'] = $script:moduleName + $PSDefaultParameterValues['Mock:ModuleName'] = $script:moduleName + $PSDefaultParameterValues['Should:ModuleName'] = $script:moduleName +} + +AfterAll { + $PSDefaultParameterValues.Remove('Mock:ModuleName') + $PSDefaultParameterValues.Remove('InModuleScope:ModuleName') + $PSDefaultParameterValues.Remove('Should:ModuleName') - Get-Module -Name $script:moduleName -ListAvailable | - Select-Object -First 1 | - Import-Module -Force -ErrorAction 'Stop' + Remove-Module -Name $script:moduleName } Describe 'Get-TemporaryFolder' -Tag 'GetTemporaryFolder' { diff --git a/tests/Unit/Public/New-InvalidArgumentException.Tests.ps1 b/tests/Unit/Public/New-InvalidArgumentException.Tests.ps1 index af110ba..6d3c05b 100644 --- a/tests/Unit/Public/New-InvalidArgumentException.Tests.ps1 +++ b/tests/Unit/Public/New-InvalidArgumentException.Tests.ps1 @@ -1,11 +1,27 @@ BeforeAll { $script:moduleName = 'DscResource.Common' - Remove-Module -Name $script:moduleName -Force -ErrorAction 'SilentlyContinue' + # If the module is not found, run the build task 'noop'. + if (-not (Get-Module -Name $script:moduleName -ListAvailable)) + { + # Redirect all streams to $null, except the error stream (stream 2) + & "$PSScriptRoot/../../build.ps1" -Tasks 'noop' 2>&1 4>&1 5>&1 6>&1 > $null + } + + # Re-import the module using force to get any code changes between runs. + Import-Module -Name $script:moduleName -Force -ErrorAction 'Stop' + + $PSDefaultParameterValues['InModuleScope:ModuleName'] = $script:moduleName + $PSDefaultParameterValues['Mock:ModuleName'] = $script:moduleName + $PSDefaultParameterValues['Should:ModuleName'] = $script:moduleName +} + +AfterAll { + $PSDefaultParameterValues.Remove('Mock:ModuleName') + $PSDefaultParameterValues.Remove('InModuleScope:ModuleName') + $PSDefaultParameterValues.Remove('Should:ModuleName') - Get-Module -Name $script:moduleName -ListAvailable | - Select-Object -First 1 | - Import-Module -Force -ErrorAction 'Stop' + Remove-Module -Name $script:moduleName } Describe 'New-InvalidArgumentException' { diff --git a/tests/Unit/Public/New-InvalidDataException.Tests.ps1 b/tests/Unit/Public/New-InvalidDataException.Tests.ps1 index c7e14a7..bcf8576 100644 --- a/tests/Unit/Public/New-InvalidDataException.Tests.ps1 +++ b/tests/Unit/Public/New-InvalidDataException.Tests.ps1 @@ -1,11 +1,27 @@ BeforeAll { $script:moduleName = 'DscResource.Common' - Remove-Module -Name $script:moduleName -Force -ErrorAction 'SilentlyContinue' + # If the module is not found, run the build task 'noop'. + if (-not (Get-Module -Name $script:moduleName -ListAvailable)) + { + # Redirect all streams to $null, except the error stream (stream 2) + & "$PSScriptRoot/../../build.ps1" -Tasks 'noop' 2>&1 4>&1 5>&1 6>&1 > $null + } + + # Re-import the module using force to get any code changes between runs. + Import-Module -Name $script:moduleName -Force -ErrorAction 'Stop' + + $PSDefaultParameterValues['InModuleScope:ModuleName'] = $script:moduleName + $PSDefaultParameterValues['Mock:ModuleName'] = $script:moduleName + $PSDefaultParameterValues['Should:ModuleName'] = $script:moduleName +} + +AfterAll { + $PSDefaultParameterValues.Remove('Mock:ModuleName') + $PSDefaultParameterValues.Remove('InModuleScope:ModuleName') + $PSDefaultParameterValues.Remove('Should:ModuleName') - Get-Module -Name $script:moduleName -ListAvailable | - Select-Object -First 1 | - Import-Module -Force -ErrorAction 'Stop' + Remove-Module -Name $script:moduleName } Describe 'New-InvalidDataException' { diff --git a/tests/Unit/Public/New-InvalidOperationException.Tests.ps1 b/tests/Unit/Public/New-InvalidOperationException.Tests.ps1 index 8d1bc91..6f50bf0 100644 --- a/tests/Unit/Public/New-InvalidOperationException.Tests.ps1 +++ b/tests/Unit/Public/New-InvalidOperationException.Tests.ps1 @@ -1,11 +1,27 @@ BeforeAll { $script:moduleName = 'DscResource.Common' - Remove-Module -Name $script:moduleName -Force -ErrorAction 'SilentlyContinue' + # If the module is not found, run the build task 'noop'. + if (-not (Get-Module -Name $script:moduleName -ListAvailable)) + { + # Redirect all streams to $null, except the error stream (stream 2) + & "$PSScriptRoot/../../build.ps1" -Tasks 'noop' 2>&1 4>&1 5>&1 6>&1 > $null + } + + # Re-import the module using force to get any code changes between runs. + Import-Module -Name $script:moduleName -Force -ErrorAction 'Stop' + + $PSDefaultParameterValues['InModuleScope:ModuleName'] = $script:moduleName + $PSDefaultParameterValues['Mock:ModuleName'] = $script:moduleName + $PSDefaultParameterValues['Should:ModuleName'] = $script:moduleName +} + +AfterAll { + $PSDefaultParameterValues.Remove('Mock:ModuleName') + $PSDefaultParameterValues.Remove('InModuleScope:ModuleName') + $PSDefaultParameterValues.Remove('Should:ModuleName') - Get-Module -Name $script:moduleName -ListAvailable | - Select-Object -First 1 | - Import-Module -Force -ErrorAction 'Stop' + Remove-Module -Name $script:moduleName } Describe 'New-InvalidOperationException' { diff --git a/tests/Unit/Public/New-InvalidResultException.Tests.ps1 b/tests/Unit/Public/New-InvalidResultException.Tests.ps1 index 93e52bd..fcd6bf9 100644 --- a/tests/Unit/Public/New-InvalidResultException.Tests.ps1 +++ b/tests/Unit/Public/New-InvalidResultException.Tests.ps1 @@ -1,11 +1,27 @@ BeforeAll { $script:moduleName = 'DscResource.Common' - Remove-Module -Name $script:moduleName -Force -ErrorAction 'SilentlyContinue' + # If the module is not found, run the build task 'noop'. + if (-not (Get-Module -Name $script:moduleName -ListAvailable)) + { + # Redirect all streams to $null, except the error stream (stream 2) + & "$PSScriptRoot/../../build.ps1" -Tasks 'noop' 2>&1 4>&1 5>&1 6>&1 > $null + } + + # Re-import the module using force to get any code changes between runs. + Import-Module -Name $script:moduleName -Force -ErrorAction 'Stop' + + $PSDefaultParameterValues['InModuleScope:ModuleName'] = $script:moduleName + $PSDefaultParameterValues['Mock:ModuleName'] = $script:moduleName + $PSDefaultParameterValues['Should:ModuleName'] = $script:moduleName +} + +AfterAll { + $PSDefaultParameterValues.Remove('Mock:ModuleName') + $PSDefaultParameterValues.Remove('InModuleScope:ModuleName') + $PSDefaultParameterValues.Remove('Should:ModuleName') - Get-Module -Name $script:moduleName -ListAvailable | - Select-Object -First 1 | - Import-Module -Force -ErrorAction 'Stop' + Remove-Module -Name $script:moduleName } Describe 'New-InvalidResultException' { diff --git a/tests/Unit/Public/New-NotImplementedException.Tests.ps1 b/tests/Unit/Public/New-NotImplementedException.Tests.ps1 index 472f5b6..d5a62ac 100644 --- a/tests/Unit/Public/New-NotImplementedException.Tests.ps1 +++ b/tests/Unit/Public/New-NotImplementedException.Tests.ps1 @@ -1,11 +1,27 @@ BeforeAll { $script:moduleName = 'DscResource.Common' - Remove-Module -Name $script:moduleName -Force -ErrorAction 'SilentlyContinue' + # If the module is not found, run the build task 'noop'. + if (-not (Get-Module -Name $script:moduleName -ListAvailable)) + { + # Redirect all streams to $null, except the error stream (stream 2) + & "$PSScriptRoot/../../build.ps1" -Tasks 'noop' 2>&1 4>&1 5>&1 6>&1 > $null + } + + # Re-import the module using force to get any code changes between runs. + Import-Module -Name $script:moduleName -Force -ErrorAction 'Stop' + + $PSDefaultParameterValues['InModuleScope:ModuleName'] = $script:moduleName + $PSDefaultParameterValues['Mock:ModuleName'] = $script:moduleName + $PSDefaultParameterValues['Should:ModuleName'] = $script:moduleName +} + +AfterAll { + $PSDefaultParameterValues.Remove('Mock:ModuleName') + $PSDefaultParameterValues.Remove('InModuleScope:ModuleName') + $PSDefaultParameterValues.Remove('Should:ModuleName') - Get-Module -Name $script:moduleName -ListAvailable | - Select-Object -First 1 | - Import-Module -Force -ErrorAction 'Stop' + Remove-Module -Name $script:moduleName } Describe 'New-NotImplementedException' { diff --git a/tests/Unit/Public/New-ObjectNotFoundException.Tests.ps1 b/tests/Unit/Public/New-ObjectNotFoundException.Tests.ps1 index 401c1f7..238ac1f 100644 --- a/tests/Unit/Public/New-ObjectNotFoundException.Tests.ps1 +++ b/tests/Unit/Public/New-ObjectNotFoundException.Tests.ps1 @@ -1,11 +1,27 @@ BeforeAll { $script:moduleName = 'DscResource.Common' - Remove-Module -Name $script:moduleName -Force -ErrorAction 'SilentlyContinue' + # If the module is not found, run the build task 'noop'. + if (-not (Get-Module -Name $script:moduleName -ListAvailable)) + { + # Redirect all streams to $null, except the error stream (stream 2) + & "$PSScriptRoot/../../build.ps1" -Tasks 'noop' 2>&1 4>&1 5>&1 6>&1 > $null + } + + # Re-import the module using force to get any code changes between runs. + Import-Module -Name $script:moduleName -Force -ErrorAction 'Stop' + + $PSDefaultParameterValues['InModuleScope:ModuleName'] = $script:moduleName + $PSDefaultParameterValues['Mock:ModuleName'] = $script:moduleName + $PSDefaultParameterValues['Should:ModuleName'] = $script:moduleName +} + +AfterAll { + $PSDefaultParameterValues.Remove('Mock:ModuleName') + $PSDefaultParameterValues.Remove('InModuleScope:ModuleName') + $PSDefaultParameterValues.Remove('Should:ModuleName') - Get-Module -Name $script:moduleName -ListAvailable | - Select-Object -First 1 | - Import-Module -Force -ErrorAction 'Stop' + Remove-Module -Name $script:moduleName } Describe 'New-ObjectNotFoundException' { diff --git a/tests/Unit/Public/Remove-CommonParameter.Tests.ps1 b/tests/Unit/Public/Remove-CommonParameter.Tests.ps1 index aba9886..390ebfd 100644 --- a/tests/Unit/Public/Remove-CommonParameter.Tests.ps1 +++ b/tests/Unit/Public/Remove-CommonParameter.Tests.ps1 @@ -1,11 +1,27 @@ BeforeAll { $script:moduleName = 'DscResource.Common' - Remove-Module -Name $script:moduleName -Force -ErrorAction 'SilentlyContinue' + # If the module is not found, run the build task 'noop'. + if (-not (Get-Module -Name $script:moduleName -ListAvailable)) + { + # Redirect all streams to $null, except the error stream (stream 2) + & "$PSScriptRoot/../../build.ps1" -Tasks 'noop' 2>&1 4>&1 5>&1 6>&1 > $null + } + + # Re-import the module using force to get any code changes between runs. + Import-Module -Name $script:moduleName -Force -ErrorAction 'Stop' + + $PSDefaultParameterValues['InModuleScope:ModuleName'] = $script:moduleName + $PSDefaultParameterValues['Mock:ModuleName'] = $script:moduleName + $PSDefaultParameterValues['Should:ModuleName'] = $script:moduleName +} + +AfterAll { + $PSDefaultParameterValues.Remove('Mock:ModuleName') + $PSDefaultParameterValues.Remove('InModuleScope:ModuleName') + $PSDefaultParameterValues.Remove('Should:ModuleName') - Get-Module -Name $script:moduleName -ListAvailable | - Select-Object -First 1 | - Import-Module -Force -ErrorAction 'Stop' + Remove-Module -Name $script:moduleName } Describe 'Remove-CommonParameter' { diff --git a/tests/Unit/Public/Test-DscParameterState.Tests.ps1 b/tests/Unit/Public/Test-DscParameterState.Tests.ps1 index 2da71e4..c1008a7 100644 --- a/tests/Unit/Public/Test-DscParameterState.Tests.ps1 +++ b/tests/Unit/Public/Test-DscParameterState.Tests.ps1 @@ -1,11 +1,27 @@ BeforeAll { $script:moduleName = 'DscResource.Common' - Remove-Module -Name $script:moduleName -Force -ErrorAction 'SilentlyContinue' + # If the module is not found, run the build task 'noop'. + if (-not (Get-Module -Name $script:moduleName -ListAvailable)) + { + # Redirect all streams to $null, except the error stream (stream 2) + & "$PSScriptRoot/../../build.ps1" -Tasks 'noop' 2>&1 4>&1 5>&1 6>&1 > $null + } + + # Re-import the module using force to get any code changes between runs. + Import-Module -Name $script:moduleName -Force -ErrorAction 'Stop' + + $PSDefaultParameterValues['InModuleScope:ModuleName'] = $script:moduleName + $PSDefaultParameterValues['Mock:ModuleName'] = $script:moduleName + $PSDefaultParameterValues['Should:ModuleName'] = $script:moduleName +} + +AfterAll { + $PSDefaultParameterValues.Remove('Mock:ModuleName') + $PSDefaultParameterValues.Remove('InModuleScope:ModuleName') + $PSDefaultParameterValues.Remove('Should:ModuleName') - Get-Module -Name $script:moduleName -ListAvailable | - Select-Object -First 1 | - Import-Module -Force -ErrorAction 'Stop' + Remove-Module -Name $script:moduleName } Describe 'Test-DscParameterState' { diff --git a/tests/Unit/Public/Test-IsNanoServer.Tests.ps1 b/tests/Unit/Public/Test-IsNanoServer.Tests.ps1 index 1952c57..6bdbd29 100644 --- a/tests/Unit/Public/Test-IsNanoServer.Tests.ps1 +++ b/tests/Unit/Public/Test-IsNanoServer.Tests.ps1 @@ -1,17 +1,25 @@ BeforeAll { - $moduleName = 'DscResource.Common' - $stubModuleName = 'DscResource.Common.Stubs' + $script:moduleName = 'DscResource.Common' + + # If the module is not found, run the build task 'noop'. + if (-not (Get-Module -Name $script:moduleName -ListAvailable)) + { + # Redirect all streams to $null, except the error stream (stream 2) + & "$PSScriptRoot/../../build.ps1" -Tasks 'noop' 2>&1 4>&1 5>&1 6>&1 > $null + } - Remove-Module -Name $moduleName -Force -ErrorAction 'SilentlyContinue' + # Re-import the module using force to get any code changes between runs. + Import-Module -Name $script:moduleName -Force -ErrorAction 'Stop' - Get-Module -Name $moduleName -ListAvailable | - Select-Object -First 1 | - Import-Module -Force -ErrorAction 'Stop' + $PSDefaultParameterValues['InModuleScope:ModuleName'] = $script:moduleName + $PSDefaultParameterValues['Mock:ModuleName'] = $script:moduleName + $PSDefaultParameterValues['Should:ModuleName'] = $script:moduleName <# This mocks the Get-CimInstance on platforms where the cmdlet does not exist, like Linux anc macOS. #> + $stubModuleName = 'DscResource.Common.Stubs' Remove-Module -Name $stubModuleName -Force -ErrorAction 'SilentlyContinue' New-Module -Name $stubModuleName -ScriptBlock { function Get-CimInstance @@ -27,6 +35,12 @@ BeforeAll { } AfterAll { + $PSDefaultParameterValues.Remove('Mock:ModuleName') + $PSDefaultParameterValues.Remove('InModuleScope:ModuleName') + $PSDefaultParameterValues.Remove('Should:ModuleName') + + Remove-Module -Name $script:moduleName + <# This removes the stub module that was imported in the BeforeAll-block.