From 56386b154d3023e7060386e972c48a5921cbe521 Mon Sep 17 00:00:00 2001 From: Hubert Bukowski Date: Tue, 22 May 2018 07:49:51 +0200 Subject: [PATCH 01/12] Fixed transcription of Write-Information command. --- .../engine/MshCommandRuntime.cs | 3 ++- .../Start-Transcript.Tests.ps1 | 12 ++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/System.Management.Automation/engine/MshCommandRuntime.cs b/src/System.Management.Automation/engine/MshCommandRuntime.cs index 90ddd5927a3..1891facc0b7 100644 --- a/src/System.Management.Automation/engine/MshCommandRuntime.cs +++ b/src/System.Management.Automation/engine/MshCommandRuntime.cs @@ -821,7 +821,8 @@ internal void WriteInformation(InformationRecord record, bool overrideInquire = CBhost.InternalUI.WriteLine(record.ToString()); } } - else + + if (!record.Tags.Contains("PSHOST") && (preference == ActionPreference.Continue)) { // Only transcribe informational messages here. Transcription of PSHost-targeted messages is done in the InternalUI.Write* methods. CBhost.InternalUI.TranscribeResult(StringUtil.Format(InternalHostUserInterfaceStrings.InformationFormatString, record.ToString())); diff --git a/test/powershell/Modules/Microsoft.Powershell.Host/Start-Transcript.Tests.ps1 b/test/powershell/Modules/Microsoft.Powershell.Host/Start-Transcript.Tests.ps1 index 5b4b3ab233d..96be4c64071 100644 --- a/test/powershell/Modules/Microsoft.Powershell.Host/Start-Transcript.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.Powershell.Host/Start-Transcript.Tests.ps1 @@ -147,4 +147,16 @@ Describe "Start-Transcript, Stop-Transcript tests" -tags "CI" { $machineName = [System.Environment]::MachineName $transcriptFilePath | Should FileContentMatch $machineName } + + It "Transcription should record Write-Information output when preference is set to Continue" { + $script = { + Start-Transcript -Path $transcriptFilePath + $InformationPreference = 'Continue' + Write-Information 'Continue' + Stop-Transcript } + & $script + Test-Path $transcriptFilePath | Should -BeTrue + + $transcriptFilePath | Should FileContentMatch 'Continue' + } } From 7536e3f94104b2eb7d27a29d6aa00ddf4c1c1739 Mon Sep 17 00:00:00 2001 From: Hubert Bukowski Date: Wed, 23 May 2018 21:02:09 +0200 Subject: [PATCH 02/12] Used new syntax for Should in Start-Transcript tests. --- .../Start-Transcript.Tests.ps1 | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/test/powershell/Modules/Microsoft.Powershell.Host/Start-Transcript.Tests.ps1 b/test/powershell/Modules/Microsoft.Powershell.Host/Start-Transcript.Tests.ps1 index 96be4c64071..95e558f3c40 100644 --- a/test/powershell/Modules/Microsoft.Powershell.Host/Start-Transcript.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.Powershell.Host/Start-Transcript.Tests.ps1 @@ -16,7 +16,7 @@ Describe "Start-Transcript, Stop-Transcript tests" -tags "CI" { #Add sample text to the file $content = "This is sample text!" $content | Out-File -FilePath $outputFilePath - Test-Path $outputFilePath | Should be $true + Test-Path $outputFilePath | Should -BeTrue } try { @@ -27,16 +27,16 @@ Describe "Start-Transcript, Stop-Transcript tests" -tags "CI" { if($expectedError) { $ps.hadErrors | Should -BeTrue - $ps.Streams.Error.FullyQualifiedErrorId | Should be $expectedError + $ps.Streams.Error.FullyQualifiedErrorId | Should -Be $expectedError } else { $ps.addscript("Get-Date").Invoke() $ps.commands.clear() $ps.addscript("Stop-Transcript").Invoke() Test-Path $outputFilePath | Should -BeTrue - $outputFilePath | should FileContentMatch "Get-Date" + $outputFilePath | Should -FileContentMatch "Get-Date" if($append) { - $outputFilePath | Should FileContentMatch $content + $outputFilePath | Should -FileContentMatch $content } } } finally { @@ -122,8 +122,8 @@ Describe "Start-Transcript, Stop-Transcript tests" -tags "CI" { } } - Test-Path $transcriptFilePath | Should be $true - $transcriptFilePath | Should FileContentMatch "After Dispose" + Test-Path $transcriptFilePath | Should -BeTrue + $transcriptFilePath | Should -FileContentMatch "After Dispose" } It "Transcription should be closed if the only runspace gets closed" { @@ -132,8 +132,8 @@ Describe "Start-Transcript, Stop-Transcript tests" -tags "CI" { Invoke-Expression $powerShellCommand Test-Path $transcriptFilePath | Should -BeTrue - $transcriptFilePath | Should FileContentMatch "Before Dispose" - $transcriptFilePath | Should FileContentMatch "PowerShell transcript end" + $transcriptFilePath | Should -FileContentMatch "Before Dispose" + $transcriptFilePath | Should -FileContentMatch "PowerShell transcript end" } It "Transcription should record native command output" { @@ -145,9 +145,9 @@ Describe "Start-Transcript, Stop-Transcript tests" -tags "CI" { Test-Path $transcriptFilePath | Should -BeTrue $machineName = [System.Environment]::MachineName - $transcriptFilePath | Should FileContentMatch $machineName + $transcriptFilePath | Should -FileContentMatch $machineName } - + It "Transcription should record Write-Information output when preference is set to Continue" { $script = { Start-Transcript -Path $transcriptFilePath @@ -157,6 +157,6 @@ Describe "Start-Transcript, Stop-Transcript tests" -tags "CI" { & $script Test-Path $transcriptFilePath | Should -BeTrue - $transcriptFilePath | Should FileContentMatch 'Continue' + $transcriptFilePath | Should -FileContentMatch 'Continue' } } From 2fe68a6c2c1eef327cfc65e7570e6ed3845fbc1a Mon Sep 17 00:00:00 2001 From: Hubert Bukowski Date: Thu, 24 May 2018 22:42:43 +0200 Subject: [PATCH 03/12] Transcribe redirected information records. --- .../engine/MshCommandRuntime.cs | 10 +++--- .../Start-Transcript.Tests.ps1 | 32 ++++++++++++++++--- 2 files changed, 33 insertions(+), 9 deletions(-) diff --git a/src/System.Management.Automation/engine/MshCommandRuntime.cs b/src/System.Management.Automation/engine/MshCommandRuntime.cs index 1891facc0b7..bfaff0620cd 100644 --- a/src/System.Management.Automation/engine/MshCommandRuntime.cs +++ b/src/System.Management.Automation/engine/MshCommandRuntime.cs @@ -821,12 +821,12 @@ internal void WriteInformation(InformationRecord record, bool overrideInquire = CBhost.InternalUI.WriteLine(record.ToString()); } } + } - if (!record.Tags.Contains("PSHOST") && (preference == ActionPreference.Continue)) - { - // Only transcribe informational messages here. Transcription of PSHost-targeted messages is done in the InternalUI.Write* methods. - CBhost.InternalUI.TranscribeResult(StringUtil.Format(InternalHostUserInterfaceStrings.InformationFormatString, record.ToString())); - } + if (!record.Tags.Contains("PSHOST") && (preference == ActionPreference.Continue)) + { + // Only transcribe informational messages here. Transcription of PSHost-targeted messages is done in the InternalUI.Write* methods. + CBhost.InternalUI.TranscribeResult(StringUtil.Format(InternalHostUserInterfaceStrings.InformationFormatString, record.ToString())); } } diff --git a/test/powershell/Modules/Microsoft.Powershell.Host/Start-Transcript.Tests.ps1 b/test/powershell/Modules/Microsoft.Powershell.Host/Start-Transcript.Tests.ps1 index 95e558f3c40..cb1b4ac7b9d 100644 --- a/test/powershell/Modules/Microsoft.Powershell.Host/Start-Transcript.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.Powershell.Host/Start-Transcript.Tests.ps1 @@ -148,15 +148,39 @@ Describe "Start-Transcript, Stop-Transcript tests" -tags "CI" { $transcriptFilePath | Should -FileContentMatch $machineName } - It "Transcription should record Write-Information output when preference is set to Continue" { + It "Transcription should record Write-Information output when InformationAction is set to Continue" { + [String]$message = New-Guid $script = { Start-Transcript -Path $transcriptFilePath - $InformationPreference = 'Continue' - Write-Information 'Continue' + Write-Information -Message $message -InformationAction Continue Stop-Transcript } & $script Test-Path $transcriptFilePath | Should -BeTrue - $transcriptFilePath | Should -FileContentMatch 'Continue' + $transcriptFilePath | Should -FileContentMatch $message + } + + It "Transcription should not record Write-Information output when InformationAction is set to SilentlyContinue" { + [String]$message = New-Guid + $script = { + Start-Transcript -Path $transcriptFilePath + Write-Information -Message $message -InformationAction SilentlyContinue + Stop-Transcript } + & $script + Test-Path $transcriptFilePath | Should -BeTrue + + $transcriptFilePath | Should -Not -FileContentMatch $message + } + + It "Transcription should not record Write-Information output when InformationAction is set to Ignore" { + [String]$message = New-Guid + $script = { + Start-Transcript -Path $transcriptFilePath + Write-Information -Message $message -InformationAction Ignore + Stop-Transcript } + & $script + Test-Path $transcriptFilePath | Should -BeTrue + + $transcriptFilePath | Should -Not -FileContentMatch $message } } From 6f5b4a12cdc2d70085be74257121ac9a782239bd Mon Sep 17 00:00:00 2001 From: Hubert Bukowski Date: Sun, 27 May 2018 21:17:31 +0200 Subject: [PATCH 04/12] Added tests for Write-Host transcription. --- .../Start-Transcript.Tests.ps1 | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/test/powershell/Modules/Microsoft.Powershell.Host/Start-Transcript.Tests.ps1 b/test/powershell/Modules/Microsoft.Powershell.Host/Start-Transcript.Tests.ps1 index cb1b4ac7b9d..e18437c0d3e 100644 --- a/test/powershell/Modules/Microsoft.Powershell.Host/Start-Transcript.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.Powershell.Host/Start-Transcript.Tests.ps1 @@ -183,4 +183,28 @@ Describe "Start-Transcript, Stop-Transcript tests" -tags "CI" { $transcriptFilePath | Should -Not -FileContentMatch $message } + + It "Transcription should record Write-Host output when InformationAction is set to Continue" { + [String]$message = New-Guid + $script = { + Start-Transcript -Path $transcriptFilePath + Write-Host -Message $message -InformationAction Continue + Stop-Transcript } + & $script + Test-Path $transcriptFilePath | Should -BeTrue + + $transcriptFilePath | Should -FileContentMatch $message + } + + It "Transcription should record Write-Host output when InformationAction is set to SilentlyContinue" { + [String]$message = New-Guid + $script = { + Start-Transcript -Path $transcriptFilePath + Write-Host -Message $message -InformationAction SilentlyContinue + Stop-Transcript } + & $script + Test-Path $transcriptFilePath | Should -BeTrue + + $transcriptFilePath | Should -FileContentMatch $message + } } From 16d7181657941c9a53779323b0b52cf6937c3378 Mon Sep 17 00:00:00 2001 From: Hubert Bukowski Date: Mon, 28 May 2018 09:39:56 +0200 Subject: [PATCH 05/12] Added test for transcription of Write-Host -InformationAction Ignore. --- .../Start-Transcript.Tests.ps1 | 22 ++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/test/powershell/Modules/Microsoft.Powershell.Host/Start-Transcript.Tests.ps1 b/test/powershell/Modules/Microsoft.Powershell.Host/Start-Transcript.Tests.ps1 index e18437c0d3e..fe228751c32 100644 --- a/test/powershell/Modules/Microsoft.Powershell.Host/Start-Transcript.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.Powershell.Host/Start-Transcript.Tests.ps1 @@ -142,8 +142,8 @@ Describe "Start-Transcript, Stop-Transcript tests" -tags "CI" { hostname Stop-Transcript } & $script - Test-Path $transcriptFilePath | Should -BeTrue + Test-Path $transcriptFilePath | Should -BeTrue $machineName = [System.Environment]::MachineName $transcriptFilePath | Should -FileContentMatch $machineName } @@ -155,8 +155,8 @@ Describe "Start-Transcript, Stop-Transcript tests" -tags "CI" { Write-Information -Message $message -InformationAction Continue Stop-Transcript } & $script - Test-Path $transcriptFilePath | Should -BeTrue + Test-Path $transcriptFilePath | Should -BeTrue $transcriptFilePath | Should -FileContentMatch $message } @@ -167,8 +167,8 @@ Describe "Start-Transcript, Stop-Transcript tests" -tags "CI" { Write-Information -Message $message -InformationAction SilentlyContinue Stop-Transcript } & $script - Test-Path $transcriptFilePath | Should -BeTrue + Test-Path $transcriptFilePath | Should -BeTrue $transcriptFilePath | Should -Not -FileContentMatch $message } @@ -179,8 +179,8 @@ Describe "Start-Transcript, Stop-Transcript tests" -tags "CI" { Write-Information -Message $message -InformationAction Ignore Stop-Transcript } & $script - Test-Path $transcriptFilePath | Should -BeTrue + Test-Path $transcriptFilePath | Should -BeTrue $transcriptFilePath | Should -Not -FileContentMatch $message } @@ -191,8 +191,8 @@ Describe "Start-Transcript, Stop-Transcript tests" -tags "CI" { Write-Host -Message $message -InformationAction Continue Stop-Transcript } & $script - Test-Path $transcriptFilePath | Should -BeTrue + Test-Path $transcriptFilePath | Should -BeTrue $transcriptFilePath | Should -FileContentMatch $message } @@ -203,8 +203,20 @@ Describe "Start-Transcript, Stop-Transcript tests" -tags "CI" { Write-Host -Message $message -InformationAction SilentlyContinue Stop-Transcript } & $script + Test-Path $transcriptFilePath | Should -BeTrue + $transcriptFilePath | Should -FileContentMatch $message + } + + It "Transcription should record Write-Host output when InformationAction is set to Ignore" { + [String]$message = New-Guid + $script = { + Start-Transcript -Path $transcriptFilePath + Write-Host -Message $message -InformationAction Ignore + Stop-Transcript } + & $script + Test-Path $transcriptFilePath | Should -BeTrue $transcriptFilePath | Should -FileContentMatch $message } } From d9e8f24cf0ebdef1d5e6fe5e4a183e006f96a090 Mon Sep 17 00:00:00 2001 From: Hubert Bukowski Date: Mon, 28 May 2018 09:44:13 +0200 Subject: [PATCH 06/12] Removed INFO: prefix from Write-Information transcription. --- src/System.Management.Automation/engine/MshCommandRuntime.cs | 2 +- .../resources/InternalHostUserInterfaceStrings.resx | 3 --- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/src/System.Management.Automation/engine/MshCommandRuntime.cs b/src/System.Management.Automation/engine/MshCommandRuntime.cs index bfaff0620cd..b876ed4fc6b 100644 --- a/src/System.Management.Automation/engine/MshCommandRuntime.cs +++ b/src/System.Management.Automation/engine/MshCommandRuntime.cs @@ -826,7 +826,7 @@ internal void WriteInformation(InformationRecord record, bool overrideInquire = if (!record.Tags.Contains("PSHOST") && (preference == ActionPreference.Continue)) { // Only transcribe informational messages here. Transcription of PSHost-targeted messages is done in the InternalUI.Write* methods. - CBhost.InternalUI.TranscribeResult(StringUtil.Format(InternalHostUserInterfaceStrings.InformationFormatString, record.ToString())); + CBhost.InternalUI.TranscribeResult(record.ToString()); } } diff --git a/src/System.Management.Automation/resources/InternalHostUserInterfaceStrings.resx b/src/System.Management.Automation/resources/InternalHostUserInterfaceStrings.resx index b55e117bac8..a40b58cebcb 100644 --- a/src/System.Management.Automation/resources/InternalHostUserInterfaceStrings.resx +++ b/src/System.Management.Automation/resources/InternalHostUserInterfaceStrings.resx @@ -186,9 +186,6 @@ DEBUG: {0} - - INFO: {0} - The host is not currently transcribing. From 056446d29a465b7983d1ece00d58f5862af41a26 Mon Sep 17 00:00:00 2001 From: Hubert Bukowski Date: Mon, 28 May 2018 10:48:37 +0200 Subject: [PATCH 07/12] Fixed transcription for Write-Information -InformationAction Inquire. --- src/System.Management.Automation/engine/MshCommandRuntime.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/System.Management.Automation/engine/MshCommandRuntime.cs b/src/System.Management.Automation/engine/MshCommandRuntime.cs index b876ed4fc6b..d8ec16f318f 100644 --- a/src/System.Management.Automation/engine/MshCommandRuntime.cs +++ b/src/System.Management.Automation/engine/MshCommandRuntime.cs @@ -822,8 +822,8 @@ internal void WriteInformation(InformationRecord record, bool overrideInquire = } } } - - if (!record.Tags.Contains("PSHOST") && (preference == ActionPreference.Continue)) + + if (!record.Tags.Contains("PSHOST") && (preference != ActionPreference.SilentlyContinue)) { // Only transcribe informational messages here. Transcription of PSHost-targeted messages is done in the InternalUI.Write* methods. CBhost.InternalUI.TranscribeResult(record.ToString()); From 2c6d0244aab1bc2d7ee6bde5c411c57bc3ea379a Mon Sep 17 00:00:00 2001 From: Hubert Bukowski Date: Mon, 28 May 2018 10:57:22 +0200 Subject: [PATCH 08/12] Fixed script block code format. --- .../Start-Transcript.Tests.ps1 | 28 ++++++++++++++----- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/test/powershell/Modules/Microsoft.Powershell.Host/Start-Transcript.Tests.ps1 b/test/powershell/Modules/Microsoft.Powershell.Host/Start-Transcript.Tests.ps1 index fe228751c32..63a26ab75e5 100644 --- a/test/powershell/Modules/Microsoft.Powershell.Host/Start-Transcript.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.Powershell.Host/Start-Transcript.Tests.ps1 @@ -140,7 +140,9 @@ Describe "Start-Transcript, Stop-Transcript tests" -tags "CI" { $script = { Start-Transcript -Path $transcriptFilePath hostname - Stop-Transcript } + Stop-Transcript + } + & $script Test-Path $transcriptFilePath | Should -BeTrue @@ -153,7 +155,9 @@ Describe "Start-Transcript, Stop-Transcript tests" -tags "CI" { $script = { Start-Transcript -Path $transcriptFilePath Write-Information -Message $message -InformationAction Continue - Stop-Transcript } + Stop-Transcript + } + & $script Test-Path $transcriptFilePath | Should -BeTrue @@ -165,7 +169,9 @@ Describe "Start-Transcript, Stop-Transcript tests" -tags "CI" { $script = { Start-Transcript -Path $transcriptFilePath Write-Information -Message $message -InformationAction SilentlyContinue - Stop-Transcript } + Stop-Transcript + } + & $script Test-Path $transcriptFilePath | Should -BeTrue @@ -177,7 +183,9 @@ Describe "Start-Transcript, Stop-Transcript tests" -tags "CI" { $script = { Start-Transcript -Path $transcriptFilePath Write-Information -Message $message -InformationAction Ignore - Stop-Transcript } + Stop-Transcript + } + & $script Test-Path $transcriptFilePath | Should -BeTrue @@ -189,7 +197,9 @@ Describe "Start-Transcript, Stop-Transcript tests" -tags "CI" { $script = { Start-Transcript -Path $transcriptFilePath Write-Host -Message $message -InformationAction Continue - Stop-Transcript } + Stop-Transcript + } + & $script Test-Path $transcriptFilePath | Should -BeTrue @@ -201,7 +211,9 @@ Describe "Start-Transcript, Stop-Transcript tests" -tags "CI" { $script = { Start-Transcript -Path $transcriptFilePath Write-Host -Message $message -InformationAction SilentlyContinue - Stop-Transcript } + Stop-Transcript + } + & $script Test-Path $transcriptFilePath | Should -BeTrue @@ -213,7 +225,9 @@ Describe "Start-Transcript, Stop-Transcript tests" -tags "CI" { $script = { Start-Transcript -Path $transcriptFilePath Write-Host -Message $message -InformationAction Ignore - Stop-Transcript } + Stop-Transcript + } + & $script Test-Path $transcriptFilePath | Should -BeTrue From b2d9103df2c2cb289488c13a8557a0fcee847ee1 Mon Sep 17 00:00:00 2001 From: Hubert Bukowski Date: Mon, 4 Jun 2018 12:08:10 +0200 Subject: [PATCH 09/12] Made Write-Host and Write-Information to use the same transcription code. Previously, the output sequencing of the message and the prompt choices differed between the host and the transcript. --- .../commands/utility/WriteConsoleCmdlet.cs | 1 - .../engine/MshCommandRuntime.cs | 8 +++++--- .../Microsoft.Powershell.Host/Start-Transcript.Tests.ps1 | 4 ++-- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WriteConsoleCmdlet.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WriteConsoleCmdlet.cs index 2c0ce8f21d0..bf51fd23542 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WriteConsoleCmdlet.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WriteConsoleCmdlet.cs @@ -137,7 +137,6 @@ protected override void ProcessRecord() } this.WriteInformation(informationMessage, new string[] { "PSHOST" }); - this.Host.UI.TranscribeResult(result); } private Boolean _notAppendNewline = false; diff --git a/src/System.Management.Automation/engine/MshCommandRuntime.cs b/src/System.Management.Automation/engine/MshCommandRuntime.cs index d8ec16f318f..5e0f6519daa 100644 --- a/src/System.Management.Automation/engine/MshCommandRuntime.cs +++ b/src/System.Management.Automation/engine/MshCommandRuntime.cs @@ -748,7 +748,7 @@ internal void WriteInformation(InformationRecord record, bool overrideInquire = // if (null == Host || null == Host.UI) { - Diagnostics.Assert(false, "No host in CommandBase.WriteVerbose()"); + Diagnostics.Assert(false, "No host in CommandBase.WriteInformation()"); throw PSTraceSource.NewInvalidOperationException(); } @@ -823,9 +823,11 @@ internal void WriteInformation(InformationRecord record, bool overrideInquire = } } - if (!record.Tags.Contains("PSHOST") && (preference != ActionPreference.SilentlyContinue)) + // Both informational and PSHost-targeted messages are transcribed here. + // The only difference between these two is that PSHost-targeted messages are transcribed + // even if InformationAction is SilentlyContinue. + if (record.Tags.Contains("PSHOST") || (preference != ActionPreference.SilentlyContinue)) { - // Only transcribe informational messages here. Transcription of PSHost-targeted messages is done in the InternalUI.Write* methods. CBhost.InternalUI.TranscribeResult(record.ToString()); } } diff --git a/test/powershell/Modules/Microsoft.Powershell.Host/Start-Transcript.Tests.ps1 b/test/powershell/Modules/Microsoft.Powershell.Host/Start-Transcript.Tests.ps1 index 63a26ab75e5..c08a0e31ac6 100644 --- a/test/powershell/Modules/Microsoft.Powershell.Host/Start-Transcript.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.Powershell.Host/Start-Transcript.Tests.ps1 @@ -220,7 +220,7 @@ Describe "Start-Transcript, Stop-Transcript tests" -tags "CI" { $transcriptFilePath | Should -FileContentMatch $message } - It "Transcription should record Write-Host output when InformationAction is set to Ignore" { + It "Transcription should not record Write-Host output when InformationAction is set to Ignore" { [String]$message = New-Guid $script = { Start-Transcript -Path $transcriptFilePath @@ -231,6 +231,6 @@ Describe "Start-Transcript, Stop-Transcript tests" -tags "CI" { & $script Test-Path $transcriptFilePath | Should -BeTrue - $transcriptFilePath | Should -FileContentMatch $message + $transcriptFilePath | Should -Not -FileContentMatch $message } } From bae894d3bbe3028550e43b02af39717ad29660a9 Mon Sep 17 00:00:00 2001 From: Hubert Bukowski Date: Mon, 4 Jun 2018 17:03:00 +0200 Subject: [PATCH 10/12] Fixed error reporting in WriteInformation method. --- src/System.Management.Automation/engine/MshCommandRuntime.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/System.Management.Automation/engine/MshCommandRuntime.cs b/src/System.Management.Automation/engine/MshCommandRuntime.cs index 5e0f6519daa..5b5b55809c1 100644 --- a/src/System.Management.Automation/engine/MshCommandRuntime.cs +++ b/src/System.Management.Automation/engine/MshCommandRuntime.cs @@ -748,8 +748,7 @@ internal void WriteInformation(InformationRecord record, bool overrideInquire = // if (null == Host || null == Host.UI) { - Diagnostics.Assert(false, "No host in CommandBase.WriteInformation()"); - throw PSTraceSource.NewInvalidOperationException(); + throw PSTraceSource.NewInvalidOperationException("No host in CommandBase.WriteInformation()"); } CBhost.InternalUI.WriteInformationRecord(record); From d00d94684ed25850b6c440e51572f59bb318b27b Mon Sep 17 00:00:00 2001 From: Hubert Bukowski Date: Tue, 5 Jun 2018 10:31:19 +0200 Subject: [PATCH 11/12] Added tests for transcription of InformationAction Inquire. --- ...ConsoleHostUserInterfacePromptForChoice.cs | 13 +++++-- .../engine/Utils.cs | 1 + .../Start-Transcript.Tests.ps1 | 38 +++++++++++++++++++ 3 files changed, 49 insertions(+), 3 deletions(-) diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterfacePromptForChoice.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterfacePromptForChoice.cs index ac50c61f7c0..e7c45a065e8 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterfacePromptForChoice.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterfacePromptForChoice.cs @@ -107,7 +107,7 @@ public override int PromptForChoice(string caption, string message, Collection PromptForChoice(string caption, WriteToConsole(PromptColor, RawUI.BackgroundColor, WrapToCurrentWindowWidth(choiceMsg)); ReadLineResult rlResult; - string response = ReadLine(false, string.Empty, out rlResult, true, true); + string response = ReadChoiceResponse(out rlResult); if (rlResult == ReadLineResult.endedOnBreak) { @@ -412,6 +412,14 @@ private void WriteChoiceHelper(string text, ConsoleColor fg, ConsoleColor bg, re WriteToConsole(fg, bg, trimEnd ? text.TrimEnd(null) : text); } + private string ReadChoiceResponse(out ReadLineResult result) + { + result = ReadLineResult.endedOnEnter; + return InternalTestHooks.ForcePromptForChoiceDefaultOption + ? string.Empty + : ReadLine(false, string.Empty, out result, true, true); + } + private void ShowChoiceHelp(Collection choices, string[,] hotkeysAndPlainLabels) { Dbg.Assert(choices != null, "choices: expected a value"); @@ -473,4 +481,3 @@ private ConsoleColor DefaultPromptColor } } } // namespace - diff --git a/src/System.Management.Automation/engine/Utils.cs b/src/System.Management.Automation/engine/Utils.cs index 5b108a97978..9d2d9a21ab2 100644 --- a/src/System.Management.Automation/engine/Utils.cs +++ b/src/System.Management.Automation/engine/Utils.cs @@ -1496,6 +1496,7 @@ public static class InternalTestHooks internal static bool UseDebugAmsiImplementation; internal static bool BypassAppLockerPolicyCaching; internal static bool BypassOnlineHelpRetrieval; + internal static bool ForcePromptForChoiceDefaultOption; // Stop/Restart/Rename Computer tests internal static bool TestStopComputer; diff --git a/test/powershell/Modules/Microsoft.Powershell.Host/Start-Transcript.Tests.ps1 b/test/powershell/Modules/Microsoft.Powershell.Host/Start-Transcript.Tests.ps1 index c08a0e31ac6..2a8ca695c6c 100644 --- a/test/powershell/Modules/Microsoft.Powershell.Host/Start-Transcript.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.Powershell.Host/Start-Transcript.Tests.ps1 @@ -161,6 +161,7 @@ Describe "Start-Transcript, Stop-Transcript tests" -tags "CI" { & $script Test-Path $transcriptFilePath | Should -BeTrue + $transcriptFilePath | Should -Not -FileContentMatch "INFO: " $transcriptFilePath | Should -FileContentMatch $message } @@ -175,6 +176,7 @@ Describe "Start-Transcript, Stop-Transcript tests" -tags "CI" { & $script Test-Path $transcriptFilePath | Should -BeTrue + $transcriptFilePath | Should -Not -FileContentMatch "INFO: " $transcriptFilePath | Should -Not -FileContentMatch $message } @@ -189,9 +191,28 @@ Describe "Start-Transcript, Stop-Transcript tests" -tags "CI" { & $script Test-Path $transcriptFilePath | Should -BeTrue + $transcriptFilePath | Should -Not -FileContentMatch "INFO: " $transcriptFilePath | Should -Not -FileContentMatch $message } + It "Transcription should record Write-Information output in correct order when InformationAction is set to Inquire" { + [String]$message = New-Guid + $newLine = [System.Environment]::NewLine + $expectedContent = "$message$($newLine)Confirm$($newLine)Continue with this operation?" + $script = { + [System.Management.Automation.Internal.InternalTestHooks]::SetTestHook('ForcePromptForChoiceDefaultOption', $True) + Start-Transcript -Path $transcriptFilePath + Write-Information -Message $message -InformationAction Inquire + Stop-Transcript + } + + & $script + + Test-Path $transcriptFilePath | Should -BeTrue + $transcriptFilePath | Should -Not -FileContentMatch "INFO: " + $transcriptFilePath | Should -FileContentMatchMultiline $expectedContent + } + It "Transcription should record Write-Host output when InformationAction is set to Continue" { [String]$message = New-Guid $script = { @@ -233,4 +254,21 @@ Describe "Start-Transcript, Stop-Transcript tests" -tags "CI" { Test-Path $transcriptFilePath | Should -BeTrue $transcriptFilePath | Should -Not -FileContentMatch $message } + + It "Transcription should record Write-Host output in correct order when InformationAction is set to Inquire" { + [String]$message = New-Guid + $newLine = [System.Environment]::NewLine + $expectedContent = "$message$($newLine)Confirm$($newLine)Continue with this operation?" + $script = { + [System.Management.Automation.Internal.InternalTestHooks]::SetTestHook('ForcePromptForChoiceDefaultOption', $True) + Start-Transcript -Path $transcriptFilePath + Write-Host -Message $message -InformationAction Inquire + Stop-Transcript + } + + & $script + + Test-Path $transcriptFilePath | Should -BeTrue + $transcriptFilePath | Should -FileContentMatchMultiline $expectedContent + } } From c67c20c5c3caa7615028cb2cafea1cdfa9fe9b26 Mon Sep 17 00:00:00 2001 From: Hubert Bukowski Date: Mon, 11 Jun 2018 13:23:10 +0200 Subject: [PATCH 12/12] Unset test hook after tests execution. Use named parameters in ReadChoiceResponse method. Simplified Start-Transcript tests. --- ...ConsoleHostUserInterfacePromptForChoice.cs | 7 +++++- .../Start-Transcript.Tests.ps1 | 23 ++++++++++--------- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterfacePromptForChoice.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterfacePromptForChoice.cs index e7c45a065e8..06dbde6c016 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterfacePromptForChoice.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterfacePromptForChoice.cs @@ -417,7 +417,12 @@ private string ReadChoiceResponse(out ReadLineResult result) result = ReadLineResult.endedOnEnter; return InternalTestHooks.ForcePromptForChoiceDefaultOption ? string.Empty - : ReadLine(false, string.Empty, out result, true, true); + : ReadLine( + endOnTab: false, + initialContent: string.Empty, + result: out result, + calledFromPipeline: true, + transcribeResult: true); } private void ShowChoiceHelp(Collection choices, string[,] hotkeysAndPlainLabels) diff --git a/test/powershell/Modules/Microsoft.Powershell.Host/Start-Transcript.Tests.ps1 b/test/powershell/Modules/Microsoft.Powershell.Host/Start-Transcript.Tests.ps1 index 2a8ca695c6c..423c4176eaa 100644 --- a/test/powershell/Modules/Microsoft.Powershell.Host/Start-Transcript.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.Powershell.Host/Start-Transcript.Tests.ps1 @@ -53,6 +53,7 @@ Describe "Start-Transcript, Stop-Transcript tests" -tags "CI" { AfterEach { Remove-Item $transcriptFilePath -ErrorAction SilentlyContinue + [System.Management.Automation.Internal.InternalTestHooks]::SetTestHook('ForcePromptForChoiceDefaultOption', $False) } It "Should create Transcript file at default path" { @@ -122,7 +123,7 @@ Describe "Start-Transcript, Stop-Transcript tests" -tags "CI" { } } - Test-Path $transcriptFilePath | Should -BeTrue + $transcriptFilePath | Should -Exist $transcriptFilePath | Should -FileContentMatch "After Dispose" } @@ -131,7 +132,7 @@ Describe "Start-Transcript, Stop-Transcript tests" -tags "CI" { $powerShellCommand = $powerShellPath + ' -c "start-transcript $transcriptFilePath; Write-Host ''Before Dispose'';"' Invoke-Expression $powerShellCommand - Test-Path $transcriptFilePath | Should -BeTrue + $transcriptFilePath | Should -Exist $transcriptFilePath | Should -FileContentMatch "Before Dispose" $transcriptFilePath | Should -FileContentMatch "PowerShell transcript end" } @@ -145,7 +146,7 @@ Describe "Start-Transcript, Stop-Transcript tests" -tags "CI" { & $script - Test-Path $transcriptFilePath | Should -BeTrue + $transcriptFilePath | Should -Exist $machineName = [System.Environment]::MachineName $transcriptFilePath | Should -FileContentMatch $machineName } @@ -160,7 +161,7 @@ Describe "Start-Transcript, Stop-Transcript tests" -tags "CI" { & $script - Test-Path $transcriptFilePath | Should -BeTrue + $transcriptFilePath | Should -Exist $transcriptFilePath | Should -Not -FileContentMatch "INFO: " $transcriptFilePath | Should -FileContentMatch $message } @@ -175,7 +176,7 @@ Describe "Start-Transcript, Stop-Transcript tests" -tags "CI" { & $script - Test-Path $transcriptFilePath | Should -BeTrue + $transcriptFilePath | Should -Exist $transcriptFilePath | Should -Not -FileContentMatch "INFO: " $transcriptFilePath | Should -Not -FileContentMatch $message } @@ -190,7 +191,7 @@ Describe "Start-Transcript, Stop-Transcript tests" -tags "CI" { & $script - Test-Path $transcriptFilePath | Should -BeTrue + $transcriptFilePath | Should -Exist $transcriptFilePath | Should -Not -FileContentMatch "INFO: " $transcriptFilePath | Should -Not -FileContentMatch $message } @@ -208,7 +209,7 @@ Describe "Start-Transcript, Stop-Transcript tests" -tags "CI" { & $script - Test-Path $transcriptFilePath | Should -BeTrue + $transcriptFilePath | Should -Exist $transcriptFilePath | Should -Not -FileContentMatch "INFO: " $transcriptFilePath | Should -FileContentMatchMultiline $expectedContent } @@ -223,7 +224,7 @@ Describe "Start-Transcript, Stop-Transcript tests" -tags "CI" { & $script - Test-Path $transcriptFilePath | Should -BeTrue + $transcriptFilePath | Should -Exist $transcriptFilePath | Should -FileContentMatch $message } @@ -237,7 +238,7 @@ Describe "Start-Transcript, Stop-Transcript tests" -tags "CI" { & $script - Test-Path $transcriptFilePath | Should -BeTrue + $transcriptFilePath | Should -Exist $transcriptFilePath | Should -FileContentMatch $message } @@ -251,7 +252,7 @@ Describe "Start-Transcript, Stop-Transcript tests" -tags "CI" { & $script - Test-Path $transcriptFilePath | Should -BeTrue + $transcriptFilePath | Should -Exist $transcriptFilePath | Should -Not -FileContentMatch $message } @@ -268,7 +269,7 @@ Describe "Start-Transcript, Stop-Transcript tests" -tags "CI" { & $script - Test-Path $transcriptFilePath | Should -BeTrue + $transcriptFilePath | Should -Exist $transcriptFilePath | Should -FileContentMatchMultiline $expectedContent } }