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

Fix Issue#55 #56

Merged
merged 10 commits into from
Apr 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -827,7 +827,14 @@ function ZipArchiveHelper

# Updating the File Creation time so that the same timestamp would be retained after expanding the compressed file.
# At this point we are sure that Get-ChildItem would succeed.
$currentArchiveEntry.LastWriteTime = (Get-Item -LiteralPath $currentFilePath).LastWriteTime
$lastWriteTime = (Get-Item -LiteralPath $currentFilePath).LastWriteTime
if ($lastWriteTime.Year -lt 1980)
{
Write-Warning "'$currentFilePath' has LastWriteTime earlier than 1980. Compress-Archive will store any files with LastWriteTime values earlier than 1980 as 1/1/1980 00:00."
$lastWriteTime = [DateTime]::Parse('1980-01-01T00:00:00')
}

$currentArchiveEntry.LastWriteTime = $lastWriteTime

$destStream = New-Object System.IO.BinaryWriter $currentArchiveEntry.Open()

Expand Down
70 changes: 69 additions & 1 deletion Tests/Pester.Commands.Cmdlets.Archive.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ Describe "Test suite for Microsoft.PowerShell.Archive module" -Tags "BVT" {
try
{
Compress-Archive -Path $path -DestinationPath $destinationPath -CompressionLevel $compressionLevel
trow "ValidateNotNullOrEmpty attribute is missing on one of parameters belonging to Path parameterset."
throw "ValidateNotNullOrEmpty attribute is missing on one of parameters belonging to Path parameterset."
}
catch
{
Expand Down Expand Up @@ -398,6 +398,46 @@ Describe "Test suite for Microsoft.PowerShell.Archive module" -Tags "BVT" {
Remove-Item -LiteralPath $TestDrive$($DS)SampleDir -Force -Recurse
}
}
It "Validate that Compress-Archive cmdlet works when it ecounters LastWriteTimeValues earlier than 1980" {
New-Item $TestDrive$($DS)SampleDir$($DS)Child-1 -Type Directory -Force | Out-Null
$file = New-Item $TestDrive$($DS)SampleDir$($DS)Test.txt -Type File -Force
$destinationPath = "$TestDrive$($DS)SampleDir$($DS)Child-*$($DS)SampleChidArchive.zip"
$sourcePath = "$TestDrive$($DS)SampleDir$($DS)Test.txt"

$file.LastWriteTime = [DateTime]::Parse('1967-03-04T06:00:00')
try
{
Compress-Archive -Path $sourcePath -DestinationPath $destinationPath -WarningAction SilentlyContinue
$destinationPath | Should Exist
}
finally
{
Remove-Item -LiteralPath $TestDrive$($DS)SampleDir -Force -Recurse
}
}
It "Validate that Compress-Archive cmdlet warns when updating the LastWriteTime for files earlier than 1980" {
New-Item $TestDrive$($DS)SampleDir$($DS)Child-1 -Type Directory -Force | Out-Null
$file = New-Item $TestDrive$($DS)SampleDir$($DS)Test.txt -Type File -Force
$destinationPath = "$TestDrive$($DS)SampleDir$($DS)Child-*$($DS)SampleChidArchive.zip"
$sourcePath = "$TestDrive$($DS)SampleDir$($DS)Test.txt"

$file.LastWriteTime = [DateTime]::Parse('1967-03-04T06:00:00')
try
{
$ps=[PowerShell]::Create()
$ps.Streams.Warning.Clear()
$script = "Import-Module Microsoft.PowerShell.Archive; Compress-Archive -Path $sourcePath -DestinationPath `"$destinationPath`" -CompressionLevel Fastest -Verbose"
$ps.AddScript($script)
$ps.Invoke()

$ps.Streams.Warning.Count -gt 0 | Should Be $True
}
finally
{
Remove-Item -LiteralPath $TestDrive$($DS)SampleDir -Force -Recurse
}
}

# This test requires a fix in PS5 to support reading paths with square bracket
It "Validate that Compress-Archive cmdlet can accept LiteralPath parameter for a directory with Special Characters in the directory name" -skip:(($PSVersionTable.psversion.Major -lt 5) -and ($PSVersionTable.psversion.Minor -lt 0)) {
$sourcePath = "$TestDrive$($DS)Source[]Dir$($DS)ChildDir[]-1"
Expand Down Expand Up @@ -1107,6 +1147,34 @@ Describe "Test suite for Microsoft.PowerShell.Archive module" -Tags "BVT" {
}
}

It "Validate that Compress-Archive/Expand-Archive work with dates earlier than 1980" {
$file1 = New-Item $TestDrive$($DS)SourceDir$($DS)EarlierThan1980.txt -Type File -Force
$file1.LastWriteTime = [DateTime]::Parse('1974-10-03T04:30:00')
$file2 = Get-Item "$TestDrive$($DS)SourceDir$($DS)Sample-1.txt"
$expandPath = "$TestDrive$($DS)EarlyYearDir"
$expectedFile1 = "$expandPath$($DS)EarlierThan1980.txt"
$expectedFile2 = "$expandPath$($DS)Sample-1.txt"
$archivePath = "$TestDrive$($DS)EarlyYear.zip"

try
{
Compress-Archive -Path @($file1, $file2) -DestinationPath $archivePath -WarningAction SilentlyContinue
$archivePath | Should Exist

Expand-Archive -Path $archivePath -DestinationPath $expandPath

$expectedFile1 | Should Exist
(Get-Item $expectedFile1).LastWriteTime | Should Be $([DateTime]::Parse('1980-01-01T00:00'))
$expectedFile2 | Should Exist
(Get-Item $expectedFile2).LastWriteTime | Should Not Be $([DateTime]::Parse('1980-01-01T00:00'))

}
finally
{
Remove-Item -LiteralPath $archivePath -Force -Recurse
}
}

# test is currently blocked by https://github.com/dotnet/corefx/issues/24832
It "Validate module can be imported when current language is not en-US" -Pending {
$currentCulture = [System.Threading.Thread]::CurrentThread.CurrentUICulture
Expand Down