-
Notifications
You must be signed in to change notification settings - Fork 1
/
helpers.go
106 lines (90 loc) · 2.67 KB
/
helpers.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
/*
Copyright 2013-2014 Canonical Ltd.
This program is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License version 3, as published
by the Free Software Foundation.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranties of
MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program. If not, see <http://www.gnu.org/licenses/>.
*/
// Package testing contains helpers for testing.
package testing
import (
"launchpad.net/ubuntu-push/logger"
"path/filepath"
"runtime"
"strings"
"sync"
)
type captureHelper struct {
outputFunc func(int, string) error
lock sync.Mutex
logEvents []string
logEventCb func(string)
}
func (h *captureHelper) Output(calldepth int, s string) error {
err := h.outputFunc(calldepth+2, s)
if err == nil {
h.lock.Lock()
defer h.lock.Unlock()
if h.logEventCb != nil {
h.logEventCb(s)
}
h.logEvents = append(h.logEvents, s+"\n")
}
return err
}
func (h *captureHelper) captured() string {
h.lock.Lock()
defer h.lock.Unlock()
return strings.Join(h.logEvents, "")
}
func (h *captureHelper) reset() {
h.lock.Lock()
defer h.lock.Unlock()
h.logEvents = nil
}
func (h *captureHelper) setLogEventCb(cb func(string)) {
h.lock.Lock()
defer h.lock.Unlock()
h.logEventCb = cb
}
// TestLogger implements logger.Logger using gocheck.C and supporting
// capturing log strings.
type TestLogger struct {
logger.Logger
helper *captureHelper
}
// NewTestLogger can be used in tests instead of NewSimpleLogger(FromMinimalLogger).
func NewTestLogger(minLog logger.MinimalLogger, level string) *TestLogger {
h := &captureHelper{outputFunc: minLog.Output}
log := &TestLogger{
Logger: logger.NewSimpleLoggerFromMinimalLogger(h, level),
helper: h,
}
return log
}
// Captured returns accumulated log events.
func (tlog *TestLogger) Captured() string {
return tlog.helper.captured()
}
// Reset resets accumulated log events.
func (tlog *TestLogger) ResetCapture() {
tlog.helper.reset()
}
// SetLogEventCb sets a callback invoked for log events.
func (tlog *TestLogger) SetLogEventCb(cb func(string)) {
tlog.helper.setLogEventCb(cb)
}
// SourceRelative produces a path relative to the source code, makes
// sense only for tests when the code is available on disk.
func SourceRelative(relativePath string) string {
_, file, _, ok := runtime.Caller(1)
if !ok {
panic("failed to get source filename using Caller()")
}
return filepath.Join(filepath.Dir(file), relativePath)
}