-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathrunner_test.go
117 lines (93 loc) · 2.58 KB
/
runner_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
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
package main
import "bytes"
import "sync"
import "testing"
type TestPipe struct {
*bytes.Buffer
}
func (t *TestPipe) Close() (err error) {
return
}
func TestRun(t *testing.T) {
stdinbuf := &bytes.Buffer{}
template := []string{"echo", "{{}}"}
runner := NewRunner(template, "{{}}", "foobar", stdinbuf)
ch, err := runner.Run()
if err != nil {
t.Errorf("runner.Run() returned an error: %s", err)
}
output := make([]string, 0)
for outputline := range ch {
output = append(output, outputline)
}
runner.Wait()
if len(output) != 1 {
t.Errorf("output length wrong. expected: %d, actual: %d", 1, len(output))
}
if output[0] != "foobar\n" {
t.Errorf("output wrong. expected: %q, actual: %q", "foobar\n", output[0])
}
}
func TestStdinbuffer(t *testing.T) {
stdinbuf := &bytes.Buffer{}
stdinbuf.WriteString("foo\nbar\n")
template := []string{"grep", "{{}}"}
runner := NewRunner(template, "{{}}", "foo", stdinbuf)
ch, err := runner.Run()
if err != nil {
t.Errorf("runner.Run() returned an error: %s", err)
}
output := make([]string, 0)
for outputline := range ch {
output = append(output, outputline)
}
runner.Wait()
if len(output) != 1 {
t.Errorf("output length wrong. expected: %d, actual: %d", 1, len(output))
}
if output[0] != "foo\n" {
t.Errorf("output wrong. expected: %q, actual: %q", "foo\n", output[0])
}
}
func TestStreamOutput(t *testing.T) {
pipe := &TestPipe{bytes.NewBufferString("foo\nbar\n")}
wg := &sync.WaitGroup{}
runner := &Runner{}
output := make([]string, 0)
wg.Add(1)
ch := runner.streamOutput(pipe, wg)
for line := range ch {
output = append(output, line)
}
wg.Wait()
if len(output) != 2 {
t.Errorf("streamed output length wrong. expected: %d, actual: %d", 2, len(output))
}
if output[0] != "foo\n" {
t.Errorf("streamed output wrong. expected: %q, actual: %q", "foo\n", output[0])
}
if output[1] != "bar\n" {
t.Errorf("streamed output wrong. expected: %q, actual: %q", "bar\n", output[0])
}
}
func TestStreamOutputWithoutTrailingNewline(t *testing.T) {
pipe := &TestPipe{bytes.NewBufferString("foo\nbar")}
wg := &sync.WaitGroup{}
runner := &Runner{}
output := make([]string, 0)
wg.Add(1)
ch := runner.streamOutput(pipe, wg)
for line := range ch {
output = append(output, line)
}
wg.Wait()
if len(output) != 2 {
t.Errorf("streamed output length wrong. expected: %d, actual: %d", 2, len(output))
}
if output[0] != "foo\n" {
t.Errorf("streamed output wrong. expected: %q, actual: %q", "foo\n", output[0])
}
if output[1] != "bar" {
t.Errorf("streamed output wrong. expected: %q, actual: %q", "bar", output[0])
}
}