Skip to content

Commit b1fa9aa

Browse files
committed
add more test cases
1 parent 7872108 commit b1fa9aa

File tree

5 files changed

+88
-8
lines changed

5 files changed

+88
-8
lines changed

main_test.go

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package main
2+
3+
import (
4+
"os"
5+
"testing"
6+
)
7+
8+
func TestMainFunction(t *testing.T) {
9+
originalArgs := os.Args
10+
defer func() { os.Args = originalArgs }()
11+
12+
os.Args = []string{"cmd", "-path=./test_data", "[email protected]"}
13+
14+
exitCode := 0
15+
func() {
16+
defer func() {
17+
if r := recover(); r != nil {
18+
if code, ok := r.(int); ok {
19+
exitCode = code
20+
} else {
21+
t.Fatalf("unexpected panic: %v", r)
22+
}
23+
}
24+
}()
25+
main()
26+
}()
27+
28+
if exitCode != 0 {
29+
t.Errorf("main() exited with code %d; expected 0", exitCode)
30+
}
31+
}

print_test.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,17 @@ func TestPrintCell(t *testing.T) {
9494
expectedMessage string
9595
}{
9696
{
97-
description: "Zero value (/8)",
97+
description: "Zero value",
9898
val: 0,
9999
maxValue: 10,
100100
expectedMessage: color.New(color.FgWhite, color.BgBlack).Sprintf(" - "),
101101
},
102+
{
103+
description: "Lower bound value (/8)",
104+
val: 1,
105+
maxValue: 10,
106+
expectedMessage: color.New(color.FgBlack, color.BgLightCyan).Sprintf(" 1 "),
107+
},
102108
{
103109
description: "Lower bound value (/4)",
104110
val: 2,

scan_test.go

+16-3
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@ func TestScanGitFolders(t *testing.T) {
1414
}
1515

1616
test := []struct {
17-
Name string
18-
Root string
19-
Want []string
17+
Name string
18+
Root string
19+
Want []string
20+
ExpectErr bool
2021
}{
2122
{
2223
Name: "5 expected repos",
@@ -34,11 +35,23 @@ func TestScanGitFolders(t *testing.T) {
3435
Root: path.Join(wd, ".github"),
3536
Want: []string{},
3637
},
38+
{
39+
Name: "path does not exist",
40+
Root: path.Join(wd, "does_not_exist"),
41+
Want: []string{},
42+
ExpectErr: true,
43+
},
3744
}
3845

3946
for _, tt := range test {
4047
t.Run(tt.Name, func(t *testing.T) {
4148
got, err := scanGitFolders(tt.Root)
49+
if tt.ExpectErr {
50+
if err == nil {
51+
t.Fatalf("expected error, got nil")
52+
}
53+
return
54+
}
4255
if err != nil {
4356
t.Fatalf("failed to scan git folders: %v", err)
4457
}

stats_test.go

+20-4
Original file line numberDiff line numberDiff line change
@@ -100,17 +100,20 @@ func TestProcessRepos(t *testing.T) {
100100
days := daysAgo(commitsDate)
101101

102102
tests := []struct {
103-
Name string
104-
Repos []string
105-
Email string
106-
Expected map[int]int
103+
Name string
104+
Repos []string
105+
Email string
106+
Expected map[int]int
107+
ExpectErr bool
107108
}{
108109
{
109110
Name: "test 1",
110111
Repos: []string{
111112
path.Join(wd, "test_data", "project_1"),
112113
path.Join(wd, "test_data", "project_2"),
113114
path.Join(wd, "test_data", "project_3"),
115+
path.Join(wd, "test_data", "project_that_has_future_commits"),
116+
path.Join(wd, "test_data", "project_by_another_contributor"),
114117
},
115118
116119
Expected: map[int]int{days: 9},
@@ -121,6 +124,13 @@ func TestProcessRepos(t *testing.T) {
121124
122125
Expected: map[int]int{},
123126
},
127+
{
128+
Name: "test 3",
129+
Repos: []string{path.Join(wd, "test_data", "repo_that_does_not_exist")},
130+
131+
Expected: map[int]int{},
132+
ExpectErr: true,
133+
},
124134
}
125135

126136
b := Boundary{
@@ -131,6 +141,12 @@ func TestProcessRepos(t *testing.T) {
131141
for _, tt := range tests {
132142
t.Run(tt.Name, func(t *testing.T) {
133143
commits := processRepos(tt.Repos, tt.Email, b)
144+
if tt.ExpectErr {
145+
if len(commits) != 0 {
146+
t.Fatalf("expected error, got commits results: %v", commits)
147+
}
148+
return
149+
}
134150
if len(commits) != len(tt.Expected) {
135151
t.Errorf("processRepos11() = %v, want %v", commits, tt.Expected)
136152
}

utils_test.go

+14
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,13 @@ func TestDaysAgo(t *testing.T) {
129129
}
130130
}
131131

132+
func TestGetGlobalEmailFromGit(t *testing.T) {
133+
email := getGlobalEmailFromGit()
134+
if email == "" {
135+
t.Errorf("Expected email, got empty string")
136+
}
137+
}
138+
132139
func TestGetMaxValue(t *testing.T) {
133140
type args struct {
134141
m map[int]int
@@ -248,6 +255,13 @@ func TestSetTimeFlags(t *testing.T) {
248255
expectedUntil: time.Time{},
249256
expectedError: "invalid 'until' date format. please use the format: 2006-01-02",
250257
},
258+
{
259+
name: "until flag is in the future",
260+
untilflag: "2099-01-01",
261+
sinceflag: "2022-01-01",
262+
expectedSince: time.Date(2022, time.January, 1, 0, 0, 0, 0, time.UTC),
263+
expectedUntil: now,
264+
},
251265
}
252266

253267
for _, tt := range tests {

0 commit comments

Comments
 (0)