. $PSScriptRoot\..\..\Load-PesterModules.ps1 $here = Split-Path -Parent $MyInvocation.MyCommand.Path $sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path) -replace '\.tests\.', '.' $functionPath = Join-Path -Path $here -ChildPath $sut Write-Host "Overriding SUT: $functionPath" Import-Module $functionPath -Force $moduleForMock = "" #region Get-FileEncoding $asciiTestString = "The quick brown fox jumps over the lazy dog" $nonasciiTestString = [string]::Join('',('z', 'a', [char]0x0306, [char]0x01FD, [char]0x03B2, [char]0xD8FF, [char]0xDCFF)) $asciiEncodingExpectedResult = ([System.Text.Encoding]::ASCII).ToString() $utf8EncodingExpectedResult = ([System.Text.Encoding]::UTF8).ToString() $uniEncodingExpectedResult = ([System.Text.Encoding]::Unicode).ToString() $utf7EncodingExpectedResult = ([System.Text.Encoding]::UTF7).ToString() Describe "Get-FileEncoding" -Tags @("Integration") { Mock -ModuleName $moduleForMock Get-LogLeadName { "UUT: Get-FileEncoding" } Mock -ModuleName $moduleForMock Write-Host {} It "reads a known ASCII file as ASCII" { $testDrivePath = "TestDrive:\ascii.txt" Set-Content -Path $testDrivePath -Value $asciiTestString -Encoding ASCII $result = Get-FileEncoding $testDrivePath $result.Encoding | Should -Be $asciiEncodingExpectedResult } It "reads a known UTF8 file with ASCII text as UTF8" { $testDrivePath = "TestDrive:\UTF8.txt" Set-Content -Path $testDrivePath -Value $asciiTestString -Encoding UTF8 $result = Get-FileEncoding $testDrivePath $result.Encoding | Should -Be $utf8EncodingExpectedResult } It "reads a known UTF8 file with non-ASCII text as UTF8" { $testDrivePath = "TestDrive:\UTF8.txt" Set-Content -Path $testDrivePath -Value $nonasciiTestString -Encoding UTF8 $result = Get-FileEncoding $testDrivePath $result.Encoding | Should -Be $utf8EncodingExpectedResult } It "reads a known Unicode file with non-ASCII text as Unicode" { $testDrivePath = "TestDrive:\Unicode.txt" Set-Content -Path $testDrivePath -Value $nonasciiTestString -Encoding Unicode $result = Get-FileEncoding $testDrivePath $result.Encoding | Should -Be $uniEncodingExpectedResult } It "reads a known Unicode file with ASCII text as Unicode" { $testDrivePath = "TestDrive:\Unicode.txt" Set-Content -Path $testDrivePath -Value $asciiTestString -Encoding Unicode $result = Get-FileEncoding $testDrivePath $result.Encoding | Should -Be $uniEncodingExpectedResult } # Not running theascii scenario for UTF7 # It's impossible to read a UTF7 file as UTF7 if it only contains ASCII. # UTF7 is a variant of base64 or urlencode for Unicode chars to ASCII files It "reads a known UTF7 file with non-ASCII text as UTF7" { $testDrivePath = "TestDrive:\UTF7.txt" Set-Content -Path $testDrivePath -Value $nonasciiTestString -Encoding UTF7 $result = Get-FileEncoding $testDrivePath -TestForUTF7 $result.Encoding | Should -Be $utf7EncodingExpectedResult } It "reads a known exe file as MZ/PE/NE" { $testDrivePath = "c:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" # This file "should" exist if we are testing on Windows. # Seems like testing for powershell is an easy win :D ows trick. See what I did there? # This is just to prove that testing for PE/NE/MZ files does the thing because .NET .dll files are also PE/NE/MZ if (Test-Path $testDrivePath) { # this is brittle because if someone updates the result from the actual function, this will break $brittleTestStringResult = 'Windows MZ/PE/NE or SYS (driver) file' $result = Get-FileEncoding $testDrivePath $result.Description | Should -Be $brittleTestStringResult } } } #endregion Get-FileEncoding