generated from fallion/go-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommits_between_test.go
More file actions
101 lines (70 loc) · 2.54 KB
/
commits_between_test.go
File metadata and controls
101 lines (70 loc) · 2.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package git
import (
"os"
"testing"
"github.com/stretchr/testify/assert"
)
func TestCommitsBetween(t *testing.T) {
testGit, err := OpenGit("./testdata/git_tags")
assert.NoError(t, err)
headHashStr, err := testGit.runGitCommand("rev-parse", "HEAD")
assert.NoError(t, err)
headHash, err := NewHash(headHashStr)
assert.NoError(t, err)
tag, err := testGit.PreviousTag(headHash)
assert.NoError(t, err)
commit, err := testGit.Commit(tag.Hash)
assert.NoError(t, err)
assert.Equal(t, "chore: first commit on master\n", commit.Message)
commits, err := testGit.CommitsBetween(headHash, tag.Hash)
assert.NoError(t, err)
assert.Len(t, commits, 3)
middleCommit, _ := testGit.Commit(commits[1])
assert.Equal(t, "chore: third commit on master\n", middleCommit.Message)
}
func TestNoToCommit(t *testing.T) {
tmpDir, testGit := setupRepo(t)
defer os.RemoveAll(tmpDir)
lastHash := createTestHistory(t, testGit)
var emptyHash Hash
commits, err := testGit.CommitsBetween(lastHash, emptyHash)
assert.Equal(t, 4, len(commits))
commit, commitErr := testGit.Commit(commits[0])
assert.NoError(t, commitErr)
assert.Equal(t, "third commit on new branch\n", commit.Message)
assert.Equal(t, err, nil)
lastCommit, _ := testGit.Commit(commits[3])
assert.Equal(t, "test commit on master\n", lastCommit.Message)
}
func TestToFromEqual(t *testing.T) {
tmpDir, testGit := setupRepo(t)
defer os.RemoveAll(tmpDir)
lastHash := createTestHistory(t, testGit)
commits, err := testGit.CommitsBetween(lastHash, lastHash)
assert.Equal(t, 0, len(commits))
assert.NoError(t, err)
}
func TestCommitsBetweenDetachedHead(t *testing.T) {
testGit, err := OpenGit("./testdata/detached_head")
assert.NoError(t, err)
// Verify we're in detached HEAD state
currentBranch, err := testGit.CurrentBranch()
assert.NoError(t, err)
assert.Equal(t, "HEAD", currentBranch.Name())
// Get HEAD commit hash (second commit)
headHash := currentBranch.Hash()
// Get origin/master commit hash (third commit)
masterHashStr, err := testGit.runGitCommand("rev-parse", "origin/master")
assert.NoError(t, err)
masterHash, err := NewHash(masterHashStr)
assert.NoError(t, err)
// CommitsBetween should work even in detached HEAD state
// Get commits between HEAD (second commit) and origin/master (third commit)
commits, err := testGit.CommitsBetween(masterHash, headHash)
assert.NoError(t, err)
// Should have 1 commit (the third commit)
assert.Equal(t, 1, len(commits))
commit, err := testGit.Commit(commits[0])
assert.NoError(t, err)
assert.Equal(t, "third commit\n", commit.Message)
}