ps/Modules/Alkami.PowerShell.Common/Public/Remove-FileSystemItem.Tests.ps1
2023-05-30 22:51:22 -07:00

106 lines
4.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
Describe "Remove-FileSystemItem" {
## https://pester.dev/docs/usage/testdrive/
$existingOrbPath = (Join-Path $TestDrive orb)
$garbageFileToBeGone = (Join-Path $existingOrbPath "letmebegone.txt")
$log4netFileToBeRemain = (Join-Path $existingOrbPath "log4net.config")
$symlinkActualFolder = (Join-Path $TestDrive "SymlinkSource")
$symlinkTargetFolder = (Join-Path $existingOrbPath "SymlinkTarget")
$symlinkDeepTargetFolder = (Join-Path $symlinkTargetFolder "DeepFolder")
$symlinkActualFile = (Join-Path $symlinkActualFolder "symlinkedfile.txt")
Context "Basic workflow test with no confirmation level" {
## Suppress Prompting
$ConfirmPreference = [System.Management.Automation.ConfirmImpact]::None
##ensure our test files exist as expected.
New-Item -Path $garbageFileToBeGone -ItemType File -Value "I should be gone after this test" -Force
Remove-FileSystemItem $existingOrbPath -Recurse
It "The file should not be there, the folder should have been deleted" {
(Test-Path $garbageFileToBeGone) | Should -Be $false
}
}
Context "Basic workflow test with force" {
##ensure our test files exist as expected.
New-Item -Path $garbageFileToBeGone -ItemType File -Value "I should be gone after this test" -Force
Remove-FileSystemItem $existingOrbPath -Force
It "The file should not be there, the folder should have been deleted" {
(Test-Path $garbageFileToBeGone) | Should -Be $false
}
}
Context "Leaves log4net.config workflow test" {
##ensure our test files exist as expected.
New-Item -Path $garbageFileToBeGone -ItemType File -Value "I should be gone after this test" -Force
New-Item -Path $log4netFileToBeRemain -ItemType File -Value "I should stick around" -Force
Remove-FileSystemItem $existingOrbPath -Force -Exclude "log4net.config"
It "The log4net file should not be deleted" {
(Test-Path $log4netFileToBeRemain) | Should -Be $true
Write-Debug (Get-Content $log4netFileToBeRemain)
}
It "The file should not be there, the contents of the folder should have been deleted" {
(Test-Path $garbageFileToBeGone) | Should -Be $false
}
}
Context "Removes Symlinked files test" {
New-Item -Path $symlinkActualFolder -ItemType Directory -Force
New-Item -Path $symlinkActualFile -ItemType File -Value "I should be gone after this test" -Force
$symlinkTargetFile = (Join-Path $symlinkTargetFolder "symlinkedfile.txt")
New-Symlink -Path $symlinkActualFile -Destination $symlinkTargetFolder
Remove-FileSystemItem $symlinkTargetFile -Force
It "The symlinked file should be gone but the actual file still present" {
(Test-Path $symlinkTargetFile) | Should -Be $false
(Test-Path $symlinkActualFile) | Should -Be $true
}
}
Context "Removes Symlinked folders test" {
New-Item -Path $symlinkActualFolder -ItemType Directory -Force
New-Item -Path $symlinkActualFile -ItemType File -Value "I should be gone after this test" -Force
New-Symlink -Path $symlinkActualFolder -Destination $symlinkTargetFolder
Remove-FileSystemItem $symlinkTargetFolder -Force
It "The symlinked folder should be gone but the actual file still present" {
(Test-Path $symlinkTargetFolder) | Should -Be $false
(Test-Path $symlinkActualFolder) | Should -Be $true
(Test-Path $symlinkActualFile) | Should -Be $true
}
}
Context "Removes Deep Symlinked folders recursion test" {
New-Item -Path $symlinkActualFolder -ItemType Directory -Force
New-Item -Path $symlinkActualFile -ItemType File -Value "I should be gone after this test" -Force
New-Symlink -Path $symlinkActualFolder -Destination $symlinkDeepTargetFolder
Remove-FileSystemItem $symlinkTargetFolder -Force
It "The symlinked folder should be gone but the actual file still present" {
(Test-Path $symlinkTargetFolder) | Should -Be $false
(Test-Path $symlinkDeepTargetFolder) | Should -Be $false
(Test-Path $symlinkActualFolder) | Should -Be $true
(Test-Path $symlinkActualFile) | Should -Be $true
}
}
}