Fix broken test & regression re: passwords

This commit is contained in:
jhoneill
2019-07-24 12:02:22 +01:00
parent 8759070636
commit dbd35721ee
3 changed files with 19 additions and 14 deletions

View File

@@ -347,8 +347,9 @@ function Import-Excel {
process {
if ($path) {
$stream = New-Object -TypeName System.IO.FileStream -ArgumentList $Path, 'Open', 'Read', 'ReadWrite'
if ($Password) {$ExcelPackage = New-Object -TypeName OfficeOpenXml.ExcelPackage -ArgumentList $stream , $Password }
else {$ExcelPackage = New-Object -TypeName OfficeOpenXml.ExcelPackage -ArgumentList $stream}
$ExcelPackage = New-Object -TypeName OfficeOpenXml.ExcelPackage
if ($Password) { $ExcelPackage.Load($stream,$Password)}
else { $ExcelPackage.Load($stream) }
}
try {
#Select worksheet

View File

@@ -1,6 +1,6 @@
#Requires -Modules Pester
Import-Module $PSScriptRoot\..\ImportExcel.psd1 -Force
if ($PSVersionTable.PSVersion.Major -gt 5) { Write-Warning "Can't test grid view on V6" }
if ($PSVersionTable.PSVersion.Major -gt 5) { Write-Warning "Can't test grid view on V6 and later" }
else {Add-Type -AssemblyName System.Windows.Forms }
Describe "Compare Worksheet" {
Context "Simple comparison output" {

View File

@@ -1,24 +1,28 @@
Describe "Password Support" {
Context "Password protected sheet" {
BeforeAll {
$password = "YouMustRememberThis"
$path = "$env:TEMP\Test.xlsx"
if ($PSVersionTable.PSVersion.Major -GT 5) {
Write-Warning "Can't test passwords on V6 and later"
return
}
Describe "Password Support" {
Context "Password protected sheet" {
BeforeAll {
$password = "YouMustRememberThis"
$path = "$env:TEMP\Test.xlsx"
Remove-Item $path -ErrorAction SilentlyContinue
Get-Service | Select-Object -First 10 | Export-excel -password $password -Path $Path -DisplayPropertySet
Get-Service | Select-Object -First 10 | Export-excel -password $password -Path $Path -DisplayPropertySet
}
it "Threw an error when the password was omitted " {
{Open-ExcelPackage -Path $path } | should throw
}
it "Was able to append when the password was included " {
{Get-Service | Select-Object -First 10 |
{Get-Service | Select-Object -First 10 |
Export-excel -password $password -Path $Path -Append } | should not throw
}
it "Kept the password on the file when it was saved " {
{Import-Excel $Path } | should throw
{Import-Excel $Path } | should throw
}
it "Could read the file when the password was included " {
(import-excel $path -Password $password).count | should be 20
it "Could read the file when the password was included " {
(import-excel $path -Password $password).count | should be 20
}
}
}