Skip to content

Commit 89951b8

Browse files
authored
Merge pull request #18 from KirkMunro/issue_4
Added -PassThru parameter to Expand-Archive and Compress-Archive
2 parents 560f585 + 7bd6b24 commit 89951b8

File tree

2 files changed

+86
-2
lines changed

2 files changed

+86
-2
lines changed

Microsoft.PowerShell.Archive/Microsoft.PowerShell.Archive.psm1

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ function Compress-Archive
3737
DefaultParameterSetName="Path",
3838
SupportsShouldProcess=$true,
3939
HelpUri="http://go.microsoft.com/fwlink/?LinkID=393252")]
40+
[OutputType([System.IO.File])]
4041
param
4142
(
4243
[parameter (mandatory=$true, Position=0, ParameterSetName="Path", ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)]
@@ -75,7 +76,10 @@ function Compress-Archive
7576
[parameter(mandatory=$true, ParameterSetName="PathWithForce", ValueFromPipeline=$false, ValueFromPipelineByPropertyName=$false)]
7677
[parameter(mandatory=$true, ParameterSetName="LiteralPathWithForce", ValueFromPipeline=$false, ValueFromPipelineByPropertyName=$false)]
7778
[switch]
78-
$Force = $false
79+
$Force = $false,
80+
81+
[switch]
82+
$PassThru = $false
7983
)
8084

8185
BEGIN
@@ -234,6 +238,10 @@ function Compress-Archive
234238
Remove-Item -LiteralPath $DestinationPath -Force -Recurse -ErrorAction SilentlyContinue
235239
}
236240
}
241+
elseif ($PassThru)
242+
{
243+
Get-Item -LiteralPath $DestinationPath
244+
}
237245
}
238246
}
239247
}
@@ -248,6 +256,7 @@ function Expand-Archive
248256
DefaultParameterSetName="Path",
249257
SupportsShouldProcess=$true,
250258
HelpUri="http://go.microsoft.com/fwlink/?LinkID=393253")]
259+
[OutputType([System.IO.FileSystemInfo])]
251260
param
252261
(
253262
[parameter (
@@ -277,7 +286,10 @@ function Expand-Archive
277286
[parameter (mandatory=$false,
278287
ValueFromPipeline=$false,
279288
ValueFromPipelineByPropertyName=$false)]
280-
[switch] $Force
289+
[switch] $Force,
290+
291+
[switch]
292+
$PassThru = $false
281293
)
282294

283295
BEGIN
@@ -410,6 +422,12 @@ function Expand-Archive
410422
$expandedItems | % { Remove-Item $_ -Force -Recurse }
411423
}
412424
}
425+
elseif ($PassThru -and $expandedItems.Count -gt 0)
426+
{
427+
# Return the expanded items, being careful to remove trailing backslashes from
428+
# any folder paths for consistency
429+
Get-Item -LiteralPath ($expandedItems -replace '\\+$')
430+
}
413431
}
414432
}
415433
}

Tests/Pester.Commands.Cmdlets.Archive.Tests.ps1

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -621,6 +621,27 @@ Describe "Test suite for Microsoft.PowerShell.Archive module" -Tags "BVT" {
621621
$ps.Dispose()
622622
}
623623
}
624+
625+
It "Validate that Compress-Archive returns nothing when -PassThru is not used" {
626+
$sourcePath = @("$TestDrive\SourceDir")
627+
$destinationPath = "$TestDrive\NoPassThruTest.zip"
628+
$archive = Compress-Archive -Path $sourcePath -DestinationPath $destinationPath
629+
$archive | Should Be $null
630+
}
631+
632+
It "Validate that Compress-Archive returns nothing when -PassThru is used with a value of $false" {
633+
$sourcePath = @("$TestDrive\SourceDir")
634+
$destinationPath = "$TestDrive\FalsePassThruTest.zip"
635+
$archive = Compress-Archive -Path $sourcePath -DestinationPath $destinationPath -PassThru:$false
636+
$archive | Should Be $null
637+
}
638+
639+
It "Validate that Compress-Archive returns the archive when invoked with -PassThru" {
640+
$sourcePath = @("$TestDrive\SourceDir")
641+
$destinationPath = "$TestDrive\PassThruTest.zip"
642+
$archive = Compress-Archive -Path $sourcePath -DestinationPath $destinationPath -PassThru
643+
$archive.FullName | Should Be $destinationPath
644+
}
624645
}
625646

626647
Context "Expand-Archive - Parameter validation test cases" {
@@ -947,5 +968,50 @@ Describe "Test suite for Microsoft.PowerShell.Archive module" -Tags "BVT" {
947968
Pop-Location
948969
}
949970
}
971+
972+
It "Validate that Expand-Archive returns nothing when -PassThru is not used" {
973+
$sourcePath = "$TestDrive\SourceDir"
974+
$archivePath = "$TestDrive\NoPassThruTestForExpand.zip"
975+
$destinationPath = "$TestDrive\NoPassThruTest"
976+
$sourceList = dir $sourcePath -Name
977+
978+
Add-CompressionAssemblies
979+
[System.IO.Compression.ZipFile]::CreateFromDirectory($sourcePath, $archivePath)
980+
981+
$contents = Expand-Archive -Path $archivePath -DestinationPath $destinationPath
982+
983+
$contents | Should Be $null
984+
}
985+
986+
It "Validate that Expand-Archive returns nothing when -PassThru is used with a value of $false" {
987+
$sourcePath = "$TestDrive\SourceDir"
988+
$archivePath = "$TestDrive\FalsePassThruTestForExpand.zip"
989+
$destinationPath = "$TestDrive\FalsePassThruTest"
990+
$sourceList = dir $sourcePath -Name
991+
992+
Add-CompressionAssemblies
993+
[System.IO.Compression.ZipFile]::CreateFromDirectory($sourcePath, $archivePath)
994+
995+
$contents = Expand-Archive -Path $archivePath -DestinationPath $destinationPath -PassThru:$false
996+
997+
$contents | Should Be $null
998+
}
999+
1000+
It "Validate that Expand-Archive returns the contents of the archive -PassThru" {
1001+
$sourcePath = "$TestDrive\SourceDir"
1002+
$archivePath = "$TestDrive\PassThruTestForExpand.zip"
1003+
$destinationPath = "$TestDrive\PassThruTest"
1004+
$sourceList = dir $sourcePath -Name
1005+
1006+
Add-CompressionAssemblies
1007+
[System.IO.Compression.ZipFile]::CreateFromDirectory($sourcePath, $archivePath)
1008+
1009+
$contents = Expand-Archive -Path $archivePath -DestinationPath $destinationPath -PassThru | Sort-Object -Property PSParentPath,PSIsDirectory,Name
1010+
# We pipe Get-ChildItem to Get-Item here because the ToString results are different between the two, and we
1011+
# need to compare with other Get-Item results
1012+
$extractedList = Get-ChildItem -Recurse -LiteralPath $destinationPath | Get-Item
1013+
1014+
Compare-Object -ReferenceObject $extractedList -DifferenceObject $contents -PassThru | Should Be $null
1015+
}
9501016
}
9511017
}

0 commit comments

Comments
 (0)