Functions/Install-XlsConverterProgram.Tests.ps1
describe "BitTitan.Runbooks.Excel/Install-XlsConverterProgram" -Tag "module", "unit" { # Import the function to test . "$($PSScriptRoot)\Install-XlsConverterProgram.ps1" # Declare mocks mock Test-Path { return $true } function Get-FileFromUrl { param ($FileUrl, $DestinationPath) return $true } mock Expand-Archive {} mock Get-FileHash {} mock Remove-Item {} context "when there are no issues" { # Declare mocks mock Get-FileFromUrl { return $true } mock Expand-Archive {} mock Get-FileHash { if ($Path -like "*Microsoft.mshtml.dll") { return @{ Hash = "BB9DFD6A5699C9318918F3971277A228" } } elseif ($Path -like "*Spire.License.dll") { return @{ Hash = "F0F4A62AB7ADE480D7B89DE488222DAB" } } elseif ($Path -like "*Spire.Pdf.dll") { return @{ Hash = "A5743F20B8753CFBC965BF54D7B07660" } } elseif ($Path -like "*Spire.Xls.dll") { return @{ Hash = "2EFFE494B006B61FD2D8A93D1D660671" } } elseif ($Path -like "*license.elic.xml") { return @{ Hash = "7881E16E3B4FED8B44A0FCDC391A326A" } } } it "installs the XLS converter program" { # Call the functions $output = Install-XlsConverterProgram -InstallFolderPath "C:\temp\xlsConverter" -ErrorAction SilentlyContinue -ErrorVariable errorVariable # Verify the mocks Assert-MockCalled Get-FileFromUrl -Times 1 -Exactly -ParameterFilter { $FileUrl -eq "https://bittitangdpreport.blob.core.windows.net/template/XlsConverter.zip" -and $DestinationPath -eq "C:\temp\xlsConverter\XlsConverter.zip" } -Scope it Assert-MockCalled Expand-Archive -Times 1 -Exactly -ParameterFilter { $Path -eq "C:\temp\xlsConverter\XlsConverter.zip" -and $DestinationPath -eq "C:\temp\xlsConverter" } -Scope it Assert-MockCalled Get-FileHash -Times 5 -Exactly -Scope it # Verify the output $errorVariable | Should BeNullOrEmpty $output | Should Be $true } } context "when the installation folder is not present" { # Declare mocks mock Test-Path { return $false } mock Get-FileFromUrl { return $true } mock Expand-Archive {} mock Get-FileHash { if ($Path -like "*Microsoft.mshtml.dll") { return @{ Hash = "BB9DFD6A5699C9318918F3971277A228" } } elseif ($Path -like "*Spire.License.dll") { return @{ Hash = "F0F4A62AB7ADE480D7B89DE488222DAB" } } elseif ($Path -like "*Spire.Pdf.dll") { return @{ Hash = "A5743F20B8753CFBC965BF54D7B07660" } } elseif ($Path -like "*Spire.Xls.dll") { return @{ Hash = "2EFFE494B006B61FD2D8A93D1D660671" } } elseif ($Path -like "*license.elic.xml") { return @{ Hash = "7881E16E3B4FED8B44A0FCDC391A326A" } } } it "outputs an error" { # Call the functions $output = Install-XlsConverterProgram -InstallFolderPath "C:\temp\xlsConverter" -ErrorAction SilentlyContinue -ErrorVariable errorVariable # Verify the mocks Assert-MockCalled Get-FileFromUrl -Times 0 -Exactly -Scope it # Verify the output $errorVariable | Should Not BeNullOrEmpty $output | Should Be $false } } context "when the download of the converter program fails" { # Declare mocks mock Get-FileFromUrl { return $false } mock Expand-Archive {} it "outputs an error" { # Call the functions $output = Install-XlsConverterProgram -InstallFolderPath "C:\temp\xlsConverter" -ErrorAction SilentlyContinue -ErrorVariable errorVariable # Verify the mocks Assert-MockCalled Get-FileFromUrl -Times 1 -Exactly -ParameterFilter { $FileUrl -eq "https://bittitangdpreport.blob.core.windows.net/template/XlsConverter.zip" -and $DestinationPath -eq "C:\temp\xlsConverter\XlsConverter.zip" } -Scope it Assert-MockCalled Expand-Archive -Times 0 -Exactly -Scope it # Verify the output $errorVariable | Should Not BeNullOrEmpty $output | Should Be $false } } context "when an exception occurs while unzipping the converter program" { # Declare mocks mock Get-FileFromUrl { return $true } mock Expand-Archive { throw "up" } mock Get-FileHash { if ($Path -like "*Microsoft.mshtml.dll") { return @{ Hash = "BB9DFD6A5699C9318918F3971277A228" } } elseif ($Path -like "*Spire.License.dll") { return @{ Hash = "F0F4A62AB7ADE480D7B89DE488222DAB" } } elseif ($Path -like "*Spire.Pdf.dll") { return @{ Hash = "A5743F20B8753CFBC965BF54D7B07660" } } elseif ($Path -like "*Spire.Xls.dll") { return @{ Hash = "2EFFE494B006B61FD2D8A93D1D660671" } } elseif ($Path -like "*license.elic.xml") { return @{ Hash = "7881E16E3B4FED8B44A0FCDC391A326A" } } } it "outputs an error" { # Call the functions $output = Install-XlsConverterProgram -InstallFolderPath "C:\temp\xlsConverter" -ErrorAction SilentlyContinue -ErrorVariable errorVariable # Verify the mocks Assert-MockCalled Get-FileFromUrl -Times 1 -Exactly -ParameterFilter { $FileUrl -eq "https://bittitangdpreport.blob.core.windows.net/template/XlsConverter.zip" -and $DestinationPath -eq "C:\temp\xlsConverter\XlsConverter.zip" } -Scope it Assert-MockCalled Expand-Archive -Times 1 -Exactly -ParameterFilter { $Path -eq "C:\temp\xlsConverter\XlsConverter.zip" -and $DestinationPath -eq "C:\temp\xlsConverter" } -Scope it Assert-MockCalled Get-FileHash -Times 0 -Exactly -Scope it # Verify the output $errorVariable | Should Not BeNullOrEmpty $output | Should Be $false } } context "when one of the files in the converter program is not present" { # Declare mocks mock Get-FileFromUrl { return $true } mock Expand-Archive {} mock Test-Path { if ($Path -like "*Microsoft.mshtml.dll") { return $false } return $true } mock Get-FileHash { if ($Path -like "*Microsoft.mshtml.dll") { return @{ Hash = "BB9DFD6A5699C9318918F3971277A228" } } elseif ($Path -like "*Spire.License.dll") { return @{ Hash = "F0F4A62AB7ADE480D7B89DE488222DAB" } } elseif ($Path -like "*Spire.Pdf.dll") { return @{ Hash = "A5743F20B8753CFBC965BF54D7B07660" } } elseif ($Path -like "*Spire.Xls.dll") { return @{ Hash = "2EFFE494B006B61FD2D8A93D1D660671" } } elseif ($Path -like "*license.elic.xml") { return @{ Hash = "7881E16E3B4FED8B44A0FCDC391A326A" } } } it "outputs an error" { # Call the functions $output = Install-XlsConverterProgram -InstallFolderPath "C:\temp\xlsConverter" -ErrorAction SilentlyContinue -ErrorVariable errorVariable # Verify the mocks Assert-MockCalled Get-FileFromUrl -Times 1 -Exactly -ParameterFilter { $FileUrl -eq "https://bittitangdpreport.blob.core.windows.net/template/XlsConverter.zip" -and $DestinationPath -eq "C:\temp\xlsConverter\XlsConverter.zip" } -Scope it Assert-MockCalled Expand-Archive -Times 1 -Exactly -ParameterFilter { $Path -eq "C:\temp\xlsConverter\XlsConverter.zip" -and $DestinationPath -eq "C:\temp\xlsConverter" } -Scope it Assert-MockCalled Get-FileHash -Times 0 -Exactly -Scope it # Verify the output $errorVariable | Should Not BeNullOrEmpty $output | Should Be $false } } context "when one of the files in the converter program does not match the expected hash value" { # Declare mocks mock Get-FileFromUrl { return $true } mock Expand-Archive {} mock Get-FileHash { if ($Path -like "*Microsoft.mshtml.dll") { return @{ Hash = "BB9DFD6A5699C9318918F3971277A228" } } elseif ($Path -like "*Spire.License.dll") { return @{ Hash = "F0F4A62AB7ADE480D7B89DE488222DAB" } } elseif ($Path -like "*Spire.Pdf.dll") { return @{ Hash = "A5743F20B8753CFBC965BF54D7B07660" } } elseif ($Path -like "*Spire.Xls.dll") { return @{ Hash = "2EFFE494B006B61FD2D8A93D1D660671" } } elseif ($Path -like "*license.elic.xml") { return @{ Hash = "0" } } } it "outputs an error" { # Call the functions $output = Install-XlsConverterProgram -InstallFolderPath "C:\temp\xlsConverter" -ErrorAction SilentlyContinue -ErrorVariable errorVariable # Verify the mocks Assert-MockCalled Get-FileFromUrl -Times 1 -Exactly -ParameterFilter { $FileUrl -eq "https://bittitangdpreport.blob.core.windows.net/template/XlsConverter.zip" -and $DestinationPath -eq "C:\temp\xlsConverter\XlsConverter.zip" } -Scope it Assert-MockCalled Expand-Archive -Times 1 -Exactly -ParameterFilter { $Path -eq "C:\temp\xlsConverter\XlsConverter.zip" -and $DestinationPath -eq "C:\temp\xlsConverter" } -Scope it Assert-MockCalled Get-FileHash -Times 5 -Exactly -Scope it # Verify the output $errorVariable | Should Not BeNullOrEmpty $output | Should Be $false } } } |