-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilters_test.go
162 lines (146 loc) · 3.27 KB
/
filters_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
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
155
156
157
158
159
160
161
162
package filters
import (
"fmt"
"io"
"testing"
)
type fakeReaderWriter struct {
Input []byte
Output []byte
i int64
}
func (f *fakeReaderWriter) Read(p []byte) (n int, err error) {
if f.i >= int64(len(f.Input)) {
return 0, io.EOF
}
n = copy(p, f.Input[f.i:])
f.i += int64(n)
return
}
func (f *fakeReaderWriter) Write(p []byte) (n int, err error) {
f.Output = append(f.Output, p...)
return len(p), nil
}
func TestNew(t *testing.T) {
f := New()
if f.Arguments == nil {
t.Error("Expected non nil slice")
}
}
func TestFilterFile(t *testing.T) {
_, err := FilterFile("filter.yml")
if err != nil {
t.Error(err)
}
}
func TestRun(t *testing.T) {
ts := "hello world"
f := New()
f.Command = "echo"
f.Argument(ts)
frw := fakeReaderWriter{}
e := f.Exec()
e.SetOutput(&frw)
err := e.Run()
if err != nil {
t.Error(err)
}
if string(frw.Output) != fmt.Sprintf("%v\n", ts) {
t.Errorf("Expected ts with carriage return received %v", string(frw.Output))
}
}
func TestChainRun(t *testing.T) {
ts := "Alphabet city is haunted\nConstantina feels right at home\n"
ts += "She probably wont say youre wrong\n"
ts += "Youre already wrong\nYoure already wrong\n"
f := New()
f.Command = "cat"
fg := New()
fg.Command = "grep"
fg.Argument("wrong")
fx := New()
fx.Command = "xargs"
fx.Argument("-n")
fx.Argument("3")
frw := fakeReaderWriter{Input: []byte(ts)}
//c := Chain{Filters: []Filter{f, fg, fx}}
c := Chain{Filters: []Filter{f, fg, fx}}
e, err := c.Exec()
if err != nil {
t.Error(err)
}
e.SetInput(&frw)
e.SetOutput(&frw)
err = e.Run()
if err != nil {
t.Error(err)
}
exp := "She probably wont\nsay youre wrong\n"
exp += "Youre already wrong\nYoure already wrong\n"
if string(frw.Output) != fmt.Sprintf("%v", exp) {
t.Errorf("Expected: \n%v \ngot \n%v", exp, string(frw.Output))
}
}
func TestChainFromFile(t *testing.T) {
_, err := ChainFile("chain.yml")
if err != nil {
t.Error(err)
}
}
func TestExecuteChainFromFile(t *testing.T) {
ts := "Alphabet city is haunted\nConstantina feels right at home\n"
ts += "She probably wont say youre wrong\n"
ts += "Youre already wrong\nYoure already wrong\n"
c, err := ChainFile("chain.yml")
if err != nil {
t.Error(err)
}
frw := fakeReaderWriter{Input: []byte(ts)}
e, err := c.Exec()
if err != nil {
t.Error(err)
}
e.SetInput(&frw)
e.SetOutput(&frw)
err = e.Run()
if err != nil {
t.Error(err)
}
exp := "She probably wont\nsay youre wrong\n"
exp += "Youre already wrong\nYoure already wrong\n"
if string(frw.Output) != fmt.Sprintf("%v", exp) {
t.Errorf("Expected: \n%v \ngot \n%v", exp, string(frw.Output))
}
}
func TestChainsFromFile(t *testing.T) {
_, err := ChainsFile("chains.yml")
if err != nil {
t.Error(err)
}
}
func TestGet(t *testing.T) {
c, err := ChainsFile("chains.yml")
if err != nil {
t.Error(err)
}
_, err = c.Get("FirstChain")
if err != nil {
t.Error(err)
}
_, err = c.Get("boguschain")
if err != ErrChainDoesNotExist {
t.Error("Expected chain does not exist error")
}
}
func TestFromFile(t *testing.T) {
file := "bogus.yml"
c := NewChains()
err := fromFile(file, c)
if err != ErrNotPointer {
t.Error("Expected not pointer error")
}
err = fromFile(file, &c)
if err.Error() != "stat bogus.yml: no such file or directory" {
t.Error("Expected bad file error")
}
}