Private/Tests/Get-DirectorySize.Tests.ps1

<#
 
# Get-DirectorySize (UTest)
 
Ohne Beschreibung
 
- **Hashtags** UTest Pester
- **Version** 2020.01.05
 
#>


BeforeAll {
    Set-StrictMode -Version Latest
    . ($PSCommandPath.Replace('.Tests.ps1', '.ps1')).Replace('\Private\Tests\', '\Public\')
}

AfterAll {
    "<[ START SCRIPT-ANALYZER ..." | Write-Warning
    Invoke-ScriptAnalyzer -Path ($PSCommandPath.Replace('.Tests.ps1', '.ps1')).Replace('\Private\Tests\', '\Public\') -Severity Error, Warning, Information, ParseError | ForEach-Object -Process {"{0,40} - {1,4} - {2}" -f $_.ScriptName, $_.Line, $_.RuleName, $_.Message | Write-Warning }
    "... ENDE SCRIPT-ANALYZER ]>" | Write-Warning
    Remove-Item -Path 'function:\Get-DirectorySize' -Force
    Set-StrictMode -Off
}

Describe "Teste Get-DirectorySize" {

    Context 'Parameter' {

        It 'Parameter-Signatur korrekt' {
            Get-Command -Name Get-DirectorySize | Should -HaveParameter Path -Type String
        }

        It 'Throw => -Path C:\NotExists => Pfad existiert nicht' {
            { Get-DirectorySize -Path C:\NotExists } | Should -Throw -ErrorId 'ParameterArgumentValidationError,Get-DirectorySize'
        }

        It 'Throw => -Path HKCU:\Microsoft => Der Container ist kein Order von FileSystem' {
            { Get-DirectorySize -Path HKCU:\Software } | Should -Throw -ErrorId 'ParameterArgumentValidationError,Get-DirectorySize'
        }
    }

    Context 'Rückgabeobjekte'  {

        BeforeAll {
            $TestDriveRoot = New-Item -Path "$env:TEMP\Get-DirectorySize-TestDrive" -ItemType 'Directory' -Force
            New-PSDrive -Name 'TestDrive' -PSProvider 'FileSystem' -Root $TestDriveRoot.FullName
            $files = @()
            $files += New-Item -Path TestDrive:\DummyFolder1\DummyFolder11\DummyFile111.txt -ItemType File -Force
            $files += New-Item -Path TestDrive:\DummyFolder1\DummyFolder11\DummyFile112.txt -ItemType File -Force
            $files += New-Item -Path TestDrive:\DummyFolder1\DummyFolder12\DummyFile121.txt -ItemType File -Force
            $files += New-Item -Path TestDrive:\DummyFolder1\DummyFolder12\DummyFile122.txt -ItemType File -Force
            $files | ForEach-Object -Process {
                $file = [System.IO.File]::Create($_.FullName)
                $file.SetLength(20MB)
                $file.Close()
            }

            $files = @()
            $files += New-Item -Path TestDrive:\DummyFolder2\DummyFolder21\DummyFile211.txt -ItemType File -Force
            $files += New-Item -Path TestDrive:\DummyFolder2\DummyFolder21\DummyFile212.txt -ItemType File -Force
            $files += New-Item -Path TestDrive:\DummyFolder2\DummyFolder22\DummyFile221.txt -ItemType File -Force
            $files += New-Item -Path TestDrive:\DummyFolder2\DummyFolder22\DummyFile222.txt -ItemType File -Force
            $files | ForEach-Object -Process {
                $file = [System.IO.File]::Create($_.FullName)
                $file.SetLength(40MB)
                $file.Close()
            }
            $files[2].attributes += [IO.FileAttributes]::System
            $files[3].attributes += [IO.FileAttributes]::Hidden
        }

        AfterAll {
            Remove-PSDrive -Name 'TestDrive' -Force
            Remove-Item -Path $TestDriveRoot.FullName -Recurse -Force
        }

        It "Get-DirectorySize -Path TestDrive:\ => PSCustomObject" {
            Get-DirectorySize -Path TestDrive:\ | Should -BeOfType PSCustomObject
        }

        It "Get-DirectorySize -Path TestDrive:\ => Count -eq 2" {
            Get-DirectorySize -Path TestDrive:\ | Should -HaveCount 2
        }

        It "Get-DirectorySize -Path TestDrive:\ => Correct results" {
            $target = Get-DirectorySize -Path TestDrive:\
            $target[0].Name     | Should -BeExactly 'DummyFolder2'
            $target[0].SizeMB   | Should -BeExactly 120
            $target[0].FullName | Should -BeLike "$env:USERPROFILE\AppData\Local\Temp\*\DummyFolder2"
            $target[1].Name     | Should -BeExactly 'DummyFolder1'
            $target[1].SizeMB   | Should -BeExactly 80
            $target[1].FullName | Should -BeLike "$env:USERPROFILE\AppData\Local\Temp\*\DummyFolder1"
        }

        It "Get-DirectorySize -Path TestDrive:\ -MinimumFileSize 40MB => Correct results" {
            $target = Get-DirectorySize -Path TestDrive:\ -MinimumFileSize 40MB
            $target.Name     | Should -BeExactly 'DummyFolder2'
            $target.SizeMB   | Should -BeExactly 120
            $target.FullName | Should -BeLike "$env:USERPROFILE\AppData\Local\Temp\*\DummyFolder2"
        }

        It "Get-DirectorySize -Path TestDrive:\ -MinimumDirectorySize 100MB => Correct results" {
            $target = Get-DirectorySize -Path TestDrive:\ -MinimumDirectorySize 100MB
            $target.Name     | Should -BeExactly 'DummyFolder2'
            $target.SizeMB   | Should -BeExactly 120
            $target.FullName | Should -BeLike "$env:USERPROFILE\AppData\Local\Temp\*\DummyFolder2"
        }

        It "Get-DirectorySize -Path TestDrive:\ -Force => Correct results" {
            $target = Get-DirectorySize -Path TestDrive:\ -Force
            $target[0].Name     | Should -BeExactly 'DummyFolder2'
            $target[0].SizeMB   | Should -BeExactly 160
            $target[0].FullName | Should -BeLike "$env:USERPROFILE\AppData\Local\Temp\*\DummyFolder2"
            $target[1].Name     | Should -BeExactly 'DummyFolder1'
            $target[1].SizeMB   | Should -BeExactly 80
            $target[1].FullName | Should -BeLike "$env:USERPROFILE\AppData\Local\Temp\*\DummyFolder1"
        }

        It "Get-DirectorySize -Path TestDrive:\ -MinimumFileSize 40MB -MinimumDirectorySize 100MB -Force => Correct results" {
            $target = Get-DirectorySize -Path TestDrive:\ -MinimumFileSize 40MB -MinimumDirectorySize 100MB -Force
            $target.Name     | Should -BeExactly 'DummyFolder2'
            $target.SizeMB   | Should -BeExactly 160
            $target.FullName | Should -BeLike "$env:USERPROFILE\AppData\Local\Temp\*\DummyFolder2"
        }
    
    }
}