/
e2e_test.go
408 lines (351 loc) · 9.33 KB
/
e2e_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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
//go:build e2e
package integration
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"testing"
"time"
"github.com/MakeNowJust/heredoc/v2"
"github.com/araddon/dateparse"
"github.com/mattn/go-runewidth"
"github.com/mattn/go-shellwords"
"github.com/sebdah/goldie/v2"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
type TestCases struct {
cmd string
name string
dir string
bordered bool
}
var cases = []TestCases{
// Cobra
{
name: "Should return cobra help menu",
cmd: ``,
dir: "cobra",
},
{
name: "Should return cobra version in default Pretty format",
cmd: `version`,
dir: "cobra",
},
{
name: "Should return cobra version in YAML format",
cmd: `version -oyaml`,
dir: "cobra",
},
{
name: "Should return cobra version in JSON format",
cmd: `version -ojson`,
dir: "cobra",
},
{
name: "Should return cobra version in short format",
cmd: `version -oshort`,
dir: "cobra",
},
{
name: "Should return cobra version in short format with full flag name",
cmd: `version --output short`,
dir: "cobra",
},
{
name: "Should return cobra version help",
cmd: `version --help`,
dir: "cobra",
},
{
name: "Should return cobra version when alias ver is used",
cmd: `version ver`,
dir: "cobra",
},
// Cobra alias
{
name: "Should return cobra version help with v alias",
cmd: `version --help`,
dir: "cobra-alias",
},
{
name: "Should return cobra version when alias v is used",
cmd: `version v`,
dir: "cobra-alias",
},
// Custom fields
{
name: "Should return version with custom fields in default Pretty format",
cmd: ``,
dir: "custom-fields",
},
{
name: "Should return version with custom fields in JSON format",
cmd: `-ojson`,
dir: "custom-fields",
},
{
name: "Should return version with custom fields in YAML format",
cmd: `-oyaml`,
dir: "custom-fields",
},
// Custom formatting
{
name: "Should return version with custom Pretty formatting",
cmd: ``,
dir: "custom-formatting",
},
// Custom layout
{
name: "Should return version with custom Pretty layout",
cmd: ``,
bordered: true,
dir: "custom-layout",
},
// Custom renderer
{
name: "Should return version with custom Pretty renderer",
cmd: ``,
bordered: true,
dir: "custom-renderer",
},
// Plain
{
name: "Should return plain version",
cmd: ``,
dir: "plain",
},
// Printer
{
name: "Should return default printer version",
cmd: ``,
dir: "printer",
},
// Printer post hook
{
name: "Should return version with executed post hook in default Pretty format",
cmd: ``,
dir: "printer-post-hook",
},
{
name: "Should return version with executed post hook in JSON format",
cmd: `-ojson`,
dir: "printer-post-hook",
},
{
name: "Should return version with executed post hook in YAML format",
cmd: `-oyaml`,
dir: "printer-post-hook",
},
// Upgrade notice cobra
{
name: "Should return version with upgrade notice in default Pretty format",
cmd: `version`,
dir: "upgrade-notice-cobra",
},
{
name: "Should return version with upgrade notice in JSON format",
cmd: `version -ojson`,
dir: "upgrade-notice-cobra",
},
{
name: "Should return version with upgrade notice in YAML format",
cmd: `version -oyaml`,
dir: "upgrade-notice-cobra",
},
{
name: "Should return version with upgrade notice in short format",
cmd: `version -oshort`,
dir: "upgrade-notice-cobra",
},
// Upgrade notice custom
{
name: "Should return version with custom upgrade notice in default Pretty format",
cmd: ``,
dir: "upgrade-notice-custom",
},
{
name: "Should return version with custom upgrade notice in JSON format",
cmd: `-ojson`,
dir: "upgrade-notice-custom",
},
{
name: "Should return version with custom upgrade notice in YAML format",
cmd: `-oyaml`,
dir: "upgrade-notice-custom",
},
{
name: "Should return version with custom upgrade notice in short format",
cmd: `-oshort`,
dir: "upgrade-notice-custom",
},
// Upgrade notice standalone
{
name: "Should return upgrade notice from standalone command",
cmd: ``,
dir: "upgrade-notice-standalone",
},
}
// TestExamplesColorOutput tests examples usage with the colored output.
//
// This test is based on golden file. To update golden file, run:
//
// UPDATE_GOLDEN=true TEST_NAME=TestExamplesColorOutput mage test:e2e
func TestExamplesColorOutput(t *testing.T) {
if os.Getenv("CI") == "true" {
t.Skip("Those tests are not stable on CI yet")
}
t.Parallel()
for _, tc := range cases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
//t.Parallel() goexpect doesn't work in multi thread
// given
binaryPath := buildBinaryAllLDFlags(t, tc.dir)
// when
result, err := Exec(binaryPath, tc.cmd).
AwaitColorResultAtMost(10 * time.Second)
// then
require.NoError(t, err)
assert.Equal(t, 0, result.ExitCode)
data := normalizeOutput(result.Stdout, tc.bordered)
g := goldie.New(t, goldie.WithNameSuffix(".golden.txt"))
g.Assert(t, t.Name(), []byte(data))
})
}
}
// TestExamplesNoColorOutput tests examples usage with the non-colored output.
//
// This test is based on golden file. To update golden file, run:
//
// UPDATE_GOLDEN=true TEST_NAME=TestExamplesNoColorOutput mage test:e2e
func TestExamplesNoColorOutput(t *testing.T) {
t.Parallel()
for _, tc := range cases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
if runtime.GOOS != "windows" {
t.Parallel()
}
// given
binaryPath := buildBinaryAllLDFlags(t, tc.dir)
// when
result, err := Exec(binaryPath, tc.cmd).
AwaitResultAtMost(10 * time.Second)
// then
require.NoError(t, err)
assert.Equal(t, 0, result.ExitCode)
data := result.Stdout + result.Stderr
data = normalizeOutput(data, tc.bordered)
g := goldie.New(t, goldie.WithNameSuffix(".golden.txt"))
g.Assert(t, t.Name(), []byte(data))
})
}
}
// TestExamplesRecheckInterval tests that the recheck interval is respected.
//
// This test is based on golden file. To update golden file, run:
//
// UPDATE_GOLDEN=true TEST_NAME=TestExamplesRecheckInterval mage test:e2e
func TestExamplesRecheckInterval(t *testing.T) {
t.Parallel()
// given
binaryPath := buildBinaryAllLDFlags(t, "upgrade-notice-sub-cmd")
// when
result, err := Exec(binaryPath, "version check").
AwaitResultAtMost(10 * time.Second)
// then
// Should return upgrade notice for the first time
require.NoError(t, err, result.Stderr)
assert.Equal(t, 0, result.ExitCode)
data := result.Stdout + result.Stderr
g := goldie.New(t, goldie.WithNameSuffix(".golden.txt"))
g.Assert(t, t.Name(), []byte(data))
// when
result, err = Exec(binaryPath, "version check").
AwaitResultAtMost(10 * time.Second)
// then
require.NoError(t, err, result.Stderr)
assert.Equal(t, 0, result.ExitCode)
data = result.Stdout + result.Stderr
g = goldie.New(t, goldie.WithNameSuffix("recheck.golden.txt"))
g.Assert(t, t.Name(), []byte(data))
}
func normalizeOutput(data string, bordered bool) string {
platform := fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH)
normalizedPlatform := "normalized"
if bordered { // if bordered we need to be careful to do not mess up the padding
padding := runewidth.StringWidth(platform) - runewidth.StringWidth(normalizedPlatform)
normalizedPlatform += strings.Repeat(" ", padding)
}
return strings.ReplaceAll(data, platform, normalizedPlatform)
}
var prettyResolvedFieldsFormat = heredoc.Doc(`
💡 %s
Version (devel)
Git Commit %s
Build Date N/A
Commit Date %s
Dirty Build %s
Go version 1.19.4
Compiler gc
Platform %s
`)
// TestExamplesColorOutput tests that version can resolve the info fields automatically.
func TestResolvesDefaultFields(t *testing.T) {
t.Parallel()
// given
var bin = "auto-resolved-fields"
if runtime.GOOS == "windows" {
bin += ".exe"
}
var (
dir = filepath.Join(exampleDir, "custom-formatting")
binaryPath = filepath.Join(dir, bin)
)
args, err := shellwords.Parse(fmt.Sprintf(`build -o %s . `, bin))
require.NoError(t, err)
cmd := exec.Command("go", args...)
cmd.Dir = dir
require.NoError(t, cmd.Run())
// when
result, err := Exec(binaryPath, "").
AwaitResultAtMost(10 * time.Second)
commit, commitDate, dirtyBuild := getGitDetails(t)
expOutput := fmt.Sprintf(prettyResolvedFieldsFormat, binaryPath, commit, commitDate, dirtyBuild, fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH))
// then
require.NoError(t, err)
assert.Equal(t, 0, result.ExitCode)
assert.Equal(t, expOutput, result.Stdout)
assert.Empty(t, result.Stderr)
}
func getGitDetails(t *testing.T) (string, string, string) {
t.Helper()
var (
commit = runGitCmd(t, `rev-parse HEAD`)
date = fmtDate(runGitCmd(t, `--no-pager log -1 --format="%cD"`))
dirty = fmtBool(runGitCmd(t, `status --short`) != "")
)
return fmt.Sprintf("%.7s", commit), date, dirty
}
func fmtBool(in bool) string {
if in {
return "yes"
}
return "no"
}
func runGitCmd(t *testing.T, rawArgs string) string {
t.Helper()
args, err := shellwords.Parse(rawArgs)
require.NoError(t, err)
out, err := exec.Command("git", args...).CombinedOutput()
require.NoError(t, err)
return strings.TrimSpace(string(out))
}
func fmtDate(in string) string {
normalized, _ := dateparse.ParseAny(in)
return normalized.Local().Format(time.RFC822)
}