Fix progress calculation divide by zero in Copy-Item (#19038)

This commit is contained in:
Aditya Patwardhan
2023-01-27 10:03:19 -08:00
committed by GitHub
parent 5f3100cd95
commit f8cbf90ccb
2 changed files with 7 additions and 1 deletions
@@ -3938,7 +3938,7 @@ namespace Microsoft.PowerShell.Commands
StringUtil.Format(FileSystemProviderStrings.CopyingLocalFileActivity, _copiedFiles, _totalFiles),
StringUtil.Format(FileSystemProviderStrings.CopyingLocalBytesStatus, Utils.DisplayHumanReadableFileSize(_copiedBytes), Utils.DisplayHumanReadableFileSize(_totalBytes), speed)
);
var percentComplete = (int)Math.Min(_copiedBytes * 100 / _totalBytes, 100);
var percentComplete = _totalBytes != 0 ? (int)Math.Min(_copiedBytes * 100 / _totalBytes, 100) : 100;
progress.PercentComplete = percentComplete;
progress.RecordType = ProgressRecordType.Processing;
WriteProgress(progress);
@@ -317,6 +317,12 @@ Describe "Basic FileSystem Provider Tests" -Tags "CI" {
Remove-Item -Path $testPath -Recurse -Force -ErrorAction SilentlyContinue
}
}
It "Copy-Item can copy 0 byte length file" {
$zeroLengthFile = New-Item -Path (Join-Path $TestDrive "zeroLengthFile.txt") -ItemType File -Force
Copy-Item -Path $zeroLengthFile -Destination "$TestDrive\zeroLengthFile2.txt" -Force
"$TestDrive\zeroLengthFile2.txt" | Should -Exist
}
}
Context "Validate behavior when access is denied" {