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
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ private void OnWriteLine(string s)
// If the host is in "transcribe only"
// mode (due to an implicitly added call to Out-Default -Transcribe),
// then don't call the actual host API.
if (!_console.TranscribeOnly)
if (_console.TranscribeOnlyCount == 0)
{
_console.WriteLine(s);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,10 @@ protected override void BeginProcessing()
mrt.MergeUnclaimedPreviousErrorResults = true;
}

_savedTranscribeOnly = Host.UI.TranscribeOnly;
if (Transcript)
{
Host.UI.TranscribeOnly = true;
_incrementedTranscibeOnlyCount = true;
Host.UI.TranscribeOnlyCount++;
}

// This needs to be done directly through the command runtime, as Out-Default
Expand Down Expand Up @@ -143,15 +143,35 @@ protected override void EndProcessing()
}

base.EndProcessing();
}

if (Transcript)
/// <summary>
/// Revert transcription state on Dispose
/// </summary>
protected override void InternalDispose()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are not you supposed to call base.InternalDispose() at some point of overriding method?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're correct. Will fix.

{
if (_disposed)
{
return;
}

try
{
base.InternalDispose();
}
finally
{
Host.UI.TranscribeOnly = _savedTranscribeOnly;
if (_incrementedTranscibeOnlyCount && --Host.UI.TranscribeOnlyCount < 0)
{
Host.UI.TranscribeOnlyCount = 0;
}
}
_disposed = true;
}

private bool _disposed = false;
private bool _incrementedTranscibeOnlyCount = false;
private ArrayList _outVarResults = null;
private bool _savedTranscribeOnly = false;
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -386,11 +386,11 @@ internal void IgnoreCommand(string commandText, InvocationInfo invocation)
}

/// <summary>
/// Flag to determine whether the host is in "Transcribe Only" mode,
/// Count to determine whether the host is in "Transcribe Only" mode,
/// so that when content is sent through Out-Default it doesn't
/// make it to the actual host.
/// make it to the actual host if count == 0
/// </summary>
internal bool TranscribeOnly { get; set; }
internal int TranscribeOnlyCount { get; set; }

/// <summary>
/// Flag to determine whether the host is transcribing.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
Describe "Out-Default Tests" -tag CI {
BeforeAll {
# due to https://github.com/PowerShell/PowerShell/issues/3405, `Out-Default -Transcript` emits output to pipeline
# as running in Pester effectively wraps everything in parenthesis, workaround is to use another powershell
# to run the test script passed as a string
$powershell = "$PSHOME/powershell"
}

It "'Out-Default -Transcript' shows up in transcript, but not host" {
$script = @"
`$null = Start-Transcript -Path "$testdrive\transcript.txt";
'hello' | Microsoft.PowerShell.Core\Out-Default -Transcript;
'bye';
`$null = Stop-Transcript
"@

& $powershell -c $script | Should BeExactly 'bye'
"TestDrive:\transcript.txt" | Should Contain 'hello'
}

It "Out-Default reverts transcription state when used more than once in a pipeline" {
& $powershell -c "Out-Default -Transcript | Out-Default -Transcript; 'Hello'" | Should BeExactly "Hello"
}

It "Out-Default reverts transcription state when exception occurs in pipeline" {
& $powershell -c "try { & { throw } | Out-Default -Transcript } catch {}; 'Hello'" | Should BeExactly "Hello"
}

It "Out-Default reverts transcription state even if Dispose() isn't called" {
$script = @"
`$sp = {Out-Default -Transcript}.GetSteppablePipeline();
`$sp.Begin(`$false);
`$sp = `$null;
[GC]::Collect();
'hello'
"@
& $powershell -c $script | Should BeExactly 'hello'
}
}