tests/Remove-OldFile.Tests.ps1

#requires -Module Pester

Set-StrictMode -Version Latest

Import-Module -Name '..\Aerie.FileUtility.psd1' -Force

Describe "Remove-OldFile" {

    InModuleScope "Aerie.FileUtility" {
        Context "WhatIf をつけない場合" {
            It "更新日時が 2019-02-04 より古いファイルが削除される" {

                $file1 = New-Item -Path 'TestDrive:\test1.txt' | Set-FileTime -LastWriteTime '2019-02-03' -PassThru
                $file2 = New-Item -Path 'TestDrive:\test2.txt' | Set-FileTime -LastWriteTime '2019-02-04' -PassThru
                $file3 = New-Item -Path 'TestDrive:\test3.txt' | Set-FileTime -LastWriteTime '2019-02-05' -PassThru

                Remove-OldFile -Path 'TestDrive:\*' -PropertyToCompare LastWriteTime -DateTimeToCompare '2019-02-04' -Verbose

                $file1 | Should -Not -Exist
                $file2 | Should -Exist
                $file3 | Should -Exist
            }
        }

        Context "WhatIf をつけた場合" {
            It "削除されない" {

                $file1 = New-Item -Path 'TestDrive:\test1.txt' | Set-FileTime -LastWriteTime '2019-02-03' -PassThru
                $file2 = New-Item -Path 'TestDrive:\test2.txt' | Set-FileTime -LastWriteTime '2019-02-04' -PassThru
                $file3 = New-Item -Path 'TestDrive:\test3.txt' | Set-FileTime -LastWriteTime '2019-02-05' -PassThru

                Remove-OldFile -Path 'TestDrive:\*' -PropertyToCompare LastWriteTime -DateTimeToCompare '2019-02-04' -WhatIf -Verbose

                $file1 | Should -Exist
                $file2 | Should -Exist
                $file3 | Should -Exist

            }
        }

        Context "ディレクトリのテスト" {
            It "削除されない" {

                $dir1 = New-Item -Path 'TestDrive:\dir1' -ItemType Directory | Set-FileTime -LastWriteTime '2019-02-03' -PassThru
                $file1 = New-Item -Path 'TestDrive:\test1.txt' | Set-FileTime -LastWriteTime '2019-02-04' -PassThru

                Remove-OldFile -Path $dir1, $file1 -PropertyToCompare LastWriteTime -DateTimeToCompare '2019-02-05' -Verbose

                $dir1 | Should -Exist
                $file1 | Should -Not -Exist
            }
        }
    }
}