Error message enhancement for clear-content cmdlet when targeting a directory (#8134)
This commit is contained in:
parent
35f1ed0011
commit
f59353d5d9
|
@ -1025,4 +1025,3 @@ namespace System.Management.Automation
|
||||||
}
|
}
|
||||||
|
|
||||||
#pragma warning restore 56500
|
#pragma warning restore 56500
|
||||||
|
|
||||||
|
|
|
@ -6605,6 +6605,13 @@ namespace Microsoft.PowerShell.Commands
|
||||||
|
|
||||||
path = NormalizePath(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
|
try
|
||||||
{
|
{
|
||||||
#if !UNIX
|
#if !UNIX
|
||||||
|
|
|
@ -284,7 +284,7 @@
|
||||||
</data>
|
</data>
|
||||||
<data name="WriteContainerContentException" xml:space="preserve">
|
<data name="WriteContainerContentException" xml:space="preserve">
|
||||||
<value>Unable to write content because it is a directory: '{0}'.</value>
|
<value>Unable to write content because it is a directory: '{0}'.</value>
|
||||||
</data>
|
</data>
|
||||||
<data name="LocationUndoStackIsEmpty" xml:space="preserve">
|
<data name="LocationUndoStackIsEmpty" xml:space="preserve">
|
||||||
<value>There is no location history left to navigate backwards.</value>
|
<value>There is no location history left to navigate backwards.</value>
|
||||||
</data>
|
</data>
|
||||||
|
@ -297,6 +297,9 @@
|
||||||
<data name="ClearContentProviderException" xml:space="preserve">
|
<data name="ClearContentProviderException" xml:space="preserve">
|
||||||
<value>Attempting to perform the ClearContent operation on the '{0}' provider failed for path '{1}'. {2}</value>
|
<value>Attempting to perform the ClearContent operation on the '{0}' provider failed for path '{1}'. {2}</value>
|
||||||
</data>
|
</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">
|
<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>
|
<value>The dynamic parameters for the ClearContent operation cannot be retrieved from the '{0}' provider for path '{1}'. {2}</value>
|
||||||
</data>
|
</data>
|
||||||
|
|
|
@ -105,6 +105,10 @@ Describe "Clear-Content cmdlet tests" -Tags "CI" {
|
||||||
}
|
}
|
||||||
|
|
||||||
Context "Proper errors should be delivered when bad locations are specified" {
|
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" {
|
It "should throw `"Cannot bind argument to parameter 'Path'`" when -Path is `$null" {
|
||||||
{ Clear-Content -Path $null -ErrorAction Stop } |
|
{ Clear-Content -Path $null -ErrorAction Stop } |
|
||||||
Should -Throw -ErrorId "ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.ClearContentCommand"
|
Should -Throw -ErrorId "ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.ClearContentCommand"
|
||||||
|
|
Loading…
Reference in a new issue