ps/Modules/Alkami.DevOps.Inventory/Public/Get-AppSettingsInventory.tests.ps1
2023-05-30 22:51:22 -07:00

119 lines
3.6 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-AppSettingsInventory" {
Context "When the app setting values are good" {
## Good file values
$GoodValues = @'
<?xml version="1.0"?>
<configuration>
<appSettings>
<add key="key1" value="value1" />
<add key="key2" value="value2" />
</appSettings>
</configuration>
'@
$global:TempFile = New-TemporaryFile
Set-Content -Path $global:TempFile.FullName -Value $GoodValues
Mock -ModuleName $moduleForMock -CommandName Get-AppSettingsInventoryConfigPaths {
return @(Get-Item $global:TempFile.FullName)
}
It "Works as expected" {
$a = (Get-AppSettingsInventory)
$a.Values.Values[1] | Should Be "value1"
$a.Values.Values[2] | Should Be "value2"
## Keys is an ICollection, and it doesn't know how to index otherwise.
## By forcing it to an array before I index it, I can read the values I expect by ID
([string[]]$a.Values.Values.Keys)[1] | Should Be "key1"
([string[]]$a.Values.Values.Keys)[2] | Should Be "key2"
## There's a filename key and the two of our object
([string[]]$a.Values.Values.Keys).Length | Should Be 3
}
Remove-Item $global:TempFile.FullName -Force
}
Context "When the app setting values are empty" {
## Good file values
$GoodValues = @'
<?xml version="1.0"?>
<configuration>
<appSettings>
</appSettings>
</configuration>
'@
$global:TempFile = New-TemporaryFile
Set-Content -Path $global:TempFile.FullName -Value $GoodValues
Mock -ModuleName $moduleForMock -CommandName Get-AppSettingsInventoryConfigPaths {
return @(Get-Item $global:TempFile.FullName)
}
It "Works as expected" {
$a = (Get-AppSettingsInventory)
## There's a filename key even when there are no KVP
([string[]]$a.Values.Values.Keys).Length | Should Be 1
}
Remove-Item $global:TempFile.FullName -Force
}
Context "When the app setting values are good but have a clear entry" {
## Good file values
$GoodValues = @'
<?xml version="1.0"?>
<configuration>
<appSettings>
<add key="key1" value="value1" />
<clear />
<add key="key2" value="value2" />
</appSettings>
</configuration>
'@
$global:TempFile = New-TemporaryFile
Set-Content -Path $global:TempFile.FullName -Value $GoodValues
Mock -ModuleName $moduleForMock -CommandName Get-AppSettingsInventoryConfigPaths {
return @(Get-Item $global:TempFile.FullName)
}
It "Works as expected" {
$a = (Get-AppSettingsInventory)
$a.Values.Values[1] | Should Be "value1"
$a.Values.Values[2] | Should Be "value2"
## Keys is an ICollection, and it doesn't know how to index otherwise.
## By forcing it to an array before I index it, I can read the values I expect by ID
([string[]]$a.Values.Values.Keys)[1] | Should Be "key1"
([string[]]$a.Values.Values.Keys)[2] | Should Be "key2"
## There's a filename key and the two of our object
([string[]]$a.Values.Values.Keys).Length | Should Be 3
}
Remove-Item $global:TempFile.FullName -Force
}
}