Skip to content
This repository has been archived by the owner on Jul 18, 2024. It is now read-only.

Archive module v2.0.1-preview3 #140

Open
wants to merge 34 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
ca2d328
added and updated tests for basic functionality
ayousuf23 Jul 27, 2022
39158b5
added and worked on Expand-Archive cmdlet but it is incomplete
ayousuf23 Jul 27, 2022
2f3da2d
worked on Expand-Archive, added IEntry class, added support for Shoul…
ayousuf23 Jul 28, 2022
9839283
added Expand-Archive.Tests.ps1 and worked on parameter set validation…
ayousuf23 Jul 28, 2022
9e6404c
added and updated tests for Expand-Archive
ayousuf23 Jul 28, 2022
6c3b296
fixed bug where wrong destination path was determined for an item in …
ayousuf23 Jul 30, 2022
128c518
added tests for basic functionality for Expand-Archive
ayousuf23 Aug 2, 2022
fda7263
added support for automatically determining DestinationPath and worke…
ayousuf23 Aug 3, 2022
e5237de
added tests
ayousuf23 Aug 9, 2022
e68ea09
added tests for special and wildcard tests, invalid DestinationPath, …
ayousuf23 Aug 9, 2022
a0452f0
updated CI to run Expand-Archive tests, worked on tar support
ayousuf23 Aug 10, 2022
cca70ed
fixed bug where percent complete was not updating
ayousuf23 Aug 10, 2022
80917c7
added progress info in Expand-Archive, addded changelog
ayousuf23 Aug 10, 2022
0210c8d
fixed failing tests
ayousuf23 Aug 10, 2022
ab31d3e
fixed a bug where removing the extension on Unix platforms would appe…
ayousuf23 Aug 10, 2022
bb24f0e
updated release build pipeline
ayousuf23 Aug 10, 2022
28c4754
added test for seeing if a file can be added to an archive while it i…
ayousuf23 Aug 10, 2022
13e6550
updated sign and package script to use prelease version from manifest
ayousuf23 Aug 10, 2022
538bd72
fixed typo in sign package script
ayousuf23 Aug 10, 2022
fea4d2e
added test, fixed an error in sign and package script
ayousuf23 Aug 10, 2022
2e977d1
worked on tar support and added test case and assertion for tar
ayousuf23 Aug 11, 2022
a7f0ee2
worked on tar support, added support for determining whether an archi…
ayousuf23 Aug 11, 2022
4f9265c
updated changelog and release notes
ayousuf23 Aug 11, 2022
1d8a21c
fixed a typo in the changelog
ayousuf23 Aug 11, 2022
4e7515f
added tests for path structure preservation, added gzip support, work…
ayousuf23 Aug 12, 2022
0a5ca6b
worked on tar.gz support
ayousuf23 Aug 13, 2022
86b7204
organized files, worked on tar.gz support, filter and flatten support
ayousuf23 Aug 14, 2022
2968644
added support for expanding tar.gz archives
ayousuf23 Aug 16, 2022
964406b
merged with preview1 branch
ayousuf23 Aug 16, 2022
6bc0787
added tests for Compress-Archive
ayousuf23 Aug 16, 2022
bb3d909
added more tests for Compress-Archive, large file tests, Flatten test…
ayousuf23 Aug 17, 2022
cb4829c
added tests for Expand-Archive
ayousuf23 Aug 17, 2022
1267b8a
added filter support and tests for Expand-Archive
ayousuf23 Aug 17, 2022
716fd39
fixed failing tar.gz tests
ayousuf23 Aug 17, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .azdevops/ReleaseBuildPipeline.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ stages:
includePreviewVersions: true

- pwsh: |
& $(Build.SourcesDirectory)/Microsoft.PowerShell.Archive/SimpleBuild.ps1
& $(Build.SourcesDirectory)/Microsoft.PowerShell.Archive/Build.ps1
displayName: Build Microsoft.PowerShell.Archive module

