Fixing issue with help progress with Get-Help not calling Completed (#8788)

This commit is contained in:
Staffan Gustafsson
2019-03-11 11:26:01 -07:00
committed by Aditya Patwardhan
parent a26d639a47
commit 0ebbdc1cc4
2 changed files with 38 additions and 11 deletions
@@ -240,9 +240,10 @@ namespace Microsoft.PowerShell.Commands
/// </summary>
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<HelpInfo> helpInfos = this.Context.HelpSystem.GetHelp(helpRequest);
IEnumerable<HelpInfo> 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);
}
@@ -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: <command>" -TestCases @(
@{ command = "help" },
@{ command = "get-help" }
){
) {
param($command)
{ & $command foobar -ErrorAction Stop } | Should -Throw -ErrorId "HelpNotFound,Microsoft.PowerShell.Commands.GetHelpCommand"