Private/Tests/Get-DirectorySize.Tests.ps1

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


using namespace System
using namespace System.Runtime.InteropServices
$ErrorActionPreference = [System.Management.Automation.ActionPreference]::Stop
Set-StrictMode -Version Latest

$CmdletRoot = Split-Path -Parent $MyInvocation.MyCommand.Path
$CmdletName = (Split-Path -Leaf $MyInvocation.MyCommand.Path) -replace '\.Tests\.ps1', [String]::Empty
Set-Location -Path (Join-Path -Path $CmdletRoot -ChildPath "..\..\Public")
. ".\$CmdletName.ps1"

Describe "Teste $CmdletName.ps1" {

    Context "Parameter" {

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

        It "Throw => -Path C:\NotExists => Pfad existiert nicht" {
            { Get-DirectorySize -Path C:\NotExists } | Should -Throw -ErrorId "ParameterArgumentValidationError"
        }
    
        It "Throw => -Path HKCU:\Microsoft => Der Container ist kein Order von FileSystem" {
            { Get-DirectorySize -Path HKCU:\Software } | Should -Throw -ErrorId "ParameterArgumentValidationError"
        }
    }
    
    Context "Rückgabeobjekte"  {
        $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 += 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(20MB)
            $file.Close()
        }
            
        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 'DummyFolder1'
            $target[0].SizeMB   | Should -BeExactly 80
            $target[0].FullName | Should -BeLike "$env:USERPROFILE\AppData\Local\Temp\*\DummyFolder1"
            $target[1].Name     | Should -BeExactly 'DummyFolder2'
            $target[1].SizeMB   | Should -BeExactly 80
            $target[1].FullName | Should -BeLike "$env:USERPROFILE\AppData\Local\Temp\*\DummyFolder2"

        }
    }
    
    Context 'ScriptAnalyzer Tests' {
        [Array]$testCase = @()
        $testCase += Invoke-ScriptAnalyzer -Path ".\$CmdletName.ps1" -Severity Error, Warning -ExcludeRule PSUseSingularNouns | 
            Foreach-Object {
                @{
                    Line       = $_.Line
                    RuleName   = $_.RuleName
                    Message    = $_.Message
                }
            }

            It "Ist die Skript-Datei frei von Regelverletzungen?" {
            $testCase.Count | Should -Be 0
        }

        if ($testCase.Count -gt 0) {
            It "REGEL-Verletzung: LINE <Line> | RULE <RuleName> | MESSAGE <Message>" -TestCases $testCase {
                param(
                    $Line,
                    $RuleName,
                    $Message
                )
                $RuleName | Should BeNullOrEmpty
            }
        }
    }
}