Skip to content

Commit 9ac701d

Browse files
hubukiSazonov
authored andcommitted
Fix conditions for transcription of Write-Information command. (#6917)
Makes a transcription of Write-Information command consistent with $InfomrationPreference variable.
1 parent 00b0af1 commit 9ac701d

File tree

6 files changed

+162
-26
lines changed

6 files changed

+162
-26
lines changed

src/Microsoft.PowerShell.Commands.Utility/commands/utility/WriteConsoleCmdlet.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,6 @@ protected override void ProcessRecord()
137137
}
138138

139139
this.WriteInformation(informationMessage, new string[] { "PSHOST" });
140-
this.Host.UI.TranscribeResult(result);
141140
}
142141

143142
private Boolean _notAppendNewline = false;

src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterfacePromptForChoice.cs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ public override int PromptForChoice(string caption, string message, Collection<C
107107
WriteChoicePrompt(hotkeysAndPlainLabels, defaultChoiceKeys, false);
108108

109109
ReadLineResult rlResult;
110-
string response = ReadLine(false, string.Empty, out rlResult, true, true);
110+
string response = ReadChoiceResponse(out rlResult);
111111

112112
if (rlResult == ReadLineResult.endedOnBreak)
113113
{
@@ -253,7 +253,7 @@ public Collection<int> PromptForChoice(string caption,
253253
WriteToConsole(PromptColor, RawUI.BackgroundColor, WrapToCurrentWindowWidth(choiceMsg));
254254

255255
ReadLineResult rlResult;
256-
string response = ReadLine(false, string.Empty, out rlResult, true, true);
256+
string response = ReadChoiceResponse(out rlResult);
257257

258258
if (rlResult == ReadLineResult.endedOnBreak)
259259
{
@@ -412,6 +412,19 @@ private void WriteChoiceHelper(string text, ConsoleColor fg, ConsoleColor bg, re
412412
WriteToConsole(fg, bg, trimEnd ? text.TrimEnd(null) : text);
413413
}
414414

415+
private string ReadChoiceResponse(out ReadLineResult result)
416+
{
417+
result = ReadLineResult.endedOnEnter;
418+
return InternalTestHooks.ForcePromptForChoiceDefaultOption
419+
? string.Empty
420+
: ReadLine(
421+
endOnTab: false,
422+
initialContent: string.Empty,
423+
result: out result,
424+
calledFromPipeline: true,
425+
transcribeResult: true);
426+
}
427+
415428
private void ShowChoiceHelp(Collection<ChoiceDescription> choices, string[,] hotkeysAndPlainLabels)
416429
{
417430
Dbg.Assert(choices != null, "choices: expected a value");
@@ -473,4 +486,3 @@ private ConsoleColor DefaultPromptColor
473486
}
474487
}
475488
} // namespace
476-

src/System.Management.Automation/engine/MshCommandRuntime.cs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -748,8 +748,7 @@ internal void WriteInformation(InformationRecord record, bool overrideInquire =
748748
//
749749
if (null == Host || null == Host.UI)
750750
{
751-
Diagnostics.Assert(false, "No host in CommandBase.WriteVerbose()");
752-
throw PSTraceSource.NewInvalidOperationException();
751+
throw PSTraceSource.NewInvalidOperationException("No host in CommandBase.WriteInformation()");
753752
}
754753

755754
CBhost.InternalUI.WriteInformationRecord(record);
@@ -821,11 +820,14 @@ internal void WriteInformation(InformationRecord record, bool overrideInquire =
821820
CBhost.InternalUI.WriteLine(record.ToString());
822821
}
823822
}
824-
else
825-
{
826-
// Only transcribe informational messages here. Transcription of PSHost-targeted messages is done in the InternalUI.Write* methods.
827-
CBhost.InternalUI.TranscribeResult(StringUtil.Format(InternalHostUserInterfaceStrings.InformationFormatString, record.ToString()));
828-
}
823+
}
824+
825+
// Both informational and PSHost-targeted messages are transcribed here.
826+
// The only difference between these two is that PSHost-targeted messages are transcribed
827+
// even if InformationAction is SilentlyContinue.
828+
if (record.Tags.Contains("PSHOST") || (preference != ActionPreference.SilentlyContinue))
829+
{
830+
CBhost.InternalUI.TranscribeResult(record.ToString());
829831
}
830832
}
831833

src/System.Management.Automation/engine/Utils.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1434,6 +1434,7 @@ public static class InternalTestHooks
14341434
internal static bool UseDebugAmsiImplementation;
14351435
internal static bool BypassAppLockerPolicyCaching;
14361436
internal static bool BypassOnlineHelpRetrieval;
1437+
internal static bool ForcePromptForChoiceDefaultOption;
14371438

