GetSTFolderSize.Tests.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#requires -version 3


# Pester 4.x tests for Get-STFolderSize in the GetSTFolderSize module.
# Joakim Borger Svendsen. Started 2018-12-17. Svendsen Tech.

Import-Module -Name Pester -ErrorAction Stop
#Import-Module -Name GetSTFolderSize -ErrorAction Stop
ipmo ..\GetSTFolderSize -Force

# This is mine also, in the PowerShell Gallery. Just crudely making it available for the tests.
Save-Module -Name RandomData -Path $Env:Temp -Force
Import-Module "$Env:Temp\RandomData" -Force

Describe Get-STFolderSize {
    
    # Set up a test directory structure.
    $STBaseDir = "$Env:Temp\GetSTFolderSize.Tests.Svendsen.Tech"
    # Clean things up in advance, just in case.
    Remove-Item -LiteralPath $STBaseDir -Recurse -Force -ErrorAction SilentlyContinue
    New-Item -Path "$STBaseDir\sub1\sub2\sub3" `
        -ItemType Directory -Force | Out-Null

    New-RandomData -Path $STBaseDir -Count 5 -Size 1024 -LineLength 128
    
    It "Calculates the size of a directory with 5x 1024-byte files correctly using -ComOnly." {
        ($ComResult = (Get-STFolderSize -Path $STBaseDir -ComOnly)).TotalBytes | Should -Be (5*1024)
    }
    
    It "Calculates the size of a directory with 5x 1024-byte files correctly using -RoboOnly." {
        ($RoboResult = (Get-STFolderSize -Path $STBaseDir -RoboOnly)).TotalBytes | Should -Be (5*1024)
    }

    It "Gives the same result with COM and RoboCopy" {
        [Int] $ComResult -eq [Int] $RoboResult | Should -Be $True
    }

    # Add subdirectories with files and test recursively from now on.
    New-RandomData -Path "$STBaseDir\sub1" -Count 5 -Size 1024 -LineLength 128
    New-RandomData -Path "$STBaseDir\sub1\sub2" -Count 5 -Size 1024 -LineLength 128
    New-RandomData -Path "$STBaseDir\sub1\sub2\sub3" -Count 5 -Size 1024 -LineLength 128
    
    It "Calculates the size of a directory tree with 5x4x 1024-byte files correctly using -ComOnly." {
        ($ComResult = (Get-STFolderSize -Path $STBaseDir -ComOnly)).TotalBytes | Should -Be (20*1024)
    }
    
    It "Calculates the size of a directory tree with 5x4x 1024-byte files correctly using -RoboOnly." {
        ($RoboResult = (Get-STFolderSize -Path $STBaseDir -RoboOnly)).TotalBytes | Should -Be (20*1024)
    }

    It "Gives the same result with COM and RoboCopy with a directory tree structure" {
        [Int] $ComResult -eq [Int] $RoboResult | Should -Be $True
    }

    # Add files with a different extension for exclusion tests.
    New-RandomData -Path $STBaseDir -Count 5 -BaseName ignored -Extension .ignored -Size 1024 -LineLength 128

    It "Ignores *.ignored files that should be 5 kB in size in the test directory tree." {
        $RoboResult = Get-STFolderSize -Path $STBaseDir -RoboOnly -ExcludeFile *.ignored
        $RoboResult.SkippedBytes | Should -Be (5*1024)
        $RoboResult.SkippedFileCount | Should -Be 5
    }

    It "Ignores a specified directory name (with files containing 5 kB worth of files)." {
        $RoboResult = Get-STFolderSize -Path $STBaseDir -RoboOnly -ExcludeDirectory sub3
        $RoboResult.SkippedDirCount | Should -Be 1
    }

}