-
Notifications
You must be signed in to change notification settings - Fork 22
/
main_test.go
221 lines (216 loc) · 5.53 KB
/
main_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
package main
import (
"bytes"
"fmt"
"os"
"strings"
"testing"
"github.com/mdomke/git-semver/v6/version"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestParseFlags(t *testing.T) {
for _, test := range []struct {
args []string
cfg *Config
hasError bool
}{
{
args: []string{"-prefix", "ver", "-format", "x.y.z-p", "-guard", "repo-root"},
cfg: &Config{
prefix: "ver",
format: "x.y.z-p",
guardRelease: true,
args: []string{"repo-root"},
},
},
{
args: []string{"-no-hash"},
cfg: &Config{excludeHash: true, args: []string{}},
},
{
args: []string{"-no-meta"},
cfg: &Config{excludeMeta: true, args: []string{}},
},
{
args: []string{"-no-pre"},
cfg: &Config{excludePreRelease: true, args: []string{}},
},
{
args: []string{"-no-patch"},
cfg: &Config{excludePatch: true, args: []string{}},
},
{
args: []string{"-no-minor"},
cfg: &Config{excludeMinor: true, args: []string{}},
},
{
args: []string{"-no-prefix"},
cfg: &Config{excludePrefix: true, args: []string{}},
},
{
args: []string{"-set-meta", "finleap"},
cfg: &Config{setMeta: "finleap", args: []string{}},
},
{
args: []string{"-target", "minor"},
cfg: &Config{releaseTarget: version.Minor, args: []string{}},
},
{
args: []string{"-help"},
hasError: true,
},
{
args: []string{"-unknown"},
hasError: true,
},
} {
t.Run(strings.Join(test.args, " "), func(t *testing.T) {
cfg, out, err := parseFlags("git-semver", test.args)
if test.hasError {
require.Error(t, err)
assert.Nil(t, cfg)
assert.NotEmpty(t, out)
} else {
test.cfg.stdout = os.Stdout
test.cfg.stderr = os.Stderr
require.NoError(t, err)
assert.Equal(t, test.cfg, cfg)
assert.Empty(t, out)
}
})
}
}
func TestSelectFormat(t *testing.T) {
defaultVersion := version.Version{Major: 1, Minor: 2, Patch: 3, Commits: 4}
for _, test := range []struct {
desc string
cfg Config
v version.Version
format string
}{
{
desc: "Exclude minor format",
cfg: Config{excludeMinor: true},
v: defaultVersion,
format: "x",
},
{
desc: "Exclude patch format",
cfg: Config{excludePatch: true},
v: defaultVersion,
format: "x.y",
},
{
desc: "Exclude prerelease format",
cfg: Config{excludePreRelease: true},
v: defaultVersion,
format: "x.y.z",
},
{
desc: "Exclude hash format",
cfg: Config{excludeHash: true},
v: defaultVersion,
format: "x.y.z-p",
},
{
desc: "Prefere format over flags",
cfg: Config{format: "x.y.z", excludeMinor: true},
v: defaultVersion,
format: "x.y.z",
},
{
desc: "Prefere longest strip",
cfg: Config{excludeHash: true, excludeMinor: true},
v: defaultVersion,
format: "x",
},
{
desc: "Default to full format",
cfg: Config{},
v: defaultVersion,
format: "x.y.z-p+m",
},
{
desc: "Use full format with pre-release version if -guard specified",
cfg: Config{guardRelease: true, excludePatch: true},
v: defaultVersion,
format: "x.y.z-p+m",
},
{
desc: "Use full format with pre-release version if -guard and shorthand -format specified",
cfg: Config{guardRelease: true, format: "x.y.z"},
v: defaultVersion,
format: "x.y.z-p+m",
},
{
desc: "Use custom format with pre-release version if -guard and suitable -format specified",
cfg: Config{guardRelease: true, format: "x.y.z-p"},
v: defaultVersion,
format: "x.y.z-p",
},
{
desc: "Allow to strip meta/hash with -guard",
cfg: Config{guardRelease: true, excludeHash: true},
v: defaultVersion,
format: "x.y.z-p",
},
{
desc: "Allow to strip patch with -guard if no pre-release version",
cfg: Config{guardRelease: true, excludePatch: true},
v: version.Version{Major: 3, Minor: 2, Patch: 1},
format: "x.y",
},
} {
t.Run(test.desc, func(t *testing.T) {
assert.Equal(t, test.format, selectFormat(&test.cfg, test.v))
})
}
}
func TestHandle(t *testing.T) {
setup := func() (*Config, *bytes.Buffer) {
var (
cfg Config
buf bytes.Buffer
)
cfg.stderr = &buf
cfg.stdout = &buf
return &cfg, &buf
}
t.Run("Path does not exist", func(t *testing.T) {
cfg, buf := setup()
assert.NoDirExists(t, "/sdf/")
retval := handle(cfg, "/sdf/")
assert.Equal(t, 1, retval)
assert.Equal(t, "failed to open repo: repository does not exist", strings.TrimSpace(buf.String()))
})
t.Run("Path is not a git repo", func(t *testing.T) {
cfg, buf := setup()
assert.DirExists(t, "/tmp/")
retval := handle(cfg, "/tmp/")
assert.Equal(t, 1, retval)
assert.Equal(t, "failed to open repo: repository does not exist", strings.TrimSpace(buf.String()))
})
t.Run("Meta can be set", func(t *testing.T) {
cfg, buf := setup()
cfg.setMeta = "finleap"
retval := handle(cfg, "")
assert.Equal(t, 0, retval)
assert.True(t, strings.HasSuffix(strings.TrimSpace(buf.String()), cfg.setMeta))
})
t.Run("Prefix can be set", func(t *testing.T) {
cfg, buf := setup()
cfg.prefix = "v"
retval := handle(cfg, "")
fmt.Println(buf.String())
assert.Equal(t, 0, retval)
assert.True(t, strings.HasPrefix(strings.TrimSpace(buf.String()), cfg.prefix))
})
t.Run("Fails with invalid format", func(t *testing.T) {
cfg, buf := setup()
cfg.format = "a.b.c"
retval := handle(cfg, "")
assert.Equal(t, 1, retval)
assert.Equal(t, fmt.Sprintf("invalid format: %s", cfg.format), strings.TrimSpace(buf.String()))
})
}