Skip to content

Commit

Permalink
Fix & test zip.ps1
Browse files Browse the repository at this point in the history
  • Loading branch information
lilith committed Jan 29, 2025
1 parent fbc447e commit bd67193
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 6 deletions.
93 changes: 93 additions & 0 deletions ci/pack_nuget/test_zip.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<#
powershell.exe -ExecutionPolicy Bypass -File .\test_zip.ps1
Reasoning: We want to create a script that will test zip.ps1.
Goal: Validate that using both forward slashes and backward slashes in paths results in correct zip file creation and renaming.
#>

<#
Reasoning: Step 1 - Set up our test environment.
Goal: Create a temporary folder with test files and define test archive filenames.
#>
# Create a temporary directory for testing
$TestDir = Join-Path $env:TEMP "zip-test-$(Get-Random)"
New-Item -ItemType Directory -Path $TestDir | Out-Null

# Create sample files
$FileA = Join-Path $TestDir "fileA.txt"
$FileB = Join-Path $TestDir "fileB.log"
New-Item -ItemType File -Path $FileA | Out-Null
New-Item -ItemType File -Path $FileB | Out-Null

# Create a subdirectory, also containing a sample file
$SubDir = Join-Path $TestDir "subfolder"
New-Item -ItemType Directory -Path $SubDir | Out-Null
$FileC = Join-Path $SubDir "fileC.txt"
New-Item -ItemType File -Path $FileC | Out-Null

# Define archives to be created (with and without .zip extension)
$ArchiveForwardSlash = Join-Path $TestDir "archive_forward"
$ArchiveBackwardSlash = Join-Path $TestDir "archive_backward.zip"


<#
Reasoning: Step 2 - Test using forward slashes in paths.
Goal: Verify that zip.ps1 correctly handles forward slashes and appends .zip if missing.
#>
Write-Host "`n--- Testing zip.ps1 with forward slashes ---"

# Invoke zip.ps1 with forward slash paths and no .zip extension
Push-Location (Split-Path $PSScriptRoot)
try {
& "$PSScriptRoot\zip.ps1" $ArchiveForwardSlash $FileA.Replace('\','/') $FileB.Replace('\','/')
}
catch {
Write-Error "Test failed with forward slash paths: $_"
}
Pop-Location

Write-Host "Checking if archive was created (with .zip extension appended)..."
$ExpectedForwardSlashZip = $ArchiveForwardSlash.TrimEnd('.zip')
if (Test-Path "$ExpectedForwardSlashZip") {
Write-Host "SUCCESS: Archive with forward slashes created as expected: $ExpectedForwardSlashZip"
} else {
Write-Error "FAIL: Archive with forward slashes was not found."
}


<# clec
Reasoning: Step 3 - Test using backward slashes in paths.
Goal: Verify that zip.ps1 handles backward slashes properly and respects existing .zip in archive name.
#>
Write-Host "`n--- Testing zip.ps1 with backward slashes ---"

Push-Location (Split-Path $PSScriptRoot)
try {
& "$PSScriptRoot\zip.ps1" $ArchiveBackwardSlash "$FileA" "$FileB" "$SubDir"
}
catch {
Write-Error "Test failed with backward slash paths: $_"
}
Pop-Location

Write-Host "Checking if archive was created (already included .zip extension)..."
if (Test-Path "$ArchiveBackwardSlash") {
Write-Host "SUCCESS: Archive with backward slashes created as expected: $ArchiveBackwardSlash"
} else {
Write-Error "FAIL: Archive with backward slashes was not found."
}


<#
Reasoning: Step 4 - Teardown and clean up test artifacts.
Goal: Remove test files and directories after testing is complete.
#>
Write-Host "`n--- Cleaning up test artifacts ---"
try {
Remove-Item -Path $TestDir -Recurse -Force
Write-Host "Cleanup successful. Test completed."
}
catch {
Write-Host "Cleanup failed: $_"
}
13 changes: 7 additions & 6 deletions ci/pack_nuget/zip.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,14 @@ param(
# Reasoning: Convert forward slashes to backslashes in the archive name for Windows compatibility.
# Goal: Preserve the user's intended archive filename.
$ArchiveFile = $ArchiveFile -replace '/', '\'
$OriginalArchiveFile = $ArchiveFile

# Get just the filename from OriginalArchiveFile using the proper api
$OriginalArchiveFileName = (Get-Item -Path $ArchiveFile).Name
$ZipAdded = $false

# Reasoning: Ensure the archive file has a .zip extension to comply with Compress-Archive requirements.
# Goal: Append .zip if the provided archive filename does not already end with .zip
if (-not $ArchiveFile.EndsWith('.zip', [System.StringComparison]::InvariantCultureIgnoreCase)) {
$ArchiveFile += '.zip'
$ZipAdded = $true
}


Expand All @@ -55,9 +54,11 @@ try {

# Reasoning: Rename the archive back to the original filename if it was modified.
# Goal: Maintain the user's intended archive filename without the .zip extension in the final output.
if ($ArchiveFile -ne $OriginalArchiveFile) {
Write-Host "Renaming '$ArchiveFile' back to '$OriginalArchiveFileName'..."
Rename-Item -Path $ArchiveFile -NewName $OriginalArchiveFileName -ErrorAction Stop
if ($ZipAdded) {
# Get just the filename from the archive file
$FinalArchiveFileName = (Get-Item -Path $ArchiveFile).Name -replace '\.zip$', ''
Write-Host "Renaming '$ArchiveFile' back to '$FinalArchiveFile'..."
Rename-Item -Path $ArchiveFile -NewName $FinalArchiveFileName -ErrorAction Stop
}

# Reasoning: Indicate successful completion of the compression and renaming process.
Expand Down

0 comments on commit bd67193

Please sign in to comment.