Skip to content

Commit

Permalink
fixed a bug where PathNotFound error was not thrown, fixed a bug when…
Browse files Browse the repository at this point in the history
… testing invalid paths
  • Loading branch information
ayousuf23 committed Aug 4, 2022
1 parent f7cdfd2 commit ab55635
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 23 deletions.
59 changes: 38 additions & 21 deletions Tests/Compress-Archive.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand Down
2 changes: 1 addition & 1 deletion src/CompressArchiveCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
1 change: 0 additions & 1 deletion src/PathHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit ab55635

Please sign in to comment.