Skip to content

Commit 70615ab

Browse files
authored
chore: add tests for git exec commands (#187)
1 parent e96b83e commit 70615ab

File tree

9 files changed

+1656
-593
lines changed

9 files changed

+1656
-593
lines changed

app/app_test.go

Lines changed: 697 additions & 22 deletions
Large diffs are not rendered by default.

app/config_test.go

Lines changed: 81 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
11
package app
22

33
import (
4-
"reflect"
54
"testing"
65

6+
"github.com/stretchr/testify/assert"
77
"github.com/thegeeklab/git-sv/sv"
88
)
99

1010
func Test_merge(t *testing.T) {
11-
boolFalse := false
12-
boolTrue := true
13-
emptyStr := ""
14-
nonEmptyStr := "something"
15-
1611
tests := []struct {
1712
name string
1813
dst Config
@@ -21,137 +16,146 @@ func Test_merge(t *testing.T) {
2116
wantErr bool
2217
}{
2318
{
24-
"overwrite string",
25-
Config{LogLevel: "info"},
26-
Config{LogLevel: "warn"},
27-
Config{LogLevel: "warn"},
28-
false,
19+
name: "overwrite string",
20+
dst: Config{LogLevel: "info"},
21+
src: Config{LogLevel: "warn"},
22+
want: Config{LogLevel: "warn"},
23+
wantErr: false,
2924
},
3025
{
31-
"default string",
32-
Config{LogLevel: "info"},
33-
Config{LogLevel: ""},
34-
Config{LogLevel: "info"},
35-
false,
26+
name: "default string",
27+
dst: Config{LogLevel: "info"},
28+
src: Config{LogLevel: ""},
29+
want: Config{LogLevel: "info"},
30+
wantErr: false,
3631
},
3732
{
38-
"overwrite list",
39-
Config{Branches: sv.BranchesConfig{Skip: []string{"a", "b"}}},
40-
Config{Branches: sv.BranchesConfig{Skip: []string{"c", "d"}}},
41-
Config{Branches: sv.BranchesConfig{Skip: []string{"c", "d"}}},
42-
false,
33+
name: "overwrite list",
34+
dst: Config{Branches: sv.BranchesConfig{Skip: []string{"a", "b"}}},
35+
src: Config{Branches: sv.BranchesConfig{Skip: []string{"c", "d"}}},
36+
want: Config{Branches: sv.BranchesConfig{Skip: []string{"c", "d"}}},
37+
wantErr: false,
4338
},
4439
{
45-
"overwrite list with empty",
46-
Config{Branches: sv.BranchesConfig{Skip: []string{"a", "b"}}},
47-
Config{Branches: sv.BranchesConfig{Skip: make([]string, 0)}},
48-
Config{Branches: sv.BranchesConfig{Skip: make([]string, 0)}},
49-
false,
40+
name: "overwrite list with empty",
41+
dst: Config{Branches: sv.BranchesConfig{Skip: []string{"a", "b"}}},
42+
src: Config{Branches: sv.BranchesConfig{Skip: make([]string, 0)}},
43+
want: Config{Branches: sv.BranchesConfig{Skip: make([]string, 0)}},
44+
wantErr: false,
5045
},
5146
{
52-
"default list",
53-
Config{Branches: sv.BranchesConfig{Skip: []string{"a", "b"}}},
54-
Config{Branches: sv.BranchesConfig{Skip: nil}},
55-
Config{Branches: sv.BranchesConfig{Skip: []string{"a", "b"}}},
56-
false,
47+
name: "default list",
48+
dst: Config{Branches: sv.BranchesConfig{Skip: []string{"a", "b"}}},
49+
src: Config{Branches: sv.BranchesConfig{Skip: nil}},
50+
want: Config{Branches: sv.BranchesConfig{Skip: []string{"a", "b"}}},
51+
wantErr: false,
5752
},
5853

5954
{
60-
"overwrite pointer bool false",
61-
Config{Branches: sv.BranchesConfig{SkipDetached: &boolFalse}},
62-
Config{Branches: sv.BranchesConfig{SkipDetached: &boolTrue}},
63-
Config{Branches: sv.BranchesConfig{SkipDetached: &boolTrue}},
64-
false,
55+
name: "overwrite pointer bool false",
56+
dst: Config{Branches: sv.BranchesConfig{SkipDetached: toPtr(false)}},
57+
src: Config{Branches: sv.BranchesConfig{SkipDetached: toPtr(true)}},
58+
want: Config{Branches: sv.BranchesConfig{SkipDetached: toPtr(true)}},
59+
wantErr: false,
6560
},
6661
{
67-
"overwrite pointer bool true",
68-
Config{Branches: sv.BranchesConfig{SkipDetached: &boolTrue}},
69-
Config{Branches: sv.BranchesConfig{SkipDetached: &boolFalse}},
70-
Config{Branches: sv.BranchesConfig{SkipDetached: &boolFalse}},
71-
false,
62+
name: "overwrite pointer bool true",
63+
dst: Config{Branches: sv.BranchesConfig{SkipDetached: toPtr(true)}},
64+
src: Config{Branches: sv.BranchesConfig{SkipDetached: toPtr(false)}},
65+
want: Config{Branches: sv.BranchesConfig{SkipDetached: toPtr(false)}},
66+
wantErr: false,
7267
},
7368
{
74-
"default pointer bool",
75-
Config{Branches: sv.BranchesConfig{SkipDetached: &boolTrue}},
76-
Config{Branches: sv.BranchesConfig{SkipDetached: nil}},
77-
Config{Branches: sv.BranchesConfig{SkipDetached: &boolTrue}},
78-
false,
69+
name: "default pointer bool",
70+
dst: Config{Branches: sv.BranchesConfig{SkipDetached: toPtr(true)}},
71+
src: Config{Branches: sv.BranchesConfig{SkipDetached: nil}},
72+
want: Config{Branches: sv.BranchesConfig{SkipDetached: toPtr(true)}},
73+
wantErr: false,
7974
},
8075
{
81-
"merge maps",
82-
Config{CommitMessage: sv.CommitMessageConfig{
76+
name: "merge maps",
77+
dst: Config{CommitMessage: sv.CommitMessageConfig{
8378
Footer: map[string]sv.CommitMessageFooterConfig{"issue": {Key: "jira"}},
8479
}},
85-
Config{CommitMessage: sv.CommitMessageConfig{
80+
src: Config{CommitMessage: sv.CommitMessageConfig{
8681
Footer: map[string]sv.CommitMessageFooterConfig{"issue2": {Key: "jira2"}},
8782
}},
88-
Config{CommitMessage: sv.CommitMessageConfig{Footer: map[string]sv.CommitMessageFooterConfig{
83+
want: Config{CommitMessage: sv.CommitMessageConfig{Footer: map[string]sv.CommitMessageFooterConfig{
8984
"issue": {Key: "jira"},
9085
"issue2": {Key: "jira2"},
9186
}}},
92-
false,
87+
wantErr: false,
9388
},
9489
{
95-
"default maps",
96-
Config{CommitMessage: sv.CommitMessageConfig{
90+
name: "default maps",
91+
dst: Config{CommitMessage: sv.CommitMessageConfig{
9792
Footer: map[string]sv.CommitMessageFooterConfig{"issue": {Key: "jira"}},
9893
}},
99-
Config{CommitMessage: sv.CommitMessageConfig{
94+
src: Config{CommitMessage: sv.CommitMessageConfig{
10095
Footer: nil,
10196
}},
102-
Config{CommitMessage: sv.CommitMessageConfig{
97+
want: Config{CommitMessage: sv.CommitMessageConfig{
10398
Footer: map[string]sv.CommitMessageFooterConfig{"issue": {Key: "jira"}},
10499
}},
105-
false,
100+
wantErr: false,
106101
},
107102
{
108-
"merge empty maps",
109-
Config{CommitMessage: sv.CommitMessageConfig{
103+
name: "merge empty maps",
104+
dst: Config{CommitMessage: sv.CommitMessageConfig{
110105
Footer: map[string]sv.CommitMessageFooterConfig{"issue": {Key: "jira"}},
111106
}},
112-
Config{CommitMessage: sv.CommitMessageConfig{
107+
src: Config{CommitMessage: sv.CommitMessageConfig{
113108
Footer: map[string]sv.CommitMessageFooterConfig{},
114109
}},
115-
Config{CommitMessage: sv.CommitMessageConfig{
110+
want: Config{CommitMessage: sv.CommitMessageConfig{
116111
Footer: map[string]sv.CommitMessageFooterConfig{"issue": {Key: "jira"}},
117112
}},
118-
false,
113+
wantErr: false,
119114
},
120115
{
121-
"overwrite tag config",
122-
Config{
116+
name: "overwrite tag config",
117+
dst: Config{
123118
LogLevel: "info",
124119
Tag: TagConfig{
125-
Pattern: &nonEmptyStr,
126-
Filter: &nonEmptyStr,
120+
Pattern: toPtr("something"),
121+
Filter: toPtr("something"),
127122
},
128123
},
129-
Config{
124+
src: Config{
130125
LogLevel: "",
131126
Tag: TagConfig{
132-
Pattern: &emptyStr,
133-
Filter: &emptyStr,
127+
Pattern: toPtr(""),
128+
Filter: toPtr(""),
134129
},
135130
},
136-
Config{
131+
want: Config{
137132
LogLevel: "info",
138133
Tag: TagConfig{
139-
Pattern: &emptyStr,
140-
Filter: &emptyStr,
134+
Pattern: toPtr(""),
135+
Filter: toPtr(""),
141136
},
142137
},
143-
false,
138+
wantErr: false,
144139
},
145140
}
141+
146142
for _, tt := range tests {
147143
t.Run(tt.name, func(t *testing.T) {
148-
if err := merge(&tt.dst, tt.src); (err != nil) != tt.wantErr {
149-
t.Errorf("merge() error = %v, wantErr %v", err, tt.wantErr)
150-
}
144+
err := merge(&tt.dst, tt.src)
145+
146+
if tt.wantErr {
147+
assert.Error(t, err)
151148

152-
if !reflect.DeepEqual(tt.dst, tt.want) {
153-
t.Errorf("merge() = %v, want %v", tt.dst, tt.want)
149+
return
154150
}
151+
152+
assert.NoError(t, err)
153+
assert.Equal(t, tt.want, tt.dst)
155154
})
156155
}
157156
}
157+
158+
// Helper function to create a pointer to any type.
159+
func toPtr[T any](v T) *T {
160+
return &v
161+
}

go.mod

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,25 @@ require (
99
github.com/goccy/go-yaml v1.17.1
1010
github.com/manifoldco/promptui v0.9.0
1111
github.com/rs/zerolog v1.34.0
12+
github.com/stretchr/testify v1.10.0
1213
github.com/urfave/cli/v3 v3.2.0
1314
)
1415

1516
require (
1617
github.com/Masterminds/goutils v1.1.1 // indirect
1718
github.com/chzyer/readline v1.5.1 // indirect
19+
github.com/davecgh/go-spew v1.1.1 // indirect
1820
github.com/google/uuid v1.6.0 // indirect
1921
github.com/huandu/xstrings v1.5.0 // indirect
2022
github.com/mattn/go-colorable v0.1.13 // indirect
2123
github.com/mattn/go-isatty v0.0.20 // indirect
2224
github.com/mitchellh/copystructure v1.2.0 // indirect
2325
github.com/mitchellh/reflectwalk v1.0.2 // indirect
26+
github.com/pmezard/go-difflib v1.0.0 // indirect
2427
github.com/rogpeppe/go-internal v1.11.0 // indirect
2528
github.com/shopspring/decimal v1.4.0 // indirect
2629
github.com/spf13/cast v1.7.0 // indirect
2730
golang.org/x/crypto v0.35.0 // indirect
2831
golang.org/x/sys v0.30.0 // indirect
32+
gopkg.in/yaml.v3 v3.0.1 // indirect
2933
)

go.sum

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
7070
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
7171
golang.org/x/sys v0.30.0 h1:QjkSwP/36a20jFYWkSue1YwXzLmsV5Gfq7Eiy72C1uc=
7272
golang.org/x/sys v0.30.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
73+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
7374
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
7475
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
7576
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

0 commit comments

Comments
 (0)