Tests/GenXdev.FileSystem/Copy-FilesToDateFolder.Tests.ps1

Pester\Describe 'Copy-FilesToDateFolder' {

    Pester\BeforeAll {
        $Script:testRoot = GenXdev.FileSystem\Expand-Path "$env:TEMP\GenXdev.FileSystem.Tests\Move-FilesToDateFolder\" -CreateDirectory
    }

    Pester\AfterAll {
        GenXdev.FileSystem\Remove-AllItems $Script:testRoot -DeleteFolder
    }

    Pester\BeforeEach {
        $Script:source = GenXdev.FileSystem\Expand-Path "$Script:testRoot\source\" -CreateDirectory
        $Script:dest = GenXdev.FileSystem\Expand-Path "$Script:testRoot\dest\" -CreateDirectory
        $Script:filePath = Microsoft.PowerShell.Management\Join-Path $Script:source 'file.txt'
        'copy test content' | Microsoft.PowerShell.Utility\Out-File -FilePath $Script:filePath -Force
    }

    Pester\AfterEach {
        @($Script:source, $Script:dest) | Microsoft.PowerShell.Core\ForEach-Object {
            GenXdev.FileSystem\Remove-AllItems -Path $_
        }
    }

    Pester\It 'Copies file into the correct date folder when moving' {
        $filePath = Microsoft.PowerShell.Management\Join-Path $Script:source 'file.txt'
        $date = GenXdev.FileSystem\Get-MediaFileCreationDate -FilePath $filePath
        $dateFolder = Microsoft.PowerShell.Management\Join-Path $date.ToString('yyyy') $date.ToString('MM')

        GenXdev.FileSystem\Copy-FilesToDateFolder -TargetFolder $Script:dest -Root $Script:source -Name '*.txt' -NoRecurse -Verbose -Confirm:$false

        $sourceExists = Microsoft.PowerShell.Management\Test-Path -LiteralPath $filePath
        $sourceExists | Pester\Should -BeTrue

        $destinationPath = Microsoft.PowerShell.Management\Join-Path -Path (Microsoft.PowerShell.Management\Join-Path -Path $Script:dest -ChildPath $dateFolder) -ChildPath 'file.txt'
        $destExists = Microsoft.PowerShell.Management\Test-Path -LiteralPath $destinationPath
        $destExists | Pester\Should -BeTrue
    }

    Pester\It 'Does not copy files when WhatIf is specified' {
        GenXdev.FileSystem\Copy-FilesToDateFolder -TargetFolder $Script:dest -Root $Script:source -Name '*.txt' -WhatIf -NoRecurse -Verbose -Confirm:$false -ErrorAction SilentlyContinue

        $sourceExists = Microsoft.PowerShell.Management\Test-Path -LiteralPath "$Script:source\file.txt"
        $sourceExists | Pester\Should -BeTrue

        $destinationPath = Microsoft.PowerShell.Management\Join-Path -Path (Microsoft.PowerShell.Management\Join-Path -Path $Script:dest -ChildPath $dateFolder) -ChildPath 'file.txt'
        $destExists = Microsoft.PowerShell.Management\Test-Path -LiteralPath $destinationPath
        $destExists | Pester\Should -BeFalse
    }

    Pester\It 'Copies files' {
        $date = GenXdev.FileSystem\Get-MediaFileCreationDate -FilePath "$Script:source\file.txt"
        $dateFolder = Microsoft.PowerShell.Management\Join-Path $date.ToString('yyyy') $date.ToString('MM')

        GenXdev.FileSystem\Copy-FilesToDateFolder -TargetFolder $Script:dest -Root $Script:source -Name '*.txt' -NoRecurse -Verbose -Confirm:$false

        $sourceExists = Microsoft.PowerShell.Management\Test-Path -LiteralPath "$Script:source\file.txt"
        $sourceExists | Pester\Should -BeTrue

        $destinationPath = Microsoft.PowerShell.Management\Join-Path -Path (Microsoft.PowerShell.Management\Join-Path -Path $Script:dest -ChildPath $dateFolder) -ChildPath 'file.txt'
        $destExists = Microsoft.PowerShell.Management\Test-Path -LiteralPath $destinationPath
        $destExists | Pester\Should -BeTrue
    }
}