Skip to content

Commit

Permalink
Fix issue with tab expansion of duplicate aliases.
Browse files Browse the repository at this point in the history
Add Pester test for bug.  Fix #421.
  • Loading branch information
rkeithhill committed Feb 16, 2017
1 parent 046876e commit c7feff3
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 7 deletions.
3 changes: 2 additions & 1 deletion src/GitTabExpansion.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,8 @@ function script:gitAliases($filter) {
}

function script:expandGitAlias($cmd, $rest) {
if ((git config --get-regexp "^alias\.$cmd`$") -match "^alias\.$cmd (?<cmd>[^!].*)`$") {
$gitAliases = @(git config --get-regexp "^alias\.$cmd`$")
if ($gitAliases[0] -match "^alias\.$cmd (?<cmd>[^!].*)`$") {
return "git $($Matches['cmd'])$rest"
}
else {
Expand Down
9 changes: 8 additions & 1 deletion test/Shared.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,19 @@ function MakeGitPath([string]$Path) {
$Path -replace '\\', '/'
}

function NewGitTempRepo {
function NewGitTempRepo([switch]$MakeInitialCommit) {
Push-Location
$temp = [System.IO.Path]::GetTempPath()
$repoPath = Join-Path $temp ([IO.Path]::GetRandomFileName())
git.exe init $repoPath *>$null
Set-Location $repoPath

if ($MakeInitialCommit) {
'readme' | Out-File .\README.md -Encoding ascii
git.exe add .\README.md *>$null
git.exe commit -m "initial commit." *>$null
}

$repoPath
}

Expand Down
36 changes: 31 additions & 5 deletions test/TabExpansion.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -142,14 +142,40 @@ Describe 'TabExpansion Tests' {
}
}

Context 'PowerShell Special Chars Tests' {
Context 'Alias TabExpansion Tests' {
BeforeAll {
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssigments', '')]
$repoPath = NewGitTempRepo
$repoPath = NewGitTempRepo -MakeInitialCommit
}
AfterAll {
RemoveGitTempRepo $repoPath
}
It 'Tab completes when there are multiple aliases of the same name' {
$addedAlias = $false
if (!(git config --global --get alias.co)) {
git.exe config --global alias.co checkout
$addedAlias = $true
}

try {
git.exe config alias.co checkout
(git.exe config --get-regexp alias\.).Count | Should Be 2

'readme' | Out-File .\README.md -Encoding ascii
git.exe add .\README.md
git.exe commit -m "initial commit."
$result = & $module GitTabExpansionInternal 'git co ma'
$result | Should BeExactly 'master'
}
finally {
if ($addedAlias) {
git config --global --unset alias.co
}
}
}
}

Context 'PowerShell Special Chars Tests' {
BeforeAll {
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssigments', '')]
$repoPath = NewGitTempRepo -MakeInitialCommit
}
AfterAll {
RemoveGitTempRepo $repoPath
Expand Down

0 comments on commit c7feff3

Please sign in to comment.