From e6702fe7803ea0b6d8e9433ac4ae5bd4f71dd3a7 Mon Sep 17 00:00:00 2001
From: PRASOON KARUNAN V <12897753+kvprasoon@users.noreply.github.com>
Date: Fri, 28 Sep 2018 21:06:13 +0530
Subject: [PATCH] Get/Add-Content throws improved error when targeting a
container (#7823)
---
.../namespaces/FileSystemProvider.cs | 16 ++++++++++++++++
.../resources/SessionStateStrings.resx | 6 ++++++
.../Add-Content.Tests.ps1 | 4 ++++
.../Get-Content.Tests.ps1 | 4 ++--
4 files changed, 28 insertions(+), 2 deletions(-)
diff --git a/src/System.Management.Automation/namespaces/FileSystemProvider.cs b/src/System.Management.Automation/namespaces/FileSystemProvider.cs
index 5fe799ae0..62ccb423a 100644
--- a/src/System.Management.Automation/namespaces/FileSystemProvider.cs
+++ b/src/System.Management.Automation/namespaces/FileSystemProvider.cs
@@ -6366,6 +6366,14 @@ namespace Microsoft.PowerShell.Commands
try
{
+ if (Directory.Exists(path))
+ {
+ string errMsg = StringUtil.Format(SessionStateStrings.GetContainerContentException, path);
+ ErrorRecord error = new ErrorRecord(new InvalidOperationException(errMsg), "GetContainerContentException", ErrorCategory.InvalidOperation, null);
+ WriteError(error);
+ return stream;
+ }
+
// Users can't both read as bytes, and specify a delimiter
if (delimiterSpecified)
{
@@ -6514,6 +6522,14 @@ namespace Microsoft.PowerShell.Commands
try
{
+ if (Directory.Exists(path))
+ {
+ string errMsg = StringUtil.Format(SessionStateStrings.WriteContainerContentException, path);
+ ErrorRecord error = new ErrorRecord(new InvalidOperationException(errMsg), "WriteContainerContentException", ErrorCategory.InvalidOperation, null);
+ WriteError(error);
+ return stream;
+ }
+
stream = new FileSystemContentReaderWriter(path, streamName, filemode, FileAccess.Write, FileShare.Write, encoding, usingByteEncoding, false, this, false, suppressNewline);
}
catch (PathTooLongException pathTooLong)
diff --git a/src/System.Management.Automation/resources/SessionStateStrings.resx b/src/System.Management.Automation/resources/SessionStateStrings.resx
index 855c7975e..c0ced013d 100644
--- a/src/System.Management.Automation/resources/SessionStateStrings.resx
+++ b/src/System.Management.Automation/resources/SessionStateStrings.resx
@@ -279,6 +279,12 @@
The dynamic parameters for the GetContentWriter operation cannot be retrieved for the '{0}' provider for path '{1}'. {2}
+
+ Unable to get content because it is a directory: '{0}'. Please use 'Get-ChildItem' instead.
+
+
+ Unable to write content because it is a directory: '{0}'.
+
There is no location history left to navigate backwards.
diff --git a/test/powershell/Modules/Microsoft.PowerShell.Management/Add-Content.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Management/Add-Content.Tests.ps1
index f6f76089a..53ad2499b 100644
--- a/test/powershell/Modules/Microsoft.PowerShell.Management/Add-Content.Tests.ps1
+++ b/test/powershell/Modules/Microsoft.PowerShell.Management/Add-Content.Tests.ps1
@@ -40,6 +40,10 @@ Describe "Add-Content cmdlet tests" -Tags "CI" {
{ Add-Content -Path $() -Value "ShouldNotWorkBecausePathIsInvalid" -ErrorAction Stop } | Should -Throw -ErrorId "ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.AddContentCommand"
}
+ It "Should throw an error on a directory" {
+ { Add-Content -Path . -Value "WriteContainerContentException" -ErrorAction Stop } | Should -Throw -ErrorId "WriteContainerContentException,Microsoft.PowerShell.Commands.AddContentCommand"
+ }
+
#[BugId(BugDatabase.WindowsOutOfBandReleases, 906022)]
It "should throw 'NotSupportedException' when you add-content to an unsupported provider" -Skip:($IsLinux -Or $IsMacOS) {
{ Add-Content -Path HKLM:\\software\\microsoft -Value "ShouldNotWorkBecausePathIsUnsupported" -ErrorAction Stop } | Should -Throw -ErrorId "NotSupported,Microsoft.PowerShell.Commands.AddContentCommand"
diff --git a/test/powershell/Modules/Microsoft.PowerShell.Management/Get-Content.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Management/Get-Content.Tests.ps1
index dfb389e02..aa1650e07 100644
--- a/test/powershell/Modules/Microsoft.PowerShell.Management/Get-Content.Tests.ps1
+++ b/test/powershell/Modules/Microsoft.PowerShell.Management/Get-Content.Tests.ps1
@@ -24,9 +24,9 @@ Describe "Get-Content" -Tags "CI" {
Remove-Item -Path $testPath2 -Force
}
- It "Should throw an error on a directory " {
+ It "Should throw an error on a directory" {
{ Get-Content . -ErrorAction Stop } |
- Should -Throw -ErrorId "GetContentReaderUnauthorizedAccessError,Microsoft.PowerShell.Commands.GetContentCommand"
+ Should -Throw -ErrorId "GetContainerContentException,Microsoft.PowerShell.Commands.GetContentCommand"
}
It "Should return an Object when listing only a single line and the correct information from a file" {