-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog_writer_test.go
132 lines (107 loc) · 4.02 KB
/
log_writer_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
package peanut_test
import (
"bytes"
"io/ioutil"
"log"
"os"
"strings"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/jimsmart/peanut"
)
var _ = Describe("LogWriter", func() {
newFn := func() (peanut.Writer, *bytes.Buffer) {
buf := &bytes.Buffer{}
w := &peanut.LogWriter{
Logger: log.New(buf, "", 0),
Verbose: true,
}
return w, buf
}
It("should write the correct text when sequential structs are written", func() {
w, buf := newFn()
testWritesAndCloseSequential(w)
expectedOutput := "<Foo> foo_string: test 1 foo_int: 1\n" +
"<Foo> foo_string: test 2 foo_int: 2\n" +
"<Foo> foo_string: test 3 foo_int: 3\n" +
"<Bar> bar_int: 1 bar_string: test 1\n" +
"<Bar> bar_int: 2 bar_string: test 2\n" +
"<Bar> bar_int: 3 bar_string: test 3\n" +
"<Baz> baz_string: test 1 baz_bool: true baz_float32: 1.234 baz_float64: 9.876 baz_int: -12345 baz_int8: -8 baz_int16: -16 baz_int32: -32 baz_int64: -64 baz_uint: 12345 baz_uint8: 8 baz_uint16: 16 baz_uint32: 32 baz_uint64: 64\n" +
"<Qux> -\n" +
"<Qux> -\n" +
"<Qux> -\n" +
"Called LogWriter.Close\n" +
"Called LogWriter.Cancel\n"
output := string(buf.Bytes())
// fmt.Println(output)
Expect(output).To(Equal(expectedOutput))
})
It("should write the correct text when interleaved structs are written", func() {
w, buf := newFn()
testWritesAndCloseInterleaved(w)
expectedOutput := "<Foo> foo_string: test 1 foo_int: 1\n" +
"<Bar> bar_int: 1 bar_string: test 1\n" +
"<Qux> -\n" +
"<Foo> foo_string: test 2 foo_int: 2\n" +
"<Bar> bar_int: 2 bar_string: test 2\n" +
"<Qux> -\n" +
"<Foo> foo_string: test 3 foo_int: 3\n" +
"<Bar> bar_int: 3 bar_string: test 3\n" +
"<Qux> -\n" +
"<Baz> baz_string: test 1 baz_bool: true baz_float32: 1.234 baz_float64: 9.876 baz_int: -12345 baz_int8: -8 baz_int16: -16 baz_int32: -32 baz_int64: -64 baz_uint: 12345 baz_uint8: 8 baz_uint16: 16 baz_uint32: 32 baz_uint64: 64\n" +
"Called LogWriter.Close\n"
output := string(buf.Bytes())
// fmt.Println(output)
Expect(output).To(Equal(expectedOutput))
})
It("should write the correct text when structs are written and cancel is called", func() {
w, buf := newFn()
testWritesAndCancel(w)
expectedOutput := "<Foo> foo_string: test 1 foo_int: 1\n" +
"<Bar> bar_int: 1 bar_string: test 1\n" +
"Called LogWriter.Cancel\n" +
"Called LogWriter.Close\n"
output := string(buf.Bytes())
Expect(output).To(Equal(expectedOutput))
})
It("should not return any errors when used with its default config", func() {
w := &peanut.LogWriter{}
// Temporarily replace Stderr, to capture log output.
// See https://stackoverflow.com/questions/25609734/testing-stdout-with-go-and-ginkgo
pr, pw, _ := os.Pipe()
tmp := os.Stderr
defer func() {
os.Stderr = tmp
}()
os.Stderr = pw
go func() {
// Run our tests.
testWritesAndCloseSequential(w)
pw.Close()
}()
stdout, _ := ioutil.ReadAll(pr)
// Output looks like:
// 2021/04/18 20:35:43 <Foo> foo_string: test 1 foo_int: 1
// 2021/04/18 20:35:43 <Foo> foo_string: test 2 foo_int: 2
// 2021/04/18 20:35:43 <Foo> foo_string: test 3 foo_int: 3
// 2021/04/18 20:35:43 <Bar> bar_int: 1 bar_string: test 1
// 2021/04/18 20:35:43 <Bar> bar_int: 2 bar_string: test 2
// 2021/04/18 20:35:43 <Bar> bar_int: 3 bar_string: test 3
// 2021/04/18 20:35:43 <Baz> baz_string: test 1 baz_bool: true baz_float32: 1.234 baz_float64: 9.876 baz_int: -12345 baz_int8: -8 baz_int16: -16 baz_int32: -32 baz_int64: -64 baz_uint: 12345 baz_uint8: 8 baz_uint16: 16 baz_uint32: 32 baz_uint64: 64"
lines := strings.Split(string(stdout), "\n")
for _, x := range lines {
// fmt.Println(x)
if len(x) > 0 {
Expect(x).To(MatchRegexp(`^\d{4}\/\d{2}\/\d{2} \d{2}:\d{2}:\d{2} <[A-Z][a-z]*> [a-z\-]`))
}
}
})
Context("when given a struct with an unsupported field type", func() {
It("should return an error with an informative message", func() {
w := &peanut.LogWriter{}
testWriteBadType(w)
// TODO(js) Do we need further checks, e.g. file not exists ...?
})
})
})