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

81 lines
2.3 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 = ""
#region Save-XMLFile
Describe Save-XMLFile {
$tempPath = [System.IO.Path]::GetTempFileName()
$sampleXml = '<documentNode><nodeWithAttribute isAwesome="true" /></documentNode>'
$prettyXml = '<foo><bar/></foo>'
Context "When the File is Valid XML" {
function Remove-TestXMLFile {
if (Test-Path $tempPath) {
Remove-Item $tempPath -Force
}
}
It "Saves the XML" {
Remove-TestXMLFile
Save-XMLFile $tempPath $sampleXml
$resultingFile = Read-XmlFile $tempPath
$resultingFile.OuterXml.ToString() | Should Match "<documentNode>"
}
It "Saves the XML Declaration Header" {
Remove-TestXMLFile
Save-XMLFile $tempPath $sampleXml
$resultingFile = Get-Content $tempPath
$resultingFile[0] | Should Match '<?xml'
}
It "Pretty Prints the XML" {
Remove-TestXMLFile
Save-XMLFile $tempPath $prettyXml
$resultingFile = Get-Content $tempPath
$resultingFile[1] | Should Match '<foo>'
$resultingFile[2] | Should Match '<bar />'
$resultingFile.Count | Should Be 4
}
It "Saves the XML in UTF-8 with BOM by Default" {
Remove-TestXMLFile
Save-XMLFile $tempPath $prettyXml
$resultingFile = Get-Content $tempPath
$resultingFile[0] | Should Match 'utf-8'
}
It "Saves the XML in the Specified Encoding" {
Remove-TestXMLFile
$encoding = [System.Text.Encoding]::ASCII
Save-XMLFile $tempPath $prettyXml $encoding
$resultingFile = Get-Content $tempPath
$resultingFile[0] | Should Match 'ascii'
}
}
Context "When the String is Invalid XML" {
It "Throws an Error" {
$invalidXml = "Hello World!"
{ Save-XMLFile $tempPath $invalidXml -ErrorAction SilentlyContinue } | Should Throw
}
}
}
#endregion Save-XMLFile