ps/Modules/Alkami.PowerShell.Configuration/Public/Get-AllAppSettingKeys.tests.ps1

116 lines
3.0 KiB
PowerShell
Raw Permalink Normal View History

2023-05-30 22:51:22 -07:00
. $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-AllAppSettingKeys" {
# 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 "Returns all keys when two appSettings sections are present" {
$appSettingsTestContents = @"
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<location path="." inheritInChildApplications="false">
<appSettings>
<add key="TestKey1" value="true"/>
<add key="TestKey2" value="false"/>
</appSettings>
</location>
<appSettings>
<add key="TestKey1" value="true"/>
<add key="TestKey2" value="false"/>
</appSettings>
</configuration>
"@
$appSettingsTestContents | Out-File $fakeConfigFile -Force
$value = Get-AllAppSettingKeys -Path $fakeConfigFile
It "still got the right value" {
$value | Should -Be @("TestKey1","TestKey2","TestKey1","TestKey2")
}
}
Context "Returns all keys when one appsetting section is present" {
$appSettingsTestContents = @"
<configuration>
<appSettings>
<add key="TestKey1" value="true"/>
<add key="TestKey2" value="false"/>
</appSettings>
</configuration>
"@
$appSettingsTestContents | Out-File $fakeConfigFile -Force
$value = Get-AllAppSettingKeys -Path $fakeConfigFile
It "still got the right value" {
$value | Should -Be @("TestKey1","TestKey2")
}
}
Context "Returns empty array if the appSettings Node Doesn't Exist" {
$appSettingsTestContents = @"
<configuration>
<appSettings2>
<add key="FakeSetting" value="FakeValue" />
</appSettings2>
</configuration>
"@
$appSettingsTestContents | Out-File $fakeConfigFile -Force
$value = Get-AllAppSettingKeys -Path $fakeConfigFile
It "still got an empty array" {
$value | Should -HaveCount 0
}
}
Context "Throws an error if the configuration Node doesn't exist" {
$appSettingsTestContents = @"
"@
$appSettingsTestContents | Out-File $fakeConfigFile -Force
$value = $null
{$value = Get-AllAppSettingKeys -Path $fakeConfigFile} | Should -Throw
}
Context "Does a happy path" {
$appSettingsTestContents = @"
<configuration>
<appSettings>
<add key="FakeSetting" value="FakeValue" />
</appSettings>
</configuration>
"@
$appSettingsTestContents | Out-File $fakeConfigFile -Force
$value = Get-AllAppSettingKeys -Path $fakeConfigFile
It "got the right value" {
$value | Should -Be @('FakeSetting')
}
}
Remove-Item $fakeConfigFile -Force
Remove-Item $tempPath -Force
}