Sort refs by name to be able to assert.Equal() consistently

Git may report in different orders depending on circumstances
This commit is contained in:
Steve Streeting 2015-08-21 16:53:16 +01:00
parent cdafe05dce
commit abf0f93441
2 changed files with 17 additions and 3 deletions

@ -1,6 +1,7 @@
package git_test // to avoid import cycles
import (
"sort"
"testing"
"time"
@ -144,13 +145,16 @@ func TestRecentBranches(t *testing.T) {
assert.Equal(t, nil, err)
expectedRefs = []*Ref{
&Ref{"master", RefTypeLocalBranch, outputs[5].Sha},
&Ref{"upstream/master", RefTypeRemoteBranch, outputs[5].Sha},
&Ref{"origin/master", RefTypeRemoteBranch, outputs[5].Sha},
&Ref{"upstream/included_branch_2", RefTypeRemoteBranch, outputs[4].Sha},
&Ref{"included_branch_2", RefTypeLocalBranch, outputs[4].Sha},
&Ref{"included_branch", RefTypeLocalBranch, outputs[3].Sha},
&Ref{"upstream/master", RefTypeRemoteBranch, outputs[5].Sha},
&Ref{"upstream/included_branch_2", RefTypeRemoteBranch, outputs[4].Sha},
&Ref{"origin/master", RefTypeRemoteBranch, outputs[5].Sha},
&Ref{"origin/included_branch", RefTypeRemoteBranch, outputs[3].Sha},
}
// Need to sort for consistent comparison
sort.Sort(test.RefsByName(expectedRefs))
sort.Sort(test.RefsByName(refs))
assert.Equal(t, expectedRefs, refs, "Refs should be correct")
// Recent, only single remote
@ -163,5 +167,8 @@ func TestRecentBranches(t *testing.T) {
&Ref{"included_branch", RefTypeLocalBranch, outputs[3].Sha},
&Ref{"origin/included_branch", RefTypeRemoteBranch, outputs[3].Sha},
}
// Need to sort for consistent comparison
sort.Sort(test.RefsByName(expectedRefs))
sort.Sort(test.RefsByName(refs))
assert.Equal(t, expectedRefs, refs, "Refs should be correct")
}

@ -370,3 +370,10 @@ func (r *PlaceholderDataReader) Read(p []byte) (int, error) {
}
return i, err
}
// RefsByName implements sort.Interface for []*git.Ref based on name
type RefsByName []*git.Ref
func (a RefsByName) Len() int { return len(a) }
func (a RefsByName) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a RefsByName) Less(i, j int) bool { return a[i].Name < a[j].Name }