From f59353d5d91be841d3efc8049074bb125f25ba52 Mon Sep 17 00:00:00 2001 From: PRASOON KARUNAN V <12897753+kvprasoon@users.noreply.github.com> Date: Thu, 1 Nov 2018 09:26:45 +0530 Subject: [PATCH] Error message enhancement for clear-content cmdlet when targeting a directory (#8134) --- .../engine/SessionStateContent.cs | 1 - .../namespaces/FileSystemProvider.cs | 7 +++++++ .../resources/SessionStateStrings.resx | 5 ++++- .../Clear-Content.Tests.ps1 | 4 ++++ 4 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/System.Management.Automation/engine/SessionStateContent.cs b/src/System.Management.Automation/engine/SessionStateContent.cs index 6e845601d..37bff16ef 100644 --- a/src/System.Management.Automation/engine/SessionStateContent.cs +++ b/src/System.Management.Automation/engine/SessionStateContent.cs @@ -1025,4 +1025,3 @@ namespace System.Management.Automation } #pragma warning restore 56500 - diff --git a/src/System.Management.Automation/namespaces/FileSystemProvider.cs b/src/System.Management.Automation/namespaces/FileSystemProvider.cs index 903c752ea..8267c5e7a 100644 --- a/src/System.Management.Automation/namespaces/FileSystemProvider.cs +++ b/src/System.Management.Automation/namespaces/FileSystemProvider.cs @@ -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 diff --git a/src/System.Management.Automation/resources/SessionStateStrings.resx b/src/System.Management.Automation/resources/SessionStateStrings.resx index c0ced013d..c0307f876 100644 --- a/src/System.Management.Automation/resources/SessionStateStrings.resx +++ b/src/System.Management.Automation/resources/SessionStateStrings.resx @@ -284,7 +284,7 @@ Unable to write content because it is a directory: '{0}'. - + There is no location history left to navigate backwards. @@ -297,6 +297,9 @@ Attempting to perform the ClearContent operation on the '{0}' provider failed for path '{1}'. {2} + + Unable to clear content of '{0}' because it is a directory. Clear-Content is only supported on files. + The dynamic parameters for the ClearContent operation cannot be retrieved from the '{0}' provider for path '{1}'. {2} diff --git a/test/powershell/Modules/Microsoft.PowerShell.Management/Clear-Content.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Management/Clear-Content.Tests.ps1 index b21a744a0..93767086f 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Management/Clear-Content.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/Clear-Content.Tests.ps1 @@ -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"