Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue with tab expansion of duplicate aliases. #422

Merged
merged 8 commits into from
Feb 18, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/GitTabExpansion.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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 (?<cmd>[^!].*)`$") {
return "git $($Matches['cmd'])$rest"
$alias = git config "alias.$cmd"
if ($alias) {
return "git $alias$rest"
}
else {
return "git $cmd$rest"
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
56 changes: 51 additions & 5 deletions test/TabExpansion.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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' {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for fleshing out these tests!

$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
Expand Down