-
Notifications
You must be signed in to change notification settings - Fork 1
/
today_test.go
244 lines (182 loc) · 5.92 KB
/
today_test.go
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package main
import (
"syscall"
"testing"
"time"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing/object"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
)
// TODO: Cleanup the larger tests which use setupRepo func into `suite` tests.
var (
currentDir, _ = syscall.Getwd()
testSignature = &git.CommitOptions{
Author: &object.Signature{
Name: "testUser",
Email: "testEmail",
When: time.Now().UTC(),
},
}
)
const (
thisRepo string = "https://github.com/jdockerty/today"
zeroTime time.Duration = 0 * time.Minute
oneMinuteSince time.Duration = 1 * time.Minute
twoDaysSince time.Duration = 48 * time.Hour
)
// Involves all tests which require reading/writing of a commit, this is requires the setup and teardown
// of a directory which is tracked by git.
type CommitSuite struct {
suite.Suite
Repo *git.Repository // Repo to use within the test suite.
Worktree *git.Worktree
}
func setupRepo(url string, t *testing.T) (*git.Repository, error) {
r, err := git.PlainClone(t.TempDir(), false, &git.CloneOptions{
URL: url,
RecurseSubmodules: git.DefaultSubmoduleRecursionDepth,
})
if err != nil {
return nil, err
}
return r, nil
}
// Setup before every test, whilst this might be a little inefficient for getting the repository before
// every test, we do so to ensure that our commit history is completely fresh from previous test commits.
func (suite *CommitSuite) SetupTest() {
r, err := setupRepo(thisRepo, suite.T())
if err != nil {
suite.FailNow("Unable to setup CommitSuite repo: %s\n", err)
}
suite.Repo = r
w, err := r.Worktree()
if err != nil {
suite.FailNow("Unable to setup CommitSuite worktree: %s\n", err)
}
suite.Worktree = w
}
func (suite *CommitSuite) TestFullContainsAuthor() {
assert := assert.New(suite.T())
w, err := suite.Repo.Worktree()
assert.Nil(err)
_, err = w.Commit("TEST", testSignature)
assert.Nil(err)
m := make(map[string]*git.Repository, 1)
m["today"] = suite.Repo
msgs, err := getCommitMessages(m, testSignature.Author.Name, true, oneMinuteSince)
assert.Nil(err)
assert.Contains(msgs, "today")
assert.GreaterOrEqual(2, len(msgs)) // Our 2 commits here and any others which are within 48 hours.
}
func (suite *CommitSuite) TestFullContainsAuthorHasNoCommits() {
assert := assert.New(suite.T())
_, err := suite.Worktree.Commit("TEST", testSignature)
assert.Nil(err)
m := make(map[string]*git.Repository, 1)
m["today"] = suite.Repo
msgs, err := getCommitMessages(m, "INVALID_COMMIT_AUTHOR", true, oneMinuteSince)
assert.Nil(err)
assert.Contains(msgs, "today")
assert.Equal(0, len(msgs["today"]))
}
func (suite *CommitSuite) TestNoResultsForZeroSinceValue() {
assert := assert.New(suite.T())
_, err := suite.Worktree.Commit("TEST", testSignature)
assert.Nil(err)
m := make(map[string]*git.Repository, 1)
m["today"] = suite.Repo
msgs, err := getCommitMessages(m, "", true, zeroTime)
assert.Nil(err)
assert.Equal(0, len(msgs["today"]))
}
func (suite *CommitSuite) TestResultsForMinimalSinceValue() {
assert := assert.New(suite.T())
_, err := suite.Worktree.Commit("TEST", testSignature)
assert.Nil(err)
m := make(map[string]*git.Repository, 1)
m["today"] = suite.Repo
msgs, err := getCommitMessages(m, "", true, oneMinuteSince)
assert.Nil(err)
assert.Contains(msgs, "today")
assert.Equal("TEST", msgs["today"][0]) // We know there is a single message here, as we made it for the test
assert.Equal(1, len(msgs))
}
func (suite *CommitSuite) TestResultsForLargerSinceValue() {
assert := assert.New(suite.T())
_, err := suite.Worktree.Commit("TEST", testSignature)
assert.Nil(err)
_, err = suite.Worktree.Commit("TEST2", testSignature)
assert.Nil(err)
m := make(map[string]*git.Repository, 1)
m["today"] = suite.Repo
msgs, err := getCommitMessages(m, "", true, twoDaysSince)
assert.Nil(err)
assert.Contains(msgs, "today")
assert.GreaterOrEqual(2, len(msgs)) // Our 2 commits here and any others which are within 48 hours.
}
func (suite *CommitSuite) TestShortCommitMessage() {
assert := assert.New(suite.T())
_, err := suite.Worktree.Commit("TEST\nNOT SEEN", testSignature)
assert.Nil(err)
m := make(map[string]*git.Repository, 1)
m["today"] = suite.Repo
msgs, err := getCommitMessages(m, "", true, oneMinuteSince)
assert.Nil(err)
assert.Equal(4, len(msgs["today"][0])) // Length of 'TEST' = 4
}
func (suite *CommitSuite) TestLongCommitMessage() {
assert := assert.New(suite.T())
_, err := suite.Worktree.Commit("TEST\nSEEN", testSignature)
assert.Nil(err)
m := make(map[string]*git.Repository, 1)
m["today"] = suite.Repo
msgs, err := getCommitMessages(m, "", false, oneMinuteSince)
assert.Nil(err)
assert.Equal("TEST\nSEEN", msgs["today"][0])
}
func TestCommitSuite(t *testing.T) {
suite.Run(t, new(CommitSuite))
}
func TestValidatePathsProducesErrorWithInvalidDir(t *testing.T) {
invalidPath := []string{"/does/not/exist"}
err := validatePaths(invalidPath)
assert.Error(t, err)
}
func TestValidatePathsProducesErrorWithNoGitDir(t *testing.T) {
gitDirNotExist := []string{"/tmp"}
err := validatePaths(gitDirNotExist)
assert.Error(t, err)
}
func TestValidatePathsWithGitDir(t *testing.T) {
gitDirExists := []string{currentDir} // Current directory is tracked by git
err := validatePaths(gitDirExists)
assert.Nil(t, err)
}
func TestGetRepositories(t *testing.T) {
gitDir := []string{currentDir}
_, err := getRepositories(gitDir)
assert.Nil(t, err)
}
func TestDoesContainAuthor(t *testing.T) {
testCommit := &object.Commit{
Author: object.Signature{
Name: "testUser",
Email: "[email protected]",
When: time.Now().UTC(),
},
}
got := containsAuthor(testCommit, "testUser")
assert.True(t, got)
}
func TestDoesNotContainAuthor(t *testing.T) {
testCommit := &object.Commit{
Author: object.Signature{
Name: "testUser",
Email: "[email protected]",
When: time.Now().UTC(),
},
}
got := containsAuthor(testCommit, "John")
assert.False(t, got)
}