-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsemv_test.go
223 lines (198 loc) Β· 4.1 KB
/
semv_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
package semv
import (
"errors"
"testing"
)
type MockedCmd struct {
Out string
Err string
}
func (c MockedCmd) Do(name string, arg ...string) ([]byte, error) {
var err error
if c.Err != "" {
err = errors.New(c.Err)
}
return []byte(c.Out), err
}
var mixed = `1.0.0
bar-0
foo
v2.3.4-rc.2
v8.8.8
v12.0.1
v12.3.0-alpha
v12.3.0-alpha.0
v12.3.0-alpha.1
v12.3.0-alpha.1.beta
v12.3.0-beta
v12.3.0-beta.5
v12.3.0-rc
v12.344.0+20130313144700
v12.345.66
v12.345.67
v13.0.0-alpha.0
`
var empty = `
`
func TestCurrent(t *testing.T) {
tests := []struct {
out string
want string
}{
{mixed, "v12.345.67"},
{empty, ""},
}
for i, tt := range tests {
tagCmder = MockedCmd{Out: tt.out}
v, err := Latest()
if err != nil {
t.Fatal(err)
}
if v.String() != tt.want {
t.Errorf("test[%d]: out = %s, Semv = %s; want %s", i, tt.out, v, tt.want)
}
}
}
func TestString(t *testing.T) {
tests := []struct {
v *Semv
want string
}{
{&Semv{}, ""},
{MustNew("1.0.0"), "v1.0.0"},
}
for i, tt := range tests {
if tt.v.String() != tt.want {
t.Errorf("test[%d]: Semv(%#v) = %s; want %s", i, tt.v, tt.v, tt.want)
}
}
}
func TestNext(t *testing.T) {
tagCmder = MockedCmd{Out: mixed}
v, err := Latest()
if err != nil {
t.Fatal(err)
}
tests := []struct {
target string
want string
}{
{"major", "v13.0.0"},
{"minor", "v12.346.0"},
{"patch", "v12.345.68"},
}
for i, tt := range tests {
vn := v.Next(tt.target)
if vn.String() != tt.want {
t.Errorf("test[%d]: Semv(%#v) = %s; want %s", i, vn, vn, tt.want)
}
}
}
func TestPreRelease(t *testing.T) {
tagCmder = MockedCmd{Out: mixed}
v, err := Latest()
if err != nil {
t.Fatal(err)
}
tests := []struct {
target string
preName string
want string
}{
{"major", "", "v13.0.0-alpha.1"},
{"major", "beta", "v13.0.0-beta.0"},
{"minor", "", "v12.346.0-alpha.0"},
{"minor", "beta", "v12.346.0-beta.0"},
{"patch", "", "v12.345.68-alpha.0"},
{"patch", "beta", "v12.345.68-beta.0"},
}
for i, tt := range tests {
vn, err := v.Next(tt.target).PreRelease(tt.preName)
if err != nil {
t.Fatal(err)
}
if vn.String() != tt.want {
t.Errorf("test[%d]: target = %s; name = %s; Semv = %s; want %s", i, tt.target, tt.preName, vn, tt.want)
}
}
}
func TestBuild(t *testing.T) {
tagCmder = MockedCmd{Out: mixed}
v, err := Latest()
if err != nil {
t.Fatal(err)
}
u := "foobar"
h := "2f994ff"
usernameCmder = MockedCmd{Out: u + "\n"}
latestCommitCmder = MockedCmd{Out: h + "\n"}
tests := []struct {
target string
buildName string
want string
}{
{"major", "", "v13.0.0+" + h + "." + u},
{"major", "foo", "v13.0.0+foo"},
{"minor", "", "v12.346.0+" + h + "." + u},
{"minor", "bar", "v12.346.0+bar"},
{"patch", "", "v12.345.68+" + h + "." + u},
{"patch", "foo-bar", "v12.345.68+foo-bar"},
}
for i, tt := range tests {
vn, err := v.Next(tt.target).Build(tt.buildName)
if err != nil {
t.Fatal(err)
}
if vn.String() != tt.want {
t.Errorf("test[%d]: target = %s; name = %s; Semv = %s; want %s", i, tt.target, tt.buildName, vn, tt.want)
}
}
}
func TestIncrementMajor(t *testing.T) {
tests := []struct {
v *Semv
want string
}{
{MustNew("1.0.0"), "v2.0.0"},
{MustNew("1.2.3"), "v2.0.0"},
{MustNew("0.0.0"), "v1.0.0"},
}
for i, tt := range tests {
tt.v.incrementMajor()
if tt.v.String() != tt.want {
t.Errorf("test[%d]: Semv(%#v) = %s; want %s", i, tt.v, tt.v, tt.want)
}
}
}
func TestIncrementMinor(t *testing.T) {
tests := []struct {
v *Semv
want string
}{
{MustNew("1.0.0"), "v1.1.0"},
{MustNew("1.2.3"), "v1.3.0"},
{MustNew("0.0.0"), "v0.1.0"},
}
for i, tt := range tests {
tt.v.incrementMinor()
if tt.v.String() != tt.want {
t.Errorf("test[%d]: Semv(%#v) = %s; want %s", i, tt.v, tt.v, tt.want)
}
}
}
func TestIncrementPatch(t *testing.T) {
tests := []struct {
v *Semv
want string
}{
{MustNew("1.0.0"), "v1.0.1"},
{MustNew("1.2.3"), "v1.2.4"},
{MustNew("0.0.0"), "v0.0.1"},
}
for i, tt := range tests {
tt.v.incrementPatch()
if tt.v.String() != tt.want {
t.Errorf("test[%d]: Semv(%#v) = %s; want %s", i, tt.v, tt.v, tt.want)
}
}
}