-
Notifications
You must be signed in to change notification settings - Fork 48
/
tty_test.go
109 lines (90 loc) · 2.25 KB
/
tty_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
package printer
import (
"bytes"
"io"
"testing"
"time"
"github.com/mszostok/codeowners-validator/internal/check"
"github.com/mszostok/codeowners-validator/internal/ptr"
"github.com/sebdah/goldie/v2"
)
func TestTTYPrinterPrintCheckResult(t *testing.T) {
t.Run("Should print all reported issues", func(t *testing.T) {
// given
tty := TTYPrinter{}
buff := &bytes.Buffer{}
restore := overrideWriter(buff)
defer restore()
// when
tty.PrintCheckResult("Foo Checker", time.Second, check.Output{
Issues: []check.Issue{
{
Severity: check.Error,
LineNo: ptr.Uint64Ptr(42),
Message: "Simulate error in line 42",
},
{
Severity: check.Warning,
LineNo: ptr.Uint64Ptr(2020),
Message: "Simulate warning in line 2020",
},
{
Severity: check.Error,
Message: "Error without line number",
},
{
Severity: check.Warning,
Message: "Warning without line number",
},
},
})
// then
g := goldie.New(t, goldie.WithNameSuffix(".golden.txt"))
g.Assert(t, t.Name(), buff.Bytes())
})
t.Run("Should print OK status on empty issues list", func(t *testing.T) {
// given
tty := TTYPrinter{}
buff := &bytes.Buffer{}
restore := overrideWriter(buff)
defer restore()
// when
tty.PrintCheckResult("Foo Checker", time.Second, check.Output{
Issues: nil,
})
// then
g := goldie.New(t, goldie.WithNameSuffix(".golden.txt"))
g.Assert(t, t.Name(), buff.Bytes())
})
}
func TestTTYPrinterPrintSummary(t *testing.T) {
t.Run("Should print number of failures", func(t *testing.T) {
// given
tty := TTYPrinter{}
buff := &bytes.Buffer{}
restore := overrideWriter(buff)
defer restore()
// when
tty.PrintSummary(20, 10)
// then
g := goldie.New(t, goldie.WithNameSuffix(".golden.txt"))
g.Assert(t, t.Name(), buff.Bytes())
})
t.Run("Should print no when there is no failures", func(t *testing.T) {
// given
tty := TTYPrinter{}
buff := &bytes.Buffer{}
restore := overrideWriter(buff)
defer restore()
// when
tty.PrintSummary(20, 0)
// then
g := goldie.New(t, goldie.WithNameSuffix(".golden.txt"))
g.Assert(t, t.Name(), buff.Bytes())
})
}
func overrideWriter(in io.Writer) func() {
old := writer
writer = in
return func() { writer = old }
}