Files
lazygit/pkg/commands/models/commit_store_test.go
Jesse Duffield a2a8755a7c Add commit store
I want an efficient way to get information about the commits of a repo.
So far, our Commit model contains a mix of immutable and mutable fields, and this means we need to throw out
commits whenever we refresh, because one of the mutable fields may have changed.

The commit store will store a new model, ImmutableCommit which never changes so we can just continue adding
commits to the store without worrying about invalidating any of it.

One use case for this store is the ability to determine if one commit is an ancestor of another, which will
help us colour the commits against each of our branches in the local branches view. Without an in-memory
store, we would need to make one git call per commit which would be super slow.

If this store proves useful, we could switch to using it as the source of truth for our commits, with
mutable stuff handled separately.
2023-10-02 16:25:15 +11:00

89 lines
2.2 KiB
Go

package models
import (
"testing"
)
func TestCommitStore(t *testing.T) {
scenarios := []struct {
testName string
commits []ImmutableCommit
hash string
ancestorHash string
expected IsAncestorResponse
}{
{
testName: "Empty commit store, same hash",
commits: []ImmutableCommit{},
hash: "a",
ancestorHash: "a",
expected: IsAncestorResponseYes,
},
{
testName: "Empty commit store, different hash",
commits: []ImmutableCommit{},
hash: "a",
ancestorHash: "b",
expected: IsAncestorResponseUnknown,
},
{
testName: "Hash found, ancestor not found",
commits: []ImmutableCommit{
NewImmutableCommit("a", []string{"c"}),
NewImmutableCommit("c", []string{}),
},
hash: "a",
ancestorHash: "b",
expected: IsAncestorResponseNo,
},
{
testName: "Hash found, ancestor is parent",
commits: []ImmutableCommit{
NewImmutableCommit("a", []string{"b"}),
NewImmutableCommit("b", []string{"c"}),
},
hash: "a",
ancestorHash: "b",
expected: IsAncestorResponseYes,
},
{
testName: "Hash found, ancestor is grandparent",
commits: []ImmutableCommit{
NewImmutableCommit("a", []string{"b"}),
NewImmutableCommit("b", []string{"c"}),
},
hash: "a",
ancestorHash: "c",
expected: IsAncestorResponseYes,
},
{
testName: "Hash found, not an ancestor",
commits: []ImmutableCommit{
NewImmutableCommit("a", []string{"b"}),
NewImmutableCommit("b", []string{"d"}),
NewImmutableCommit("c", []string{"d"}),
NewImmutableCommit("d", []string{}),
},
hash: "a",
ancestorHash: "c",
expected: IsAncestorResponseNo,
},
}
for _, scenario := range scenarios {
t.Run(scenario.testName, func(t *testing.T) {
commitStore := NewCommitStore()
commitStore.AddSlice(scenario.commits)
response := commitStore.IsAncestor(scenario.hash, scenario.ancestorHash)
if response != scenario.expected {
responseStr := IsAncestorResponseStrings[response]
expectedStr := IsAncestorResponseStrings[scenario.expected]
t.Errorf("Expected %s, got %s", expectedStr, responseStr)
}
})
}
}