Skip to content

Commit

Permalink
Update unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
johlju committed Jul 18, 2022
1 parent 6524548 commit 82178e5
Show file tree
Hide file tree
Showing 23 changed files with 408 additions and 92 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ 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

Expand Down
18 changes: 14 additions & 4 deletions tests/Unit/Private/Test-DscObjectHasProperty.Tests.ps1
Original file line number Diff line number Diff line change
@@ -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' {
Expand Down
18 changes: 14 additions & 4 deletions tests/Unit/Private/Test-DscPropertyState.Tests.ps1
Original file line number Diff line number Diff line change
@@ -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' {
Expand Down
18 changes: 14 additions & 4 deletions tests/Unit/Public/Assert-BoundParameter.Tests.ps1
Original file line number Diff line number Diff line change
@@ -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' {
Expand Down
19 changes: 14 additions & 5 deletions tests/Unit/Public/Assert-IPAddress.Tests.ps1
Original file line number Diff line number Diff line change
@@ -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' {
Expand Down
16 changes: 11 additions & 5 deletions tests/Unit/Public/Assert-Module.Tests.ps1
Original file line number Diff line number Diff line change
@@ -1,21 +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('InModuleScope:ModuleName')
$PSDefaultParameterValues.Remove('Mock:ModuleName')
$PSDefaultParameterValues.Remove('InModuleScope:ModuleName')
$PSDefaultParameterValues.Remove('Should:ModuleName')

Remove-Module -Name $script:moduleName
}

Describe 'Assert-Module' {
Expand Down
24 changes: 20 additions & 4 deletions tests/Unit/Public/Compare-DscParameterState.Tests.ps1
Original file line number Diff line number Diff line change
@@ -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' {
Expand Down
24 changes: 20 additions & 4 deletions tests/Unit/Public/Compare-ResourcePropertyState.Tests.ps1
Original file line number Diff line number Diff line change
@@ -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' {
Expand Down
24 changes: 20 additions & 4 deletions tests/Unit/Public/ConvertFrom-DscResourceInstance.Tests.ps1
Original file line number Diff line number Diff line change
@@ -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' {
Expand Down
24 changes: 20 additions & 4 deletions tests/Unit/Public/ConvertTo-CimInstance.Tests.ps1
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
24 changes: 20 additions & 4 deletions tests/Unit/Public/ConvertTo-Hashtable.Tests.ps1
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
24 changes: 20 additions & 4 deletions tests/Unit/Public/Get-ComputerName.Tests.ps1
Original file line number Diff line number Diff line change
@@ -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' {
Expand Down
24 changes: 20 additions & 4 deletions tests/Unit/Public/Get-LocalizedData.Tests.ps1
Original file line number Diff line number Diff line change
@@ -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' {
Expand Down
Loading

0 comments on commit 82178e5

Please sign in to comment.