Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ For older change log history see the [historic changelog](HISTORIC_CHANGELOG.md)

## [Unreleased]

- Vhd
- `Test-TargetResource` no longer throws when `Path` or `ParentPath` does
not exist. Returns the correct boolean based on `Ensure` instead of
aborting the configuration. This aligns with the
[guidance from Microsoft](https://learn.microsoft.com/powershell/dsc/resources/authoringresourcemof?view=dsc-1.1)
that `Test-TargetResource` should return `$false` when the resource is
not in the desired state, not throw an exception - Fixes
[issue #225](https://github.com/dsccommunity/HyperVDsc/issues/225).
- HyperVDsc
- BREAKING CHANGE
- Renamed _xHyper-V_ to _HyperVDsc - fixes [Issue #69](https://github.com/dsccommunity/HyperVDsc/issues/213).
Expand Down
6 changes: 4 additions & 2 deletions source/DSCResources/DSC_VHD/DSC_Vhd.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,8 @@ function Test-TargetResource
{
if (!(Test-Path -Path $ParentPath))
{
throw "$ParentPath does not exists"
Write-Verbose -Message ($script:localizedData.PathDoesNotExist -f $ParentPath)
return ($Ensure -eq 'Absent')
}

# Check if the generation matches parenting disk
Expand All @@ -323,7 +324,8 @@ function Test-TargetResource

if (!(Test-Path -Path $Path))
{
throw "$Path does not exists"
Write-Verbose -Message ($script:localizedData.PathDoesNotExist -f $Path)
return ($Ensure -eq 'Absent')
}

# Construct the full path for the vhdFile
Expand Down
1 change: 1 addition & 0 deletions source/DSCResources/DSC_VHD/en-US/DSC_Vhd.strings.psd1
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
ConvertFrom-StringData @'
PathDoesNotExist = {0} does not exist.
'@
40 changes: 36 additions & 4 deletions tests/Unit/DSC_VHD.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,27 @@ try
}

Context 'ParentPath specified' {
It 'Should throw when ParentPath does not exist' {
It 'Should return $false when ParentPath does not exist and Ensure is Present' {
Mock -CommandName Test-Path -MockWith { $false }

$result = Test-TargetResource -Name 'server' -Path 'C:\VMs' -Type 'Differencing' -ParentPath 'c:\boguspath' -Ensure 'Present'

$result | Should -BeFalse
}

It 'Should return $true when ParentPath does not exist and Ensure is Absent' {
Mock -CommandName Test-Path -MockWith { $false }

$result = Test-TargetResource -Name 'server' -Path 'C:\VMs' -Type 'Differencing' -ParentPath 'c:\boguspath' -Ensure 'Absent'

$result | Should -BeTrue
}

It 'Should not throw when ParentPath does not exist' {
Mock -CommandName Test-Path -MockWith { $false }

{ Test-TargetResource -Name 'server' -Path 'C:\VMs' -Type 'Differencing' -ParentPath 'c:\boguspath' } |
Should -Throw 'c:\boguspath does not exists'
Should -Not -Throw
}

# "Generation $Generation should match ParentPath extension $($ParentPath.Split('.')[-1])"
Expand All @@ -165,11 +181,27 @@ try
}

Context 'Path does not exist' {
It 'Should throw when the path does not exist' {
It 'Should return $false when Ensure is Present' {
Mock -CommandName Test-Path -MockWith { $false }

$result = Test-TargetResource -Name 'server.vhdx' -Path 'C:\VMs' -Type 'Fixed' -MaximumSizeBytes 1GB -Ensure 'Present'

$result | Should -BeFalse
}

It 'Should return $true when Ensure is Absent' {
Mock -CommandName Test-Path -MockWith { $false }

$result = Test-TargetResource -Name 'server.vhdx' -Path 'C:\VMs' -Type 'Fixed' -MaximumSizeBytes 1GB -Ensure 'Absent'

$result | Should -BeTrue
}

It 'Should not throw' {
Mock -CommandName Test-Path -MockWith { $false }

{ Test-TargetResource -Name 'server.vhdx' -Path 'C:\VMs' -Type 'Fixed' -MaximumSizeBytes 1GB } |
Should -Throw 'C:\VMs does not exists'
Should -Not -Throw
}
}

Expand Down