Error message enhancement for clear-content cmdlet when targeting a directory (#8134)

This commit is contained in:
PRASOON KARUNAN V 2018-11-01 09:26:45 +05:30 committed by Ilya
parent 35f1ed0011
commit f59353d5d9
4 changed files with 15 additions and 2 deletions

View file

@ -1025,4 +1025,3 @@ namespace System.Management.Automation
}
#pragma warning restore 56500

View file

@ -6605,6 +6605,13 @@ namespace Microsoft.PowerShell.Commands
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

View file

@ -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>
@ -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>

View file

@ -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"