Add quote handling in Verb, StrictModeVersion, Scope & PropertyType Argument Completers with single helper method (#24839)

This commit is contained in:
Armaan Mcleod
2025-02-01 14:52:52 +05:00
committed by GitHub
parent c505f4ba39
commit 459fc8d285
7 changed files with 241 additions and 148 deletions
@@ -771,31 +771,50 @@ ConstructorTestClass(int i, bool b)
Context 'Start-Process -Verb parameter completion' {
BeforeAll {
function GetProcessInfoVerbs($path) {
(New-Object -TypeName System.Diagnostics.ProcessStartInfo -ArgumentList $path).Verbs
function GetProcessInfoVerbs([string]$path, [switch]$singleQuote, [switch]$doubleQuote) {
$verbs = (New-Object -TypeName System.Diagnostics.ProcessStartInfo -ArgumentList $path).Verbs
if ($singleQuote) {
return ($verbs | ForEach-Object { "'$_'" })
}
elseif ($doubleQuote) {
return ($verbs | ForEach-Object { """$_""" })
}
return $verbs
}
$cmdPath = Join-Path -Path $TestDrive -ChildPath 'test.cmd'
$cmdVerbs = GetProcessInfoVerbs $cmdPath
$cmdVerbs = GetProcessInfoVerbs -Path $cmdPath
$cmdVerbsSingleQuote = GetProcessInfoVerbs -Path $cmdPath -SingleQuote
$cmdVerbsDoubleQuote = GetProcessInfoVerbs -Path $cmdPath -DoubleQuote
$exePath = Join-Path -Path $TestDrive -ChildPath 'test.exe'
$exeVerbs = GetProcessInfoVerbs $exePath
$exeVerbs = GetProcessInfoVerbs -Path $exePath
$exeVerbsStartingWithRun = $exeVerbs | Where-Object { $_ -like 'run*' }
$exeVerbsSingleQuote = GetProcessInfoVerbs -Path $exePath -SingleQuote
$exeVerbsStartingWithRunSingleQuote = $exeVerbsSingleQuote | Where-Object { $_ -like "'run*" }
$exeVerbsDoubleQuote = GetProcessInfoVerbs -Path $exePath -DoubleQuote
$exeVerbsStartingWithRunDoubleQuote = $exeVerbsDoubleQuote | Where-Object { $_ -like """run*" }
$powerShellExeWithNoExtension = 'powershell'
$txtPath = Join-Path -Path $TestDrive -ChildPath 'test.txt'
$txtVerbs = GetProcessInfoVerbs $txtPath
$txtVerbs = GetProcessInfoVerbs -Path $txtPath
$wavPath = Join-Path -Path $TestDrive -ChildPath 'test.wav'
$wavVerbs = GetProcessInfoVerbs $wavPath
$wavVerbs = GetProcessInfoVerbs -Path $wavPath
$docxPath = Join-Path -Path $TestDrive -ChildPath 'test.docx'
$docxVerbs = GetProcessInfoVerbs $docxPath
$docxVerbs = GetProcessInfoVerbs -Path $docxPath
$fileWithNoExtensionPath = Join-Path -Path $TestDrive -ChildPath 'test'
$fileWithNoExtensionVerbs = GetProcessInfoVerbs $fileWithNoExtensionPath
$fileWithNoExtensionVerbs = GetProcessInfoVerbs -Path $fileWithNoExtensionPath
}
It "Should complete Verb parameter for '<TextInput>'" -Skip:(!([System.Management.Automation.Platform]::IsWindowsDesktop)) -TestCases @(
@{ TextInput = 'Start-Process -Verb '; ExpectedVerbs = '' }
@{ TextInput = "Start-Process -FilePath $cmdPath -Verb "; ExpectedVerbs = $cmdVerbs -join ' ' }
@{ TextInput = "Start-Process -FilePath $cmdPath -Verb '"; ExpectedVerbs = $cmdVerbsSingleQuote -join ' ' }
@{ TextInput = "Start-Process -FilePath $cmdPath -Verb """; ExpectedVerbs = $cmdVerbsDoubleQuote -join ' ' }
@{ TextInput = "Start-Process -FilePath $exePath -Verb "; ExpectedVerbs = $exeVerbs -join ' ' }
@{ TextInput = "Start-Process -FilePath $exePath -Verb run"; ExpectedVerbs = $exeVerbsStartingWithRun -join ' ' }
@{ TextInput = "Start-Process -FilePath $exePath -Verb 'run"; ExpectedVerbs = $exeVerbsStartingWithRunSingleQuote -join ' ' }
@{ TextInput = "Start-Process -FilePath $exePath -Verb ""run"; ExpectedVerbs = $exeVerbsStartingWithRunDoubleQuote -join ' ' }
@{ TextInput = "Start-Process -FilePath $powerShellExeWithNoExtension -Verb "; ExpectedVerbs = $exeVerbs -join ' ' }
@{ TextInput = "Start-Process -FilePath $txtPath -Verb "; ExpectedVerbs = $txtVerbs -join ' ' }
@{ TextInput = "Start-Process -FilePath $wavPath -Verb "; ExpectedVerbs = $wavVerbs -join ' ' }
@@ -812,17 +831,33 @@ ConstructorTestClass(int i, bool b)
Context 'Scope parameter completion' {
BeforeAll {
$allScopes = 'Global Local Script'
$allScopesSingleQuote = "'Global' 'Local' 'Script'"
$allScopesDoubleQuote = """Global"" ""Local"" ""Script"""
$globalScope = 'Global'
$globalScopeSingleQuote = "'Global'"
$globalScopeDoubleQuote = """Global"""
$localScope = 'Local'
$localScopeSingleQuote = "'Local'"
$localScopeDoubleQuote = """Local"""
$scriptScope = 'Script'
$scriptScopeSingleQuote = "'Script'"
$scriptScopeDoubleQuote = """Script"""
$allScopeCommands = 'Clear-Variable', 'Export-Alias', 'Get-Alias', 'Get-PSDrive', 'Get-Variable', 'Import-Alias', 'New-Alias', 'New-PSDrive', 'New-Variable', 'Remove-Alias', 'Remove-PSDrive', 'Remove-Variable', 'Set-Alias', 'Set-Variable'
}
It "Should complete '<ParameterInput>' for '<Commands>'" -TestCases @(
@{ Commands = $allScopeCommands; ParameterInput = "-Scope "; ExpectedScopes = $allScopes }
@{ Commands = $allScopeCommands; ParameterInput = "-Scope '"; ExpectedScopes = $allScopesSingleQuote }
@{ Commands = $allScopeCommands; ParameterInput = "-Scope """; ExpectedScopes = $allScopesDoubleQuote }
@{ Commands = $allScopeCommands; ParameterInput = "-Scope G"; ExpectedScopes = $globalScope }
@{ Commands = $allScopeCommands; ParameterInput = "-Scope 'G"; ExpectedScopes = $globalScopeSingleQuote }
@{ Commands = $allScopeCommands; ParameterInput = "-Scope ""G"; ExpectedScopes = $globalScopeDoubleQuote }
@{ Commands = $allScopeCommands; ParameterInput = "-Scope Lo"; ExpectedScopes = $localScope }
@{ Commands = $allScopeCommands; ParameterInput = "-Scope 'Lo"; ExpectedScopes = $localScopeSingleQuote }
@{ Commands = $allScopeCommands; ParameterInput = "-Scope ""Lo"; ExpectedScopes = $localScopeDoubleQuote }
@{ Commands = $allScopeCommands; ParameterInput = "-Scope Scr"; ExpectedScopes = $scriptScope }
@{ Commands = $allScopeCommands; ParameterInput = "-Scope 'Scr"; ExpectedScopes = $scriptScopeSingleQuote }
@{ Commands = $allScopeCommands; ParameterInput = "-Scope ""Scr"; ExpectedScopes = $scriptScopeDoubleQuote }
@{ Commands = $allScopeCommands; ParameterInput = "-Scope NonExistentScope"; ExpectedScopes = '' }
) {
param($Commands, $ParameterInput, $ExpectedScopes)
@@ -838,28 +873,42 @@ ConstructorTestClass(int i, bool b)
Context 'Get-Verb & Get-Command -Verb parameter completion' {
BeforeAll {
$allVerbs = 'Add Approve Assert Backup Block Build Checkpoint Clear Close Compare Complete Compress Confirm Connect Convert ConvertFrom ConvertTo Copy Debug Deny Deploy Disable Disconnect Dismount Edit Enable Enter Exit Expand Export Find Format Get Grant Group Hide Import Initialize Install Invoke Join Limit Lock Measure Merge Mount Move New Open Optimize Out Ping Pop Protect Publish Push Read Receive Redo Register Remove Rename Repair Request Reset Resize Resolve Restart Restore Resume Revoke Save Search Select Send Set Show Skip Split Start Step Stop Submit Suspend Switch Sync Test Trace Unblock Undo Uninstall Unlock Unprotect Unpublish Unregister Update Use Wait Watch Write'
$allVerbsSingleQuote = "'Add' 'Approve' 'Assert' 'Backup' 'Block' 'Build' 'Checkpoint' 'Clear' 'Close' 'Compare' 'Complete' 'Compress' 'Confirm' 'Connect' 'Convert' 'ConvertFrom' 'ConvertTo' 'Copy' 'Debug' 'Deny' 'Deploy' 'Disable' 'Disconnect' 'Dismount' 'Edit' 'Enable' 'Enter' 'Exit' 'Expand' 'Export' 'Find' 'Format' 'Get' 'Grant' 'Group' 'Hide' 'Import' 'Initialize' 'Install' 'Invoke' 'Join' 'Limit' 'Lock' 'Measure' 'Merge' 'Mount' 'Move' 'New' 'Open' 'Optimize' 'Out' 'Ping' 'Pop' 'Protect' 'Publish' 'Push' 'Read' 'Receive' 'Redo' 'Register' 'Remove' 'Rename' 'Repair' 'Request' 'Reset' 'Resize' 'Resolve' 'Restart' 'Restore' 'Resume' 'Revoke' 'Save' 'Search' 'Select' 'Send' 'Set' 'Show' 'Skip' 'Split' 'Start' 'Step' 'Stop' 'Submit' 'Suspend' 'Switch' 'Sync' 'Test' 'Trace' 'Unblock' 'Undo' 'Uninstall' 'Unlock' 'Unprotect' 'Unpublish' 'Unregister' 'Update' 'Use' 'Wait' 'Watch' 'Write'"
$allVerbsDoubleQuote = """Add"" ""Approve"" ""Assert"" ""Backup"" ""Block"" ""Build"" ""Checkpoint"" ""Clear"" ""Close"" ""Compare"" ""Complete"" ""Compress"" ""Confirm"" ""Connect"" ""Convert"" ""ConvertFrom"" ""ConvertTo"" ""Copy"" ""Debug"" ""Deny"" ""Deploy"" ""Disable"" ""Disconnect"" ""Dismount"" ""Edit"" ""Enable"" ""Enter"" ""Exit"" ""Expand"" ""Export"" ""Find"" ""Format"" ""Get"" ""Grant"" ""Group"" ""Hide"" ""Import"" ""Initialize"" ""Install"" ""Invoke"" ""Join"" ""Limit"" ""Lock"" ""Measure"" ""Merge"" ""Mount"" ""Move"" ""New"" ""Open"" ""Optimize"" ""Out"" ""Ping"" ""Pop"" ""Protect"" ""Publish"" ""Push"" ""Read"" ""Receive"" ""Redo"" ""Register"" ""Remove"" ""Rename"" ""Repair"" ""Request"" ""Reset"" ""Resize"" ""Resolve"" ""Restart"" ""Restore"" ""Resume"" ""Revoke"" ""Save"" ""Search"" ""Select"" ""Send"" ""Set"" ""Show"" ""Skip"" ""Split"" ""Start"" ""Step"" ""Stop"" ""Submit"" ""Suspend"" ""Switch"" ""Sync"" ""Test"" ""Trace"" ""Unblock"" ""Undo"" ""Uninstall"" ""Unlock"" ""Unprotect"" ""Unpublish"" ""Unregister"" ""Update"" ""Use"" ""Wait"" ""Watch"" ""Write"""
$verbsStartingWithRe = 'Read Receive Redo Register Remove Rename Repair Request Reset Resize Resolve Restart Restore Resume Revoke'
$verbsStartingWithEx = 'Exit Expand Export'
$verbsStartingWithConv = 'Convert ConvertFrom ConvertTo'
$lifeCycleVerbsStartingWithRe = 'Register Request Restart Resume'
$lifeCycleVerbsStartingWithReSingleQuote = "'Register' 'Request' 'Restart' 'Resume'"
$lifeCycleVerbsStartingWithReDoubleQuote = """Register"" ""Request"" ""Restart"" ""Resume"""
$dataVerbsStartingwithEx = 'Expand Export'
$lifeCycleAndCommmonVerbsStartingWithRe = 'Redo Register Remove Rename Request Reset Resize Restart Resume'
$allLifeCycleAndCommonVerbs = 'Add Approve Assert Build Clear Close Complete Confirm Copy Deny Deploy Disable Enable Enter Exit Find Format Get Hide Install Invoke Join Lock Move New Open Optimize Pop Push Redo Register Remove Rename Request Reset Resize Restart Resume Search Select Set Show Skip Split Start Step Stop Submit Suspend Switch Undo Uninstall Unlock Unregister Wait Watch'
$allJsonVerbs = 'ConvertFrom ConvertTo Test'
$jsonVerbsStartingWithConv = 'ConvertFrom ConvertTo'
$jsonVerbsStartingWithConvSingleQuote = "'ConvertFrom' 'ConvertTo'"
$jsonVerbsStartingWithConvDoubleQuote = """ConvertFrom"" ""ConvertTo"""
$allJsonAndJobVerbs = 'ConvertFrom ConvertTo Debug Get Receive Remove Start Stop Test Wait'
$jsonAndJobVerbsStartingWithSt = 'Start Stop'
$allObjectVerbs = 'Compare ForEach Group Measure New Select Sort Tee Where'
$utilityModuleObjectVerbs = 'Compare Group Measure New Select Sort Tee'
$utilityModuleObjectVerbsStartingWithS = 'Select Sort'
$utilityModuleObjectVerbsStartingWithSSingleQuote = "'Select' 'Sort'"
$utilityModuleObjectVerbsStartingWithSDoubleQuote = """Select"" ""Sort"""
$utilityModuleObjectVerbsStartingWithS
$coreModuleObjectVerbs = 'ForEach Where'
}
It "Should complete Verb parameter for '<TextInput>'" -TestCases @(
@{ TextInput = 'Get-Verb -Verb '; ExpectedVerbs = $allVerbs }
@{ TextInput = "Get-Verb -Verb '"; ExpectedVerbs = $allVerbsSingleQuote }
@{ TextInput = "Get-Verb -Verb """; ExpectedVerbs = $allVerbsDoubleQuote }
@{ TextInput = 'Get-Verb -Group Lifecycle, Common -Verb '; ExpectedVerbs = $allLifeCycleAndCommonVerbs }
@{ TextInput = 'Get-Verb -Verb Re'; ExpectedVerbs = $verbsStartingWithRe }
@{ TextInput = 'Get-Verb -Group Lifecycle -Verb Re'; ExpectedVerbs = $lifeCycleVerbsStartingWithRe }
@{ TextInput = "Get-Verb -Group Lifecycle -Verb 'Re"; ExpectedVerbs = $lifeCycleVerbsStartingWithReSingleQuote }
@{ TextInput = "Get-Verb -Group Lifecycle -Verb ""Re"; ExpectedVerbs = $lifeCycleVerbsStartingWithReDoubleQuote }
@{ TextInput = 'Get-Verb -Group Lifecycle -Verb Re'; ExpectedVerbs = $lifeCycleVerbsStartingWithRe }
@{ TextInput = 'Get-Verb -Group Lifecycle, Common -Verb Re'; ExpectedVerbs = $lifeCycleAndCommmonVerbsStartingWithRe }
@{ TextInput = 'Get-Verb -Verb Ex'; ExpectedVerbs = $verbsStartingWithEx }
@{ TextInput = 'Get-Verb -Group Data -Verb Ex'; ExpectedVerbs = $dataVerbsStartingwithEx }
@@ -871,12 +920,16 @@ ConstructorTestClass(int i, bool b)
@{ TextInput = 'Get-Command -Verb Conv'; ExpectedVerbs = $verbsStartingWithConv }
@{ TextInput = 'Get-Command -Noun Json -Verb '; ExpectedVerbs = $allJsonVerbs }
@{ TextInput = 'Get-Command -Noun Json -Verb Conv'; ExpectedVerbs = $jsonVerbsStartingWithConv }
@{ TextInput = "Get-Command -Noun Json -Verb 'Conv"; ExpectedVerbs = $jsonVerbsStartingWithConvSingleQuote }
@{ TextInput = "Get-Command -Noun Json -Verb ""Conv"; ExpectedVerbs = $jsonVerbsStartingWithConvDoubleQuote }
@{ TextInput = 'Get-Command -Noun Json, Job -Verb '; ExpectedVerbs = $allJsonAndJobVerbs }
@{ TextInput = 'Get-Command -Noun Json, Job -Verb St'; ExpectedVerbs = $jsonAndJobVerbsStartingWithSt }
@{ TextInput = 'Get-Command -Noun NonExistentNoun -Verb '; ExpectedVerbs = '' }
@{ TextInput = 'Get-Command -Noun Object -Module Microsoft.PowerShell.Utility,Microsoft.PowerShell.Core -Verb '; ExpectedVerbs = $allObjectVerbs }
@{ TextInput = 'Get-Command -Noun Object -Module Microsoft.PowerShell.Utility -Verb '; ExpectedVerbs = $utilityModuleObjectVerbs }
@{ TextInput = 'Get-Command -Noun Object -Module Microsoft.PowerShell.Utility -Verb S'; ExpectedVerbs = $utilityModuleObjectVerbsStartingWithS }
@{ TextInput = "Get-Command -Noun Object -Module Microsoft.PowerShell.Utility -Verb 'S"; ExpectedVerbs = $utilityModuleObjectVerbsStartingWithSSingleQuote }
@{ TextInput = "Get-Command -Noun Object -Module Microsoft.PowerShell.Utility -Verb ""S"; ExpectedVerbs = $utilityModuleObjectVerbsStartingWithSDoubleQuote }
@{ TextInput = 'Get-Command -Noun Object -Module Microsoft.PowerShell.Core -Verb '; ExpectedVerbs = $coreModuleObjectVerbs }
) {
param($TextInput, $ExpectedVerbs)
@@ -889,18 +942,26 @@ ConstructorTestClass(int i, bool b)
Context 'StrictMode Version parameter completion' {
BeforeAll {
$allStrictModeVersions = '1.0 2.0 3.0 Latest'
$allStrictModeVersionsSingleQuote = "'1.0' '2.0' '3.0' 'Latest'"
$allStrictModeVersionsDoubleQuote = """1.0"" ""2.0"" ""3.0"" ""Latest"""
$versionOne = '1.0'
$versionTwo = '2.0'
$versionThree = '3.0'
$latestVersion = 'Latest'
$latestVersionSingleQuote = "'Latest'"
$latestVersionDoubleQuote = """Latest"""
}
It "Should complete Version for '<TextInput>'" -TestCases @(
@{ TextInput = "Set-StrictMode -Version "; ExpectedVersions = $allStrictModeVersions }
@{ TextInput = "Set-StrictMode -Version '"; ExpectedVersions = $allStrictModeVersionsSingleQuote }
@{ TextInput = "Set-StrictMode -Version """; ExpectedVersions = $allStrictModeVersionsDoubleQuote }
@{ TextInput = "Set-StrictMode -Version 1"; ExpectedVersions = $versionOne }
@{ TextInput = "Set-StrictMode -Version 2"; ExpectedVersions = $versionTwo }
@{ TextInput = "Set-StrictMode -Version 3"; ExpectedVersions = $versionThree }
@{ TextInput = "Set-StrictMode -Version Lat"; ExpectedVersions = $latestVersion }
@{ TextInput = "Set-StrictMode -Version 'Lat"; ExpectedVersions = $latestVersionSingleQuote }
@{ TextInput = "Set-StrictMode -Version ""Lat"; ExpectedVersions = $latestVersionDoubleQuote }
@{ TextInput = "Set-StrictMode -Version NonExistentVersion"; ExpectedVersions = '' }
) {
param($TextInput, $ExpectedVersions)