Skip to content

Commit

Permalink
Error message enhancement for clear-content cmdlet when targeting a d…
Browse files Browse the repository at this point in the history
…irectory (PowerShell#8134)
  • Loading branch information
kvprasoon authored and iSazonov committed Nov 1, 2018
1 parent 35f1ed0 commit f59353d
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1025,4 +1025,3 @@ private object ClearContentDynamicParameters(
}

#pragma warning restore 56500

Original file line number Diff line number Diff line change
Expand Up @@ -6605,6 +6605,13 @@ public void ClearContent(string path)

path = NormalizePath(path);

if (Directory.Exists(path))
{
string errorMsg = StringUtil.Format(SessionStateStrings.ClearDirectoryContent, path);
WriteError(new ErrorRecord(new NotSupportedException(errorMsg), "ClearDirectoryContent", ErrorCategory.InvalidOperation, path));
return;
}

try
{
#if !UNIX
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@
</data>
<data name="WriteContainerContentException" xml:space="preserve">
<value>Unable to write content because it is a directory: '{0}'.</value>
</data>
</data>
<data name="LocationUndoStackIsEmpty" xml:space="preserve">
<value>There is no location history left to navigate backwards.</value>
</data>
Expand All @@ -297,6 +297,9 @@
<data name="ClearContentProviderException" xml:space="preserve">
<value>Attempting to perform the ClearContent operation on the '{0}' provider failed for path '{1}'. {2}</value>
</data>
<data name="ClearDirectoryContent" xml:space="preserve">
<value>Unable to clear content of '{0}' because it is a directory. Clear-Content is only supported on files.</value>
</data>
<data name="ClearContentDynamicParametersProviderException" xml:space="preserve">
<value>The dynamic parameters for the ClearContent operation cannot be retrieved from the '{0}' provider for path '{1}'. {2}</value>
</data>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ Describe "Clear-Content cmdlet tests" -Tags "CI" {
}

Context "Proper errors should be delivered when bad locations are specified" {
It "should throw when targetting a directory." {
{ Clear-Content -Path . -ErrorAction Stop } | Should -Throw -ErrorId "ClearDirectoryContent"
}

It "should throw `"Cannot bind argument to parameter 'Path'`" when -Path is `$null" {
{ Clear-Content -Path $null -ErrorAction Stop } |
Should -Throw -ErrorId "ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.ClearContentCommand"
Expand Down

0 comments on commit f59353d

Please sign in to comment.