Skip to content

Commit f7688b5

Browse files
committed
Moved disk check into funcion to enable mocking
1 parent 9941adb commit f7688b5

2 files changed

Lines changed: 62 additions & 22 deletions

File tree

DSCResources/MSFT_xVirtualMemory/MSFT_xVirtualMemory.psm1

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -139,13 +139,7 @@ function Set-TargetResource
139139
Set-AutoManagePaging -State Disable
140140
}
141141

142-
$driveInfo = [System.IO.DriveInfo] $Drive
143-
144-
if (-not $driveInfo.IsReady)
145-
{
146-
New-InvalidOperationException `
147-
-Message ($script:localizedData.DriveNotReadyError -f $driveInfo.Name)
148-
}
142+
$driveInfo = Get-DriveInfo -Drive $Drive
149143

150144
$existingPageFileSetting = Get-PageFileSetting `
151145
-Drive $($driveInfo.Name.Substring(0,2))
@@ -179,13 +173,7 @@ function Set-TargetResource
179173
Set-AutoManagePaging -State Disable
180174
}
181175

182-
$driveInfo = [System.IO.DriveInfo] $Drive
183-
184-
if (-not $driveInfo.IsReady)
185-
{
186-
New-InvalidOperationException `
187-
-Message ($script:localizedData.DriveNotReadyError -f $driveInfo.Name)
188-
}
176+
$driveInfo = Get-DriveInfo -Drive $Drive
189177

190178
$existingPageFileSetting = Get-PageFileSetting `
191179
-Drive $($driveInfo.Name.Substring(0,2))
@@ -217,13 +205,7 @@ function Set-TargetResource
217205
Set-AutoManagePaging -State Disable
218206
}
219207

220-
$driveInfo = [System.IO.DriveInfo] $Drive
221-
222-
if (-not $driveInfo.IsReady)
223-
{
224-
New-InvalidOperationException `
225-
-Message ($script:localizedData.DriveNotReadyError -f $driveInfo.Name)
226-
}
208+
$driveInfo = Get-DriveInfo -Drive $Drive
227209

228210
$existingPageFileSetting = Get-PageFileSetting `
229211
-Drive $($driveInfo.Name.Substring(0,2))
@@ -491,4 +473,36 @@ function New-PageFile
491473
}
492474
}
493475

476+
<#
477+
.SYNOPSIS
478+
Gets the Drive info object for a specified
479+
Drive. It will throw an exception if the drive
480+
is invalid or does not exist.
481+
482+
.PARAMETER Drive
483+
The letter of the drive to get the drive info
484+
for.
485+
#>
486+
function Get-DriveInfo
487+
{
488+
[CmdletBinding()]
489+
[OutputType([System.IO.DriveInfo])]
490+
param
491+
(
492+
[Parameter(Mandatory = $true)]
493+
[System.String]
494+
$Drive
495+
)
496+
497+
$driveInfo = [System.IO.DriveInfo] $Drive
498+
499+
if (-not $driveInfo.IsReady)
500+
{
501+
New-InvalidOperationException `
502+
-Message ($script:localizedData.DriveNotReadyError -f $driveInfo.Name)
503+
}
504+
505+
return $driveInfo
506+
}
507+
494508
Export-ModuleMember -Function *-TargetResource

Tests/Unit/MSFT_xVirtualMemory.Tests.ps1

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,17 @@ try {
4141
)
4242
}
4343

44-
$testDrive = 'D:'
44+
$testDrive = 'K:'
4545
$testInitialSize = 10
4646
$testMaximumSize = 20
4747
$testPageFileName = "$testDrive\pagefile.sys"
4848

49+
$mockGetDriveInfo = {
50+
[PSObject] @{
51+
Name = "$testDrive\"
52+
}
53+
}
54+
4955
$mockAutomaticPagefileEnabled = {
5056
[PSObject] @{
5157
AutomaticManagedPageFile = $true
@@ -251,6 +257,26 @@ try {
251257
}
252258

253259
Describe 'MSFT_xVirtualMemory\Set-TargetResource' {
260+
BeforeEach {
261+
<#
262+
These mocks are to handle when disk drive
263+
used for testing does not exist.
264+
#>
265+
Mock `
266+
-CommandName Get-DriveInfo `
267+
-ParameterFilter { $Drive -eq $testDrive } `
268+
-MockWith $mockGetDriveInfo
269+
270+
Mock `
271+
-CommandName Join-Path `
272+
-ParameterFilter {
273+
$Path -eq "$testDrive\" -and `
274+
$ChildPath -eq 'pagefile.sys'
275+
} `
276+
-MockWith { "$testDrive\pagefile.sys"}
277+
278+
}
279+
254280
Context 'When automatic managed page file should be enabled' {
255281
Mock `
256282
-CommandName Get-CimInstance `

0 commit comments

Comments
 (0)