-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrecorder_test.go
86 lines (79 loc) · 2.11 KB
/
recorder_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
package recorder_test
import (
"fmt"
"os"
"strings"
"sync"
"testing"
"time"
"github.com/amalfra/recorder/v4"
)
var wg sync.WaitGroup
func TestStdout(t *testing.T) {
stdoutText := `This should be recorded.
second line that should be recorded~~~
end output`
stderrText := "this goes to stderr"
stdoutRecorder := new(recorder.Stdout)
fmt.Println("before recording")
err := stdoutRecorder.Start()
if err != nil {
t.Fatalf("Error initializing recorder: %s", err)
}
fmt.Println(stdoutText)
fmt.Fprintf(os.Stderr, "%s", stderrText)
stdoutRecorder.Stop()
recordedOutput := strings.TrimSpace(stdoutRecorder.GetOutput())
if stdoutText != recordedOutput {
t.Fatalf("Wrong output recorded: %s", recordedOutput)
}
}
func TestStderr(t *testing.T) {
stderrText := `This should be recorded.
second line that should be recorded~~~
end output`
stdoutText := "this goes to stdout"
stderrRecorder := new(recorder.Stderr)
fmt.Println("before recording")
err := stderrRecorder.Start()
if err != nil {
t.Fatalf("Error initializing recorder: %s", err)
}
fmt.Println(stdoutText)
fmt.Fprintf(os.Stderr, "%s", stderrText)
stderrRecorder.Stop()
recordedOutput := strings.TrimSpace(stderrRecorder.GetOutput())
if stderrText != recordedOutput {
t.Fatalf("Wrong output recorded: %s", recordedOutput)
}
}
func TestStdoutGoRoutine(t *testing.T) {
stdoutText := `This should be recorded.
second line that should be recorded~~~
end output`
stderrText := "this goes to stderr"
stdoutRecorder := new(recorder.Stdout)
fmt.Println("before recording")
err := stdoutRecorder.Start()
if err != nil {
t.Fatalf("Error initializing recorder: %s", err)
}
fmt.Println(stdoutText)
wg.Add(1)
go goRoutine()
wg.Wait()
fmt.Fprintf(os.Stderr, "%s", stderrText)
stdoutRecorder.Stop()
recordedOutput := strings.TrimSpace(stdoutRecorder.GetOutput())
stdoutText += "\nin goRoutine"
stdoutText += "\nout goRoutine"
if stdoutText != recordedOutput {
t.Fatalf("Wrong output recorded: %s", recordedOutput)
}
}
func goRoutine() {
defer wg.Done()
fmt.Println("in goRoutine")
time.Sleep(1000 * time.Millisecond)
fmt.Println("out goRoutine")
}