- pwsh: |
Expand Down
2 changes: 1 addition & 1 deletion .azdevops/RunTests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Import-Module -Name "Pester" -MinimumVersion $pesterMinVersion -MaximumVersion $
# Run tests
$OutputFile = "$PWD/build-unit-tests.xml"
$results = $null
$results = Invoke-Pester -Script ./Tests/Compress-Archive.Tests.ps1 -OutputFile $OutputFile -PassThru -OutputFormat NUnitXml -Show Failed, Context, Describe, Fails
$results = Invoke-Pester -Script ./Tests -OutputFile $OutputFile -PassThru -OutputFormat NUnitXml -Show Failed, Context, Describe, Fails
Write-Host "##vso[artifact.upload containerfolder=testResults;artifactname=testResults]$OutputFile"
if(!$results -or $results.FailedCount -gt 0 -or !$results.TotalCount)
{
Expand Down
5 changes: 4 additions & 1 deletion .azdevops/SignAndPackageModule.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ $BuildOutputDir = Join-Path $root "\src\bin\Release"
$ManifestPath = "${BuildOutputDir}\${Name}.psd1"
$ManifestData = Import-PowerShellDataFile -Path $ManifestPath
$Version = $ManifestData.ModuleVersion
#$Prerelease = $ManifestPath.PrivateData.PSData.Prerelease

# this takes the files for the module and publishes them to a created, local repository
# so the nupkg can be used to publish to the PSGallery
Expand All @@ -31,7 +32,9 @@ function Export-Module
Publish-Module -Path $packageRoot -Repository $repoName
Unregister-PSRepository -Name $repoName
Get-ChildItem -Recurse -Name $packageRoot | Write-Verbose
$nupkgName = "{0}.{1}-preview1.nupkg" -f ${Name},${Version}
$nupkgName = "{0}.{1}-preview2.nupkg" -f ${Name},${Version}


$nupkgPath = Join-Path $packageRoot $nupkgName
if ($env:TF_BUILD) {
# In Azure DevOps
Expand Down
20 changes: 20 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Changelog

## 2.0.1-preview2

- Rewrote `Expand-Archive` cmdlet in C#
- Added `-Format` parameter to `Expand-Archive`
- Added `-WriteMode` parameter to `Expand-Archive`
- Added support for zip64 to `Expand-Archive`
- Fixed a bug where the entry names of files in a directory would not be correct when compressing an archive
- `Compress-Archive` by default skips writing an entry to an archive if an error occurs while doing so

## 2.0.1-preview1

- Rewrote `Compress-Archive` cmdlet in C#
- Added `-Format` parameter to `Compress-Archive`
- Added `-WriteMode` parameter to `Compress-Archive`
- Added support for relative path structure preservation when paths relative to the working directory are specified to `-Path` or `-LiteralPath` in `Compress-Archive`
- Added support for zip64 to `Compress-Archive`
- Fixed a bug where empty directories would not be compressed
- Fixed a bug where an abrupt stop when compressing empty directories would not delete the newly created archive
42 changes: 42 additions & 0 deletions Tests/Assertions/Should-BeArchiveOnlyContaining.psm1
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
function Should-BeArchiveOnlyContaining {
<#
.SYNOPSIS
Checks if a zip archive contains the entries $ExpectedValue
.EXAMPLE
"C:\Users\<user>\archive.zip" | Should -BeZipArchiveContaining @("file1.txt")

Checks if archive.zip only contains file1.txt
#>

[CmdletBinding()]
Param (
[string] $ActualValue,
[string[]] $ExpectedValue,
[switch] $Negate,
[string] $Because,
[switch] $LiteralPath,
$CallerSessionState,
[string] $Format
)

if ($Format -eq "Zip") {
return Should-BeZipArchiveOnlyContaining -ActualValue $ActualValue -ExpectedValue $ExpectedValue -Negate:$Negate -Because $Because -LiteralPath:$LiteralPath -CallerSessionState $CallerSessionState
}
if ($Format -eq "Tar") {
return Should-BeTarArchiveOnlyContaining -ActualValue $ActualValue -ExpectedValue $ExpectedValue -Negate:$Negate -Because $Because -LiteralPath:$LiteralPath -CallerSessionState $CallerSessionState
}
if ($Format -eq "Tgz") {
# Get a temp file
$gzipFolderPath = Join-Path ([System.IO.Path]::GetTempPath()) ([System.IO.Path]::GetRandomFileName())
New-Item -Path $gzipFolderPath -ItemType Directory
"7z e $ActualValue -o${gzipFolderPath} -tgzip" | Invoke-Expression
$tarFilePath = (Get-ChildItem $gzipFolderPath)[0].FullName
return Should-BeTarArchiveOnlyContaining -ActualValue $tarFilePath -ExpectedValue $ExpectedValue -Negate:$Negate -Because $Because -LiteralPath:$LiteralPath -CallerSessionState $CallerSessionState
}
return [pscustomobject]@{
Succeeded = $false
FailureMessage = "Format ${Format} is not supported."
}

}
Add-ShouldOperator -Name BeArchiveOnlyContaining -InternalName 'Should-BeArchiveOnlyContaining' -Test ${function:Should-BeArchiveOnlyContaining}
148 changes: 148 additions & 0 deletions Tests/Assertions/Should-BeTarArchiveOnlyContaining.psm1
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
function Should-BeTarArchiveOnlyContaining {
<#
.SYNOPSIS
Checks if a tar archive contains the entries $ExpectedValue
.EXAMPLE
"C:\Users\<user>\archive.zip" | Should -BeZipArchiveContaining @("file1.txt")

Checks if archive.zip only contains file1.txt
#>

[CmdletBinding()]
Param (
[string] $ActualValue,
[string[]] $ExpectedValue,
[switch] $Negate,
[string] $Because,
[switch] $LiteralPath,
$CallerSessionState
)

# ActualValue is supposed to be a path to an archive
# It could be a path to a custom PSDrive, so it needes to be converted
if ($LiteralPath) {
$ActualValue = Convert-Path -LiteralPath $ActualValue
} else {
$ActualValue = Convert-Path -Path $ActualValue
}


# Ensure ActualValue is a valid path
if ($LiteralPath) {
$testPathResult = Test-Path -LiteralPath $ActualValue
} else {
$testPathResult = Test-Path -Path $ActualValue
}

# Don't continue processing if ActualValue is not an actual path
# Determine if the assertion succeeded or failed and then return
if (-not $testPathResult) {
$succeeded = $Negate
if (-not $succeeded) {
$failureMessage = "The path ${ActualValue} does not exist"
}
return [pscustomobject]@{
Succeeded = $succeeded
FailureMessage = $failureMessage
}
}

# Get 7-zip to list the contents of the archive
if ($IsWindows) {
$output = 7z.exe l $ActualValue -ba -ttar
} else {
$output = 7z l $ActualValue -ba -ttar
}

# Check if the output is null
if ($null -eq $output) {
if ($null -eq $ExpectedValue -or $ExpectedValue.Length -eq 0) {
$succeeded = -not $Negate
} else {
$succeeded = $Negate
}

if (-not $succeeded) {
$failureMessage = "Archive {0} contains nothing, but it was expected to contain something"
}

return [pscustomobject]@{
Succeeded = $succeeded
FailureMessage = $failureMessage
}
}

# Filter the output line by line
$lines = $output -split [System.Environment]::NewLine

# Stores the entry names
$entryNames = @()

# Go through each line and split it by whitespace
foreach ($line in $lines) {
$lineComponents = $line -split " +"

# Example of some lines:
#2022-08-05 15:54:04 D.... 0 0 SourceDir
#2022-08-05 15:54:04 ..... 11 11 SourceDir/Sample-1.txt

# First component is date
# 2nd component is time
# 3rd componnent is attributes
# 4th component is size
# 5th component is compressed size
# 6th component is entry name

$entryName = $lineComponents[$lineComponents.Length - 1]

# Since 7zip does not show trailing forwardslash for directories, we need to check the attributes to see if it starts with 'D'
# If so, it means the entry is a directory and we should append a forwardslash to the entry name

if ($lineComponents[2].StartsWith('D')) {
$entryName += '/'
}

# Replace backslashes to forwardslashes
$dirSeperatorChar = [System.IO.Path]::DirectorySeparatorChar
$entryName = $entryName.Replace($dirSeperatorChar, "/")

$entryNames += $entryName
}

$itemsNotInArchive = @()

# Go through each item in ExpectedValue and ensure it is in entryNames
foreach ($expectedItem in $ExpectedValue) {
if ($entryNames -notcontains $expectedItem) {
$itemsNotInArchive += $expectedItem
}
}

if ($itemsNotInArchive.Length -gt 0 -and -not $Negate) {
# Create a comma-seperated string from $itemsNotInEnryName
$commaSeperatedItemsNotInArchive = $itemsNotInArchive -join ","
$failureMessage = "'$ActualValue' does not contain $commaSeperatedItemsNotInArchive $(if($Because) { "because $Because"})."
$succeeded = $false
}

# Ensure the length of $entryNames is equal to that of $ExpectedValue
if ($null -eq $succeeded -and $entryNames.Length -ne $ExpectedValue.Length -and -not $Negate) {
$failureMessage = "${ActualValue} does not contain the same number of items as ${ExpectedValue -join ""} (expected ${ExpectedValue.Length} entries but found ${entryNames.Length}) $(if($Because) { "because $Because"})."
$succeeded = $false
}

if ($null -eq $succeeded) {
$succeeded = -not $Negate
if (-not $succeeded) {
$failureMessage = "Expected ${ActualValue} to not contain the entries ${ExpectedValue -join ""} only $(if($Because) { "because $Because"})."
}
}

$ObjProperties = @{
Succeeded = $succeeded
FailureMessage = $failureMessage
}
return New-Object PSObject -Property $ObjProperties
}

Add-ShouldOperator -Name BeTarArchiveOnlyContaining -InternalName 'Should-BeTarArchiveOnlyContaining' -Test ${function:Should-BeTarArchiveOnlyContaining}
4 changes: 2 additions & 2 deletions Tests/Assertions/Should-BeZipArchiveOnlyContaining.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ function Should-BeZipArchiveOnlyContaining {

# Get 7-zip to list the contents of the archive
if ($IsWindows) {
$output = 7z.exe l $ActualValue -ba
$output = 7z.exe l $ActualValue -ba -tzip
} else {
$output = 7z l $ActualValue -ba
$output = 7z l $ActualValue -ba -tzip
}

# Check if the output is null
Expand Down
Loading