Skip to content

Commit 8ccfd77

Browse files
committed
adds tests for checking between tags
1 parent c77cfe7 commit 8ccfd77

File tree

3 files changed

+150
-10
lines changed

3 files changed

+150
-10
lines changed

log/log.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,13 +210,14 @@ func CommitLog(currentRepository *git.Repository, startCommitString string, endC
210210
key = strings.SplitN(strings.TrimSpace(key), ":", 2)[0]
211211
normalizedHash := c.Hash.String() + " - " + normalizeCommit(c.Message, scopedKey)
212212

213-
logContainer.AddCommit(key, normalizedHash, skipClassification)
214-
215213
if endHash == nil && previousTag != nil && previousTag.Hash == c.Hash {
216214
break
217215
} else if endHash != nil && c.Hash == endHash.Hash {
218216
break
219217
}
218+
219+
logContainer.AddCommit(key, normalizedHash, skipClassification)
220+
220221
}
221222
return logContainer.ToMarkdown(skipClassification), ErrMessage{}
222223
}

log/log_test.go

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,6 @@ func TestCommitLogStartHash(t *testing.T) {
126126
t.Log("Commits: ", expectedCommits)
127127
t.Log("Start At:", startCommitHash)
128128

129-
// include only feature commits
130129
log, _ := CommitLog(repo, startCommitHash, "", SupportedKeys, true)
131130
if log == "" {
132131
t.Fail()
@@ -151,12 +150,11 @@ func TestCommitLogEndHash(t *testing.T) {
151150

152151
endCommitHash := expectedCommits[1]
153152
firstCommit := expectedCommits[0]
154-
acceptedCommitHashes := expectedCommits[1:]
153+
acceptedCommitHashes := expectedCommits[2:]
155154

156155
t.Log("Commits: ", expectedCommits)
157156
t.Log("End At:", endCommitHash)
158157

159-
// include only feature commits
160158
log, _ := CommitLog(repo, "", endCommitHash, SupportedKeys, true)
161159
if log == "" {
162160
t.Fail()
@@ -176,8 +174,3 @@ func TestCommitLogEndHash(t *testing.T) {
176174

177175
t.Log("\n", log)
178176
}
179-
180-
// TODO:
181-
// - Tests for checking between tags
182-
// - Variation of the above to check between 2 tags
183-
// - Another variation where one tag points to the head of the repo

log/tag_test.go

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
package commitlog
2+
3+
import (
4+
"strings"
5+
"testing"
6+
7+
"github.com/go-git/go-git/v5"
8+
"github.com/go-git/go-git/v5/plumbing"
9+
)
10+
11+
func TestCommitLogSingleTag(t *testing.T) {
12+
secondCommit := expectedCommits[1]
13+
acceptedCommits := expectedCommits[2:]
14+
15+
t.Log("Commits:", expectedCommits)
16+
t.Log("Tagged:", secondCommit)
17+
18+
hash, err := repo.ResolveRevision(plumbing.Revision(secondCommit))
19+
bail(err)
20+
21+
_, err = repo.CreateTag("0.0.0", *hash, &git.CreateTagOptions{
22+
Message: "0.0.0",
23+
})
24+
bail(err)
25+
26+
log, _ := CommitLog(repo, "", "", SupportedKeys, true)
27+
if log == "" {
28+
t.Fail()
29+
}
30+
31+
if strings.Contains(log, expectedCommits[0]) {
32+
t.Fail()
33+
}
34+
35+
for _, commit := range acceptedCommits {
36+
if !strings.Contains(log, commit) {
37+
t.Fail()
38+
}
39+
}
40+
41+
t.Log(log)
42+
43+
// clean-up
44+
bail(repo.DeleteTag("0.0.0"))
45+
46+
}
47+
48+
// Test with 2 tags, one on the second commit and one on the 2nd last commit,
49+
// should only have the last commit in the log
50+
func TestCommitLogDualTag(t *testing.T) {
51+
secondCommit := expectedCommits[1]
52+
secondLastCommit := expectedCommits[len(expectedCommits)-2]
53+
acceptedCommit := expectedCommits[len(expectedCommits)-1]
54+
55+
t.Log("Commits:", expectedCommits)
56+
t.Log("Tagged:", secondCommit, secondLastCommit)
57+
58+
secondHash, err := repo.ResolveRevision(plumbing.Revision(secondCommit))
59+
bail(err)
60+
61+
secondLastHash, err := repo.ResolveRevision(plumbing.Revision(secondLastCommit))
62+
bail(err)
63+
64+
_, err = repo.CreateTag("0.0.0", *secondHash, &git.CreateTagOptions{
65+
Message: "0.0.0",
66+
})
67+
bail(err)
68+
69+
_, err = repo.CreateTag("0.0.1", *secondLastHash, &git.CreateTagOptions{
70+
Message: "0.0.1",
71+
})
72+
bail(err)
73+
74+
log, _ := CommitLog(repo, "", "", SupportedKeys, true)
75+
if log == "" {
76+
t.Fail()
77+
}
78+
79+
for _, commit := range expectedCommits {
80+
if commit == acceptedCommit {
81+
if !strings.Contains(log, acceptedCommit) {
82+
t.Fail()
83+
}
84+
} else {
85+
if strings.Contains(log, commit) {
86+
t.Fail()
87+
}
88+
}
89+
90+
}
91+
92+
t.Log(log)
93+
94+
// clean-up
95+
bail(repo.DeleteTag("0.0.0"))
96+
bail(repo.DeleteTag("0.0.1"))
97+
}
98+
99+
// Test with 2 tags, one on the second commit and one on the last commit,
100+
// should give all commits till the 1st tag
101+
func TestCommitLogHeadTag(t *testing.T) {
102+
secondCommit := expectedCommits[1]
103+
lastCommit := expectedCommits[len(expectedCommits)-1]
104+
105+
t.Log("Commits:", expectedCommits)
106+
t.Log("Tagged:", secondCommit, lastCommit)
107+
108+
secondHash, err := repo.ResolveRevision(plumbing.Revision(secondCommit))
109+
bail(err)
110+
111+
lastHash, err := repo.ResolveRevision(plumbing.Revision(lastCommit))
112+
bail(err)
113+
114+
_, err = repo.CreateTag("0.0.0", *secondHash, &git.CreateTagOptions{
115+
Message: "0.0.0",
116+
})
117+
bail(err)
118+
119+
_, err = repo.CreateTag("0.0.1", *lastHash, &git.CreateTagOptions{
120+
Message: "0.0.1",
121+
})
122+
bail(err)
123+
124+
log, _ := CommitLog(repo, "", "", SupportedKeys, true)
125+
if log == "" {
126+
t.Fail()
127+
}
128+
129+
for index, commit := range expectedCommits {
130+
if index <= 1 {
131+
if strings.Contains(log, commit) {
132+
t.Fail()
133+
}
134+
}
135+
if index > 1 && !strings.Contains(log, commit) {
136+
t.Fail()
137+
}
138+
139+
}
140+
141+
t.Log(log)
142+
143+
// clean-up
144+
bail(repo.DeleteTag("0.0.0"))
145+
bail(repo.DeleteTag("0.0.1"))
146+
}

0 commit comments

Comments
 (0)