From f57f924caf23d17f12e4b8af65e76efc8d128bfa Mon Sep 17 00:00:00 2001 From: Francisco Gamino Date: Wed, 1 Feb 2017 17:56:52 -0800 Subject: [PATCH] Adding '$pshome/cultureName/default.help.txt' to PowerShell Windows Core project. (#3032) * Adding PowerShellHelpFiles package which contains default.help.txt to powershell-win-core * Adding test case to validate that //default.help.txt is present. This is done by calling 'Get-Help' * Updating get-help to skip searching for the help file when the InternalTestHooks.BypassOnlineHelpRetrieval is enable. This way, we force get-help to generate a metadata driven help object, which includes a helpUri that points to the fwlink defined in the cmdlet code. * Updating get-help -online tests to not delete the help files. Instead, I've added logic to get-help to not find the help file when the test hook BypassOnlineHelpRetrieval is enable. --- .../help/CommandHelpProvider.cs | 19 ++++++++++++++++--- .../help/ProviderContext.cs | 8 ++++++++ src/powershell-win-core/project.json | 3 ++- .../Help/HelpSystem.OnlineHelp.Tests.ps1 | 15 ++++----------- .../engine/Help/HelpSystem.Tests.ps1 | 11 +++++++++++ 5 files changed, 41 insertions(+), 15 deletions(-) diff --git a/src/System.Management.Automation/help/CommandHelpProvider.cs b/src/System.Management.Automation/help/CommandHelpProvider.cs index ff96a56e8..63fb98885 100644 --- a/src/System.Management.Automation/help/CommandHelpProvider.cs +++ b/src/System.Management.Automation/help/CommandHelpProvider.cs @@ -210,15 +210,21 @@ namespace System.Management.Automation string moduleDir = null; string nestedModulePath = null; + // When InternalTestHooks.BypassOnlineHelpRetrieval is enable, we force get-help to generate a metadata + // driven object, which includes a helpUri that points to the fwlink defined in the cmdlet code. + // This means that we are not going to load the help content from the GetFromCommandCache and + // we are not going to read the help file. + // Only gets help for Cmdlet or script command if (!isCmdlet && !isScriptCommand) return null; // Check if the help of the command is already in the cache. // If not, try load the file specified by HelpFile property and retrieve help. - if (isCmdlet) + if (isCmdlet && !InternalTestHooks.BypassOnlineHelpRetrieval) { result = GetFromCommandCache(cmdletInfo.ModuleName, cmdletInfo.Name, cmdletInfo.HelpCategory); + if (null == result) { // Try load the help file specified by CmdletInfo.HelpFile property @@ -291,7 +297,7 @@ namespace System.Management.Automation } } - if (!String.IsNullOrEmpty(helpFile)) + if (!String.IsNullOrEmpty(helpFile) && !InternalTestHooks.BypassOnlineHelpRetrieval) { if (!_helpFiles.Contains(helpFile)) { @@ -306,7 +312,7 @@ namespace System.Management.Automation // in the appropriate UI culture subfolder of ModuleBase, and retrieve help // If still not able to get help, try search for a file called -Help.xml // under the ModuleBase and the NestedModule's directory, and retrieve help - if (null == result) + if (null == result && !InternalTestHooks.BypassOnlineHelpRetrieval) { // Get the name and ModuleBase directory of the command's module // and the nested module that implements the command @@ -542,6 +548,13 @@ namespace System.Management.Automation /// private string FindHelpFile(CmdletInfo cmdletInfo) { + if (InternalTestHooks.BypassOnlineHelpRetrieval) + { + // By returning null, we force get-help to generate a metadata driven help object, + // which includes a helpUri that points to the fwlink defined in the cmdlet code. + return null; + } + if (cmdletInfo == null) { throw PSTraceSource.NewArgumentNullException("cmdletInfo"); diff --git a/src/System.Management.Automation/help/ProviderContext.cs b/src/System.Management.Automation/help/ProviderContext.cs index 8c9294510..1cca401cb 100644 --- a/src/System.Management.Automation/help/ProviderContext.cs +++ b/src/System.Management.Automation/help/ProviderContext.cs @@ -4,6 +4,7 @@ Copyright (c) Microsoft Corporation. All rights reserved. using System.Management.Automation.Provider; using System.Xml; +using System.Management.Automation.Internal; using Dbg = System.Management.Automation.Diagnostics; @@ -51,6 +52,13 @@ namespace System.Management.Automation /// internal MamlCommandHelpInfo GetProviderSpecificHelpInfo(string helpItemName) { + if (InternalTestHooks.BypassOnlineHelpRetrieval) + { + // By returning null, we force get-help to return generic help + // which includes a helpUri that points to the fwlink defined in the cmdlet code. + return null; + } + // Get the provider. ProviderInfo providerInfo = null; PSDriveInfo driveInfo = null; diff --git a/src/powershell-win-core/project.json b/src/powershell-win-core/project.json index 52746787f..5b602472c 100644 --- a/src/powershell-win-core/project.json +++ b/src/powershell-win-core/project.json @@ -85,7 +85,8 @@ "Microsoft.PowerShell.LocalAccounts": "6.0.0-*", "Microsoft.Management.Infrastructure.CimCmdlets": "6.0.0-*", "Microsoft.WSMan.Management": "6.0.0-*", - "PSDesiredStateConfiguration": "1.0.0-alpha01" + "PSDesiredStateConfiguration": "1.0.0-alpha01", + "PowerShellHelpFiles": "1.0.0-alpha01" }, "frameworks": { diff --git a/test/powershell/engine/Help/HelpSystem.OnlineHelp.Tests.ps1 b/test/powershell/engine/Help/HelpSystem.OnlineHelp.Tests.ps1 index 77897052a..e3b469f01 100644 --- a/test/powershell/engine/Help/HelpSystem.OnlineHelp.Tests.ps1 +++ b/test/powershell/engine/Help/HelpSystem.OnlineHelp.Tests.ps1 @@ -3,23 +3,16 @@ # The csv files (V2Cmdlets.csv and V3Cmdlets.csv) contain a list of cmdlets and expected HelpURIs. # The HelpURI is part of the cmdlet metadata, and when the user runs 'get-help -online' # the browser navigates to the address in the HelpURI. However, if a help file is present, the HelpURI - # on the file take precedence over the one in the cmdlet metadata. Therefore, the help content - # in the box needs to be deleted before running the tests, because otherwise, the HelpURI - # (when calling get-help -online) might not matched the one in the csv file. + # on the file take precedence over the one in the cmdlet metadata. BeforeAll { $SavedProgressPreference = $ProgressPreference $ProgressPreference = "SilentlyContinue" - # Enable the test hook + # Enable the test hook. This does the following: + # 1) get-help will not find a help file; instead, it will generate a metadata driven object. + # 2) get-help -online will return the helpuri instead of opening the default web browser. [system.management.automation.internal.internaltesthooks]::SetTestHook('BypassOnlineHelpRetrieval', $true) - - # Remove the help content - Write-Verbose "Deleting help content for get-help -online tests" -Verbose - foreach ($path in @("$pshome\en-US", "$pshome\Modules")) - { - Get-ChildItem $path -Include "*help.xml" -Recurse -ea SilentlyContinue | Remove-Item -Force -ErrorAction SilentlyContinue - } } AfterAll { diff --git a/test/powershell/engine/Help/HelpSystem.Tests.ps1 b/test/powershell/engine/Help/HelpSystem.Tests.ps1 index cbf446a61..39be0a543 100644 --- a/test/powershell/engine/Help/HelpSystem.Tests.ps1 +++ b/test/powershell/engine/Help/HelpSystem.Tests.ps1 @@ -62,6 +62,17 @@ function RunTestCase } } +Describe "Validate that //default.help.txt is present" -Tags @('CI') { + + It "Get-Help returns information about the help system." { + + $help = Get-Help + $help.Name | Should Be "default" + $help.Category | Should Be "HelpFile" + $help.Synopsis | Should Match "SHORT DESCRIPTION" + } +} + Describe "Validate that get-help works" -Tags @('CI', 'RequireAdminOnWindows') { BeforeAll { $SavedProgressPreference = $ProgressPreference