-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathl15h_test.go
369 lines (308 loc) · 9.79 KB
/
l15h_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
// Copyright © 2017 Genome Research Limited
// Author: Sendu Bala <sb10@sanger.ac.uk>.
//
// This file is part of l15h.
//
// l15h is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// l15h is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with l15h. If not, see <http://www.gnu.org/licenses/>.
package l15h_test
import (
"bytes"
log "github.com/inconshreveable/log15"
"github.com/sb10/l15h"
. "github.com/smartystreets/goconvey/convey"
"os"
"os/exec"
"testing"
)
func TestStore(t *testing.T) {
Convey("You can set up the StoreHandler", t, func() {
buff := new(bytes.Buffer)
store := l15h.NewStore()
h := log.MultiHandler(
l15h.StoreHandler(store, log.LogfmtFormat()),
log.StreamHandler(buff, log.LogfmtFormat()),
)
log.Root().SetHandler(h)
Convey("You can log multiple messages", func() {
log.Info("one")
one := buff.String()
So(one, ShouldContainSubstring, "msg=one")
buff.Reset()
log.Info("two")
two := buff.String()
So(two, ShouldContainSubstring, "msg=two")
buff.Reset()
log.Info("three")
three := buff.String()
So(three, ShouldContainSubstring, "msg=three")
buff.Reset()
Convey("You can get all messages with Log()", func() {
So(store.Logs(), ShouldResemble, []string{one, two, three})
Convey("You can Clear()", func() {
store.Clear()
So(store.Logs(), ShouldBeEmpty)
Convey("And add more logs", func() {
log.Info("four")
four := buff.String()
So(four, ShouldContainSubstring, "msg=four")
buff.Reset()
So(store.Logs(), ShouldResemble, []string{four})
})
})
})
})
Reset(func() {
buff.Reset()
})
})
}
func TestCaller(t *testing.T) {
Convey("You can set up the CallerInfoHandler", t, func() {
buff := new(bytes.Buffer)
msg := "msg"
em := " msg=msg"
ec := " caller=l15h_test.go:"
h := l15h.CallerInfoHandler(log.StreamHandler(buff, log.LogfmtFormat()))
log.Root().SetHandler(h)
Convey("Debug() includes caller", func() {
log.Debug(msg)
lm := buff.String()
So(lm, ShouldContainSubstring, em)
So(lm, ShouldContainSubstring, ec)
})
Convey("Info() doesn't include caller", func() {
log.Info(msg)
lm := buff.String()
So(lm, ShouldContainSubstring, em)
So(lm, ShouldNotContainSubstring, ec)
})
Convey("Warn() includes caller", func() {
log.Warn(msg)
lm := buff.String()
So(lm, ShouldContainSubstring, em)
So(lm, ShouldContainSubstring, ec)
})
Convey("Error() includes caller", func() {
log.Error(msg)
lm := buff.String()
So(lm, ShouldContainSubstring, em)
So(lm, ShouldContainSubstring, ec)
})
Convey("Crit() includes stack", func() {
log.Crit(msg)
lm := buff.String()
So(lm, ShouldContainSubstring, em)
So(lm, ShouldContainSubstring, ` stack="[github.com/sb10/l15h/l15h_test.go:`)
})
Reset(func() {
buff.Reset()
})
})
}
func TestChanger(t *testing.T) {
Convey("You can set up the ChangeableHandler", t, func() {
buff := new(bytes.Buffer)
changer := l15h.NewChanger(log.DiscardHandler())
log.Root().SetHandler(l15h.ChangeableHandler(changer))
Convey("You can log a message to the original handler", func() {
log.Info("one")
So(buff.String(), ShouldBeEmpty)
Convey("You can create a new logger that inherits the changer and adds a new Handler", func() {
child := log.New("child", "true")
store := l15h.NewStore()
l15h.AddHandler(child, l15h.StoreHandler(store, log.LogfmtFormat()))
log.Info("two")
So(buff.String(), ShouldBeEmpty)
child.Info("1")
So(buff.String(), ShouldBeEmpty)
So(len(store.Logs()), ShouldEqual, 1)
Convey("You can change the logger and it affects the root and child logger", func() {
changer.SetHandler(log.StreamHandler(buff, log.LogfmtFormat()))
log.Info("three")
So(buff.String(), ShouldContainSubstring, " msg=three")
So(buff.String(), ShouldNotContainSubstring, "child")
buff.Reset()
child.Info("2")
So(buff.String(), ShouldContainSubstring, " msg=2")
So(buff.String(), ShouldContainSubstring, " child=true")
So(len(store.Logs()), ShouldEqual, 2)
})
})
})
})
}
func TestPanic(t *testing.T) {
Convey("You can Panic() at the root level", t, func() {
buff := new(bytes.Buffer)
msg := "msg"
h := log.StreamHandler(buff, log.LogfmtFormat())
log.Root().SetHandler(h)
So(func() { l15h.Panic(msg) }, ShouldPanic)
So(buff.String(), ShouldContainSubstring, " lvl=crit msg=msg panic=true")
buff.Reset()
Convey("And on a logger with context", func() {
logger := log.New("child", "context")
So(func() { l15h.PanicContext(logger, msg, "extra", "stuff") }, ShouldPanic)
logger.Crit(msg)
So(buff.String(), ShouldContainSubstring, " lvl=crit msg=msg child=context extra=stuff panic=true")
})
})
}
func TestFatal(t *testing.T) {
msg := "msg"
if os.Getenv("L15H_TEST_FATAL") == "1" {
h := log.StreamHandler(os.Stderr, log.LogfmtFormat())
log.Root().SetHandler(h)
if os.Getenv("L15H_TEST_FATALCONTEXT") == "1" {
logger := log.New("child", "context")
l15h.FatalContext(logger, msg, "extra", "stuff")
} else {
l15h.Fatal(msg)
}
return
}
Convey("You can Fatal() at the root level", t, func() {
// confirm it normally exits non-zero
cmd := exec.Command(os.Args[0], "-test.run=TestFatal")
cmd.Env = append(os.Environ(), "L15H_TEST_FATAL=1")
out, err := cmd.CombinedOutput()
e, ok := err.(*exec.ExitError)
So(ok && !e.Success(), ShouldBeTrue)
So(string(out), ShouldContainSubstring, " lvl=crit msg=msg fatal=true")
// get test coverage
buff := new(bytes.Buffer)
h := log.StreamHandler(buff, log.LogfmtFormat())
log.Root().SetHandler(h)
var i int
l15h.SetExitFunc(func(code int) {
i = code
})
l15h.Fatal(msg)
So(i, ShouldEqual, 1)
So(buff.String(), ShouldContainSubstring, " lvl=crit msg=msg fatal=true")
Convey("And on a logger with context", func() {
cmd = exec.Command(os.Args[0], "-test.run=TestFatal")
cmd.Env = append(os.Environ(), "L15H_TEST_FATAL", "L15H_TEST_FATALCONTEXT=1")
out, err = cmd.CombinedOutput()
e, ok = err.(*exec.ExitError)
So(ok && !e.Success(), ShouldBeTrue)
So(string(out), ShouldContainSubstring, " lvl=crit msg=msg child=context extra=stuff fatal=true")
})
})
}
// Client is our fake smtp client, code taken straight from
// https://github.com/go-playground/log/blob/master/handlers/email/email_test.go
// type Client struct {
// conn net.Conn
// address string
// time int64
// bufin *bufio.Reader
// bufout *bufio.Writer
// }
// func (c *Client) w(s string) {
// c.bufout.WriteString(s + "\r\n")
// c.bufout.Flush()
// }
// func (c *Client) r() string {
// reply, err := c.bufin.ReadString('\n')
// if err != nil {
// fmt.Println("e ", err)
// }
// return reply
// }
// func handleClient(c *Client, closePrematurly bool) string {
// var msg []byte
// c.w("220 Welcome to the Jungle")
// msg = append(msg, c.r()...)
// c.w("250 No one says helo anymore")
// msg = append(msg, c.r()...)
// c.w("250 Sender")
// msg = append(msg, c.r()...)
// c.w("250 Recipient")
// msg = append(msg, c.r()...)
// c.w("354 Ok Send data ending with <CRLF>.<CRLF>")
// for {
// text := c.r()
// bytes := []byte(text)
// msg = append(msg, bytes...)
// // 46 13 10
// if bytes[0] == 46 && bytes[1] == 13 && bytes[2] == 10 {
// break
// }
// }
// if !closePrematurly {
// c.w("250 server has transmitted the message")
// }
// c.conn.Close()
// return string(msg)
// }
// func TestLogEmail(t *testing.T) {
// // create a fake smtp server listening on port 3041
// var email string
// server, err := net.Listen("tcp", ":3041")
// if err != nil {
// t.Errorf("Expected <nil> Got '%s'", err)
// return
// }
// defer server.Close()
// proceed := make(chan bool)
// defer close(proceed)
// go func() {
// for {
// conn, err := server.Accept()
// if err != nil {
// email = ""
// break
// }
// if conn == nil {
// continue
// }
// c := &Client{
// conn: conn,
// address: conn.RemoteAddr().String(),
// time: time.Now().Unix(),
// bufin: bufio.NewReader(conn),
// bufout: bufio.NewWriter(conn),
// }
// email = handleClient(c, false)
// proceed <- true
// }
// }()
// SetAppName("multi")
// ToEmail(DebugAndHigher, "localhost", 3041, "", "", "from@email.com", []string{"to@email.com"}, "")
// msg := "test msg"
// Convey("When logging to email works with an app name set", t, func() {
// Convey("Debug(msg) works", func() {
// Debug(msg)
// <-proceed
// expectedPrefix := `EHLO localhost\s+MAIL FROM:<from@email.com>\s+RCPT TO:<to@email.com>`
// //*** for some reason ShouldStartWith doesn't work here with a
// // literal version of above, don't know why
// prefixR := regexp.MustCompile(expectedPrefix)
// So(prefixR.MatchString(email), ShouldBeTrue)
// So(email, ShouldContainSubstring, "From: from@email.com")
// So(email, ShouldContainSubstring, "To: to@email.com")
// So(email, ShouldContainSubstring, "Subject: test msg")
// So(email, ShouldContainSubstring, "<h2>test msg</h2>")
// So(email, ShouldContainSubstring, "<h4>multi</h4>") // *** this randomly fails!
// So(email, ShouldContainSubstring, "<p>DEBUG</p>")
// })
// Convey("Info(msg) works", func() {
// Info(msg)
// <-proceed
// So(email, ShouldContainSubstring, "<p>INFO</p>")
// })
// // *** Panic() creates an unreadable mess in emails atm...
// })
// }