-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspy_integration_test.go
More file actions
116 lines (90 loc) · 2.24 KB
/
spy_integration_test.go
File metadata and controls
116 lines (90 loc) · 2.24 KB
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
//+build integration
package spy_test
import (
"bytes"
"fmt"
"spy"
"testing"
"time"
"github.com/google/go-cmp/cmp"
)
func TestExecuteIntegration(t *testing.T) {
commandLine := "echo Hello, World!"
wantRecorder := fmt.Sprintf("%s\nHello, World!\n", commandLine)
wantTerminal := "Hello, World!\n"
s, err := spy.NewSession("/tmp/test-file.log")
if err != nil {
t.Fatal(err)
}
var logFile, terminal bytes.Buffer
s.Recorder = &logFile
s.Terminal = &terminal
err = s.Execute(commandLine)
if err != nil {
t.Fatal(err)
}
gotRecorder := logFile.String()
gotTerminal := terminal.String()
if !cmp.Equal(wantRecorder, gotRecorder) {
t.Error(cmp.Diff(wantRecorder, gotRecorder))
}
if !cmp.Equal(wantTerminal, gotTerminal) {
t.Error(cmp.Diff(wantTerminal, gotTerminal))
}
}
func TestExecuteIntegrationInvalid(t *testing.T) {
var logFile, terminal bytes.Buffer
want := "Error parsing command\n"
s, err := spy.NewSession("/tmp/test-file.log")
if err != nil {
t.Error(err)
}
s.Terminal = &terminal
s.Recorder = &logFile
err = s.Execute("echo Hello'")
if err != nil {
t.Error(err)
}
got := terminal.String()
if want != got {
t.Errorf("Want: %q, got: %q", want, got)
}
}
func TestWithTimestamp(t *testing.T) {
wantTimestamp := "2020-04-04T20:15:02Z\n"
currentTime := time.Date(2020, time.April, 4, 20, 15, 02, 00, time.UTC)
fakeRecorder := bytes.Buffer{}
s, _ := spy.NewSession("/tmp/test-file.log")
s.TimestampMode = true
s.Recorder = &fakeRecorder
s.RecordTime(currentTime)
got := fakeRecorder.String()
if !cmp.Equal(wantTimestamp, got) {
t.Error(cmp.Diff(wantTimestamp, got))
}
}
func TestRun(t *testing.T) {
input := "echo Hi!"
wantRecorder := fmt.Sprintf("%s\nHi!\n", input)
wantTerminal := "Hi!\n"
s, err := spy.NewSession("/tmp/test-file.log")
if err != nil {
t.Fatal(err)
}
s.Input = bytes.NewReader([]byte(input))
var logFile, terminal bytes.Buffer
s.Recorder = &logFile
s.Terminal = &terminal
err = s.Run()
if err != nil {
t.Fatal(err)
}
gotRecorder := logFile.String()
gotTerminal := terminal.String()
if !cmp.Equal(wantRecorder, gotRecorder) {
t.Error(cmp.Diff(wantRecorder, gotRecorder))
}
if !cmp.Equal(wantTerminal, gotTerminal) {
t.Error(cmp.Diff(wantTerminal, gotTerminal))
}
}