Skip to content

Commit e6702fe

Browse files
kvprasooniSazonov
authored andcommitted
Get/Add-Content throws improved error when targeting a container (#7823)
1 parent bd5f771 commit e6702fe

4 files changed

Lines changed: 28 additions & 2 deletions

File tree

src/System.Management.Automation/namespaces/FileSystemProvider.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6366,6 +6366,14 @@ public IContentReader GetContentReader(string path)
63666366

63676367
try
63686368
{
6369+
if (Directory.Exists(path))
6370+
{
6371+
string errMsg = StringUtil.Format(SessionStateStrings.GetContainerContentException, path);
6372+
ErrorRecord error = new ErrorRecord(new InvalidOperationException(errMsg), "GetContainerContentException", ErrorCategory.InvalidOperation, null);
6373+
WriteError(error);
6374+
return stream;
6375+
}
6376+
63696377
// Users can't both read as bytes, and specify a delimiter
63706378
if (delimiterSpecified)
63716379
{
@@ -6514,6 +6522,14 @@ public IContentWriter GetContentWriter(string path)
65146522

65156523
try
65166524
{
6525+
if (Directory.Exists(path))
6526+
{
6527+
string errMsg = StringUtil.Format(SessionStateStrings.WriteContainerContentException, path);
6528+
ErrorRecord error = new ErrorRecord(new InvalidOperationException(errMsg), "WriteContainerContentException", ErrorCategory.InvalidOperation, null);
6529+
WriteError(error);
6530+
return stream;
6531+
}
6532+
65176533
stream = new FileSystemContentReaderWriter(path, streamName, filemode, FileAccess.Write, FileShare.Write, encoding, usingByteEncoding, false, this, false, suppressNewline);
65186534
}
65196535
catch (PathTooLongException pathTooLong)

src/System.Management.Automation/resources/SessionStateStrings.resx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,12 @@
279279
<data name="GetContentWriterDynamicParametersProviderException" xml:space="preserve">
280280
<value>The dynamic parameters for the GetContentWriter operation cannot be retrieved for the '{0}' provider for path '{1}'. {2}</value>
281281
</data>
282+
<data name="GetContainerContentException" xml:space="preserve">
283+
<value>Unable to get content because it is a directory: '{0}'. Please use 'Get-ChildItem' instead.</value>
284+
</data>
285+
<data name="WriteContainerContentException" xml:space="preserve">
286+
<value>Unable to write content because it is a directory: '{0}'.</value>
287+
</data>
282288
<data name="LocationUndoStackIsEmpty" xml:space="preserve">
283289
<value>There is no location history left to navigate backwards.</value>
284290
</data>

test/powershell/Modules/Microsoft.PowerShell.Management/Add-Content.Tests.ps1

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ Describe "Add-Content cmdlet tests" -Tags "CI" {
4040
{ Add-Content -Path $() -Value "ShouldNotWorkBecausePathIsInvalid" -ErrorAction Stop } | Should -Throw -ErrorId "ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.AddContentCommand"
4141
}
4242

43+
It "Should throw an error on a directory" {
44+
{ Add-Content -Path . -Value "WriteContainerContentException" -ErrorAction Stop } | Should -Throw -ErrorId "WriteContainerContentException,Microsoft.PowerShell.Commands.AddContentCommand"
45+
}
46+
4347
#[BugId(BugDatabase.WindowsOutOfBandReleases, 906022)]
4448
It "should throw 'NotSupportedException' when you add-content to an unsupported provider" -Skip:($IsLinux -Or $IsMacOS) {
4549
{ Add-Content -Path HKLM:\\software\\microsoft -Value "ShouldNotWorkBecausePathIsUnsupported" -ErrorAction Stop } | Should -Throw -ErrorId "NotSupported,Microsoft.PowerShell.Commands.AddContentCommand"

test/powershell/Modules/Microsoft.PowerShell.Management/Get-Content.Tests.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ Describe "Get-Content" -Tags "CI" {
2424
Remove-Item -Path $testPath2 -Force
2525
}
2626

27-
It "Should throw an error on a directory " {
27+
It "Should throw an error on a directory" {
2828
{ Get-Content . -ErrorAction Stop } |
29-
Should -Throw -ErrorId "GetContentReaderUnauthorizedAccessError,Microsoft.PowerShell.Commands.GetContentCommand"
29+
Should -Throw -ErrorId "GetContainerContentException,Microsoft.PowerShell.Commands.GetContentCommand"
3030
}
3131

3232
It "Should return an Object when listing only a single line and the correct information from a file" {

0 commit comments

Comments
 (0)