ps/Modules/Alkami.PowerShell.Configuration/Public/Rename-TemporaryConfigFiles.tests.ps1

79 lines
2.4 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 = ""
#region Rename-TemporaryConfigFiles
Describe Rename-TemporaryConfigFiles {
# Temp file to write content to
$tempFile = [System.IO.Path]::GetTempFileName()
$tempPath = $tempFile.Split(".") | Select-Object -First 1
New-Item -ItemType Directory $tempPath -ErrorAction SilentlyContinue | Out-Null
$tempOrb = ($tempPath + "\ORB")
if (!(Test-Path $tempOrb))
{
New-Item -ItemType Directory $tempOrb | Out-Null
}
Write-Warning ("Using temp path: $tempOrb for tests")
# Make Directories for Tests
$bankServiceDirectory = New-Item -ItemType Directory ($tempOrb + "\BankService")
$coreServiceDirectory = New-Item -ItemType Directory ($tempOrb + "\CoreService")
# Make Files for Tests
$newBankFile = (Join-Path $bankServiceDirectory "new.web.config")
"newFile" | Out-File $newBankFile
$newCoreFile = (Join-Path $coreServiceDirectory "new.web.config")
"newFile" | Out-File $newCoreFile
$originalCoreFile = (Join-Path $coreServiceDirectory "web.config")
"test" | Out-File $originalCoreFile
It "Renames files without conflict" {
$files = Get-ConfigurationFiles $tempOrb $true
Rename-TemporaryConfigFiles $files
Test-Path $newBankFile | Should Be $false
$bankFile = (Join-Path $bankServiceDirectory "web.config")
Test-Path $bankFile | Should Be $true
GC $bankFile | Should Match "newFile"
}
It "Writes a warning when the file already exists" {
$files = Get-ConfigurationFiles $tempOrb $true
{ (Rename-TemporaryConfigFiles $files 3>&1) -match "No changes will be made" } | Should Be $true
}
It "Does not overwrite the existing file if it already exits by default" {
$files = Get-ConfigurationFiles $tempOrb $true
Rename-TemporaryConfigFiles $files
Get-Content $originalCoreFile | Should Match "test"
Test-Path $newCoreFile | Should Be $true
}
It "Overwrites the existing file if the Force parameter is supplied" {
$files = Get-ConfigurationFiles $tempOrb $true
Rename-TemporaryConfigFiles $files -Force
Test-Path $newCoreFile | Should Be $false
Test-Path $originalCoreFile | Should Be $true
GC $originalCoreFile | Should Match "newFile"
}
}
#endregion Rename-TemporaryConfigFiles