From ab55635638d28502287cd556e096672391551fe0 Mon Sep 17 00:00:00 2001 From: ayousuf3 <23.abdullah.y@gmail.com> Date: Thu, 4 Aug 2022 16:17:31 -0700 Subject: [PATCH] fixed a bug where PathNotFound error was not thrown, fixed a bug when testing invalid paths --- Tests/Compress-Archive.Tests.ps1 | 59 ++++++++++++++++++++------------ src/CompressArchiveCommand.cs | 2 +- src/PathHelper.cs | 1 - 3 files changed, 39 insertions(+), 23 deletions(-) diff --git a/Tests/Compress-Archive.Tests.ps1 b/Tests/Compress-Archive.Tests.ps1 index 20c8979..01f1524 100644 --- a/Tests/Compress-Archive.Tests.ps1 +++ b/Tests/Compress-Archive.Tests.ps1 @@ -136,31 +136,48 @@ } } - It "Validate errors from Compress-Archive when invalid path (non-existing path / non-filesystem path) is supplied for Path or LiteralPath parameters" -ForEach @( - @{ Path = "TestDrive:/InvalidPath" } - @{ Path = @("TestDrive:/", "TestDrive:/InvalidPath") } - ) { + It "Validate errors from Compress-Archive when invalid path is supplied for Path or LiteralPath parameters" -ForEach @( + @{ Path = "Env:/Path" } + @{ Path = @("TestDrive:/", "Env:/Path") } + ) -Tag this1 { $DestinationPath = "TestDrive:/archive2.zip" + Compress-Archive -Path $Path -DestinationPath $DestinationPath -ErrorAction SilentlyContinue -ErrorVariable error + $error.Count | Should -Be 1 + $error[0].FullyQualifiedErrorId | Should -Be "InvalidPath,Microsoft.PowerShell.Archive.CompressArchiveCommand" + Remove-Item -Path $DestinationPath + + Compress-Archive -LiteralPath $Path -DestinationPath $DestinationPath -ErrorAction SilentlyContinue -ErrorVariable error + $error.Count | Should -Be 1 + $error[0].FullyQualifiedErrorId | Should -Be "InvalidPath,Microsoft.PowerShell.Archive.CompressArchiveCommand" + Remove-Item -Path $DestinationPath + } + + It "Throws terminating error when non-existing path is supplied for Path or LiteralPath parameters" -ForEach @( + @{ Path = "TestDrive:/DoesNotExist" } + @{ Path = @("TestDrive:/", "TestDrive:/DoesNotExist") } + ) -Tag this2 { + $DestinationPath = "TestDrive:/archive3.zip" + try - { - Compress-Archive -Path $Path -DestinationPath $DestinationPath - throw "Failed to validate that an invalid Path was supplied as input to Compress-Archive cmdlet." - } - catch - { - $_.FullyQualifiedErrorId | Should -Be "PathNotFound,Microsoft.PowerShell.Archive.CompressArchiveCommand" - } + { + Compress-Archive -Path $Path -DestinationPath $DestinationPath + throw "Failed to validate that an invalid Path was supplied as input to Compress-Archive cmdlet." + } + catch + { + $_.FullyQualifiedErrorId | Should -Be "PathNotFound,Microsoft.PowerShell.Archive.CompressArchiveCommand" + } - try - { - Compress-Archive -LiteralPath $Path -DestinationPath $DestinationPath - throw "Failed to validate that an invalid LiteralPath was supplied as input to Compress-Archive cmdlet." - } - catch - { - $_.FullyQualifiedErrorId | Should -Be "PathNotFound,Microsoft.PowerShell.Archive.CompressArchiveCommand" - } + try + { + Compress-Archive -LiteralPath $Path -DestinationPath $DestinationPath + throw "Failed to validate that an invalid LiteralPath was supplied as input to Compress-Archive cmdlet." + } + catch + { + $_.FullyQualifiedErrorId | Should -Be "PathNotFound,Microsoft.PowerShell.Archive.CompressArchiveCommand" + } } It "Validate error from Compress-Archive when duplicate paths are supplied as input to Path parameter" { diff --git a/src/CompressArchiveCommand.cs b/src/CompressArchiveCommand.cs index 126085f..de521d9 100644 --- a/src/CompressArchiveCommand.cs +++ b/src/CompressArchiveCommand.cs @@ -138,7 +138,7 @@ protected override void EndProcessing() if (_nonexistentPaths.Count > 0) { // Get a comma-seperated string containg the non-existent paths string commaSeperatedNonExistentPaths = string.Join(',', _nonexistentPaths); - var errorRecord = ErrorMessages.GetErrorRecord(ErrorCode.InvalidPath, commaSeperatedNonExistentPaths); + var errorRecord = ErrorMessages.GetErrorRecord(ErrorCode.PathNotFound, commaSeperatedNonExistentPaths); ThrowTerminatingError(errorRecord); } diff --git a/src/PathHelper.cs b/src/PathHelper.cs index dbcf487..fd57bb8 100644 --- a/src/PathHelper.cs +++ b/src/PathHelper.cs @@ -6,7 +6,6 @@ using System.Collections.Generic; using System.Diagnostics; using System.IO; -using System.Linq; using System.Management.Automation; namespace Microsoft.PowerShell.Archive