From df811929eb8395cc0fe341fb094de0093afa7236 Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Thu, 31 Oct 2019 13:09:37 -0700 Subject: [PATCH] Fix tab completion for parameters so that it shows common parameters as available (#10850) --- .../engine/MergedCommandParameterMetadata.cs | 2 +- .../TabCompletion/TabCompletion.Tests.ps1 | 16 ++- .../Get-Member.Tests.ps1 | 131 ++++++++---------- 3 files changed, 74 insertions(+), 75 deletions(-) diff --git a/src/System.Management.Automation/engine/MergedCommandParameterMetadata.cs b/src/System.Management.Automation/engine/MergedCommandParameterMetadata.cs index a1c945e387..a1d5530b65 100644 --- a/src/System.Management.Automation/engine/MergedCommandParameterMetadata.cs +++ b/src/System.Management.Automation/engine/MergedCommandParameterMetadata.cs @@ -513,7 +513,7 @@ namespace System.Management.Automation } } - if (filteredParameters.Count == 1) + if (tryExactMatching && filteredParameters.Count == 1) { matchingParameters = filteredParameters; } diff --git a/test/powershell/Host/TabCompletion/TabCompletion.Tests.ps1 b/test/powershell/Host/TabCompletion/TabCompletion.Tests.ps1 index 4be91d6010..acaadb627c 100644 --- a/test/powershell/Host/TabCompletion/TabCompletion.Tests.ps1 +++ b/test/powershell/Host/TabCompletion/TabCompletion.Tests.ps1 @@ -895,6 +895,20 @@ dir -Recurse ` $res.CompletionMatches | Should -HaveCount 33 $res.CompletionMatches[0].CompletionText | Should -BeExactly "Commands" } + + It "Test completion with common parameters" { + $inputStr = 'invoke-webrequest -out' + $res = TabExpansion2 -inputScript $inputStr -cursorColumn $inputStr.Length + $res.CompletionMatches | Should -HaveCount 3 + [string]::Join(',', ($res.CompletionMatches.completiontext | Sort-Object)) | Should -BeExactly "-OutBuffer,-OutFile,-OutVariable" + } + + It "Test completion with exact match" { + $inputStr = 'get-content -wa' + $res = TabExpansion2 -inputScript $inputStr -cursorColumn $inputStr.Length + $res.CompletionMatches | Should -HaveCount 4 + [string]::Join(',', ($res.CompletionMatches.completiontext | Sort-Object)) | Should -BeExactly "-wa,-Wait,-WarningAction,-WarningVariable" + } } Context "Module completion for 'using module'" { @@ -1322,7 +1336,7 @@ Describe "WSMan Config Provider tab complete tests" -Tags Feature,RequireAdminOn } It "Tab completion gets dynamic parameters for '' using ''" -TestCases @( - @{path = ""; parameter = "-co"; expected = "ConnectionURI"}, + @{path = ""; parameter = "-conn"; expected = "ConnectionURI"}, @{path = ""; parameter = "-op"; expected = "OptionSet"}, @{path = ""; parameter = "-au"; expected = "Authentication"}, @{path = ""; parameter = "-ce"; expected = "CertificateThumbprint"}, diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Get-Member.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Get-Member.Tests.ps1 index acb6a6f68c..7e8379e5c1 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Get-Member.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Get-Member.Tests.ps1 @@ -2,43 +2,43 @@ # Licensed under the MIT License. Describe "Get-Member" -Tags "CI" { It "Should be able to be called on string objects, ints, arrays, etc" { - $a = 1 #test numbers - $b = 1.3 - $c = $false #test bools - $d = @(1,3) # test arrays - $e = "anoeduntodeu" #test strings - $f = 'asntoheusth' #test strings + $a = 1 #test numbers + $b = 1.3 + $c = $false #test bools + $d = @(1, 3) # test arrays + $e = "anoeduntodeu" #test strings + $f = 'asntoheusth' #test strings - Get-Member -InputObject $a | Should -Not -BeNullOrEmpty - Get-Member -InputObject $b | Should -Not -BeNullOrEmpty - Get-Member -InputObject $c | Should -Not -BeNullOrEmpty - Get-Member -InputObject $d | Should -Not -BeNullOrEmpty - Get-Member -InputObject $e | Should -Not -BeNullOrEmpty - Get-Member -InputObject $f | Should -Not -BeNullOrEmpty + Get-Member -InputObject $a | Should -Not -BeNullOrEmpty + Get-Member -InputObject $b | Should -Not -BeNullOrEmpty + Get-Member -InputObject $c | Should -Not -BeNullOrEmpty + Get-Member -InputObject $d | Should -Not -BeNullOrEmpty + Get-Member -InputObject $e | Should -Not -BeNullOrEmpty + Get-Member -InputObject $f | Should -Not -BeNullOrEmpty } It "Should be able to extract a field from string objects, ints, arrays, etc" { - $a = 1 #test numbers - $b = 1.3 - $c = $false #test bools - $d = @(1,3) # test arrays - $e = "anoeduntodeu" #test strings - $f = 'asntoheusth' #test strings + $a = 1 #test numbers + $b = 1.3 + $c = $false #test bools + $d = @(1, 3) # test arrays + $e = "anoeduntodeu" #test strings + $f = 'asntoheusth' #test strings - $a | Should -BeOfType 'Int32' - $b | Should -BeOfType 'Double' - $c | Should -BeOfType 'Boolean' - ,$d | Should -BeOfType 'Object[]' - $e | Should -BeOfType 'String' - $f | Should -BeOfType 'String' + $a | Should -BeOfType 'Int32' + $b | Should -BeOfType 'Double' + $c | Should -BeOfType 'Boolean' + , $d | Should -BeOfType 'Object[]' + $e | Should -BeOfType 'String' + $f | Should -BeOfType 'String' } It "Should be able to be called on a newly created PSObject" { - $o = New-Object psobject - # this creates a dependency on the Add-Member cmdlet. - Add-Member -InputObject $o -MemberType NoteProperty -Name proppy -Value "superVal" + $o = New-Object psobject + # this creates a dependency on the Add-Member cmdlet. + Add-Member -InputObject $o -MemberType NoteProperty -Name proppy -Value "superVal" - Get-Member -InputObject $o | Should -Not -BeNullOrEmpty + Get-Member -InputObject $o | Should -Not -BeNullOrEmpty } It "Should be able to be called on IntPtr" { @@ -47,13 +47,17 @@ Describe "Get-Member" -Tags "CI" { $results[0].Name | Should -BeExactly 'Size' $results[1].Name | Should -BeExactly 'Zero' } + + It "Should work with incomplete parameter '-i'" { + $a = 1 + { Get-Member -i $a } | Should -Not -Throw + } } Describe "Get-Member DRT Unit Tests" -Tags "CI" { Context "Verify Get-Member with Class" { - if(-not ([System.Management.Automation.PSTypeName]'Employee').Type) - { - Add-Type -TypeDefinition @" + if (-not ([System.Management.Automation.PSTypeName]'Employee').Type) { + Add-Type -TypeDefinition @" public class Employee { private string firstName; @@ -104,10 +108,10 @@ Describe "Get-Member DRT Unit Tests" -Tags "CI" { } } "@ - } + } $fileToDeleteName = Join-Path $TestDrive -ChildPath "getMemberTest.ps1xml" - $XMLFile= @" + $XMLFile = @" Employee @@ -175,19 +179,15 @@ Describe "Get-Member DRT Unit Tests" -Tags "CI" { $results.Length | Should -Be $methodList.Length $methodFound = @() - for ($i = 0;$i -lt $methodList.Length;$i++) - { - for ($j = 0;$j -lt $results.Length;$j++) - { - if ($results[$j].Name.Equals($methodList[$i])) - { + for ($i = 0; $i -lt $methodList.Length; $i++) { + for ($j = 0; $j -lt $results.Length; $j++) { + if ($results[$j].Name.Equals($methodList[$i])) { $methodFound += $true } } } - for ($i = 0;$i -lt $methodList.Length;$i++) - { + for ($i = 0; $i -lt $methodList.Length; $i++) { $methodFound[$i] | Should -BeTrue } } @@ -206,12 +206,9 @@ Describe "Get-Member DRT Unit Tests" -Tags "CI" { $obj = New-Object -TypeName System.Int32 $results = $obj | Get-Member -Static $members = "MaxValue", "MinValue", "Parse", "TryParse" - foreach ($member in $members) - { - foreach ($result in $results) - { - if($result.Name.Equals($member)) - { + foreach ($member in $members) { + foreach ($result in $results) { + if ($result.Name.Equals($member)) { return $true } } @@ -222,12 +219,9 @@ Describe "Get-Member DRT Unit Tests" -Tags "CI" { It "Get the static properties and methods of int instance" { $results = 1 | Get-Member -Static $members = "MaxValue", "MinValue", "Parse", "TryParse" - foreach ($member in $members) - { - foreach ($result in $results) - { - if($result.Name.Equals($member)) - { + foreach ($member in $members) { + foreach ($result in $results) { + if ($result.Name.Equals($member)) { return $true } } @@ -238,12 +232,9 @@ Describe "Get-Member DRT Unit Tests" -Tags "CI" { It "Get the static properties and methods of int instance Wrapped" { $results = [pscustomobject]1 | Get-Member -Static $members = "MaxValue", "MinValue", "Parse", "TryParse" - foreach ($member in $members) - { - foreach ($result in $results) - { - if($result.Name.Equals($member)) - { + foreach ($member in $members) { + foreach ($result in $results) { + if ($result.Name.Equals($member)) { return $true } } @@ -260,15 +251,12 @@ Describe "Get-Member DRT Unit Tests" -Tags "CI" { $results | Where-Object Name -eq CreateNode | Should -Not -BeNullOrEmpty } - It 'Get hidden members'{ + It 'Get hidden members' { $results = 'abc' | Get-Member -force $hiddenMembers = "psbase", "psextended", "psadapted", "pstypenames", "psobject" - foreach ($member in $hiddenMembers) - { - foreach ($result in $results) - { - if($result.Name.Equals($member)) - { + foreach ($member in $hiddenMembers) { + foreach ($result in $results) { + if ($result.Name.Equals($member)) { return $true } } @@ -276,15 +264,12 @@ Describe "Get-Member DRT Unit Tests" -Tags "CI" { } } - It 'Get Set Property Accessors On PsBase'{ + It 'Get Set Property Accessors On PsBase' { $results = ('abc').psbase | Get-Member -force get_* $expectedMembers = "get_Chars", "get_Length" - foreach ($member in $expectedMembers) - { - foreach ($result in $results) - { - if($result.Name.Equals($member)) - { + foreach ($member in $expectedMembers) { + foreach ($result in $results) { + if ($result.Name.Equals($member)) { return $true } }