forked from ipfs/go-ipfs-cmds
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresponseemitter_test.go
83 lines (70 loc) · 1.78 KB
/
responseemitter_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
package cli
import (
"bytes"
"fmt"
"testing"
cmds "github.com/ipfs/go-ipfs-cmds"
)
type tcCloseWithError struct {
stdout, stderr *bytes.Buffer
exStdout, exStderr string
exExit int
f func(re ResponseEmitter, t *testing.T)
}
func (tc tcCloseWithError) Run(t *testing.T) {
req := &cmds.Request{}
cmdsre, err := NewResponseEmitter(tc.stdout, tc.stderr, req)
if err != nil {
t.Fatal(err)
}
tc.f(cmdsre, t)
if cmdsre.Status() != tc.exExit {
t.Fatalf("expected exit code %d, got %d", tc.exExit, cmdsre.Status())
}
if tc.stdout.String() != tc.exStdout {
t.Fatalf(`expected stdout string "%s" but got "%s"`, tc.exStdout, tc.stdout.String())
}
if tc.stderr.String() != tc.exStderr {
t.Fatalf(`expected stderr string "%s" but got "%s"`, tc.exStderr, tc.stderr.String())
}
t.Logf("stdout:\n---\n%s---\n", tc.stdout.Bytes())
t.Logf("stderr:\n---\n%s---\n", tc.stderr.Bytes())
}
func TestCloseWithError(t *testing.T) {
tcs := []tcCloseWithError{
{
stdout: bytes.NewBuffer(nil),
stderr: bytes.NewBuffer(nil),
exStdout: "a\n",
exStderr: "Error: some error\n",
exExit: 1,
f: func(re ResponseEmitter, t *testing.T) {
re.Emit("a")
re.CloseWithError(fmt.Errorf("some error"))
re.Emit("b")
},
},
{
stdout: bytes.NewBuffer(nil),
stderr: bytes.NewBuffer(nil),
exStdout: "a\n",
exStderr: "Error: some error\n",
exExit: 1,
f: func(re ResponseEmitter, t *testing.T) {
re.Emit("a")
err := re.CloseWithError(fmt.Errorf("some error"))
if err != nil {
t.Fatal("unexpected error:", err)
}
err = re.Close()
if err != cmds.ErrClosingClosedEmitter {
t.Fatal("expected double close error, got:", err)
}
},
},
}
for i, tc := range tcs {
t.Log(i)
tc.Run(t)
}
}