ps/Modules/Alkami.PowerShell.Configuration/Public/Set-AppSetting.tests.ps1
2023-05-30 22:51:22 -07:00

76 lines
2.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
# Have to do this to use the private function for Set-AppSettingPrivate
# Write-Host "Overriding SUT: $functionPath"
# Import-Module $functionPath -Force
$moduleForMock = "Alkami.PowerShell.Configuration"
Describe "Set-AppSetting" {
# Temp file to write content to
$tempFile = [System.IO.Path]::GetTempFileName()
$tempPath = $tempFile.Split(".") | Select-Object -First 1
$fakeConfigFile = Join-Path $tempPath "fake.config"
New-Item -ItemType Directory $tempPath -ErrorAction SilentlyContinue | Out-Null
Write-Host ("Using temp path: $tempPath for tests")
Context "`$null check still works if no file found at path" {
Mock -ModuleName $moduleForMock -CommandName Write-Error -MockWith {}
Mock -ModuleName $moduleForMock -CommandName Write-Host -MockWith {}
Mock -ModuleName $moduleForMock -CommandName Write-Warning -MockWith {}
$value = Set-AppSetting -Path "C:\TotallyNotARealPath\TotallyNotARealConfig.config" -Key "Not a real key value" -value "Not a real value"
It "still got the right value (null)" {
$value | Should -BeNull
}
It "did not throw an error" {
Assert-MockCalled -ModuleName $moduleForMock -CommandName Write-Error -Exactly -Times 0 -Scope Context
}
It "wrote a warning to the console" {
Assert-MockCalled -ModuleName $moduleForMock -CommandName Write-Warning -Exactly -Times 1 -Scope Context
}
It "did not output useless information" {
Assert-MockCalled -ModuleName $moduleForMock -CommandName Write-Host -Exactly -Times 0 -Scope Context
}
}
Context "Throws an error if the configuration Node doesn't exist" {
$appSettingsTestContents = @"
"@
Mock -ModuleName $moduleForMock -CommandName Write-Error -MockWith {}
Mock -ModuleName $moduleForMock -CommandName Write-Host -MockWith {}
Mock -ModuleName $moduleForMock -CommandName Write-Warning -MockWith {}
$appSettingsTestContents | Out-File $fakeConfigFile -Force
{Set-AppSetting -Path $fakeConfigFile -Key "FakeSetting" -Value "FakeValue"} | Should -Throw
It "did not write an error" {
Assert-MockCalled -ModuleName $moduleForMock -CommandName Write-Error -Exactly -Times 0 -Scope Context
}
It "wrote a warning to the console" {
Assert-MockCalled -ModuleName $moduleForMock -CommandName Write-Warning -Exactly -Times 0 -Scope Context
}
It "wrote two paths" {
Assert-MockCalled -ModuleName $moduleForMock -CommandName Write-Host -Exactly -Times 0 -Scope Context
}
}
Remove-Item $fakeConfigFile -Force
Remove-Item $tempPath -Force
}