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

71 lines
3.9 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 = ""
$fake_Output_OnlyConnectionErrors = @(
"[NuGet] Not able to contact source 'https://packagerepo.orb.alkamitech.com/nuget/choco.dev/'. Error was Unable to connect to the remote server",
"[NuGet] Not able to contact source 'https://packagerepo.orb.alkamitech.com/nuget/SRETools'. Error was Unable to connect to the remote server",
"Error retrieving packages from source 'https://packagerepo.orb.alkamitech.com/nuget/choco.dev/':",
" Unable to connect to the remote server",
"Error retrieving packages from source 'https://packagerepo.orb.alkamitech.com/nuget/SRETools':",
" Unable to connect to the remote server"
)
$fake_Output_Packages_Plus_ConnectionErrors = @(
"[NuGet] Not able to contact source 'https://packagerepo.orb.alkamitech.com/nuget/choco.dev/'. Error was Unable to connect to the remote server",
"[NuGet] Not able to contact source 'https://packagerepo.orb.alkamitech.com/nuget/SRETools'. Error was Unable to connect to the remote server",
"Error retrieving packages from source 'https://packagerepo.orb.alkamitech.com/nuget/choco.dev/':",
" Unable to connect to the remote server",
"Error retrieving packages from source 'https://packagerepo.orb.alkamitech.com/nuget/SRETools':",
" Unable to connect to the remote server",
"test.package.name|1.0.0"
)
$fake_NormalOutput = @("test.package.name|1.0.0","test.otherpackage.name|1.0.0")
Describe "Get-ChocoState" {
Mock -ModuleName $moduleForMock -CommandName Get-LogLeadName -MockWith {return ""}
Mock -ModuleName $moduleForMock -CommandName Write-Verbose -MockWith {}
Mock -ModuleName $moduleForMock -CommandName Write-Host -MockWith {}
Mock -ModuleName $moduleForMock -CommandName Write-Error -MockWith {}
Mock -ModuleName $moduleForMock -CommandName Invoke-Expression -MockWith {}
#Mock -ModuleName $moduleForMock -CommandName Format-ParseChocoPackages -MockWith {}
Mock -ModuleName $moduleForMock -CommandName Get-CimInstance -MockWith {}
Context "Connection_Error_Handling" {
It "WithOnlyConnectionErrors_Returns_NullOrEmpty" {
Mock -ModuleName $moduleForMock -CommandName Invoke-Expression -MockWith {
return $fake_Output_OnlyConnectionErrors
}
Get-ChocoState | Should -HaveCount 0
Assert-MockCalled -CommandName Write-Error -ModuleName $moduleForMock -ParameterFilter {
$Message -match "Unable to connect"
} -Times 1 -Scope It
}
It "WithBothPackagesAndConnectionErrors_Writes_Error" {
# This one is weird because of mocking and ErrorAction and Pester
# There may or may not be results, but there should always be a Write-Error
# This needs more consideration - TR
Mock -ModuleName $moduleForMock -CommandName Invoke-Expression -MockWith {
return $fake_Output_Packages_Plus_ConnectionErrors
}
Get-ChocoState #| Should -HaveCount 0
Assert-MockCalled -CommandName Write-Error -ModuleName $moduleForMock -ParameterFilter {
$Message -match "Unable to connect"
} -Times 1 -Scope It
}
It "NoConnectionErrors_Returns_List" {
Mock -ModuleName $moduleForMock -CommandName Invoke-Expression -MockWith {
return $fake_NormalOutput
}
Get-ChocoState | Should -HaveCount 2
Assert-MockCalled -CommandName Write-Error -ModuleName $moduleForMock -ParameterFilter {
$Message -match "Unable to connect"
} -Times 0 -Scope It
}
}
}