diff options
author | Michael Muré <batolettre@gmail.com> | 2020-09-26 14:06:17 +0200 |
---|---|---|
committer | Michael Muré <batolettre@gmail.com> | 2020-09-29 20:42:21 +0200 |
commit | 9408f1eb0e99cddee1a7e1739bd786de543c30e7 (patch) | |
tree | 7ee51f388499731ae72cec4e6f597e0d9e10ac7e /repository/repo_testing.go | |
parent | cedcc2772c88b78bc62594afd06c44c7a7c244e4 (diff) | |
download | git-bug-9408f1eb0e99cddee1a7e1739bd786de543c30e7.tar.gz |
repo: test both plain and bare, test clocks
Diffstat (limited to 'repository/repo_testing.go')
-rw-r--r-- | repository/repo_testing.go | 284 |
1 files changed, 157 insertions, 127 deletions
diff --git a/repository/repo_testing.go b/repository/repo_testing.go index 28eb9a21..53375a5b 100644 --- a/repository/repo_testing.go +++ b/repository/repo_testing.go @@ -7,8 +7,9 @@ import ( "strings" "testing" - "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + + "github.com/MichaelMure/git-bug/util/lamport" ) func CleanupTestRepos(repos ...Repo) { @@ -47,136 +48,165 @@ type RepoCleaner func(repos ...Repo) // Test suite for a Repo implementation func RepoTest(t *testing.T, creator RepoCreator, cleaner RepoCleaner) { - t.Run("Blob-Tree-Commit-Ref", func(t *testing.T) { - repo := creator(false) - defer cleaner(repo) - - // Blob - - data := randomData() - - blobHash1, err := repo.StoreData(data) - require.NoError(t, err) - assert.True(t, blobHash1.IsValid()) - - blob1Read, err := repo.ReadData(blobHash1) - require.NoError(t, err) - assert.Equal(t, data, blob1Read) - - // Tree - - blobHash2, err := repo.StoreData(randomData()) - require.NoError(t, err) - blobHash3, err := repo.StoreData(randomData()) - require.NoError(t, err) - - tree1 := []TreeEntry{ - { - ObjectType: Blob, - Hash: blobHash1, - Name: "blob1", - }, - { - ObjectType: Blob, - Hash: blobHash2, - Name: "blob2", - }, - } - - treeHash1, err := repo.StoreTree(tree1) - require.NoError(t, err) - assert.True(t, treeHash1.IsValid()) - - tree1Read, err := repo.ReadTree(treeHash1) - require.NoError(t, err) - assert.ElementsMatch(t, tree1, tree1Read) - - tree2 := []TreeEntry{ - { - ObjectType: Tree, - Hash: treeHash1, - Name: "tree1", - }, - { - ObjectType: Blob, - Hash: blobHash3, - Name: "blob3", - }, - } - - treeHash2, err := repo.StoreTree(tree2) - require.NoError(t, err) - assert.True(t, treeHash2.IsValid()) - - tree2Read, err := repo.ReadTree(treeHash2) - require.NoError(t, err) - assert.ElementsMatch(t, tree2, tree2Read) - - // Commit - - commit1, err := repo.StoreCommit(treeHash1) - require.NoError(t, err) - assert.True(t, commit1.IsValid()) - - treeHash1Read, err := repo.GetTreeHash(commit1) - require.NoError(t, err) - assert.Equal(t, treeHash1, treeHash1Read) - - commit2, err := repo.StoreCommitWithParent(treeHash2, commit1) - require.NoError(t, err) - assert.True(t, commit2.IsValid()) - - treeHash2Read, err := repo.GetTreeHash(commit2) - require.NoError(t, err) - assert.Equal(t, treeHash2, treeHash2Read) + for bare, name := range map[bool]string{ + false: "Plain", + true: "Bare", + } { + t.Run(name, func(t *testing.T) { + t.Run("Blob-Tree-Commit-Ref", func(t *testing.T) { + repo := creator(bare) + defer cleaner(repo) + + // Blob + + data := randomData() + + blobHash1, err := repo.StoreData(data) + require.NoError(t, err) + require.True(t, blobHash1.IsValid()) + + blob1Read, err := repo.ReadData(blobHash1) + require.NoError(t, err) + require.Equal(t, data, blob1Read) + + // Tree + + blobHash2, err := repo.StoreData(randomData()) + require.NoError(t, err) + blobHash3, err := repo.StoreData(randomData()) + require.NoError(t, err) + + tree1 := []TreeEntry{ + { + ObjectType: Blob, + Hash: blobHash1, + Name: "blob1", + }, + { + ObjectType: Blob, + Hash: blobHash2, + Name: "blob2", + }, + } + + treeHash1, err := repo.StoreTree(tree1) + require.NoError(t, err) + require.True(t, treeHash1.IsValid()) + + tree1Read, err := repo.ReadTree(treeHash1) + require.NoError(t, err) + require.ElementsMatch(t, tree1, tree1Read) + + tree2 := []TreeEntry{ + { + ObjectType: Tree, + Hash: treeHash1, + Name: "tree1", + }, + { + ObjectType: Blob, + Hash: blobHash3, + Name: "blob3", + }, + } + + treeHash2, err := repo.StoreTree(tree2) + require.NoError(t, err) + require.True(t, treeHash2.IsValid()) + + tree2Read, err := repo.ReadTree(treeHash2) + require.NoError(t, err) + require.ElementsMatch(t, tree2, tree2Read) + + // Commit + + commit1, err := repo.StoreCommit(treeHash1) + require.NoError(t, err) + require.True(t, commit1.IsValid()) + + treeHash1Read, err := repo.GetTreeHash(commit1) + require.NoError(t, err) + require.Equal(t, treeHash1, treeHash1Read) + + commit2, err := repo.StoreCommitWithParent(treeHash2, commit1) + require.NoError(t, err) + require.True(t, commit2.IsValid()) + + treeHash2Read, err := repo.GetTreeHash(commit2) + require.NoError(t, err) + require.Equal(t, treeHash2, treeHash2Read) + + // Ref + + exist1, err := repo.RefExist("refs/bugs/ref1") + require.NoError(t, err) + require.False(t, exist1) + + err = repo.UpdateRef("refs/bugs/ref1", commit2) + require.NoError(t, err) + + exist1, err = repo.RefExist("refs/bugs/ref1") + require.NoError(t, err) + require.True(t, exist1) + + ls, err := repo.ListRefs("refs/bugs") + require.NoError(t, err) + require.ElementsMatch(t, []string{"refs/bugs/ref1"}, ls) + + err = repo.CopyRef("refs/bugs/ref1", "refs/bugs/ref2") + require.NoError(t, err) + + ls, err = repo.ListRefs("refs/bugs") + require.NoError(t, err) + require.ElementsMatch(t, []string{"refs/bugs/ref1", "refs/bugs/ref2"}, ls) + + commits, err := repo.ListCommits("refs/bugs/ref2") + require.NoError(t, err) + require.ElementsMatch(t, []Hash{commit1, commit2}, commits) + + // Graph + + commit3, err := repo.StoreCommitWithParent(treeHash1, commit1) + require.NoError(t, err) + + ancestorHash, err := repo.FindCommonAncestor(commit2, commit3) + require.NoError(t, err) + require.Equal(t, commit1, ancestorHash) + + err = repo.RemoveRef("refs/bugs/ref1") + require.NoError(t, err) + }) + + t.Run("Local config", func(t *testing.T) { + repo := creator(bare) + defer cleaner(repo) + + testConfig(t, repo.LocalConfig()) + }) + + t.Run("Clocks", func(t *testing.T) { + repo := creator(bare) + defer cleaner(repo) - // Ref + clock, err := repo.GetOrCreateClock("foo") + require.NoError(t, err) + require.Equal(t, lamport.Time(1), clock.Time()) - exist1, err := repo.RefExist("refs/bugs/ref1") - require.NoError(t, err) - assert.False(t, exist1) + time, err := clock.Increment() + require.NoError(t, err) + require.Equal(t, lamport.Time(1), time) + require.Equal(t, lamport.Time(2), clock.Time()) - err = repo.UpdateRef("refs/bugs/ref1", commit2) - require.NoError(t, err) + clock2, err := repo.GetOrCreateClock("foo") + require.NoError(t, err) + require.Equal(t, lamport.Time(2), clock2.Time()) - exist1, err = repo.RefExist("refs/bugs/ref1") - require.NoError(t, err) - assert.True(t, exist1) - - ls, err := repo.ListRefs("refs/bugs") - require.NoError(t, err) - assert.ElementsMatch(t, []string{"refs/bugs/ref1"}, ls) - - err = repo.CopyRef("refs/bugs/ref1", "refs/bugs/ref2") - require.NoError(t, err) - - ls, err = repo.ListRefs("refs/bugs") - require.NoError(t, err) - assert.ElementsMatch(t, []string{"refs/bugs/ref1", "refs/bugs/ref2"}, ls) - - commits, err := repo.ListCommits("refs/bugs/ref2") - require.NoError(t, err) - assert.ElementsMatch(t, []Hash{commit1, commit2}, commits) - - // Graph - - commit3, err := repo.StoreCommitWithParent(treeHash1, commit1) - require.NoError(t, err) - - ancestorHash, err := repo.FindCommonAncestor(commit2, commit3) - require.NoError(t, err) - assert.Equal(t, commit1, ancestorHash) - - err = repo.RemoveRef("refs/bugs/ref1") - require.NoError(t, err) - }) - - t.Run("Local config", func(t *testing.T) { - repo := creator(false) - defer cleaner(repo) - - testConfig(t, repo.LocalConfig()) - }) + clock3, err := repo.GetOrCreateClock("bar") + require.NoError(t, err) + require.Equal(t, lamport.Time(1), clock3.Time()) + }) + }) + } } func randomData() []byte { |