Skip to content

Commit

Permalink
Fix Get-Help issue wih -Parameter option #8697
Browse files Browse the repository at this point in the history
  • Loading branch information
throwaway774 committed Jan 27, 2019
1 parent db1b309 commit 40bca28
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/System.Management.Automation/help/BaseCommandHelpInfo.cs
Expand Up @@ -393,8 +393,20 @@ internal override PSObject[] GetParameter(string pattern)
return base.GetParameter(pattern);
}

// The Maml format simplifies array fields containing only one object
// by transforming them into the objects themselves. To ensure the consistency
// of the help command result we change it back into an array.
var param = prmts.Properties["parameter"].Value;
PSObject paramPSObj = param as PSObject;
PSObject[] paramAsPSObjArray = new PSObject[1];

if (paramPSObj != null)
{
paramAsPSObjArray[0] = paramPSObj;
}

PSObject[] prmtArray = (PSObject[])LanguagePrimitives.ConvertTo(
prmts.Properties["parameter"].Value,
paramAsPSObjArray[0] != null ? paramAsPSObjArray : param,
typeof(PSObject[]),
CultureInfo.InvariantCulture);

Expand Down
44 changes: 44 additions & 0 deletions test/powershell/engine/Help/HelpSystem.Tests.ps1
Expand Up @@ -503,6 +503,50 @@ Describe "Get-Help should accept arrays as the -Parameter parameter value" -Tags
}
}

Describe "Get-Help for function parameter should be consistent" -Tags 'CI' {
BeforeAll {
$test1 = @'
function test1 {
param (
$First
)
}
'@
$test2 = @'
function test2 {
param (
$First,
$Second
)
}
'@
$test1Path = Join-Path $TestDrive "test1.ps1"
New-Item -ItemType File -Path $test1Path -Force > $null
Set-Content -Path $test1Path -Value $test1

$test2Path = Join-Path $TestDrive "test2.ps1"
New-Item -ItemType File -Path $test2Path -Force > $null
Set-Content -Path $test2Path -Value $test2

Import-Module $test1Path
Import-Module $test2Path
}

AfterAll {
Remove-Module -Name "test1"
Remove-Module -Name "test2"

Remove-Item $test1Path -Force -ErrorAction SilentlyContinue
Remove-Item $test2Path -Force -ErrorAction SilentlyContinue
}

It "Get-Help for function parameter should be consistent" {
$test1Help = (Get-Help test1 -Parameter First)
$test2Help = (Get-Help test2 -Parameter First)
$test1Help | Should -BeExactly $test2Help
}
}

Describe "Help failure cases" -Tags Feature {
It "An error is returned for a topic that doesn't exist: <command>" -TestCases @(
@{ command = "help" },
Expand Down

0 comments on commit 40bca28

Please sign in to comment.