From 0ebbdc1cc48e54ce1552e65e8aa4f1cfb8043784 Mon Sep 17 00:00:00 2001 From: Staffan Gustafsson Date: Mon, 11 Mar 2019 19:26:01 +0100 Subject: [PATCH] Fixing issue with help progress with `Get-Help` not calling `Completed` (#8788) --- .../help/HelpCommands.cs | 32 +++++++++++++------ .../engine/Help/HelpSystem.Tests.ps1 | 17 ++++++++-- 2 files changed, 38 insertions(+), 11 deletions(-) diff --git a/src/System.Management.Automation/help/HelpCommands.cs b/src/System.Management.Automation/help/HelpCommands.cs index ab269c2c94..dcf76088a8 100644 --- a/src/System.Management.Automation/help/HelpCommands.cs +++ b/src/System.Management.Automation/help/HelpCommands.cs @@ -240,9 +240,10 @@ namespace Microsoft.PowerShell.Commands /// protected override void ProcessRecord() { + HelpSystem helpSystem = this.Context.HelpSystem; try { - this.Context.HelpSystem.OnProgress += new HelpSystem.HelpProgressHandler(HelpSystem_OnProgress); + helpSystem.OnProgress += new HelpSystem.HelpProgressHandler(HelpSystem_OnProgress); bool failed = false; HelpCategory helpCategory = ToHelpCategory(Category, ref failed); @@ -268,7 +269,7 @@ namespace Microsoft.PowerShell.Commands // the idea is to use yield statement in the help lookup to speed up // perceived user experience....So HelpSystem.GetHelp returns an // IEnumerable.. - IEnumerable helpInfos = this.Context.HelpSystem.GetHelp(helpRequest); + IEnumerable helpInfos = helpSystem.GetHelp(helpRequest); // HelpCommand acts differently when there is just one help object and when // there are more than one object...so handling this behavior through // some variables. @@ -319,13 +320,13 @@ namespace Microsoft.PowerShell.Commands // show errors only if there is no wildcard search or VerboseHelpErrors is true. if (((countOfHelpInfos == 0) && (!WildcardPattern.ContainsWildcardCharacters(helpRequest.Target))) - || this.Context.HelpSystem.VerboseHelpErrors) + || helpSystem.VerboseHelpErrors) { // Check if there is any error happened. If yes, // pipe out errors. - if (this.Context.HelpSystem.LastErrors.Count > 0) + if (helpSystem.LastErrors.Count > 0) { - foreach (ErrorRecord errorRecord in this.Context.HelpSystem.LastErrors) + foreach (ErrorRecord errorRecord in helpSystem.LastErrors) { WriteError(errorRecord); } @@ -334,9 +335,11 @@ namespace Microsoft.PowerShell.Commands } finally { - this.Context.HelpSystem.OnProgress -= new HelpSystem.HelpProgressHandler(HelpSystem_OnProgress); + helpSystem.OnProgress -= new HelpSystem.HelpProgressHandler(HelpSystem_OnProgress); + HelpSystem_OnComplete(); + // finally clear the ScriptBlockAst -> Token[] cache - this.Context.HelpSystem.ClearScriptBlockTokenCache(); + helpSystem.ClearScriptBlockTokenCache(); } } @@ -657,9 +660,20 @@ namespace Microsoft.PowerShell.Commands private void HelpSystem_OnProgress(object sender, HelpProgressInfo arg) { - ProgressRecord record = new ProgressRecord(0, this.CommandInfo.Name, arg.Activity); + var record = new ProgressRecord(0, this.CommandInfo.Name, arg.Activity) + { + PercentComplete = arg.PercentComplete + }; - record.PercentComplete = arg.PercentComplete; + WriteProgress(record); + } + + private void HelpSystem_OnComplete() + { + var record = new ProgressRecord(0, this.CommandInfo.Name, "Completed") + { + RecordType = ProgressRecordType.Completed + }; WriteProgress(record); } diff --git a/test/powershell/engine/Help/HelpSystem.Tests.ps1 b/test/powershell/engine/Help/HelpSystem.Tests.ps1 index 2b090a62a5..e961c2f963 100644 --- a/test/powershell/engine/Help/HelpSystem.Tests.ps1 +++ b/test/powershell/engine/Help/HelpSystem.Tests.ps1 @@ -96,7 +96,20 @@ Describe "Validate that get-help works for CurrentUserScope" -Tags @('CI') { } } -Describe "Validate that get-help works for AllUsers Scope" -Tags @('Feature','RequireAdminOnWindows', 'RequireSudoOnUnix') { +Describe "Testing Get-Help Progress" -Tags @('Feature') { + It "Last ProgressRecord should be Completed" { + try { + $j = Start-Job { Get-Help DoesNotExist } + $j | Wait-Job + $j.ChildJobs[0].Progress[-1].RecordType | Should -Be ([System.Management.Automation.ProgressRecordType]::Completed) + } + finally { + $j | Remove-Job + } + } +} + +Describe "Validate that get-help works for AllUsers Scope" -Tags @('Feature', 'RequireAdminOnWindows', 'RequireSudoOnUnix') { BeforeAll { $SavedProgressPreference = $ProgressPreference $ProgressPreference = "SilentlyContinue" @@ -546,7 +559,7 @@ Describe "Help failure cases" -Tags Feature { It "An error is returned for a topic that doesn't exist: " -TestCases @( @{ command = "help" }, @{ command = "get-help" } - ){ + ) { param($command) { & $command foobar -ErrorAction Stop } | Should -Throw -ErrorId "HelpNotFound,Microsoft.PowerShell.Commands.GetHelpCommand"