Skip to content
Closed
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
76 changes: 52 additions & 24 deletions src/System.Management.Automation/engine/MshCommandRuntime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -930,8 +930,6 @@ internal void SetupOutVariable()
{
this.OutputPipe.AddVariableList(VariableStreamKind.Output, _outVarList);
}

_state.PSVariable.Set(this.OutVariable, _outVarList);
}
else
{
Expand Down Expand Up @@ -2498,34 +2496,21 @@ internal void SetupVariable(VariableStreamKind streamKind, string variableName,

if (variableName.StartsWith("+", StringComparison.Ordinal))
{
varList = new ArrayList();

variableName = variableName.Substring(1);
object oldValue = PSObject.Base(_state.PSVariable.GetValue(variableName));
varList = oldValue as IList;
if (null == varList)
{
varList = new ArrayList();

if (null != oldValue && AutomationNull.Value != oldValue)
if (oldValue is object[] oldArray)
{
foreach (object streamObject in oldArray)
{
IEnumerable enumerable = LanguagePrimitives.GetEnumerable(oldValue);
if (null != enumerable)
{
foreach (object o in enumerable)
{
varList.Add(o);
}
}
else
{
varList.Add(oldValue);
}
varList.Add(streamObject);
}
}
else if (varList.IsFixedSize)
else if (oldValue != null && oldValue != AutomationNull.Value)
{
ArrayList varListNew = new ArrayList();
varListNew.AddRange(varList);
varList = varListNew;
varList.Add(oldValue);
}
}
else
Expand All @@ -2537,7 +2522,6 @@ internal void SetupVariable(VariableStreamKind streamKind, string variableName,
{
this.OutputPipe.AddVariableList(streamKind, varList);
}
_state.PSVariable.Set(variableName, varList);
} // SetupVariable

/// <summary>
Expand Down Expand Up @@ -3677,21 +3661,25 @@ internal void RemoveVariableListsInPipe()

if (_outVarList != null)
{
SetNonArrayListVariable(OutVariable, _outVarList);
this.OutputPipe.RemoveVariableList(VariableStreamKind.Output, _outVarList);
}

if (_errorVarList != null)
{
SetNonArrayListVariable(ErrorVariable, _errorVarList);
this.OutputPipe.RemoveVariableList(VariableStreamKind.Error, _errorVarList);
}

if (_warningVarList != null)
{
SetNonArrayListVariable(WarningVariable, _warningVarList);
this.OutputPipe.RemoveVariableList(VariableStreamKind.Warning, _warningVarList);
}

if (_informationVarList != null)
{
SetNonArrayListVariable(InformationVariable, _informationVarList);
this.OutputPipe.RemoveVariableList(VariableStreamKind.Information, _informationVarList);
}

Expand All @@ -3701,5 +3689,45 @@ internal void RemoveVariableListsInPipe()
_state.PSVariable.Remove(this.PipelineVariable);
}
}

/// <summary>
/// Stream variables are setup as ArrayLists, but we want this to be
/// transparent so that the following are indistinguishable:
///
/// $ov = Write-Output @(1,2); $ov.GetType().FullName
///
/// Write-Output -OutVariable ov @(1,2); $ov.GetType().FullName
///
/// So when we clean up internally, set the variables back to the
/// ordinary PowerShell return types
/// </summary>
/// <param name="variableName">the name of the variable to set</param>
/// <param name="varList">the variable value stored in the ArrayList</param>
private void SetNonArrayListVariable(string variableName, IList varList)
{
if (variableName == null)
{
return;
}

// No values -> output is null
if (varList.Count == 0)
{
_state.PSVariable.Set(variableName, null);
return;
}

// One value -> output is object
if (varList.Count == 1)
{
_state.PSVariable.Set(variableName, varList[0]);
return;
}

// Multiple items in the list -> output is object[]
var valArray = new object[varList.Count];
varList.CopyTo(valArray, 0);
_state.PSVariable.Set(variableName.StartsWith('+') ? variableName.Substring(1) : variableName, valArray);
}
}
}
40 changes: 40 additions & 0 deletions test/powershell/Language/Scripting/OutErrorVariable.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,46 @@ Describe "Tests OutVariable only" -Tags "CI" {
$script:a | Should Be 'foo'
$b | Should Be @("bar", "foo")
}

It "OutVariable type test: <Name>" -TestCases @(
@{ Name = 'Pass single object through OutVariable';
Value = 'a';
Expected = 'a';
ExpectedType = 'System.String'
},
@{ Name = 'Pass array of objects through OutVariable';
Value = @(1, 2, 3);
Expected = @(1, 2, 3);
ExpectedType = 'System.Object[]'
},
@{ Name = 'Pass collection of objects through OutVariable';
Value = [System.Collections.ArrayList]::new(@(1, 2, 3));
Expected = @(1, 2, 3);
ExpectedType = 'System.Object[]'
},
@{ Name = 'Pass lifted collection of object through OutVariable';
Value = @(, [System.Collections.ArrayList]::new(@(1, 2, 3)));
Expected = @(1, 2, 3);
ExpectedType = 'System.Collections.ArrayList'
},
@{ Name = 'Pass $null through OutVariable';
Value = $null;
Expected = $null
},
@{ Name = 'Pass AutomationNull.Value through OutVariable';
Value = [System.Management.Automation.Internal.AutomationNull]::Value;
Expected = $null
}
) {
param($Name, $Value, $Expected, $ExpectedType)

$null = Write-Output -OutVariable outVar $Value
$outVar | Should -BeExactly $Expected
if ($Value)
{
$outVar.GetType().FullName | Should -BeExactly $ExpectedType
}
}
}

Describe "Test ErrorVariable only" -Tags "CI" {
Expand Down