diff --git a/src/GitTabExpansion.ps1 b/src/GitTabExpansion.ps1 index f4f2829eb..922a08c62 100644 --- a/src/GitTabExpansion.ps1 +++ b/src/GitTabExpansion.ps1 @@ -188,12 +188,13 @@ function script:gitAliases($filter) { $alias } } - } | Sort-Object + } | Sort-Object -Unique } function script:expandGitAlias($cmd, $rest) { - if ((git config --get-regexp "^alias\.$cmd`$") -match "^alias\.$cmd (?[^!].*)`$") { - return "git $($Matches['cmd'])$rest" + $alias = git config "alias.$cmd" + if ($alias) { + return "git $alias$rest" } else { return "git $cmd$rest" diff --git a/test/Shared.ps1 b/test/Shared.ps1 index 0f8f49977..68dae17fe 100644 --- a/test/Shared.ps1 +++ b/test/Shared.ps1 @@ -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 } diff --git a/test/TabExpansion.Tests.ps1 b/test/TabExpansion.Tests.ps1 index f15875fc0..62094248d 100644 --- a/test/TabExpansion.Tests.ps1 +++ b/test/TabExpansion.Tests.ps1 @@ -142,14 +142,60 @@ Describe 'TabExpansion Tests' { } } - Context 'PowerShell Special Chars Tests' { + Context 'Alias TabExpansion Tests' { + $addedAliases = @() + function Add-GlobalTestAlias($Name, $Value) { + if (!(git config --global "alias.$Name")) { + git.exe config --global "alias.$Name" $Value + $addedAliases += $Name + } + } BeforeAll { [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssigments', '')] - $repoPath = NewGitTempRepo + $repoPath = NewGitTempRepo -MakeInitialCommit + } + AfterAll { + $addedAliases | Where-Object { $_ } | ForEach-Object { + git.exe config --global --unset "alias.$_" 2>$null + } + + RemoveGitTempRepo $repoPath + } + It 'Command completion includes unique list of aliases' { + $alias = "test-$(New-Guid)" + + Add-GlobalTestAlias $alias config + git.exe config alias.$alias help + (git.exe config --get-all alias.$alias).Count | Should Be 2 + + $result = @(& $module GitTabExpansionInternal "git $alias") + $result.Count | Should Be 1 + $result[0] | Should BeExactly $alias + } + It 'Tab completes when there is one alias of a given name' { + $alias = "test-$(New-Guid)" - 'readme' | Out-File .\README.md -Encoding ascii - git.exe add .\README.md - git.exe commit -m "initial commit." + git.exe config alias.$alias checkout + (git.exe config --get-all alias.$alias).Count | Should Be 1 + + $result = & $module GitTabExpansionInternal "git $alias ma" + $result | Should BeExactly 'master' + } + It 'Tab completes when there are multiple aliases of the same name' { + Add-GlobalTestAlias co checkout + + git.exe config alias.co checkout + (git.exe config --get-all alias.co).Count | Should BeGreaterThan 1 + + $result = & $module GitTabExpansionInternal 'git co ma' + $result | Should BeExactly 'master' + } + } + + Context 'PowerShell Special Chars Tests' { + BeforeAll { + [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssigments', '')] + $repoPath = NewGitTempRepo -MakeInitialCommit } AfterAll { RemoveGitTempRepo $repoPath