Skip to content

Commit c51f734

Browse files
AdmiringWormgep13
authored andcommitted
(#669) Add version completion for install and upgrade commands
Add chocoRemotePackageVersions function to fetch package versions from remote source and integrate it into tab completion for install and upgrade commands with --version option. This enables listing of available versions for a specified package, improving user experience when specifying versions. Add Pester tests to verify version completions appear correctly for various --version input scenarios and cursor positions, ensuring reliable behavior.
1 parent fc7fd5c commit c51f734

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed

src/chocolatey.resources/helpers/ChocolateyTabExpansion.ps1

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,17 @@ function script:chocoRemotePackages($filter) {
160160
ForEach-Object { $_.Split('|')[0] }
161161
}
162162

163+
function script:chocoRemotePackageVersions($Name, $Version) {
164+
$packageVersions = & $script:choco search $Name --exact --all-versions --limit-output --include-headers --order-by='LastPublished' |
165+
ConvertFrom-Csv -Delimiter '|'
166+
167+
if ($Version -and $packageVersions) {
168+
$packageVersions | Where-Object Version -Like "$Version*" | Select-Object -ExpandProperty Version -First 30
169+
} else {
170+
$packageVersions | Select-Object -ExpandProperty Version -First 30
171+
}
172+
}
173+
163174
function Get-AliasPattern($exe) {
164175
$aliases = @($exe) + @(Get-Alias | Where-Object { $_.Definition -eq $exe } | Select-Object -Exp Name)
165176

@@ -205,6 +216,11 @@ function ChocolateyTabExpansion($lastBlock) {
205216
@('disable', 'enable', 'get', 'list', '--help') | Where-Object { $_ -like "$($matches['subcommand'])*" }
206217
}
207218

219+
# Handles install/upgrade package versions for a specific package
220+
"^(install|upgrade)\s+(?<package>[^\.\-][^\s]+).*--version='?(?<version>[^\s']*)'?$" {
221+
chocoRemotePackageVersions -Name $matches['package'] -Version $matches['version'] | ForEach-Object { "--version='$_'" }
222+
}
223+
208224
# Handles install package names
209225
"^(install)\s+(?<package>[^\.][^-\s]+)$" {
210226
chocoRemotePackages $matches['package']

tests/pester-tests/chocolateyProfile.Tests.ps1

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,5 +432,70 @@ Describe "Chocolatey Profile" -Tag Chocolatey, Profile, Environment {
432432
$Completions | Should -Contain "--user=''" -Because $becauseCompletions
433433
$Completions | Should -Contain "--version=''" -Because $becauseCompletions
434434
}
435+
436+
It "Should list versions for <_> isdependency --version=" -ForEach @('install', 'upgrade') {
437+
$Command = "choco $_ isdependency --version="
438+
$Completions = (TabExpansion2 -inputScript $Command -cursorColumn $Command.Length).CompletionMatches.CompletionText
439+
440+
$becauseCompletions = ($Completions -Join ", ")
441+
442+
$Completions | Should -Contain "--version='2.1.0'" -Because $becauseCompletions
443+
$Completions | Should -Contain "--version='2.0.0'" -Because $becauseCompletions
444+
$Completions | Should -Contain "--version='1.1.0'" -Because $becauseCompletions
445+
$Completions | Should -Contain "--version='1.0.1'" -Because $becauseCompletions
446+
$Completions | Should -Contain "--version='1.0.0'" -Because $becauseCompletions
447+
}
448+
449+
It "Should list versions for <_> isdependency --version='" -ForEach @('install', 'upgrade') {
450+
$Command = "choco $_ isdependency --version='"
451+
$Completions = (TabExpansion2 -inputScript $Command -cursorColumn $Command.Length).CompletionMatches.CompletionText
452+
453+
$becauseCompletions = ($Completions -Join ", ")
454+
455+
$Completions | Should -Contain "--version='2.1.0'" -Because $becauseCompletions
456+
$Completions | Should -Contain "--version='2.0.0'" -Because $becauseCompletions
457+
$Completions | Should -Contain "--version='1.1.0'" -Because $becauseCompletions
458+
$Completions | Should -Contain "--version='1.0.1'" -Because $becauseCompletions
459+
$Completions | Should -Contain "--version='1.0.0'" -Because $becauseCompletions
460+
}
461+
462+
It "Should list versions for <_> isdependency --version=''" -ForEach @('install', 'upgrade') {
463+
$Command = "choco $_ isdependency --version=''"
464+
$Completions = (TabExpansion2 -inputScript $Command -cursorColumn ($Command.Length - 1)).CompletionMatches.CompletionText
465+
466+
$becauseCompletions = ($Completions -Join ", ")
467+
468+
$Completions | Should -Contain "--version='2.1.0'" -Because $becauseCompletions
469+
$Completions | Should -Contain "--version='2.0.0'" -Because $becauseCompletions
470+
$Completions | Should -Contain "--version='1.1.0'" -Because $becauseCompletions
471+
$Completions | Should -Contain "--version='1.0.1'" -Because $becauseCompletions
472+
$Completions | Should -Contain "--version='1.0.0'" -Because $becauseCompletions
473+
}
474+
475+
It "Should list versions for <_> isdependency --version='' without moving cursor" -ForEach @('install', 'upgrade') {
476+
$Command = "choco $_ isdependency --version=''"
477+
$Completions = (TabExpansion2 -inputScript $Command -cursorColumn ($Command.Length)).CompletionMatches.CompletionText
478+
479+
$becauseCompletions = ($Completions -Join ", ")
480+
481+
$Completions | Should -Contain "--version='2.1.0'" -Because $becauseCompletions
482+
$Completions | Should -Contain "--version='2.0.0'" -Because $becauseCompletions
483+
$Completions | Should -Contain "--version='1.1.0'" -Because $becauseCompletions
484+
$Completions | Should -Contain "--version='1.0.1'" -Because $becauseCompletions
485+
$Completions | Should -Contain "--version='1.0.0'" -Because $becauseCompletions
486+
}
487+
488+
It "Should list 2.x versions for <_> isdependency --version='2" -ForEach @('install', 'upgrade') {
489+
$Command = "choco $_ isdependency --version='2"
490+
$Completions = (TabExpansion2 -inputScript $Command -cursorColumn ($Command.Length)).CompletionMatches.CompletionText
491+
492+
$becauseCompletions = ($Completions -Join ", ")
493+
494+
$Completions | Should -Contain "--version='2.1.0'" -Because $becauseCompletions
495+
$Completions | Should -Contain "--version='2.0.0'" -Because $becauseCompletions
496+
$Completions | Should -Not -Contain "--version='1.1.0'" -Because $becauseCompletions
497+
$Completions | Should -Not -Contain "--version='1.0.1'" -Because $becauseCompletions
498+
$Completions | Should -Not -Contain "--version='1.0.0'" -Because $becauseCompletions
499+
}
435500
}
436501
}

0 commit comments

Comments
 (0)