ps/Modules/Alkami.PowerShell.Common/Public/Get-FolderSizeMb.tests.ps1
2023-05-30 22:51:22 -07:00

113 lines
5.8 KiB
PowerShell

. $PSScriptRoot\..\..\Load-PesterModules.ps1
$here = Split-Path -Parent $MyInvocation.MyCommand.Path
$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path) -replace '\.tests\.', '.'
$functionPath = Join-Path -Path $here -ChildPath $sut
Write-Host "Overriding SUT: $functionPath"
Import-Module $functionPath -Force
$moduleForMock = ''
Describe 'Get-FolderSizeMb' {
$testSize = 1024 * 1024
$testPath = 'TestDrive:\Test'
$testServer = 'server1.test.local'
Mock -CommandName Test-Path -ModuleName $moduleForMock -MockWith { return $true }
Mock -CommandName Get-ChildItem -ModuleName $moduleForMock -MockWith { return @(@{Length = $testSize }) }
Mock -CommandName Measure-Object -ModuleName $moduleForMock -MockWith { return @{Sum = $testSize } }
Mock -CommandName Compare-StringToLocalMachineIdentifiers -ModuleName $moduleForMock -MockWith { return $true }
Mock -CommandName Test-StringIsNullOrWhitespace -ModuleName $moduleForMock -MockWith { return $true }
Mock -CommandName Invoke-Command -ModuleName $moduleForMock -MockWith {
return $ScriptBlock.Invoke(@($testPath))
}
Context 'Input Validation' {
It 'Throws if Path is null' {
{ Get-FolderSizeMb -Path $null } | Should -Throw
}
It 'Throws if Path is empty' {
{ Get-FolderSizeMb -Path '' } | Should -Throw
}
}
Context 'Logic' {
It 'Performs command on localhost by default' {
Get-FolderSizeMb -Path $testPath | Out-Null
Assert-MockCalled -ModuleName $moduleForMock -CommandName Test-StringIsNullOrWhitespace -Times 1 -Exactly -Scope It
Assert-MockCalled -ModuleName $moduleForMock -CommandName Compare-StringToLocalMachineIdentifiers -Times 0 -Exactly -Scope It
Assert-MockCalled -ModuleName $moduleForMock -CommandName Test-Path -Times 1 -Exactly -Scope It
Assert-MockCalled -ModuleName $moduleForMock -CommandName Get-ChildItem -Times 1 -Exactly -Scope It
Assert-MockCalled -ModuleName $moduleForMock -CommandName Measure-Object -Times 1 -Exactly -Scope It
Assert-MockCalled -ModuleName $moduleForMock -CommandName Invoke-Command -Times 1 -Exactly -Scope It `
-ParameterFilter { ( -not $PSBoundParameters.ContainsKey('ComputerName')) }
}
It 'Performs command on localhost if ComputerName matches current server' {
Mock -CommandName Test-StringIsNullOrWhitespace -ModuleName $moduleForMock -MockWith { return $false }
Get-FolderSizeMb -Path $testPath -ComputerName $testServer | Out-Null
Assert-MockCalled -ModuleName $moduleForMock -CommandName Test-StringIsNullOrWhitespace -Times 1 -Exactly -Scope It
Assert-MockCalled -ModuleName $moduleForMock -CommandName Compare-StringToLocalMachineIdentifiers -Times 1 -Exactly -Scope It `
-ParameterFilter { $stringToCheck -eq $testServer }
Assert-MockCalled -ModuleName $moduleForMock -CommandName Invoke-Command -Times 1 -Exactly -Scope It `
-ParameterFilter { ( -not $PSBoundParameters.ContainsKey('ComputerName')) }
Mock -CommandName Test-StringIsNullOrWhitespace -ModuleName $moduleForMock -MockWith { return $true }
}
It 'Performs command on remote server if ComputerName does not match current server' {
Mock -CommandName Test-StringIsNullOrWhitespace -ModuleName $moduleForMock -MockWith { return $false }
Mock -CommandName Compare-StringToLocalMachineIdentifiers -ModuleName $moduleForMock -MockWith { return $false }
$testServer2 = 'server2.test.local'
Get-FolderSizeMb -Path $testPath -ComputerName $testServer2 | Out-Null
Assert-MockCalled -ModuleName $moduleForMock -CommandName Test-StringIsNullOrWhitespace -Times 1 -Exactly -Scope It
Assert-MockCalled -ModuleName $moduleForMock -CommandName Compare-StringToLocalMachineIdentifiers -Times 1 -Exactly -Scope It `
-ParameterFilter { $stringToCheck -eq $testServer2 }
Assert-MockCalled -ModuleName $moduleForMock -CommandName Invoke-Command -Times 1 -Exactly -Scope It `
-ParameterFilter { $ComputerName -eq $testServer2 }
Mock -CommandName Test-StringIsNullOrWhitespace -ModuleName $moduleForMock -MockWith { return $true }
Mock -CommandName Compare-StringToLocalMachineIdentifiers -ModuleName $moduleForMock -MockWith { return $true }
}
It 'Returns zero if path not found' {
Mock -CommandName Test-Path -ModuleName $moduleForMock -MockWith { return $false }
$result = Get-FolderSizeMb -Path $testPath
$result | Should -BeExactly 0
Assert-MockCalled -ModuleName $moduleForMock -CommandName Test-Path -Times 1 -Exactly -Scope It
Assert-MockCalled -ModuleName $moduleForMock -CommandName Get-ChildItem -Times 0 -Exactly -Scope It
Assert-MockCalled -ModuleName $moduleForMock -CommandName Measure-Object -Times 0 -Exactly -Scope It
Assert-MockCalled -ModuleName $moduleForMock -CommandName Invoke-Command -Times 1 -Exactly -Scope It
Mock -CommandName Test-Path -ModuleName $moduleForMock -MockWith { return $true }
}
It 'Returns size in MB if path is found' {
$result = Get-FolderSizeMb -Path $testPath
$result | Should -BeExactly ($testSize / 1Mb)
Assert-MockCalled -ModuleName $moduleForMock -CommandName Test-Path -Times 1 -Exactly -Scope It
Assert-MockCalled -ModuleName $moduleForMock -CommandName Get-ChildItem -Times 1 -Exactly -Scope It
Assert-MockCalled -ModuleName $moduleForMock -CommandName Measure-Object -Times 1 -Exactly -Scope It
Assert-MockCalled -ModuleName $moduleForMock -CommandName Invoke-Command -Times 1 -Exactly -Scope It
}
}
}