/
scan_test.go
212 lines (198 loc) · 5.18 KB
/
scan_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
package main
import (
"io"
"os"
"path/filepath"
"strings"
"testing"
)
type initiator func()
func defaultOptions() []gitOption {
maxAuthors = defaultMaxAuthors
maxCommits = defaultMaxCommits
var opts = []gitOption{
optionMaxCommits(defaultMaxCommits),
optionBranch(""),
}
return opts
}
func TestScan(t *testing.T) {
opts := defaultOptions()
commits, authors, err := ScanCWD(opts...)
if len(commits) == 0 || err != nil {
t.Errorf("zero commits or error:%v", err)
t.FailNow()
}
if commits[len(commits)-1].Message != "initial commit" {
t.Error("Expected initial commit at the end")
}
_ = authors
}
func TestAlignments(t *testing.T) {
opts := defaultOptions()
commits, authors, _ := ScanCWD(opts...)
SetAuthorAlignments(commits, authors)
emptyalignment := alignment{}
for i := range authors {
if authors[i].alignment == emptyalignment {
t.Error("alignment not set for", authors[i].Name)
}
}
}
func TestFile(t *testing.T) {
var tests = []struct {
file string
maxCommits int
maxAuthors int
}{
{maxCommits: 200, maxAuthors: 100, file: "testdata/awesomego.log"},
{maxCommits: 315, maxAuthors: 20, file: "testdata/deal.log"},
{maxCommits: 2, maxAuthors: 20, file: "testdata/deal.log"},
{maxCommits: 20, maxAuthors: 20, file: "testdata/deal.log"},
{maxCommits: 80, maxAuthors: 20, file: "testdata/deal.log"},
}
for i := range tests {
maxAuthors, maxCommits = tests[i].maxAuthors, tests[i].maxCommits
commits, authors := scanFromLogFile(t, tests[i].file)
if len(commits) == 0 || len(commits) < tests[i].maxCommits || len(authors) == 0 {
t.Error("bad length of results " + tests[i].file)
}
err := SetAuthorAlignments(commits, authors)
if err != nil {
t.Error(err)
}
emptyalignment := alignment{}
for i := range authors {
if authors[i].alignment == emptyalignment {
t.Errorf("%d:alignment not set for %v", tests[i].maxCommits, authors[i].Stats())
}
}
}
}
func TestTokenize(t *testing.T) {
var tests = []struct {
file string
maxAuthors, maxCommits int
}{
{maxCommits: 200, maxAuthors: 100, file: "testdata/awesomego.log"},
{maxCommits: 315, maxAuthors: 20, file: "testdata/deal.log"},
{maxCommits: 2, maxAuthors: 20, file: "testdata/deal.log"},
{maxCommits: 20, maxAuthors: 20, file: "testdata/deal.log"},
{maxCommits: 80, maxAuthors: 20, file: "testdata/deal.log"},
}
for i := range tests {
maxAuthors, maxCommits = tests[i].maxAuthors, tests[i].maxCommits
commits, _ := scanFromLogFile(t, tests[i].file)
tokens, err := tokenizeCommits(commits)
if err != nil {
t.Error(err)
}
atCommit := 0
last := -1
for i := range tokens {
if tokens[i].Tag == "." {
// f(&commits[atCommit], tokens[last+1:i])
msg := replacecommits.Replace(commits[atCommit].Message)
splitsies := strings.Fields(msg)
for j := range splitsies {
if splitsies[j] != "" && splitsies[j] != tokens[last+1+j].Text {
t.Errorf("expected %q=%q\n in %v=%v\n\n", splitsies[j], tokens[last+1+j].Text, splitsies, tokens[last+1:i])
}
}
last = i
atCommit++
}
}
}
}
func TestDisplay(t *testing.T) {
var tests = []struct {
file string
maxCommits int
maxAuthors int
}{
{maxCommits: 200, maxAuthors: 100, file: "testdata/awesomego.log"},
{maxCommits: 315, maxAuthors: 20, file: "testdata/deal.log"},
{maxCommits: 2, maxAuthors: 20, file: "testdata/deal.log"},
{maxCommits: 20, maxAuthors: 20, file: "testdata/deal.log"},
{maxCommits: 80, maxAuthors: 20, file: "testdata/deal.log"},
}
for i := range tests {
maxAuthors = tests[i].maxAuthors
commits, authors := scanFromLogFile(t, tests[i].file)
err := WriteAuthorAlignments(io.Discard, authors)
if err != nil {
t.Error(err)
}
err = SetCommitAlignments(commits, authors)
if err != nil {
t.Error(err)
}
err = WriteCommitAlignments(io.Discard, commits)
if err != nil {
t.Error(err)
}
err = WriteNLPTags(io.Discard, commits)
if err != nil {
t.Error(err)
}
}
}
func scanFromLogFile(t *testing.T, filename string) ([]commit, []author) {
f, err := os.Open(filename)
if err != nil {
t.FailNow()
}
t.Cleanup(func() { f.Close() })
commits, authors, err := GitLogScan(f)
if err != nil && err != io.EOF {
t.Error(err)
t.FailNow()
}
return commits, authors
}
func TestNoFolderError(t *testing.T) {
opts := defaultOptions()
dir, err := filepath.Abs(".")
if err != nil {
t.FailNow()
}
base := filepath.Base(dir)
err = os.Chdir("../")
t.Cleanup(func() {
os.Chdir(base)
})
if err != nil {
t.Error(err)
t.FailNow()
}
commits, _, err := ScanCWD(opts...)
if err == nil || len(commits) > 0 {
t.Error("expected error and no commits detected being in non git dir")
}
}
func TestFindAuthor(t *testing.T) {
firstCommiter := "Patricio Whittingslow"
opts := []gitOption{
optionAuthorPattern(firstCommiter),
optionMaxCommits(4),
}
maxAuthors = 1
commits, authors, err := ScanCWD(opts...)
if err != nil {
t.Error(err)
t.FailNow()
}
if len(authors) != 1 || len(commits) == 0 {
t.Error("got more than 1 author or no commits")
}
if authors[0].Name != firstCommiter {
t.Error("could not find", firstCommiter, "author")
}
}
func TestRun(t *testing.T) {
err := run()
if err != nil {
t.Error(err)
}
}