Skip to content
Merged
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 @@ -137,7 +137,6 @@ protected override void ProcessRecord()
}

this.WriteInformation(informationMessage, new string[] { "PSHOST" });
this.Host.UI.TranscribeResult(result);
Copy link
Collaborator

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?

Copy link
Contributor Author

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.

Copy link
Collaborator

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?

Copy link
Member

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?

Copy link
Collaborator

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?

Copy link
Collaborator

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
, inject in PromptForChoice and use in the test.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's great. Thanks.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Tests added.

}

private Boolean _notAppendNewline = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public override int PromptForChoice(string caption, string message, Collection<C
WriteChoicePrompt(hotkeysAndPlainLabels, defaultChoiceKeys, false);

ReadLineResult rlResult;
string response = ReadLine(false, string.Empty, out rlResult, true, true);
string response = ReadChoiceResponse(out rlResult);

if (rlResult == ReadLineResult.endedOnBreak)
{
Expand Down Expand Up @@ -253,7 +253,7 @@ public Collection<int> 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)
{
Expand Down Expand Up @@ -412,6 +412,19 @@ 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(
endOnTab: false,
initialContent: string.Empty,
result: out result,
calledFromPipeline: true,
transcribeResult: true);
}

private void ShowChoiceHelp(Collection<ChoiceDescription> choices, string[,] hotkeysAndPlainLabels)
{
Dbg.Assert(choices != null, "choices: expected a value");
Expand Down Expand Up @@ -473,4 +486,3 @@ private ConsoleColor DefaultPromptColor
}
}
} // namespace

16 changes: 9 additions & 7 deletions src/System.Management.Automation/engine/MshCommandRuntime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -748,8 +748,7 @@ internal void WriteInformation(InformationRecord record, bool overrideInquire =
//
if (null == Host || null == Host.UI)
{
Diagnostics.Assert(false, "No host in CommandBase.WriteVerbose()");
throw PSTraceSource.NewInvalidOperationException();
throw PSTraceSource.NewInvalidOperationException("No host in CommandBase.WriteInformation()");
}

CBhost.InternalUI.WriteInformationRecord(record);
Expand Down Expand Up @@ -821,11 +820,14 @@ internal void WriteInformation(InformationRecord record, bool overrideInquire =
CBhost.InternalUI.WriteLine(record.ToString());
}
}
else
{
// 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()));
}
}

// 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))
{
CBhost.InternalUI.TranscribeResult(record.ToString());
}
}

Expand Down
1 change: 1 addition & 0 deletions src/System.Management.Automation/engine/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,6 @@
<data name="DebugFormatString" xml:space="preserve">
<value>DEBUG: {0}</value>
</data>
<data name="InformationFormatString" xml:space="preserve">
<value>INFO: {0}</value>
</data>
<data name="HostNotTranscribing" xml:space="preserve">
<value>The host is not currently transcribing.</value>
</data>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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" {
Expand Down Expand Up @@ -122,29 +123,153 @@ Describe "Start-Transcript, Stop-Transcript tests" -tags "CI" {
}
}

Test-Path $transcriptFilePath | Should be $true
$transcriptFilePath | Should FileContentMatch "After Dispose"
$transcriptFilePath | Should -Exist
$transcriptFilePath | Should -FileContentMatch "After Dispose"
}

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

Test-Path $transcriptFilePath | Should -BeTrue
$transcriptFilePath | Should FileContentMatch "Before Dispose"
$transcriptFilePath | Should FileContentMatch "PowerShell transcript end"
$transcriptFilePath | Should -Exist
$transcriptFilePath | Should -FileContentMatch "Before Dispose"
$transcriptFilePath | Should -FileContentMatch "PowerShell transcript end"
}

It "Transcription should record native command output" {
$script = {
Start-Transcript -Path $transcriptFilePath
hostname
Stop-Transcript }
Stop-Transcript
}

& $script
Test-Path $transcriptFilePath | Should -BeTrue

$transcriptFilePath | Should -Exist
$machineName = [System.Environment]::MachineName
$transcriptFilePath | Should FileContentMatch $machineName
$transcriptFilePath | Should -FileContentMatch $machineName
}

It "Transcription should record Write-Information output when InformationAction is set to Continue" {
[String]$message = New-Guid
$script = {
Start-Transcript -Path $transcriptFilePath
Write-Information -Message $message -InformationAction Continue
Stop-Transcript
}

& $script

$transcriptFilePath | Should -Exist
$transcriptFilePath | Should -Not -FileContentMatch "INFO: "
$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

$transcriptFilePath | Should -Exist
$transcriptFilePath | Should -Not -FileContentMatch "INFO: "
$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

$transcriptFilePath | Should -Exist
$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)
Copy link
Member

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

Start-Transcript -Path $transcriptFilePath
Write-Information -Message $message -InformationAction Inquire
Stop-Transcript
}

& $script

$transcriptFilePath | Should -Exist
$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 = {
Start-Transcript -Path $transcriptFilePath
Write-Host -Message $message -InformationAction Continue
Stop-Transcript
}

& $script

$transcriptFilePath | Should -Exist
$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
Copy link
Collaborator

@iSazonov iSazonov May 28, 2018

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed

Stop-Transcript
}

& $script

$transcriptFilePath | Should -Exist
$transcriptFilePath | Should -FileContentMatch $message
}

It "Transcription should not 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

$transcriptFilePath | Should -Exist
$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

$transcriptFilePath | Should -Exist
$transcriptFilePath | Should -FileContentMatchMultiline $expectedContent
}
}