-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdebug_test.go
78 lines (71 loc) · 1.51 KB
/
debug_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
// Copyright 2020 Guoyao Wu. All rights reserved.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file.
package soon
import (
"bytes"
"io"
"log"
"os"
"sync"
"testing"
"github.com/stretchr/testify/assert"
)
func TestIsDebugging(t *testing.T) {
tests := []struct {
mode string
expected bool
}{
{DebugMode, true},
{ReleaseMode, false},
{TestMode, false},
}
for _, tt := range tests {
SetMode(tt.mode)
assert.Equal(t, tt.expected, IsDebugging())
}
}
func TestDebugPrint(t *testing.T) {
got := captureOutput(t, func() {
SetMode(DebugMode)
SetMode(ReleaseMode)
debugPrint("DEBUG this!")
SetMode(TestMode)
debugPrint("DEBUG this!")
SetMode(DebugMode)
debugPrint("these are %d %s", 2, "error messages")
SetMode(TestMode)
})
expected := "[SOON-debug] these are 2 error messages\n"
assert.Equal(t, expected, got)
}
func captureOutput(t *testing.T, f func()) string {
reader, writer, err := os.Pipe()
if err != nil {
panic(err)
}
defaultWriter := DefaultWriter
defaultErrorWriter := DefaultErrorWriter
defer func() {
DefaultWriter = defaultWriter
DefaultErrorWriter = defaultErrorWriter
log.SetOutput(os.Stderr)
}()
DefaultWriter = writer
DefaultErrorWriter = writer
log.SetOutput(writer)
out := make(chan string)
wg := new(sync.WaitGroup)
wg.Add(1)
go func() {
var buf bytes.Buffer
wg.Done()
_, err := io.Copy(&buf, reader)
assert.Nil(t, err)
out <- buf.String()
}()
wg.Wait()
f()
writer.Close()
return <-out
}