-
Notifications
You must be signed in to change notification settings - Fork 8
/
main_test.go
65 lines (54 loc) · 913 Bytes
/
main_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
package main
import (
"os"
"testing"
)
type FakeStdout struct {
stdout *os.File
File *os.File
}
func (f *FakeStdout) Close() *os.File {
if f.stdout != nil {
os.Stdout = f.stdout
f.stdout = nil
}
if f.File != nil {
f.File.Seek(0, os.SEEK_SET)
}
return f.File
}
func (f *FakeStdout) CloseAll() {
if f.Close() != nil {
os.Remove(f.File.Name())
f.File.Close()
f.File = nil
}
}
func NewFakeStdout(t *testing.T) *FakeStdout {
file, err := os.CreateTemp(".", "fakeio.*.txt")
if err != nil {
t.Fatalf("failed to os.CreateTemp(): %v", err)
}
stdout := os.Stdout
os.Stdout = file
return &FakeStdout{
stdout: stdout,
File: file,
}
}
type FakeExit struct {
Code int
osexit func(int)
}
func (f *FakeExit) Close() {
osExit = f.osexit
}
func NewFakeExit(t *testing.T) *FakeExit {
f := &FakeExit{
osexit: osExit,
}
osExit = func(rc int) {
f.Code = rc
}
return f
}