forked from ipfs/go-ipfs-cmds
-
Notifications
You must be signed in to change notification settings - Fork 0
/
executor_test.go
179 lines (152 loc) · 3.5 KB
/
executor_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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package cmds
import (
"bytes"
"context"
"errors"
"io"
"testing"
)
var errGeneric = errors.New("an error occurred")
var root = &Command{
Subcommands: map[string]*Command{
"test": {
Run: func(req *Request, re ResponseEmitter, env Environment) error {
re.Emit(env)
return nil
},
},
"testError": {
Run: func(req *Request, re ResponseEmitter, env Environment) error {
err := errGeneric
if err != nil {
return err
}
re.Emit(env)
return nil
},
},
},
}
type wc struct {
io.Writer
io.Closer
}
type env int
func TestExecutor(t *testing.T) {
env := env(42)
req, err := NewRequest(context.Background(), []string{"test"}, nil, nil, nil, root)
if err != nil {
t.Fatal(err)
}
var buf bytes.Buffer
re, err := NewWriterResponseEmitter(wc{&buf, nopCloser{}}, req)
if err != nil {
t.Fatal(err)
}
x := NewExecutor(root)
x.Execute(req, re, &env)
if out := buf.String(); out != "42\n" {
t.Errorf("expected output \"42\" but got %q", out)
}
}
func TestExecutorError(t *testing.T) {
env := env(42)
req, err := NewRequest(context.Background(), []string{"testError"}, nil, nil, nil, root)
if err != nil {
t.Fatal(err)
}
re, res := NewChanResponsePair(req)
x := NewExecutor(root)
x.Execute(req, re, &env)
_, err = res.Next()
if err == nil {
t.Fatal("expected error but got nil")
}
expErr := "an error occurred"
if err.Error() != expErr {
t.Fatalf("expected error message %q but got: %s", expErr, err)
}
}
func TestExecutorNotTyper(t *testing.T) {
testCmd := &Command{
Run: func(*Request, ResponseEmitter, Environment) error {
return nil
},
PostRun: PostRunMap{
CLI: func(response Response, emitter ResponseEmitter) error { return nil },
},
}
testRoot := &Command{
Subcommands: map[string]*Command{
"test": testCmd,
},
}
req, err := NewRequest(context.Background(), []string{"test"}, nil, nil, nil, testRoot)
if err != nil {
t.Fatal(err)
}
emitter, resp := NewChanResponsePair(req)
x := NewExecutor(testRoot)
err = x.Execute(req, emitter, nil)
if err != nil {
t.Fatal(err)
}
_, err = resp.Next()
if err != io.EOF {
t.Fatalf("expected EOF but got: %s", err)
}
}
func TestExecutorPostRun(t *testing.T) {
expectedValue := errors.New("postrun ran")
testCmd := &Command{
Run: func(*Request, ResponseEmitter, Environment) error {
return nil
},
PostRun: PostRunMap{
CLI: func(response Response, emitter ResponseEmitter) error {
return expectedValue
},
},
}
testRoot := &Command{
Subcommands: map[string]*Command{
"test": testCmd,
},
}
req, err := NewRequest(context.Background(), []string{"test"}, nil, nil, nil, testRoot)
if err != nil {
t.Fatal(err)
}
t.Run("with", func(t *testing.T) {
var (
emitter, resp = NewChanResponsePair(req)
x = NewExecutor(testRoot)
)
emitter = cliMockEmitter{emitter}
if err := x.Execute(req, emitter, nil); err != nil {
t.Fatal(err)
}
_, err := resp.Next()
if err != expectedValue {
t.Fatalf("expected Response to return %s but got: %s",
expectedValue, err)
}
})
t.Run("without", func(t *testing.T) {
testCmd.PostRun = nil
var (
emitter, resp = NewChanResponsePair(req)
x = NewExecutor(testRoot)
)
emitter = cliMockEmitter{emitter}
if err := x.Execute(req, emitter, nil); err != nil {
t.Fatal(err)
}
_, err := resp.Next()
if err != io.EOF {
t.Fatalf("expected EOF but got: %s", err)
}
})
}
type cliMockEmitter struct{ ResponseEmitter }
func (cliMockEmitter) Type() PostRunType { return CLI }