14381439
// Stop/Restart/Rename Computer tests
14391440
internal static bool TestStopComputer;

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -186,9 +186,6 @@
186186
<data name="DebugFormatString" xml:space="preserve">
187187
<value>DEBUG: {0}</value>
188188
</data>
189-
<data name="InformationFormatString" xml:space="preserve">
190-
<value>INFO: {0}</value>
191-
</data>
192189
<data name="HostNotTranscribing" xml:space="preserve">
193190
<value>The host is not currently transcribing.</value>
194191
</data>

test/powershell/Modules/Microsoft.Powershell.Host/Start-Transcript.Tests.ps1

Lines changed: 137 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Describe "Start-Transcript, Stop-Transcript tests" -tags "CI" {
1616
#Add sample text to the file
1717
$content = "This is sample text!"
1818
$content | Out-File -FilePath $outputFilePath
19-
Test-Path $outputFilePath | Should be $true
19+
Test-Path $outputFilePath | Should -BeTrue
2020
}
2121

2222
try {
@@ -27,16 +27,16 @@ Describe "Start-Transcript, Stop-Transcript tests" -tags "CI" {
2727

2828
if($expectedError) {
2929
$ps.hadErrors | Should -BeTrue
30-
$ps.Streams.Error.FullyQualifiedErrorId | Should be $expectedError
30+
$ps.Streams.Error.FullyQualifiedErrorId | Should -Be $expectedError
3131
} else {
3232
$ps.addscript("Get-Date").Invoke()
3333
$ps.commands.clear()
3434
$ps.addscript("Stop-Transcript").Invoke()
3535

3636
Test-Path $outputFilePath | Should -BeTrue
37-
$outputFilePath | should FileContentMatch "Get-Date"
37+
$outputFilePath | Should -FileContentMatch "Get-Date"
3838
if($append) {
39-
$outputFilePath | Should FileContentMatch $content
39+
$outputFilePath | Should -FileContentMatch $content
4040
}
4141
}
4242
} finally {
@@ -53,6 +53,7 @@ Describe "Start-Transcript, Stop-Transcript tests" -tags "CI" {
5353

5454
AfterEach {
5555
Remove-Item $transcriptFilePath -ErrorAction SilentlyContinue
56+
[System.Management.Automation.Internal.InternalTestHooks]::SetTestHook('ForcePromptForChoiceDefaultOption', $False)
5657
}
5758

5859
It "Should create Transcript file at default path" {
@@ -122,29 +123,153 @@ Describe "Start-Transcript, Stop-Transcript tests" -tags "CI" {
122123
}
123124
}
124125

125-
Test-Path $transcriptFilePath | Should be $true
126-
$transcriptFilePath | Should FileContentMatch "After Dispose"
126+
$transcriptFilePath | Should -Exist
127+
$transcriptFilePath | Should -FileContentMatch "After Dispose"
127128
}
128129

129130
It "Transcription should be closed if the only runspace gets closed" {
130131
$powerShellPath = [System.Diagnostics.Process]::GetCurrentProcess().Path
131132
$powerShellCommand = $powerShellPath + ' -c "start-transcript $transcriptFilePath; Write-Host ''Before Dispose'';"'
132133
Invoke-Expression $powerShellCommand
133134

134-
Test-Path $transcriptFilePath | Should -BeTrue
135-
$transcriptFilePath | Should FileContentMatch "Before Dispose"
136-
$transcriptFilePath | Should FileContentMatch "PowerShell transcript end"
135+
$transcriptFilePath | Should -Exist
136+
$transcriptFilePath | Should -FileContentMatch "Before Dispose"
137+
$transcriptFilePath | Should -FileContentMatch "PowerShell transcript end"
137138
}
138139

139140
It "Transcription should record native command output" {
140141
$script = {
141142
Start-Transcript -Path $transcriptFilePath
142143
hostname
143-
Stop-Transcript }
144+
Stop-Transcript
145+
}
146+
144147
& $script
145-
Test-Path $transcriptFilePath | Should -BeTrue
146148

149+
$transcriptFilePath | Should -Exist
147150
$machineName = [System.Environment]::MachineName
148-
$transcriptFilePath | Should FileContentMatch $machineName
151+
$transcriptFilePath | Should -FileContentMatch $machineName
152+
}
153+
154+
It "Transcription should record Write-Information output when InformationAction is set to Continue" {
155+
[String]$message = New-Guid
156+
$script = {
157+
Start-Transcript -Path $transcriptFilePath
158+
Write-Information -Message $message -InformationAction Continue
159+
Stop-Transcript
160+
}
161+
162+
& $script
163+
164+
$transcriptFilePath | Should -Exist
165+
$transcriptFilePath | Should -Not -FileContentMatch "INFO: "
166+
$transcriptFilePath | Should -FileContentMatch $message
167+
}
168+
169+
It "Transcription should not record Write-Information output when InformationAction is set to SilentlyContinue" {
170+
[String]$message = New-Guid
171+
$script = {
172+
Start-Transcript -Path $transcriptFilePath
173+
Write-Information -Message $message -InformationAction SilentlyContinue
174+
Stop-Transcript
175+
}
176+
177+
& $script
178+
179+
$transcriptFilePath | Should -Exist
180+
$transcriptFilePath | Should -Not -FileContentMatch "INFO: "
181+
$transcriptFilePath | Should -Not -FileContentMatch $message
182+
}
183+
184+
It "Transcription should not record Write-Information output when InformationAction is set to Ignore" {
185+
[String]$message = New-Guid
186+
$script = {
187+
Start-Transcript -Path $transcriptFilePath
188+
Write-Information -Message $message -InformationAction Ignore
189+
Stop-Transcript
190+
}
191+
192+
& $script
193+
194+
$transcriptFilePath | Should -Exist
195+
$transcriptFilePath | Should -Not -FileContentMatch "INFO: "
196+
$transcriptFilePath | Should -Not -FileContentMatch $message
197+
}
198+
199+
It "Transcription should record Write-Information output in correct order when InformationAction is set to Inquire" {
200+
[String]$message = New-Guid
201+
$newLine = [System.Environment]::NewLine
202+
$expectedContent = "$message$($newLine)Confirm$($newLine)Continue with this operation?"
203+
$script = {
204+
[System.Management.Automation.Internal.InternalTestHooks]::SetTestHook('ForcePromptForChoiceDefaultOption', $True)
205+
Start-Transcript -Path $transcriptFilePath
206+
Write-Information -Message $message -InformationAction Inquire
207+
Stop-Transcript
208+
}
209+
210+
& $script
211+
212+
$transcriptFilePath | Should -Exist
213+
$transcriptFilePath | Should -Not -FileContentMatch "INFO: "
214+
$transcriptFilePath | Should -FileContentMatchMultiline $expectedContent
215+
}
216+
217+
It "Transcription should record Write-Host output when InformationAction is set to Continue" {
218+
[String]$message = New-Guid
219+
$script = {
220+
Start-Transcript -Path $transcriptFilePath
221+
Write-Host -Message $message -InformationAction Continue
222+
Stop-Transcript
223+
}
224+
225+
& $script
226+
227+
$transcriptFilePath | Should -Exist
228+
$transcriptFilePath | Should -FileContentMatch $message
229+
}
230+
231+
It "Transcription should record Write-Host output when InformationAction is set to SilentlyContinue" {
232+
[String]$message = New-Guid
233+
$script = {
234+
Start-Transcript -Path $transcriptFilePath
235+
Write-Host -Message $message -InformationAction SilentlyContinue
236+
Stop-Transcript
237+
}
238+
239+
& $script
240+
241+
$transcriptFilePath | Should -Exist
242+
$transcriptFilePath | Should -FileContentMatch $message
243+
}
244+
245+
It "Transcription should not record Write-Host output when InformationAction is set to Ignore" {
246+
[String]$message = New-Guid
247+
$script = {
248+
Start-Transcript -Path $transcriptFilePath
249+
Write-Host -Message $message -InformationAction Ignore
250+
Stop-Transcript
251+
}
252+
253+
& $script
254+
255+
$transcriptFilePath | Should -Exist
256+
$transcriptFilePath | Should -Not -FileContentMatch $message
257+
}
258+
259+
It "Transcription should record Write-Host output in correct order when InformationAction is set to Inquire" {
260+
[String]$message = New-Guid
261+
$newLine = [System.Environment]::NewLine
262+
$expectedContent = "$message$($newLine)Confirm$($newLine)Continue with this operation?"
263+
$script = {
264+
[System.Management.Automation.Internal.InternalTestHooks]::SetTestHook('ForcePromptForChoiceDefaultOption', $True)
265+
Start-Transcript -Path $transcriptFilePath
266+
Write-Host -Message $message -InformationAction Inquire
267+
Stop-Transcript
268+
}
269+
270+
& $script
271+
272+
$transcriptFilePath | Should -Exist
273+
$transcriptFilePath | Should -FileContentMatchMultiline $expectedContent
149274
}
150275
}

0 commit comments

Comments
 (0)