Skip to content

Commit b5e23c2

Browse files
committed
fixed a bug where PathNotFound error was not thrown, fixed a bug when testing invalid paths
1 parent f7cdfd2 commit b5e23c2

File tree

3 files changed

+42
-26
lines changed

3 files changed

+42
-26
lines changed

Tests/Compress-Archive.Tests.ps1

Lines changed: 38 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -136,31 +136,48 @@
136136
}
137137
}
138138

139-
It "Validate errors from Compress-Archive when invalid path (non-existing path / non-filesystem path) is supplied for Path or LiteralPath parameters" -ForEach @(
140-
@{ Path = "TestDrive:/InvalidPath" }
141-
@{ Path = @("TestDrive:/", "TestDrive:/InvalidPath") }
142-
) {
139+
It "Validate errors from Compress-Archive when invalid path is supplied for Path or LiteralPath parameters" -ForEach @(
140+
@{ Path = "Env:/Path" }
141+
@{ Path = @("TestDrive:/", "Env:/Path") }
142+
) -Tag this1 {
143143
$DestinationPath = "TestDrive:/archive2.zip"
144144

145+
Compress-Archive -Path $Path -DestinationPath $DestinationPath -ErrorAction SilentlyContinue -ErrorVariable error
146+
$error.Count | Should -Be 1
147+
$error[0].FullyQualifiedErrorId | Should -Be "InvalidPath,Microsoft.PowerShell.Archive.CompressArchiveCommand"
148+
Remove-Item -Path $DestinationPath
149+
150+
Compress-Archive -LiteralPath $Path -DestinationPath $DestinationPath -ErrorAction SilentlyContinue -ErrorVariable error
151+
$error.Count | Should -Be 1
152+
$error[0].FullyQualifiedErrorId | Should -Be "InvalidPath,Microsoft.PowerShell.Archive.CompressArchiveCommand"
153+
Remove-Item -Path $DestinationPath
154+
}
155+
156+
It "Throws terminating error when non-existing path is supplied for Path or LiteralPath parameters" -ForEach @(
157+
@{ Path = "TestDrive:/DoesNotExist" }
158+
@{ Path = @("TestDrive:/", "TestDrive:/DoesNotExist") }
159+
) -Tag this2 {
160+
$DestinationPath = "TestDrive:/archive3.zip"
161+
145162
try
146-
{
147-
Compress-Archive -Path $Path -DestinationPath $DestinationPath
148-
throw "Failed to validate that an invalid Path was supplied as input to Compress-Archive cmdlet."
149-
}
150-
catch
151-
{
152-
$_.FullyQualifiedErrorId | Should -Be "PathNotFound,Microsoft.PowerShell.Archive.CompressArchiveCommand"
153-
}
163+
{
164+
Compress-Archive -Path $Path -DestinationPath $DestinationPath
165+
throw "Failed to validate that an invalid Path was supplied as input to Compress-Archive cmdlet."
166+
}
167+
catch
168+
{
169+
$_.FullyQualifiedErrorId | Should -Be "PathNotFound,Microsoft.PowerShell.Archive.CompressArchiveCommand"
170+
}
154171

155-
try
156-
{
157-
Compress-Archive -LiteralPath $Path -DestinationPath $DestinationPath
158-
throw "Failed to validate that an invalid LiteralPath was supplied as input to Compress-Archive cmdlet."
159-
}
160-
catch
161-
{
162-
$_.FullyQualifiedErrorId | Should -Be "PathNotFound,Microsoft.PowerShell.Archive.CompressArchiveCommand"
163-
}
172+
try
173+
{
174+
Compress-Archive -LiteralPath $Path -DestinationPath $DestinationPath
175+
throw "Failed to validate that an invalid LiteralPath was supplied as input to Compress-Archive cmdlet."
176+
}
177+
catch
178+
{
179+
$_.FullyQualifiedErrorId | Should -Be "PathNotFound,Microsoft.PowerShell.Archive.CompressArchiveCommand"
180+
}
164181
}
165182

166183
It "Validate error from Compress-Archive when duplicate paths are supplied as input to Path parameter" {

src/CompressArchiveCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ protected override void EndProcessing()
138138
if (_nonexistentPaths.Count > 0) {
139139
// Get a comma-seperated string containg the non-existent paths
140140
string commaSeperatedNonExistentPaths = string.Join(',', _nonexistentPaths);
141-
var errorRecord = ErrorMessages.GetErrorRecord(ErrorCode.InvalidPath, commaSeperatedNonExistentPaths);
141+
var errorRecord = ErrorMessages.GetErrorRecord(ErrorCode.PathNotFound, commaSeperatedNonExistentPaths);
142142
ThrowTerminatingError(errorRecord);
143143
}
144144

src/PathHelper.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
using System.Collections.Generic;
77
using System.Diagnostics;
88
using System.IO;
9-
using System.Linq;
109
using System.Management.Automation;
1110

1211
namespace Microsoft.PowerShell.Archive
@@ -204,10 +203,10 @@ private bool TryGetPathRelativeToCurrentWorkingDirectory(string path, out string
204203
return relativePathToWorkingDirectory is not null;
205204
}
206205

207-
internal string[]? GetResolvedPathFromPSProviderPath(string path, HashSet<string> nonexistentPaths) {
206+
internal System.Collections.ObjectModel.Collection<string>? GetResolvedPathFromPSProviderPath(string path, HashSet<string> nonexistentPaths) {
208207
// Keep the exception at the top, then when an error occurs, use the exception to create an ErrorRecord
209208
Exception? exception = null;
210-
string[]? fullyQualifiedPaths = null;
209+
System.Collections.ObjectModel.Collection<string>? fullyQualifiedPaths = null;
211210
try
212211
{
213212
// Resolve path
@@ -220,7 +219,7 @@ private bool TryGetPathRelativeToCurrentWorkingDirectory(string path, out string
220219
var exceptionMsg = ErrorMessages.GetErrorMessage(ErrorCode.InvalidPath);
221220
exception = new ArgumentException(exceptionMsg);
222221
} else {
223-
fullyQualifiedPaths = resolvedPaths.ToArray();
222+
fullyQualifiedPaths = resolvedPaths;
224223
}
225224
}
226225
catch (System.Management.Automation.ProviderNotFoundException providerNotFoundException)

0 commit comments

Comments
 (0)