forked from gookit/goutil
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestutil.go
154 lines (130 loc) · 2.65 KB
/
testutil.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
// Package testutil provide some test help util functions. eg: http test, mock ENV value
package testutil
import (
"io"
"os"
"time"
)
var oldStdout, oldStderr, newReader *os.File
// DiscardStdout Discard os.Stdout output
//
// Usage:
//
// DiscardStdout()
// fmt.Println("Hello, playground")
// RestoreStdout()
func DiscardStdout() error {
// save old os.Stdout
oldStdout = os.Stdout
stdout, err := os.OpenFile(os.DevNull, os.O_WRONLY, 0)
if err == nil {
os.Stdout = stdout
}
return err
}
// ReadOutput restore os.Stdout
// func ReadOutput() (s string) {
// }
// RewriteStdout rewrite os.Stdout
//
// Usage:
//
// RewriteStdout()
// fmt.Println("Hello, playground")
// msg := RestoreStdout()
func RewriteStdout() {
if oldStdout != nil {
return
}
oldStdout = os.Stdout
r, w, _ := os.Pipe()
newReader = r
os.Stdout = w
}
// RestoreStdout restore os.Stdout
func RestoreStdout(printData ...bool) (s string) {
if oldStdout == nil {
return
}
// Notice: must close writer before read data
// close now reader
_ = os.Stdout.Close()
// restore
os.Stdout = oldStdout
oldStdout = nil
if newReader == nil {
return
}
// read output data
out, _ := io.ReadAll(newReader)
s = string(out)
// print the read data to stdout
if len(printData) > 0 && printData[0] {
_, _ = os.Stdout.WriteString(s)
}
// close reader
_ = newReader.Close()
newReader = nil
return
}
// RewriteStderr rewrite os.Stderr
//
// Usage:
//
// RewriteStderr()
// fmt.Fprintln(os.Stderr, "Hello, playground")
// msg := RestoreStderr()
func RewriteStderr() {
if oldStderr != nil {
return
}
oldStderr = os.Stderr
r, w, _ := os.Pipe()
newReader = r
os.Stderr = w
}
// RestoreStderr restore os.Stderr
func RestoreStderr(printData ...bool) (s string) {
if oldStderr == nil {
return
}
// Notice: must close writer before read data
// close now reader
_ = os.Stderr.Close()
// restore
os.Stderr = oldStderr
oldStderr = nil
if newReader == nil {
return
}
// read output data
bts, _ := io.ReadAll(newReader)
s = string(bts)
// print the read data to stderr
if len(printData) > 0 && printData[0] {
_, _ = os.Stderr.WriteString(s)
}
// close reader
_ = newReader.Close()
newReader = nil
return
}
var timeLocBak *time.Location
// SetTimeLocal custom time.Local for testing.
func SetTimeLocal(tl *time.Location) {
if timeLocBak != nil {
panic("time local already set, please restore it before set")
}
timeLocBak = time.Local
time.Local = tl
}
// SetTimeLocalUTC custom time.Local=UTC for testing.
func SetTimeLocalUTC() {
SetTimeLocal(time.UTC)
}
// RestoreTimeLocal restore time.Local
func RestoreTimeLocal() {
if timeLocBak != nil {
time.Local = timeLocBak
}
}