Skip to content

Commit

Permalink
Fix Get-Help PSTypeName issue with -Parameter when only one par…
Browse files Browse the repository at this point in the history
…ameter is declared (#8754)
  • Loading branch information
pougetat authored and adityapatwardhan committed Jan 30, 2019
1 parent 5097e2a commit 1d7651f
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/System.Management.Automation/help/BaseCommandHelpInfo.cs
Expand Up @@ -393,8 +393,19 @@ 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[] paramAsPSObjArray = new PSObject[1];

if (param is PSObject paramPSObj)
{
paramAsPSObjArray[0] = paramPSObj;
}

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

Expand Down
39 changes: 39 additions & 0 deletions test/powershell/engine/Help/HelpSystem.Tests.ps1
Expand Up @@ -503,6 +503,45 @@ 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"
Set-Content -Path $test1Path -Value $test1

$test2Path = Join-Path $TestDrive "test2.ps1"
Set-Content -Path $test2Path -Value $test2

Import-Module $test1Path
Import-Module $test2Path
}

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

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

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 1d7651f

Please sign in to comment.