Functions/Get-TextFileEncoding.Tests.ps1

describe "BitTitan.Runbooks.Common/Get-TextFileEncoding" -Tag "module", "unit" {

    # Import the function to test
    . "$($PSScriptRoot)\Get-TextFileEncoding.ps1"

    # Declare a temporary file to hold the text
    $tempTextFilePath = Join-Path $env:TEMP "$(New-Guid).txt"

    it -TestCases @(
        @{
            Encoding = "UTF8"
        },
        @{
            Encoding = "UTF32"
        },
        @{
            Encoding = "BigEndianUnicode"
        },
        @{
            Encoding = "BigEndianUTF32"
        },
        @{
            Encoding = "Unicode"
        }
    ) "detects that a text file encoding is <Encoding>" {
        param ($Encoding)

        # Set the contents of the file
        Set-Content -Path $tempTextFilePath -Value "File contents" -Encoding $Encoding -Force -Confirm:$false

        # Call the function
        $encoding = Get-TextFileEncoding -Path $tempTextFilePath

        # Verify the output
        $encoding | Should Be $Encoding
    }

    it "outputs an error if the file does not have a text file extension" {
        # Call the function
        $encoding = Get-TextFileEncoding -Path $tempTextFilePath.Replace(".txt", ".pdf") -ErrorVariable errorVariable 2>$null

        # Verify the output
        $errorVariable | Should Not BeNullOrEmpty
        $encoding | Should BeNullOrEmpty
    }

    # Delete the temporary file
    if (![String]::IsNullOrWhiteSpace($tempTextFilePath)) {
        Remove-Item -Path $tempTextFilePath -Force -Confirm:$false
    }
}