Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 9 commits into from Nov 1, 2018
Expand Up @@ -1025,4 +1025,3 @@ internal void ClearContent(string[] paths, bool force, bool literalPath)
}

#pragma warning restore 56500

Expand Up @@ -6605,6 +6605,13 @@ public void ClearContent(string path)

path = NormalizePath(path);

if (Directory.Exists(path))
iSazonov marked this conversation as resolved.
Show resolved Hide resolved
{
string errorMsg = StringUtil.Format(SessionStateStrings.ClearDirectoryContent, path);
WriteError(new ErrorRecord(new NotSupportedException(errorMsg), "ClearDirectoryContent", ErrorCategory.InvalidOperation, path));
return;
}

try
{
#if !UNIX
Expand Down
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
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