-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
bug_report_test.go
64 lines (51 loc) · 1.38 KB
/
bug_report_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
//go:build !int
package cmd
import (
"bytes"
"io"
"os"
"path/filepath"
"strings"
"testing"
"github.com/google/go-cmp/cmp"
)
func TestBugReport(t *testing.T) {
t.Parallel()
t.Run("Check bug-report --help", func(t *testing.T) {
b := bytes.NewBufferString("")
copyRootCmd := newRootCmd()
copyRootCmd.SetOut(b)
copyRootCmd.SetArgs([]string{"bug-report", "--help"})
if err := copyRootCmd.Execute(); err != nil {
t.Fatal(err)
}
gotBytes, err := io.ReadAll(b)
if err != nil {
t.Fatal(err)
}
gotBytes = bytes.ReplaceAll(gotBytes, []byte("\r\n"), []byte("\n"))
wantBytes, err := os.ReadFile(filepath.Join("testdata", "bug_report", "bug_report.txt"))
if err != nil {
t.Fatal(err)
}
wantBytes = bytes.ReplaceAll(wantBytes, []byte("\r\n"), []byte("\n"))
if diff := cmp.Diff(strings.TrimSpace(string(gotBytes)), strings.TrimSpace(string(wantBytes))); diff != "" {
t.Errorf("value is mismatch (-want got):\n%s", diff)
}
})
}
func Test_bugReport(t *testing.T) {
t.Parallel()
cmd := newBugReportCmd()
cmd.Version = "v0.0.0"
wantReturnVal := 0
gotReturnVal := bugReport(cmd, nil, func(s string) bool {
if !strings.Contains(s, "v0.0.0") {
t.Errorf("Expected bug report to contain version number 'v0.0.0', but got: %s", s)
}
return true
})
if gotReturnVal != wantReturnVal {
t.Errorf("bugReport() = %d; want %d", gotReturnVal, wantReturnVal)
}
}