Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions src/System.Management.Automation/engine/Pipe.cs
Original file line number Diff line number Diff line change
Expand Up @@ -559,15 +559,28 @@ internal object Retrieve()
else if (_enumeratorToProcess != null)
{
if (_enumeratorToProcessIsEmpty)
return AutomationNull.Value;

if (!ParserOps.MoveNext(_context, null, _enumeratorToProcess))
{
_enumeratorToProcessIsEmpty = true;
return AutomationNull.Value;
}

return ParserOps.Current(null, _enumeratorToProcess);
while (true)
{
if (!ParserOps.MoveNext(_context, errorPosition: null, _enumeratorToProcess))
{
_enumeratorToProcessIsEmpty = true;
return AutomationNull.Value;
}

object retValue = ParserOps.Current(errorPosition: null, _enumeratorToProcess);
if (retValue == AutomationNull.Value)
{
// 'AutomationNull.Value' from the enumerator won't be sent to the pipeline.
// We try to get the next value in this case.
continue;
}

return retValue;
}
}
else if (ExternalReader != null)
{
Expand Down
21 changes: 21 additions & 0 deletions test/powershell/Language/Scripting/PipelineBehaviour.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -602,3 +602,24 @@ Describe 'Function Pipeline Behaviour' -Tag 'CI' {
#>
}
}

Describe 'Other Pipeline Behaviour' -Tag 'CI' {
It "Array with 'Automation.Null' elements can be piped to pipeline" {
$automationNull = & {}

## 'Automation.Null' elements will not be sent to the pipeline (skipped).
1, $automationNull, 2, $automationNull, 3, 4, 5 | ForEach-Object { $_ } | Should -Be (1..5)
$array = 1, $automationNull, 2, $automationNull, 3, 4, 5
$array.Count | Should -Be 7
$array | ForEach-Object { $_ } | Should -Be (1..5)
}

It "Automation.Null is not written to pipeline in a function" {
$automationNull = & {}

function MyTest { 1, $automationNull, 2, $automationNull, 3, 4, 5 }
$result = MyTest
$result.Count | Should -Be 5
MyTest | Should -Be (1..5)
}
}