-
Notifications
You must be signed in to change notification settings - Fork 7.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix conditions for transcription of Write-Information command. #6917
Conversation
@hubuk Thanks for your contribution. I believe we need to get a conclusion in Issue before we continue the PR. |
@iSazonov Sure. I need to check why a new test is failing. It was OK on my local machine. |
I found that tests are executed with redirection to a pipe. |
@mklement0 Could you please review the PR? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Leave a comment
& $script | ||
Test-Path $transcriptFilePath | Should -BeTrue | ||
|
||
$transcriptFilePath | Should FileContentMatch 'Continue' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use new syntax Should -FileContentMatch
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps I'm missing something, but it it looks like you accidentally reverted the original fix.
@mklement0 You are right. My bad... |
It should be OK now. Test was passing for the same reason why my previous test was failing. |
Can you clarify what that means, specifically, and perhaps give an example? |
@hubuk Please don't fix CodeFactor issues in the PR (It's hard to review meaningful changes) - all style changes should be in new PR. |
@iSazonov Reverted the change.
Sorry, for wrong information I provided: |
Write-Host is related to this code so you have to add tests for it. |
@iSazonov I added tests for Additionally removing |
I'm currently only aware of problems with the transcription behavior, not with console output, so the guiding principle should be to align transcription behavior with that of the current console-output behavior. If, with respect to console-output behavior, you're referring to |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Style comments for all added tests
[String]$message = New-Guid | ||
$script = { | ||
Start-Transcript -Path $transcriptFilePath | ||
Write-Host -Message $message -InformationAction SilentlyContinue |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add test for Ignore for current behavior.
I think we should limit the PR to fix only transcript issues.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed
Stop-Transcript } | ||
& $script | ||
Test-Path $transcriptFilePath | Should -BeTrue | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please remove extra line.
Write-Information -Message $message -InformationAction Continue | ||
Stop-Transcript } | ||
& $script | ||
Test-Path $transcriptFilePath | Should -BeTrue |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add new line.
$script = { | ||
Start-Transcript -Path $transcriptFilePath | ||
Write-Information -Message $message -InformationAction Continue | ||
Stop-Transcript } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use common formatting.
$script = {
Start-Transcript -Path $transcriptFilePath
Write-Information -Message $message -InformationAction Continue
Stop-Transcript
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I used the same formatting as already applied for Transcription should record native command output
test.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see. I believe we should use the right pattern because It block and other code use right patterns.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe it is fixed, but $script block seems to be not needed and may be omitted.
What do you think?
It "Transcription should record Write-Host output when InformationAction is set to Ignore" {
[String]$message = New-Guid
Start-Transcript -Path $transcriptFilePath
Write-Host -Message $message -InformationAction Ignore
Stop-Transcript
Test-Path $transcriptFilePath | Should -BeTrue
$transcriptFilePath | Should -FileContentMatch $message
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems it is an isolation. I'am not sure that we should remove thos.
I'm not sure I need to remove the INFO: prefix. I suppose it was made that distinguish the output of the cmdlet. Although if we want to get the exact snapshot of the console then we need to remove this prefix |
There are two issues still present:
in transcript file:
The reason of 2. is that Current transcript implementation for |
If there are no side effects, it seems like a good change to make (but it's not my call). On a side note: the transcription of the prompt choices with the raw "accelerator chars." (
Yes, consistency with console output is important, I think. However, you could argue that the While changing that wouldn't technically be a breaking change - for-display output is not part of the contract with the user - it would be a noticeable one. Either way, let's discuss it in a separate issue: #6951 |
Now that we know that the |
@mklement0 @iSazonov Unfortunately "accelerator chars." are handled by It seems like there could be much more cases in which transcription file is different from the console output. For example there is no transcription of an invalid accelerator keys user pressed. Better approach would be to make a transcript part of host classes which will make it easier to ensure consistency but it would be a major refactoring. |
@@ -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(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please remove the Diagnostics.Assert an move the message string in
throw PSTraceSource.NewInvalidOperationException("No host in CommandBase.WriteInformation()");
{ | ||
// Only transcribe informational messages here. Transcription of PSHost-targeted messages is done in the InternalUI.Write* methods. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems new comment about 'Inquire' and PSHost-targets will be useful.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Most important change is that transcription of Write-Host
and Write-Information
is handled in the same place now.
The part about fixing Inquire
has only historical value and does not introduce any new information about current behavior.
Anyway, take a look at my comment and let me know if you want it stated differently as English is not my native language.
@@ -137,7 +137,6 @@ protected override void ProcessRecord() | |||
} | |||
|
|||
this.WriteInformation(informationMessage, new string[] { "PSHOST" }); | |||
this.Host.UI.TranscribeResult(result); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Have we a test for this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I already created three test for transcription of Write-Host command (Continue, SilentlyContinue, Ignore).
I have no idea how costly would it be to create a test for Inquire action as it makes the test interactive.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ideally we should mosk but I don't know how.
@adityapatwardhan Can we ignore the lack of this test?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it possible to add a test hook?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not clear where the hook should be to emulate the user interaction. Maybe in PromptForChoice method in ConsoleHostUserInterfacePromptForChoice.cs file?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@hubuk You can add hook here
public static class InternalTestHooks |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's great. Thanks.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tests added.
@hubuk: Thanks for the update; my sense is that - at least for now and in the context of this PR - we needn't worry about transcribing 100% faithfully in all cases as long as all the information is ultimately there. |
@@ -1,5 +1,8 @@ | |||
# Copyright (c) Microsoft Corporation. All rights reserved. | |||
# Licensed under the MIT License. | |||
|
|||
using namespace System.Management.Automation.Internal |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Previously we don't use this in our tests so maybe remove this and use full names in tests.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
result = ReadLineResult.endedOnEnter; | ||
return InternalTestHooks.ForcePromptForChoiceDefaultOption | ||
? string.Empty | ||
: ReadLine(false, "", out result, true, true); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I skipped this - please use string.Empty as in previous line.
Thank you for your patience. Seems this is my last comment.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed. Not a problem.
result = ReadLineResult.endedOnEnter; | ||
return InternalTestHooks.ForcePromptForChoiceDefaultOption | ||
? string.Empty | ||
: ReadLine(false, string.Empty, out result, true, true); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you use named parameters here?
$newLine = [System.Environment]::NewLine | ||
$expectedContent = "$message$($newLine)Confirm$($newLine)Continue with this operation?" | ||
$script = { | ||
[System.Management.Automation.Internal.InternalTestHooks]::SetTestHook('ForcePromptForChoiceDefaultOption', $True) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems like there should be an AfterEach{}
that resets the test hook
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
|
||
& $script | ||
|
||
Test-Path $transcriptFilePath | Should -BeTrue |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps a search and replace to:
$transcriptFilePath | Should -Exist
…ode. Previously, the output sequencing of the message and the prompt choices differed between the host and the transcript.
…oiceResponse method. Simplified Start-Transcript tests.
@SteveL-MSFT Is there anything else you would like to be changed? |
@SteveL-MSFT Please update your review. |
I have a fear of removal FORWARDED condition - do it really not used? /cc @SteveL-MSFT Who can confirm? |
@iSazonov PowerShell/src/System.Management.Automation/engine/MshCommandRuntime.cs Lines 756 to 757 in c67c20c
PowerShell/src/System.Management.Automation/engine/MshCommandRuntime.cs Lines 824 to 828 in e177fca
PowerShell/src/System.Management.Automation/engine/MshCommandRuntime.cs Lines 828 to 831 in c67c20c
Before my changes
After my changes
I hope this is somehow readable. |
@hubuk Sorry, sometimes GitHub shows diffs in very strange way. |
@hubuk Thank you for your contribution and your patience! |
Happy to contribute. Thanks! |
PR Summary
Fix for #6916.
This change makes transcription of Write-Information command consistent with
$InfomrationPreference
variable.Currently Write-Information command is being transcribed if
$InfomrationPreference
is set to'SilentlyContinue'
and not transcribed if set to'Continue'
.The problem is in if/else statement which starts here:
PowerShell/src/System.Management.Automation/engine/MshCommandRuntime.cs
Lines 757 to 758 in 948532a
and has a continuation here:
PowerShell/src/System.Management.Automation/engine/MshCommandRuntime.cs
Lines 824 to 828 in 948532a
Transcription of Write-Host commands are handled in InternalUI.Write* methods as the comment suggests. These commands are distinguished by "PSHOST" tag.
Transcription of Write-Information commands is supposed to take place in the else statement but code in this statement is being executed only if
This condition is invalid. Correct condition is:
PR Checklist
.h
,.cpp
,.cs
,.ps1
and.psm1
files have the correct copyright headerWIP:
to the beginning of the title and remove the prefix when the PR is ready.[feature]
if the change is significant or affects feature tests